blob: 810489fb71e3dfaa86f00e79e2c4cc90d6920a5f [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>
Willy Tarreaub059b892018-10-16 17:57:36 +020028#include <common/compiler.h>
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +020029#include <common/hathreads.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010030#include <common/initcall.h>
31#include <common/xref.h>
Thierry FOURNIER380d0932015-01-23 14:27:52 +010032
William Lallemand9ed62032016-11-21 17:49:11 +010033#include <types/cli.h>
Thierry FOURNIER380d0932015-01-23 14:27:52 +010034#include <types/hlua.h>
35#include <types/proxy.h>
William Lallemand9ed62032016-11-21 17:49:11 +010036#include <types/stats.h>
Thierry FOURNIER380d0932015-01-23 14:27:52 +010037
Thierry FOURNIER55da1652015-01-23 11:36:30 +010038#include <proto/arg.h>
Willy Tarreau8a8d83b2015-04-13 13:24:54 +020039#include <proto/applet.h>
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010040#include <proto/channel.h>
William Lallemand9ed62032016-11-21 17:49:11 +010041#include <proto/cli.h>
Willy Tarreaua71f6422016-11-16 17:00:14 +010042#include <proto/connection.h>
William Lallemand9ed62032016-11-21 17:49:11 +010043#include <proto/stats.h>
Thierry FOURNIER9a819e72015-02-16 20:22:55 +010044#include <proto/hdr_idx.h>
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +010045#include <proto/hlua.h>
Thierry Fournierfb0b5462016-01-21 09:28:58 +010046#include <proto/hlua_fcn.h>
Willy Tarreau79e57332018-10-02 16:01:16 +020047#include <proto/http_fetch.h>
Willy Tarreau61c112a2018-10-02 16:43:32 +020048#include <proto/http_rules.h>
Thierry FOURNIER3def3932015-04-07 11:27:54 +020049#include <proto/map.h>
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010050#include <proto/obj_type.h>
Patrick Hemmer268a7072018-05-11 12:52:31 -040051#include <proto/queue.h>
Thierry FOURNIER83758bb2015-02-04 13:21:04 +010052#include <proto/pattern.h>
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +010053#include <proto/payload.h>
54#include <proto/proto_http.h>
55#include <proto/sample.h>
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010056#include <proto/server.h>
Willy Tarreaufeb76402015-04-03 14:10:06 +020057#include <proto/session.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020058#include <proto/stream.h>
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010059#include <proto/stream_interface.h>
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +010060#include <proto/task.h>
Willy Tarreau39713102016-11-25 15:49:32 +010061#include <proto/tcp_rules.h>
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +020062#include <proto/vars.h>
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +010063
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +010064/* Lua uses longjmp to perform yield or throwing errors. This
65 * macro is used only for identifying the function that can
66 * not return because a longjmp is executed.
67 * __LJMP marks a prototype of hlua file that can use longjmp.
68 * WILL_LJMP() marks an lua function that will use longjmp.
69 * MAY_LJMP() marks an lua function that may use longjmp.
70 */
71#define __LJMP
Willy Tarreau4e7cc332018-10-20 17:45:48 +020072#define WILL_LJMP(func) do { func; my_unreachable(); } while(0)
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +010073#define MAY_LJMP(func) func
74
Thierry FOURNIERbabae282015-09-17 11:36:37 +020075/* This couple of function executes securely some Lua calls outside of
76 * the lua runtime environment. Each Lua call can return a longjmp
77 * if it encounter a memory error.
78 *
79 * Lua documentation extract:
80 *
81 * If an error happens outside any protected environment, Lua calls
82 * a panic function (see lua_atpanic) and then calls abort, thus
83 * exiting the host application. Your panic function can avoid this
84 * exit by never returning (e.g., doing a long jump to your own
85 * recovery point outside Lua).
86 *
87 * The panic function runs as if it were a message handler (see
88 * §2.3); in particular, the error message is at the top of the
89 * stack. However, there is no guarantee about stack space. To push
90 * anything on the stack, the panic function must first check the
91 * available space (see §4.2).
92 *
93 * We must check all the Lua entry point. This includes:
94 * - The include/proto/hlua.h exported functions
95 * - the task wrapper function
96 * - The action wrapper function
97 * - The converters wrapper function
98 * - The sample-fetch wrapper functions
99 *
100 * It is tolerated that the initilisation function returns an abort.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800101 * Before each Lua abort, an error message is written on stderr.
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200102 *
103 * The macro SET_SAFE_LJMP initialise the longjmp. The Macro
104 * RESET_SAFE_LJMP reset the longjmp. These function must be macro
105 * because they must be exists in the program stack when the longjmp
106 * is called.
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +0200107 *
108 * Note that the Lua processing is not really thread safe. It provides
109 * heavy system which consists to add our own lock function in the Lua
110 * code and recompile the library. This system will probably not accepted
111 * by maintainers of various distribs.
112 *
113 * Our main excution point of the Lua is the function lua_resume(). A
114 * quick looking on the Lua sources displays a lua_lock() a the start
115 * of function and a lua_unlock() at the end of the function. So I
116 * conclude that the Lua thread safe mode just perform a mutex around
117 * all execution. So I prefer to do this in the HAProxy code, it will be
118 * easier for distro maintainers.
119 *
120 * Note that the HAProxy lua functions rounded by the macro SET_SAFE_LJMP
121 * and RESET_SAFE_LJMP manipulates the Lua stack, so it will be careful
122 * to set mutex around these functions.
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200123 */
Willy Tarreau86abe442018-11-25 20:12:18 +0100124__decl_spinlock(hlua_global_lock);
Thierry FOURNIERffbad792017-07-12 11:39:04 +0200125THREAD_LOCAL jmp_buf safe_ljmp_env;
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200126static int hlua_panic_safe(lua_State *L) { return 0; }
127static int hlua_panic_ljmp(lua_State *L) { longjmp(safe_ljmp_env, 1); }
128
129#define SET_SAFE_LJMP(__L) \
130 ({ \
131 int ret; \
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100132 HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200133 if (setjmp(safe_ljmp_env) != 0) { \
134 lua_atpanic(__L, hlua_panic_safe); \
135 ret = 0; \
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100136 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200137 } else { \
138 lua_atpanic(__L, hlua_panic_ljmp); \
139 ret = 1; \
140 } \
141 ret; \
142 })
143
144/* If we are the last function catching Lua errors, we
145 * must reset the panic function.
146 */
147#define RESET_SAFE_LJMP(__L) \
148 do { \
149 lua_atpanic(__L, hlua_panic_safe); \
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100150 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200151 } while(0)
152
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200153/* Applet status flags */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200154#define APPLET_DONE 0x01 /* applet processing is done. */
155#define APPLET_100C 0x02 /* 100 continue expected. */
156#define APPLET_HDR_SENT 0x04 /* Response header sent. */
157#define APPLET_CHUNKED 0x08 /* Use transfer encoding chunked. */
158#define APPLET_LAST_CHK 0x10 /* Last chunk sent. */
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +0100159#define APPLET_HTTP11 0x20 /* Last chunk sent. */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200160
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100161/* The main Lua execution context. */
162struct hlua gL;
163
Thierry FOURNIERebed6e92016-12-16 11:54:07 +0100164/* This is the memory pool containing struct lua for applets
165 * (including cli).
166 */
Willy Tarreau8ceae722018-11-26 11:58:30 +0100167DECLARE_STATIC_POOL(pool_head_hlua, "hlua", sizeof(struct hlua));
Thierry FOURNIERebed6e92016-12-16 11:54:07 +0100168
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100169/* Used for Socket connection. */
170static struct proxy socket_proxy;
171static struct server socket_tcp;
172#ifdef USE_OPENSSL
173static struct server socket_ssl;
174#endif
175
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +0100176/* List head of the function called at the initialisation time. */
177struct list hlua_init_functions = LIST_HEAD_INIT(hlua_init_functions);
178
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +0100179/* The following variables contains the reference of the different
180 * Lua classes. These references are useful for identify metadata
181 * associated with an object.
182 */
Thierry FOURNIER65f34c62015-02-16 20:11:43 +0100183static int class_txn_ref;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100184static int class_socket_ref;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +0100185static int class_channel_ref;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +0100186static int class_fetches_ref;
Thierry FOURNIER594afe72015-03-10 23:58:30 +0100187static int class_converters_ref;
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100188static int class_http_ref;
Thierry FOURNIER3def3932015-04-07 11:27:54 +0200189static int class_map_ref;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200190static int class_applet_tcp_ref;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200191static int class_applet_http_ref;
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +0100192
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100193/* Global Lua execution timeout. By default Lua, execution linked
Willy Tarreau87b09662015-04-03 00:22:06 +0200194 * with stream (actions, sample-fetches and converters) have a
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100195 * short timeout. Lua linked with tasks doesn't have a timeout
196 * because a task may remain alive during all the haproxy execution.
197 */
198static unsigned int hlua_timeout_session = 4000; /* session timeout. */
199static unsigned int hlua_timeout_task = TICK_ETERNITY; /* task timeout. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200200static unsigned int hlua_timeout_applet = 4000; /* applet timeout. */
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100201
Thierry FOURNIERee9f8022015-03-03 17:37:37 +0100202/* Interrupts the Lua processing each "hlua_nb_instruction" instructions.
203 * it is used for preventing infinite loops.
204 *
205 * I test the scheer with an infinite loop containing one incrementation
206 * and one test. I run this loop between 10 seconds, I raise a ceil of
207 * 710M loops from one interrupt each 9000 instructions, so I fix the value
208 * to one interrupt each 10 000 instructions.
209 *
210 * configured | Number of
211 * instructions | loops executed
212 * between two | in milions
213 * forced yields |
214 * ---------------+---------------
215 * 10 | 160
216 * 500 | 670
217 * 1000 | 680
218 * 5000 | 700
219 * 7000 | 700
220 * 8000 | 700
221 * 9000 | 710 <- ceil
222 * 10000 | 710
223 * 100000 | 710
224 * 1000000 | 710
225 *
226 */
227static unsigned int hlua_nb_instruction = 10000;
228
Willy Tarreau32f61e22015-03-18 17:54:59 +0100229/* Descriptor for the memory allocation state. If limit is not null, it will
230 * be enforced on any memory allocation.
231 */
232struct hlua_mem_allocator {
233 size_t allocated;
234 size_t limit;
235};
236
237static struct hlua_mem_allocator hlua_global_allocator;
238
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200239static const char error_500[] =
Jarno Huuskonen16ad94a2017-01-09 14:17:10 +0200240 "HTTP/1.0 500 Internal Server Error\r\n"
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200241 "Cache-Control: no-cache\r\n"
242 "Connection: close\r\n"
243 "Content-Type: text/html\r\n"
244 "\r\n"
Joseph Herlant7fe15772018-11-15 10:07:32 -0800245 "<html><body><h1>500 Internal Server Error</h1>\nAn internal server error occurred.\n</body></html>\n";
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200246
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100247/* These functions converts types between HAProxy internal args or
248 * sample and LUA types. Another function permits to check if the
249 * LUA stack contains arguments according with an required ARG_T
250 * format.
251 */
252static int hlua_arg2lua(lua_State *L, const struct arg *arg);
253static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100254__LJMP static int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp,
David Carlier0c437f42016-04-27 16:21:56 +0100255 uint64_t mask, struct proxy *p);
Willy Tarreau5eadada2015-03-10 17:28:54 +0100256static int hlua_smp2lua(lua_State *L, struct sample *smp);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100257static int hlua_smp2lua_str(lua_State *L, struct sample *smp);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100258static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp);
259
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200260__LJMP static int hlua_http_get_headers(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg);
261
Thierry FOURNIER23bc3752015-09-11 19:15:43 +0200262#define SEND_ERR(__be, __fmt, __args...) \
263 do { \
264 send_log(__be, LOG_ERR, __fmt, ## __args); \
265 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) \
Christopher Faulet767a84b2017-11-24 16:50:31 +0100266 ha_alert(__fmt, ## __args); \
Thierry FOURNIER23bc3752015-09-11 19:15:43 +0200267 } while (0)
268
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100269/* Used to check an Lua function type in the stack. It creates and
270 * returns a reference of the function. This function throws an
271 * error if the rgument is not a "function".
272 */
273__LJMP unsigned int hlua_checkfunction(lua_State *L, int argno)
274{
275 if (!lua_isfunction(L, argno)) {
Thierry FOURNIERfd1e9552018-02-23 18:41:18 +0100276 const char *msg = lua_pushfstring(L, "function expected, got %s", luaL_typename(L, argno));
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100277 WILL_LJMP(luaL_argerror(L, argno, msg));
278 }
279 lua_pushvalue(L, argno);
280 return luaL_ref(L, LUA_REGISTRYINDEX);
281}
282
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200283/* Return the string that is of the top of the stack. */
284const char *hlua_get_top_error_string(lua_State *L)
285{
286 if (lua_gettop(L) < 1)
287 return "unknown error";
288 if (lua_type(L, -1) != LUA_TSTRING)
289 return "unknown error";
290 return lua_tostring(L, -1);
291}
292
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200293__LJMP static const char *hlua_traceback(lua_State *L)
294{
295 lua_Debug ar;
296 int level = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +0200297 struct buffer *msg = get_trash_chunk();
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200298 int filled = 0;
299
300 while (lua_getstack(L, level++, &ar)) {
301
302 /* Add separator */
303 if (filled)
304 chunk_appendf(msg, ", ");
305 filled = 1;
306
307 /* Fill fields:
308 * 'S': fills in the fields source, short_src, linedefined, lastlinedefined, and what;
309 * 'l': fills in the field currentline;
310 * 'n': fills in the field name and namewhat;
311 * 't': fills in the field istailcall;
312 */
313 lua_getinfo(L, "Slnt", &ar);
314
315 /* Append code localisation */
316 if (ar.currentline > 0)
317 chunk_appendf(msg, "%s:%d ", ar.short_src, ar.currentline);
318 else
319 chunk_appendf(msg, "%s ", ar.short_src);
320
321 /*
322 * Get function name
323 *
324 * if namewhat is no empty, name is defined.
325 * what contains "Lua" for Lua function, "C" for C function,
326 * or "main" for main code.
327 */
328 if (*ar.namewhat != '\0' && ar.name != NULL) /* is there a name from code? */
329 chunk_appendf(msg, "%s '%s'", ar.namewhat, ar.name); /* use it */
330
331 else if (*ar.what == 'm') /* "main", the code is not executed in a function */
332 chunk_appendf(msg, "main chunk");
333
334 else if (*ar.what != 'C') /* for Lua functions, use <file:line> */
335 chunk_appendf(msg, "C function line %d", ar.linedefined);
336
337 else /* nothing left... */
338 chunk_appendf(msg, "?");
339
340
341 /* Display tailed call */
342 if (ar.istailcall)
343 chunk_appendf(msg, " ...");
344 }
345
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200346 return msg->area;
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200347}
348
349
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100350/* This function check the number of arguments available in the
351 * stack. If the number of arguments available is not the same
352 * then <nb> an error is throwed.
353 */
354__LJMP static inline void check_args(lua_State *L, int nb, char *fcn)
355{
356 if (lua_gettop(L) == nb)
357 return;
358 WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb));
359}
360
Mark Lakes22154b42018-01-29 14:38:40 -0800361/* This function pushes an error string prefixed by the file name
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100362 * and the line number where the error is encountered.
363 */
364static int hlua_pusherror(lua_State *L, const char *fmt, ...)
365{
366 va_list argp;
367 va_start(argp, fmt);
368 luaL_where(L, 1);
369 lua_pushvfstring(L, fmt, argp);
370 va_end(argp);
371 lua_concat(L, 2);
372 return 1;
373}
374
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100375/* This functions is used with sample fetch and converters. It
376 * converts the HAProxy configuration argument in a lua stack
377 * values.
378 *
379 * It takes an array of "arg", and each entry of the array is
380 * converted and pushed in the LUA stack.
381 */
382static int hlua_arg2lua(lua_State *L, const struct arg *arg)
383{
384 switch (arg->type) {
385 case ARGT_SINT:
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100386 case ARGT_TIME:
387 case ARGT_SIZE:
Thierry FOURNIERf90838b2015-03-06 13:48:32 +0100388 lua_pushinteger(L, arg->data.sint);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100389 break;
390
391 case ARGT_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200392 lua_pushlstring(L, arg->data.str.area, arg->data.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100393 break;
394
395 case ARGT_IPV4:
396 case ARGT_IPV6:
397 case ARGT_MSK4:
398 case ARGT_MSK6:
399 case ARGT_FE:
400 case ARGT_BE:
401 case ARGT_TAB:
402 case ARGT_SRV:
403 case ARGT_USR:
404 case ARGT_MAP:
405 default:
406 lua_pushnil(L);
407 break;
408 }
409 return 1;
410}
411
412/* This function take one entrie in an LUA stack at the index "ud",
413 * and try to convert it in an HAProxy argument entry. This is useful
414 * with sample fetch wrappers. The input arguments are gived to the
415 * lua wrapper and converted as arg list by thi function.
416 */
417static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg)
418{
419 switch (lua_type(L, ud)) {
420
421 case LUA_TNUMBER:
422 case LUA_TBOOLEAN:
423 arg->type = ARGT_SINT;
424 arg->data.sint = lua_tointeger(L, ud);
425 break;
426
427 case LUA_TSTRING:
428 arg->type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200429 arg->data.str.area = (char *)lua_tolstring(L, ud, (size_t *)&arg->data.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100430 break;
431
432 case LUA_TUSERDATA:
433 case LUA_TNIL:
434 case LUA_TTABLE:
435 case LUA_TFUNCTION:
436 case LUA_TTHREAD:
437 case LUA_TLIGHTUSERDATA:
438 arg->type = ARGT_SINT;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +0200439 arg->data.sint = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100440 break;
441 }
442 return 1;
443}
444
445/* the following functions are used to convert a struct sample
446 * in Lua type. This useful to convert the return of the
447 * fetchs or converters.
448 */
Willy Tarreau5eadada2015-03-10 17:28:54 +0100449static int hlua_smp2lua(lua_State *L, struct sample *smp)
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100450{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200451 switch (smp->data.type) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100452 case SMP_T_SINT:
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100453 case SMP_T_BOOL:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200454 lua_pushinteger(L, smp->data.u.sint);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100455 break;
456
457 case SMP_T_BIN:
458 case SMP_T_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200459 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100460 break;
461
462 case SMP_T_METH:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200463 switch (smp->data.u.meth.meth) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100464 case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break;
465 case HTTP_METH_GET: lua_pushstring(L, "GET"); break;
466 case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break;
467 case HTTP_METH_POST: lua_pushstring(L, "POST"); break;
468 case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break;
469 case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break;
470 case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break;
471 case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break;
472 case HTTP_METH_OTHER:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200473 lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100474 break;
475 default:
476 lua_pushnil(L);
477 break;
478 }
479 break;
480
481 case SMP_T_IPV4:
482 case SMP_T_IPV6:
483 case SMP_T_ADDR: /* This type is never used to qualify a sample. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200484 if (sample_casts[smp->data.type][SMP_T_STR] &&
485 sample_casts[smp->data.type][SMP_T_STR](smp))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200486 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Willy Tarreau5eadada2015-03-10 17:28:54 +0100487 else
488 lua_pushnil(L);
489 break;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100490 default:
491 lua_pushnil(L);
492 break;
493 }
494 return 1;
495}
496
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100497/* the following functions are used to convert a struct sample
498 * in Lua strings. This is useful to convert the return of the
499 * fetchs or converters.
500 */
501static int hlua_smp2lua_str(lua_State *L, struct sample *smp)
502{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200503 switch (smp->data.type) {
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100504
505 case SMP_T_BIN:
506 case SMP_T_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200507 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100508 break;
509
510 case SMP_T_METH:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200511 switch (smp->data.u.meth.meth) {
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100512 case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break;
513 case HTTP_METH_GET: lua_pushstring(L, "GET"); break;
514 case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break;
515 case HTTP_METH_POST: lua_pushstring(L, "POST"); break;
516 case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break;
517 case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break;
518 case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break;
519 case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break;
520 case HTTP_METH_OTHER:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200521 lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100522 break;
523 default:
524 lua_pushstring(L, "");
525 break;
526 }
527 break;
528
529 case SMP_T_SINT:
530 case SMP_T_BOOL:
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100531 case SMP_T_IPV4:
532 case SMP_T_IPV6:
533 case SMP_T_ADDR: /* This type is never used to qualify a sample. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200534 if (sample_casts[smp->data.type][SMP_T_STR] &&
535 sample_casts[smp->data.type][SMP_T_STR](smp))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200536 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100537 else
538 lua_pushstring(L, "");
539 break;
540 default:
541 lua_pushstring(L, "");
542 break;
543 }
544 return 1;
545}
546
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100547/* the following functions are used to convert an Lua type in a
548 * struct sample. This is useful to provide data from a converter
549 * to the LUA code.
550 */
551static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp)
552{
553 switch (lua_type(L, ud)) {
554
555 case LUA_TNUMBER:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200556 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200557 smp->data.u.sint = lua_tointeger(L, ud);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100558 break;
559
560
561 case LUA_TBOOLEAN:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200562 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200563 smp->data.u.sint = lua_toboolean(L, ud);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100564 break;
565
566 case LUA_TSTRING:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200567 smp->data.type = SMP_T_STR;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100568 smp->flags |= SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200569 smp->data.u.str.area = (char *)lua_tolstring(L, ud, (size_t *)&smp->data.u.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100570 break;
571
572 case LUA_TUSERDATA:
573 case LUA_TNIL:
574 case LUA_TTABLE:
575 case LUA_TFUNCTION:
576 case LUA_TTHREAD:
577 case LUA_TLIGHTUSERDATA:
Thierry FOURNIER93405e12015-08-26 14:19:03 +0200578 case LUA_TNONE:
579 default:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200580 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200581 smp->data.u.sint = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100582 break;
583 }
584 return 1;
585}
586
587/* This function check the "argp" builded by another conversion function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800588 * is in accord with the expected argp defined by the "mask". The function
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100589 * returns true or false. It can be adjust the types if there compatibles.
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100590 *
591 * This function assumes thant the argp argument contains ARGM_NBARGS + 1
592 * entries.
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100593 */
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100594__LJMP int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp,
David Carlier0c437f42016-04-27 16:21:56 +0100595 uint64_t mask, struct proxy *p)
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100596{
597 int min_arg;
598 int idx;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100599 struct proxy *px;
600 char *sname, *pname;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100601
602 idx = 0;
603 min_arg = ARGM(mask);
604 mask >>= ARGM_BITS;
605
606 while (1) {
607
608 /* Check oversize. */
609 if (idx >= ARGM_NBARGS && argp[idx].type != ARGT_STOP) {
Cyril Bonté577a36a2015-03-02 00:08:38 +0100610 WILL_LJMP(luaL_argerror(L, first + idx, "Malformed argument mask"));
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100611 }
612
613 /* Check for mandatory arguments. */
614 if (argp[idx].type == ARGT_STOP) {
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100615 if (idx < min_arg) {
616
617 /* If miss other argument than the first one, we return an error. */
618 if (idx > 0)
619 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
620
621 /* If first argument have a certain type, some default values
622 * may be used. See the function smp_resolve_args().
623 */
624 switch (mask & ARGT_MASK) {
625
626 case ARGT_FE:
627 if (!(p->cap & PR_CAP_FE))
628 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
629 argp[idx].data.prx = p;
630 argp[idx].type = ARGT_FE;
631 argp[idx+1].type = ARGT_STOP;
632 break;
633
634 case ARGT_BE:
635 if (!(p->cap & PR_CAP_BE))
636 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
637 argp[idx].data.prx = p;
638 argp[idx].type = ARGT_BE;
639 argp[idx+1].type = ARGT_STOP;
640 break;
641
642 case ARGT_TAB:
643 argp[idx].data.prx = p;
644 argp[idx].type = ARGT_TAB;
645 argp[idx+1].type = ARGT_STOP;
646 break;
647
648 default:
649 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
650 break;
651 }
652 }
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100653 return 0;
654 }
655
656 /* Check for exceed the number of requiered argument. */
657 if ((mask & ARGT_MASK) == ARGT_STOP &&
658 argp[idx].type != ARGT_STOP) {
659 WILL_LJMP(luaL_argerror(L, first + idx, "Last argument expected"));
660 }
661
662 if ((mask & ARGT_MASK) == ARGT_STOP &&
663 argp[idx].type == ARGT_STOP) {
664 return 0;
665 }
666
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100667 /* Convert some argument types. */
668 switch (mask & ARGT_MASK) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100669 case ARGT_SINT:
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100670 if (argp[idx].type != ARGT_SINT)
671 WILL_LJMP(luaL_argerror(L, first + idx, "integer expected"));
672 argp[idx].type = ARGT_SINT;
673 break;
674
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100675 case ARGT_TIME:
676 if (argp[idx].type != ARGT_SINT)
677 WILL_LJMP(luaL_argerror(L, first + idx, "integer expected"));
Thierry FOURNIER29176f32015-07-07 00:41:29 +0200678 argp[idx].type = ARGT_TIME;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100679 break;
680
681 case ARGT_SIZE:
682 if (argp[idx].type != ARGT_SINT)
683 WILL_LJMP(luaL_argerror(L, first + idx, "integer expected"));
Thierry FOURNIER29176f32015-07-07 00:41:29 +0200684 argp[idx].type = ARGT_SIZE;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100685 break;
686
687 case ARGT_FE:
688 if (argp[idx].type != ARGT_STR)
689 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200690 memcpy(trash.area, argp[idx].data.str.area,
691 argp[idx].data.str.data);
692 trash.area[argp[idx].data.str.data] = 0;
693 argp[idx].data.prx = proxy_fe_by_name(trash.area);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100694 if (!argp[idx].data.prx)
695 WILL_LJMP(luaL_argerror(L, first + idx, "frontend doesn't exist"));
696 argp[idx].type = ARGT_FE;
697 break;
698
699 case ARGT_BE:
700 if (argp[idx].type != ARGT_STR)
701 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200702 memcpy(trash.area, argp[idx].data.str.area,
703 argp[idx].data.str.data);
704 trash.area[argp[idx].data.str.data] = 0;
705 argp[idx].data.prx = proxy_be_by_name(trash.area);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100706 if (!argp[idx].data.prx)
707 WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist"));
708 argp[idx].type = ARGT_BE;
709 break;
710
711 case ARGT_TAB:
712 if (argp[idx].type != ARGT_STR)
713 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200714 memcpy(trash.area, argp[idx].data.str.area,
715 argp[idx].data.str.data);
716 trash.area[argp[idx].data.str.data] = 0;
717 argp[idx].data.prx = proxy_tbl_by_name(trash.area);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100718 if (!argp[idx].data.prx)
719 WILL_LJMP(luaL_argerror(L, first + idx, "table doesn't exist"));
720 argp[idx].type = ARGT_TAB;
721 break;
722
723 case ARGT_SRV:
724 if (argp[idx].type != ARGT_STR)
725 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200726 memcpy(trash.area, argp[idx].data.str.area,
727 argp[idx].data.str.data);
728 trash.area[argp[idx].data.str.data] = 0;
729 sname = strrchr(trash.area, '/');
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100730 if (sname) {
731 *sname++ = '\0';
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200732 pname = trash.area;
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200733 px = proxy_be_by_name(pname);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100734 if (!px)
735 WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist"));
736 }
737 else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200738 sname = trash.area;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100739 px = p;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100740 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100741 argp[idx].data.srv = findserver(px, sname);
742 if (!argp[idx].data.srv)
743 WILL_LJMP(luaL_argerror(L, first + idx, "server doesn't exist"));
744 argp[idx].type = ARGT_SRV;
745 break;
746
747 case ARGT_IPV4:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200748 memcpy(trash.area, argp[idx].data.str.area,
749 argp[idx].data.str.data);
750 trash.area[argp[idx].data.str.data] = 0;
751 if (inet_pton(AF_INET, trash.area, &argp[idx].data.ipv4))
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100752 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 address"));
753 argp[idx].type = ARGT_IPV4;
754 break;
755
756 case ARGT_MSK4:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200757 memcpy(trash.area, argp[idx].data.str.area,
758 argp[idx].data.str.data);
759 trash.area[argp[idx].data.str.data] = 0;
760 if (!str2mask(trash.area, &argp[idx].data.ipv4))
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100761 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 mask"));
762 argp[idx].type = ARGT_MSK4;
763 break;
764
765 case ARGT_IPV6:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200766 memcpy(trash.area, argp[idx].data.str.area,
767 argp[idx].data.str.data);
768 trash.area[argp[idx].data.str.data] = 0;
769 if (inet_pton(AF_INET6, trash.area, &argp[idx].data.ipv6))
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100770 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 address"));
771 argp[idx].type = ARGT_IPV6;
772 break;
773
774 case ARGT_MSK6:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200775 memcpy(trash.area, argp[idx].data.str.area,
776 argp[idx].data.str.data);
777 trash.area[argp[idx].data.str.data] = 0;
778 if (!str2mask6(trash.area, &argp[idx].data.ipv6))
Tim Duesterhusb814da62018-01-25 16:24:50 +0100779 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 mask"));
780 argp[idx].type = ARGT_MSK6;
781 break;
782
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100783 case ARGT_MAP:
784 case ARGT_REG:
785 case ARGT_USR:
786 WILL_LJMP(luaL_argerror(L, first + idx, "type not yet supported"));
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100787 break;
788 }
789
790 /* Check for type of argument. */
791 if ((mask & ARGT_MASK) != argp[idx].type) {
792 const char *msg = lua_pushfstring(L, "'%s' expected, got '%s'",
793 arg_type_names[(mask & ARGT_MASK)],
794 arg_type_names[argp[idx].type & ARGT_MASK]);
795 WILL_LJMP(luaL_argerror(L, first + idx, msg));
796 }
797
798 /* Next argument. */
799 mask >>= ARGT_BITS;
800 idx++;
801 }
802}
803
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100804/*
805 * The following functions are used to make correspondance between the the
806 * executed lua pointer and the "struct hlua *" that contain the context.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100807 *
808 * - hlua_gethlua : return the hlua context associated with an lua_State.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100809 * - hlua_sethlua : create the association between hlua context and lua_state.
810 */
811static inline struct hlua *hlua_gethlua(lua_State *L)
812{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +0100813 struct hlua **hlua = lua_getextraspace(L);
814 return *hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100815}
816static inline void hlua_sethlua(struct hlua *hlua)
817{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +0100818 struct hlua **hlua_store = lua_getextraspace(hlua->T);
819 *hlua_store = hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100820}
821
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100822/* This function is used to send logs. It try to send on screen (stderr)
823 * and on the default syslog server.
824 */
825static inline void hlua_sendlog(struct proxy *px, int level, const char *msg)
826{
827 struct tm tm;
828 char *p;
829
830 /* Cleanup the log message. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200831 p = trash.area;
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100832 for (; *msg != '\0'; msg++, p++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200833 if (p >= trash.area + trash.size - 1) {
Thierry FOURNIERccf00632015-09-16 12:47:03 +0200834 /* Break the message if exceed the buffer size. */
835 *(p-4) = ' ';
836 *(p-3) = '.';
837 *(p-2) = '.';
838 *(p-1) = '.';
839 break;
840 }
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100841 if (isprint(*msg))
842 *p = *msg;
843 else
844 *p = '.';
845 }
846 *p = '\0';
847
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200848 send_log(px, level, "%s\n", trash.area);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100849 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
Willy Tarreaua678b432015-08-28 10:14:59 +0200850 get_localtime(date.tv_sec, &tm);
851 fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n",
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100852 log_levels[level], tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec,
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200853 (int)getpid(), trash.area);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100854 fflush(stderr);
855 }
856}
857
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100858/* This function just ensure that the yield will be always
859 * returned with a timeout and permit to set some flags
860 */
861__LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx,
Thierry FOURNIERf90838b2015-03-06 13:48:32 +0100862 lua_KFunction k, int timeout, unsigned int flags)
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100863{
864 struct hlua *hlua = hlua_gethlua(L);
865
866 /* Set the wake timeout. If timeout is required, we set
867 * the expiration time.
868 */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +0200869 hlua->wake_time = timeout;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100870
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +0100871 hlua->flags |= flags;
872
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100873 /* Process the yield. */
Willy Tarreau9635e032018-10-16 17:52:55 +0200874 MAY_LJMP(lua_yieldk(L, nresults, ctx, k));
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100875}
876
Willy Tarreau87b09662015-04-03 00:22:06 +0200877/* This function initialises the Lua environment stored in the stream.
878 * It must be called at the start of the stream. This function creates
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100879 * an LUA coroutine. It can not be use to crete the main LUA context.
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200880 *
881 * This function is particular. it initialises a new Lua thread. If the
882 * initialisation fails (example: out of memory error), the lua function
883 * throws an error (longjmp).
884 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800885 * This function manipulates two Lua stacks: the main and the thread. Only
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200886 * the main stack can fail. The thread is not manipulated. This function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800887 * MUST NOT manipulate the created thread stack state, because it is not
888 * proctected against errors thrown by the thread stack.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100889 */
890int hlua_ctx_init(struct hlua *lua, struct task *task)
891{
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200892 if (!SET_SAFE_LJMP(gL.T)) {
893 lua->Tref = LUA_REFNIL;
894 return 0;
895 }
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100896 lua->Mref = LUA_REFNIL;
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +0100897 lua->flags = 0;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100898 LIST_INIT(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100899 lua->T = lua_newthread(gL.T);
900 if (!lua->T) {
901 lua->Tref = LUA_REFNIL;
Thierry FOURNIER0a976202017-07-12 11:18:00 +0200902 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100903 return 0;
904 }
905 hlua_sethlua(lua);
906 lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
907 lua->task = task;
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200908 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100909 return 1;
910}
911
Willy Tarreau87b09662015-04-03 00:22:06 +0200912/* Used to destroy the Lua coroutine when the attached stream or task
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100913 * is destroyed. The destroy also the memory context. The struct "lua"
914 * is not freed.
915 */
916void hlua_ctx_destroy(struct hlua *lua)
917{
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100918 if (!lua)
Thierry FOURNIERa718b292015-03-04 16:48:34 +0100919 return;
920
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100921 if (!lua->T)
922 goto end;
923
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100924 /* Purge all the pending signals. */
Thierry FOURNIERd6975962017-07-12 14:31:10 +0200925 notification_purge(&lua->com);
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100926
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200927 if (!SET_SAFE_LJMP(lua->T))
928 return;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100929 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200930 RESET_SAFE_LJMP(lua->T);
Thierry FOURNIER5a50a852015-09-23 16:59:28 +0200931
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200932 if (!SET_SAFE_LJMP(gL.T))
933 return;
934 luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref);
935 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER5a50a852015-09-23 16:59:28 +0200936 /* Forces a garbage collecting process. If the Lua program is finished
937 * without error, we run the GC on the thread pointer. Its freed all
938 * the unused memory.
939 * If the thread is finnish with an error or is currently yielded,
940 * it seems that the GC applied on the thread doesn't clean anything,
941 * so e run the GC on the main thread.
942 * NOTE: maybe this action locks all the Lua threads untiml the en of
943 * the garbage collection.
944 */
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +0200945 if (lua->flags & HLUA_MUST_GC) {
Thierry FOURNIER7bd10d52017-07-17 00:44:40 +0200946 if (!SET_SAFE_LJMP(gL.T))
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200947 return;
Thierry FOURNIER7bd10d52017-07-17 00:44:40 +0200948 lua_gc(gL.T, LUA_GCCOLLECT, 0);
949 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +0200950 }
Thierry FOURNIER5a50a852015-09-23 16:59:28 +0200951
Thierry FOURNIERa7b536b2015-09-21 22:50:24 +0200952 lua->T = NULL;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100953
954end:
Willy Tarreaubafbe012017-11-24 17:34:44 +0100955 pool_free(pool_head_hlua, lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100956}
957
958/* This function is used to restore the Lua context when a coroutine
959 * fails. This function copy the common memory between old coroutine
960 * and the new coroutine. The old coroutine is destroyed, and its
961 * replaced by the new coroutine.
962 * If the flag "keep_msg" is set, the last entry of the old is assumed
963 * as string error message and it is copied in the new stack.
964 */
965static int hlua_ctx_renew(struct hlua *lua, int keep_msg)
966{
967 lua_State *T;
968 int new_ref;
969
970 /* Renew the main LUA stack doesn't have sense. */
971 if (lua == &gL)
972 return 0;
973
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100974 /* New Lua coroutine. */
975 T = lua_newthread(gL.T);
976 if (!T)
977 return 0;
978
979 /* Copy last error message. */
980 if (keep_msg)
981 lua_xmove(lua->T, T, 1);
982
983 /* Copy data between the coroutines. */
984 lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref);
985 lua_xmove(lua->T, T, 1);
986 new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Valur poped. */
987
988 /* Destroy old data. */
989 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
990
991 /* The thread is garbage collected by Lua. */
992 luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref);
993
994 /* Fill the struct with the new coroutine values. */
995 lua->Mref = new_ref;
996 lua->T = T;
997 lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
998
999 /* Set context. */
1000 hlua_sethlua(lua);
1001
1002 return 1;
1003}
1004
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001005void hlua_hook(lua_State *L, lua_Debug *ar)
1006{
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001007 struct hlua *hlua = hlua_gethlua(L);
1008
1009 /* Lua cannot yield when its returning from a function,
1010 * so, we can fix the interrupt hook to 1 instruction,
1011 * expecting that the function is finnished.
1012 */
1013 if (lua_gethookmask(L) & LUA_MASKRET) {
1014 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1);
1015 return;
1016 }
1017
1018 /* restore the interrupt condition. */
1019 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
1020
1021 /* If we interrupt the Lua processing in yieldable state, we yield.
1022 * If the state is not yieldable, trying yield causes an error.
1023 */
1024 if (lua_isyieldable(L))
Willy Tarreau9635e032018-10-16 17:52:55 +02001025 MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD));
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001026
Thierry FOURNIERa85cfb12015-03-13 14:50:06 +01001027 /* If we cannot yield, update the clock and check the timeout. */
1028 tv_update_date(0, 1);
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001029 hlua->run_time += now_ms - hlua->start_time;
1030 if (hlua->max_time && hlua->run_time >= hlua->max_time) {
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001031 lua_pushfstring(L, "execution timeout");
1032 WILL_LJMP(lua_error(L));
1033 }
1034
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001035 /* Update the start time. */
1036 hlua->start_time = now_ms;
1037
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001038 /* Try to interrupt the process at the end of the current
1039 * unyieldable function.
1040 */
1041 lua_sethook(hlua->T, hlua_hook, LUA_MASKRET|LUA_MASKCOUNT, hlua_nb_instruction);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001042}
1043
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001044/* This function start or resumes the Lua stack execution. If the flag
1045 * "yield_allowed" if no set and the LUA stack execution returns a yield
1046 * The function return an error.
1047 *
1048 * The function can returns 4 values:
1049 * - HLUA_E_OK : The execution is terminated without any errors.
1050 * - HLUA_E_AGAIN : The execution must continue at the next associated
1051 * task wakeup.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001052 * - HLUA_E_ERRMSG : An error has occurred, an error message is set in
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001053 * the top of the stack.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001054 * - HLUA_E_ERR : An error has occurred without error message.
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001055 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001056 * If an error occurred, the stack is renewed and it is ready to run new
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001057 * LUA code.
1058 */
1059static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed)
1060{
1061 int ret;
1062 const char *msg;
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001063 const char *trace;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001064
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001065 /* Initialise run time counter. */
1066 if (!HLUA_IS_RUNNING(lua))
1067 lua->run_time = 0;
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001068
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001069 /* Lock the whole Lua execution. This lock must be before the
1070 * label "resume_execution".
1071 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001072 HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock);
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001073
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001074resume_execution:
1075
1076 /* This hook interrupts the Lua processing each 'hlua_nb_instruction'
1077 * instructions. it is used for preventing infinite loops.
1078 */
1079 lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
1080
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +01001081 /* Remove all flags except the running flags. */
Thierry FOURNIER2f3867f2015-09-28 01:02:01 +02001082 HLUA_SET_RUN(lua);
1083 HLUA_CLR_CTRLYIELD(lua);
1084 HLUA_CLR_WAKERESWR(lua);
1085 HLUA_CLR_WAKEREQWR(lua);
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +01001086
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001087 /* Update the start time. */
1088 lua->start_time = now_ms;
1089
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001090 /* Call the function. */
1091 ret = lua_resume(lua->T, gL.T, lua->nargs);
1092 switch (ret) {
1093
1094 case LUA_OK:
1095 ret = HLUA_E_OK;
1096 break;
1097
1098 case LUA_YIELD:
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001099 /* Check if the execution timeout is expired. It it is the case, we
1100 * break the Lua execution.
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001101 */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001102 tv_update_date(0, 1);
1103 lua->run_time += now_ms - lua->start_time;
1104 if (lua->max_time && lua->run_time > lua->max_time) {
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001105 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001106 ret = HLUA_E_ETMOUT;
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001107 break;
1108 }
1109 /* Process the forced yield. if the general yield is not allowed or
1110 * if no task were associated this the current Lua execution
1111 * coroutine, we resume the execution. Else we want to return in the
1112 * scheduler and we want to be waked up again, to continue the
1113 * current Lua execution. So we schedule our own task.
1114 */
1115 if (HLUA_IS_CTRLYIELDING(lua)) {
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001116 if (!yield_allowed || !lua->task)
1117 goto resume_execution;
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001118 task_wakeup(lua->task, TASK_WOKEN_MSG);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001119 }
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001120 if (!yield_allowed) {
1121 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001122 ret = HLUA_E_YIELD;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001123 break;
1124 }
1125 ret = HLUA_E_AGAIN;
1126 break;
1127
1128 case LUA_ERRRUN:
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001129
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001130 /* Special exit case. The traditional exit is returned as an error
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001131 * because the errors ares the only one mean to return immediately
1132 * from and lua execution.
1133 */
1134 if (lua->flags & HLUA_EXIT) {
1135 ret = HLUA_E_OK;
Thierry FOURNIERe1587b32015-08-28 09:54:13 +02001136 hlua_ctx_renew(lua, 0);
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001137 break;
1138 }
1139
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001140 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001141 if (!lua_checkstack(lua->T, 1)) {
1142 ret = HLUA_E_ERR;
1143 break;
1144 }
1145 msg = lua_tostring(lua->T, -1);
1146 lua_settop(lua->T, 0); /* Empty the stack. */
1147 lua_pop(lua->T, 1);
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001148 trace = hlua_traceback(lua->T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001149 if (msg)
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001150 lua_pushfstring(lua->T, "runtime error: %s from %s", msg, trace);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001151 else
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001152 lua_pushfstring(lua->T, "unknown runtime error from %s", trace);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001153 ret = HLUA_E_ERRMSG;
1154 break;
1155
1156 case LUA_ERRMEM:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001157 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001158 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001159 ret = HLUA_E_NOMEM;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001160 break;
1161
1162 case LUA_ERRERR:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001163 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001164 if (!lua_checkstack(lua->T, 1)) {
1165 ret = HLUA_E_ERR;
1166 break;
1167 }
1168 msg = lua_tostring(lua->T, -1);
1169 lua_settop(lua->T, 0); /* Empty the stack. */
1170 lua_pop(lua->T, 1);
1171 if (msg)
1172 lua_pushfstring(lua->T, "message handler error: %s", msg);
1173 else
1174 lua_pushfstring(lua->T, "message handler error");
1175 ret = HLUA_E_ERRMSG;
1176 break;
1177
1178 default:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001179 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001180 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001181 ret = HLUA_E_ERR;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001182 break;
1183 }
1184
Thierry FOURNIER6ab4d8e2015-09-27 22:17:19 +02001185 /* This GC permits to destroy some object when a Lua timeout strikes. */
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +02001186 if (lua->flags & HLUA_MUST_GC &&
1187 ret != HLUA_E_AGAIN)
Thierry FOURNIER6ab4d8e2015-09-27 22:17:19 +02001188 lua_gc(lua->T, LUA_GCCOLLECT, 0);
1189
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001190 switch (ret) {
1191 case HLUA_E_AGAIN:
1192 break;
1193
1194 case HLUA_E_ERRMSG:
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001195 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001196 hlua_ctx_renew(lua, 1);
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001197 HLUA_CLR_RUN(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001198 break;
1199
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001200 case HLUA_E_ETMOUT:
1201 case HLUA_E_NOMEM:
1202 case HLUA_E_YIELD:
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001203 case HLUA_E_ERR:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001204 HLUA_CLR_RUN(lua);
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001205 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001206 hlua_ctx_renew(lua, 0);
1207 break;
1208
1209 case HLUA_E_OK:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001210 HLUA_CLR_RUN(lua);
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001211 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001212 break;
1213 }
1214
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001215 /* This is the main exit point, remove the Lua lock. */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001216 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock);
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001217
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001218 return ret;
1219}
1220
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001221/* This function exit the current code. */
1222__LJMP static int hlua_done(lua_State *L)
1223{
1224 struct hlua *hlua = hlua_gethlua(L);
1225
1226 hlua->flags |= HLUA_EXIT;
1227 WILL_LJMP(lua_error(L));
1228
1229 return 0;
1230}
1231
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001232/* This function is an LUA binding. It provides a function
1233 * for deleting ACL from a referenced ACL file.
1234 */
1235__LJMP static int hlua_del_acl(lua_State *L)
1236{
1237 const char *name;
1238 const char *key;
1239 struct pat_ref *ref;
1240
1241 MAY_LJMP(check_args(L, 2, "del_acl"));
1242
1243 name = MAY_LJMP(luaL_checkstring(L, 1));
1244 key = MAY_LJMP(luaL_checkstring(L, 2));
1245
1246 ref = pat_ref_lookup(name);
1247 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001248 WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001249
1250 pat_ref_delete(ref, key);
1251 return 0;
1252}
1253
1254/* This function is an LUA binding. It provides a function
1255 * for deleting map entry from a referenced map file.
1256 */
1257static int hlua_del_map(lua_State *L)
1258{
1259 const char *name;
1260 const char *key;
1261 struct pat_ref *ref;
1262
1263 MAY_LJMP(check_args(L, 2, "del_map"));
1264
1265 name = MAY_LJMP(luaL_checkstring(L, 1));
1266 key = MAY_LJMP(luaL_checkstring(L, 2));
1267
1268 ref = pat_ref_lookup(name);
1269 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001270 WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001271
1272 pat_ref_delete(ref, key);
1273 return 0;
1274}
1275
1276/* This function is an LUA binding. It provides a function
1277 * for adding ACL pattern from a referenced ACL file.
1278 */
1279static int hlua_add_acl(lua_State *L)
1280{
1281 const char *name;
1282 const char *key;
1283 struct pat_ref *ref;
1284
1285 MAY_LJMP(check_args(L, 2, "add_acl"));
1286
1287 name = MAY_LJMP(luaL_checkstring(L, 1));
1288 key = MAY_LJMP(luaL_checkstring(L, 2));
1289
1290 ref = pat_ref_lookup(name);
1291 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001292 WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001293
1294 if (pat_ref_find_elt(ref, key) == NULL)
1295 pat_ref_add(ref, key, NULL, NULL);
1296 return 0;
1297}
1298
1299/* This function is an LUA binding. It provides a function
1300 * for setting map pattern and sample from a referenced map
1301 * file.
1302 */
1303static int hlua_set_map(lua_State *L)
1304{
1305 const char *name;
1306 const char *key;
1307 const char *value;
1308 struct pat_ref *ref;
1309
1310 MAY_LJMP(check_args(L, 3, "set_map"));
1311
1312 name = MAY_LJMP(luaL_checkstring(L, 1));
1313 key = MAY_LJMP(luaL_checkstring(L, 2));
1314 value = MAY_LJMP(luaL_checkstring(L, 3));
1315
1316 ref = pat_ref_lookup(name);
1317 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001318 WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001319
1320 if (pat_ref_find_elt(ref, key) != NULL)
1321 pat_ref_set(ref, key, value, NULL);
1322 else
1323 pat_ref_add(ref, key, value, NULL);
1324 return 0;
1325}
1326
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001327/* A class is a lot of memory that contain data. This data can be a table,
1328 * an integer or user data. This data is associated with a metatable. This
1329 * metatable have an original version registred in the global context with
1330 * the name of the object (_G[<name>] = <metable> ).
1331 *
1332 * A metable is a table that modify the standard behavior of a standard
1333 * access to the associated data. The entries of this new metatable are
1334 * defined as is:
1335 *
1336 * http://lua-users.org/wiki/MetatableEvents
1337 *
1338 * __index
1339 *
1340 * we access an absent field in a table, the result is nil. This is
1341 * true, but it is not the whole truth. Actually, such access triggers
1342 * the interpreter to look for an __index metamethod: If there is no
1343 * such method, as usually happens, then the access results in nil;
1344 * otherwise, the metamethod will provide the result.
1345 *
1346 * Control 'prototype' inheritance. When accessing "myTable[key]" and
1347 * the key does not appear in the table, but the metatable has an __index
1348 * property:
1349 *
1350 * - if the value is a function, the function is called, passing in the
1351 * table and the key; the return value of that function is returned as
1352 * the result.
1353 *
1354 * - if the value is another table, the value of the key in that table is
1355 * asked for and returned (and if it doesn't exist in that table, but that
1356 * table's metatable has an __index property, then it continues on up)
1357 *
1358 * - Use "rawget(myTable,key)" to skip this metamethod.
1359 *
1360 * http://www.lua.org/pil/13.4.1.html
1361 *
1362 * __newindex
1363 *
1364 * Like __index, but control property assignment.
1365 *
1366 * __mode - Control weak references. A string value with one or both
1367 * of the characters 'k' and 'v' which specifies that the the
1368 * keys and/or values in the table are weak references.
1369 *
1370 * __call - Treat a table like a function. When a table is followed by
1371 * parenthesis such as "myTable( 'foo' )" and the metatable has
1372 * a __call key pointing to a function, that function is invoked
1373 * (passing any specified arguments) and the return value is
1374 * returned.
1375 *
1376 * __metatable - Hide the metatable. When "getmetatable( myTable )" is
1377 * called, if the metatable for myTable has a __metatable
1378 * key, the value of that key is returned instead of the
1379 * actual metatable.
1380 *
1381 * __tostring - Control string representation. When the builtin
1382 * "tostring( myTable )" function is called, if the metatable
1383 * for myTable has a __tostring property set to a function,
1384 * that function is invoked (passing myTable to it) and the
1385 * return value is used as the string representation.
1386 *
1387 * __len - Control table length. When the table length is requested using
1388 * the length operator ( '#' ), if the metatable for myTable has
1389 * a __len key pointing to a function, that function is invoked
1390 * (passing myTable to it) and the return value used as the value
1391 * of "#myTable".
1392 *
1393 * __gc - Userdata finalizer code. When userdata is set to be garbage
1394 * collected, if the metatable has a __gc field pointing to a
1395 * function, that function is first invoked, passing the userdata
1396 * to it. The __gc metamethod is not called for tables.
1397 * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html)
1398 *
1399 * Special metamethods for redefining standard operators:
1400 * http://www.lua.org/pil/13.1.html
1401 *
1402 * __add "+"
1403 * __sub "-"
1404 * __mul "*"
1405 * __div "/"
1406 * __unm "!"
1407 * __pow "^"
1408 * __concat ".."
1409 *
1410 * Special methods for redfining standar relations
1411 * http://www.lua.org/pil/13.2.html
1412 *
1413 * __eq "=="
1414 * __lt "<"
1415 * __le "<="
1416 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001417
1418/*
1419 *
1420 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001421 * Class Map
1422 *
1423 *
1424 */
1425
1426/* Returns a struct hlua_map if the stack entry "ud" is
1427 * a class session, otherwise it throws an error.
1428 */
1429__LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud)
1430{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001431 return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001432}
1433
1434/* This function is the map constructor. It don't need
1435 * the class Map object. It creates and return a new Map
1436 * object. It must be called only during "body" or "init"
1437 * context because it process some filesystem accesses.
1438 */
1439__LJMP static int hlua_map_new(struct lua_State *L)
1440{
1441 const char *fn;
1442 int match = PAT_MATCH_STR;
1443 struct sample_conv conv;
1444 const char *file = "";
1445 int line = 0;
1446 lua_Debug ar;
1447 char *err = NULL;
1448 struct arg args[2];
1449
1450 if (lua_gettop(L) < 1 || lua_gettop(L) > 2)
1451 WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument."));
1452
1453 fn = MAY_LJMP(luaL_checkstring(L, 1));
1454
1455 if (lua_gettop(L) >= 2) {
1456 match = MAY_LJMP(luaL_checkinteger(L, 2));
1457 if (match < 0 || match >= PAT_MATCH_NUM)
1458 WILL_LJMP(luaL_error(L, "'new' needs a valid match method."));
1459 }
1460
1461 /* Get Lua filename and line number. */
1462 if (lua_getstack(L, 1, &ar)) { /* check function at level */
1463 lua_getinfo(L, "Sl", &ar); /* get info about it */
1464 if (ar.currentline > 0) { /* is there info? */
1465 file = ar.short_src;
1466 line = ar.currentline;
1467 }
1468 }
1469
1470 /* fill fake sample_conv struct. */
1471 conv.kw = ""; /* unused. */
1472 conv.process = NULL; /* unused. */
1473 conv.arg_mask = 0; /* unused. */
1474 conv.val_args = NULL; /* unused. */
1475 conv.out_type = SMP_T_STR;
1476 conv.private = (void *)(long)match;
1477 switch (match) {
1478 case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break;
1479 case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break;
1480 case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break;
1481 case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break;
1482 case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break;
1483 case PAT_MATCH_END: conv.in_type = SMP_T_STR; break;
1484 case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001485 case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001486 case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break;
1487 default:
1488 WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode."));
1489 }
1490
1491 /* fill fake args. */
1492 args[0].type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001493 args[0].data.str.area = (char *)fn;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001494 args[1].type = ARGT_STOP;
1495
1496 /* load the map. */
1497 if (!sample_load_map(args, &conv, file, line, &err)) {
1498 /* error case: we cant use luaL_error because we must
1499 * free the err variable.
1500 */
1501 luaL_where(L, 1);
1502 lua_pushfstring(L, "'new': %s.", err);
1503 lua_concat(L, 2);
1504 free(err);
1505 WILL_LJMP(lua_error(L));
1506 }
1507
1508 /* create the lua object. */
1509 lua_newtable(L);
1510 lua_pushlightuserdata(L, args[0].data.map);
1511 lua_rawseti(L, -2, 0);
1512
1513 /* Pop a class Map metatable and affect it to the userdata. */
1514 lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref);
1515 lua_setmetatable(L, -2);
1516
1517
1518 return 1;
1519}
1520
1521__LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str)
1522{
1523 struct map_descriptor *desc;
1524 struct pattern *pat;
1525 struct sample smp;
1526
1527 MAY_LJMP(check_args(L, 2, "lookup"));
1528 desc = MAY_LJMP(hlua_checkmap(L, 1));
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001529 if (desc->pat.expect_type == SMP_T_SINT) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001530 smp.data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001531 smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001532 }
1533 else {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001534 smp.data.type = SMP_T_STR;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001535 smp.flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001536 smp.data.u.str.area = (char *)MAY_LJMP(luaL_checklstring(L, 2, (size_t *)&smp.data.u.str.data));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001537 }
1538
1539 pat = pattern_exec_match(&desc->pat, &smp, 1);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001540 if (!pat || !pat->data) {
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001541 if (str)
1542 lua_pushstring(L, "");
1543 else
1544 lua_pushnil(L);
1545 return 1;
1546 }
1547
1548 /* The Lua pattern must return a string, so we can't check the returned type */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001549 lua_pushlstring(L, pat->data->u.str.area, pat->data->u.str.data);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001550 return 1;
1551}
1552
1553__LJMP static int hlua_map_lookup(struct lua_State *L)
1554{
1555 return _hlua_map_lookup(L, 0);
1556}
1557
1558__LJMP static int hlua_map_slookup(struct lua_State *L)
1559{
1560 return _hlua_map_lookup(L, 1);
1561}
1562
1563/*
1564 *
1565 *
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001566 * Class Socket
1567 *
1568 *
1569 */
1570
1571__LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud)
1572{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001573 return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001574}
1575
1576/* This function is the handler called for each I/O on the established
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001577 * connection. It is used for notify space available to send or data
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001578 * received.
1579 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001580static void hlua_socket_handler(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001581{
Willy Tarreau00a37f02015-04-13 12:05:19 +02001582 struct stream_interface *si = appctx->owner;
Olivier Houchard9aaf7782017-09-13 18:30:23 +02001583 struct connection *c = cs_conn(objt_cs(si_opposite(si)->end));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001584
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001585 if (appctx->ctx.hlua_cosocket.die) {
1586 si_shutw(si);
1587 si_shutr(si);
1588 si_ic(si)->flags |= CF_READ_NULL;
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001589 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1590 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001591 stream_shutdown(si_strm(si), SF_ERR_KILLED);
1592 }
1593
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001594 /* If the connection object is not available, close all the
1595 * streams and wakeup everything waiting for.
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001596 */
1597 if (!c) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001598 si_shutw(si);
1599 si_shutr(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01001600 si_ic(si)->flags |= CF_READ_NULL;
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001601 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1602 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Willy Tarreaud4da1962015-04-20 01:31:23 +02001603 return;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001604 }
1605
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001606 /* If we cant write, wakeup the pending write signals. */
1607 if (channel_output_closed(si_ic(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001608 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001609
1610 /* If we cant read, wakeup the pending read signals. */
1611 if (channel_input_closed(si_oc(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001612 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001613
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001614 /* if the connection is not estabkished, inform the stream that we want
1615 * to be notified whenever the connection completes.
1616 */
1617 if (!(c->flags & CO_FL_CONNECTED)) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01001618 si_cant_get(si);
Willy Tarreau12c24232018-12-06 15:29:50 +01001619 si_rx_conn_blk(si);
Willy Tarreau3367d412018-11-15 10:57:41 +01001620 si_rx_endp_more(si);
Willy Tarreaud4da1962015-04-20 01:31:23 +02001621 return;
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001622 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001623
1624 /* This function is called after the connect. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01001625 appctx->ctx.hlua_cosocket.connected = 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001626
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001627 /* Wake the tasks which wants to write if the buffer have available space. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001628 if (channel_may_recv(si_ic(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001629 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001630
1631 /* Wake the tasks which wants to read if the buffer contains data. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001632 if (!channel_is_empty(si_oc(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001633 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001634
1635 /* Some data were injected in the buffer, notify the stream
1636 * interface.
1637 */
1638 if (!channel_is_empty(si_ic(si)))
1639 stream_int_update(si);
1640
1641 /* If write notifications are registered, we considers we want
Willy Tarreau3367d412018-11-15 10:57:41 +01001642 * to write, so we clear the blocking flag.
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001643 */
1644 if (notification_registered(&appctx->ctx.hlua_cosocket.wake_on_write))
Willy Tarreau3367d412018-11-15 10:57:41 +01001645 si_rx_endp_more(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001646}
1647
Willy Tarreau87b09662015-04-03 00:22:06 +02001648/* This function is called when the "struct stream" is destroyed.
1649 * Remove the link from the object to this stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001650 * Wake all the pending signals.
1651 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001652static void hlua_socket_release(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001653{
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001654 struct xref *peer;
1655
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001656 /* Remove my link in the original object. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001657 peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref);
1658 if (peer)
1659 xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001660
1661 /* Wake all the task waiting for me. */
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001662 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1663 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001664}
1665
1666/* If the garbage collectio of the object is launch, nobody
Willy Tarreau87b09662015-04-03 00:22:06 +02001667 * uses this object. If the stream does not exists, just quit.
1668 * Send the shutdown signal to the stream. In some cases,
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001669 * pending signal can rest in the read and write lists. destroy
1670 * it.
1671 */
1672__LJMP static int hlua_socket_gc(lua_State *L)
1673{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001674 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001675 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001676 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001677
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001678 MAY_LJMP(check_args(L, 1, "__gc"));
1679
1680 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001681 peer = xref_get_peer_and_lock(&socket->xref);
1682 if (!peer)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001683 return 0;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001684 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001685
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001686 /* Set the flag which destroy the session. */
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001687 appctx->ctx.hlua_cosocket.die = 1;
1688 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001689
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001690 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001691 xref_disconnect(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001692 return 0;
1693}
1694
1695/* The close function send shutdown signal and break the
Willy Tarreau87b09662015-04-03 00:22:06 +02001696 * links between the stream and the object.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001697 */
sada05ed3302018-05-11 11:48:18 -07001698__LJMP static int hlua_socket_close_helper(lua_State *L)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001699{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001700 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001701 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001702 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001703
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001704 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001705
1706 /* Check if we run on the same thread than the xreator thread.
1707 * We cannot access to the socket if the thread is different.
1708 */
1709 if (socket->tid != tid)
1710 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1711
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001712 peer = xref_get_peer_and_lock(&socket->xref);
1713 if (!peer)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001714 return 0;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001715 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001716
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001717 /* Set the flag which destroy the session. */
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001718 appctx->ctx.hlua_cosocket.die = 1;
1719 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001720
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001721 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001722 xref_disconnect(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001723 return 0;
1724}
1725
sada05ed3302018-05-11 11:48:18 -07001726/* The close function calls close_helper.
1727 */
1728__LJMP static int hlua_socket_close(lua_State *L)
1729{
1730 MAY_LJMP(check_args(L, 1, "close"));
1731 return hlua_socket_close_helper(L);
1732}
1733
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001734/* This Lua function assumes that the stack contain three parameters.
1735 * 1 - USERDATA containing a struct socket
1736 * 2 - INTEGER with values of the macro defined below
1737 * If the integer is -1, we must read at most one line.
1738 * If the integer is -2, we ust read all the data until the
1739 * end of the stream.
1740 * If the integer is positive value, we must read a number of
1741 * bytes corresponding to this value.
1742 */
1743#define HLSR_READ_LINE (-1)
1744#define HLSR_READ_ALL (-2)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001745__LJMP static int hlua_socket_receive_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001746{
1747 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
1748 int wanted = lua_tointeger(L, 2);
1749 struct hlua *hlua = hlua_gethlua(L);
1750 struct appctx *appctx;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001751 size_t len;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001752 int nblk;
Willy Tarreau206ba832018-06-14 15:27:31 +02001753 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001754 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02001755 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001756 size_t len2;
Thierry FOURNIER00543922015-03-09 18:35:06 +01001757 int skip_at_end = 0;
Willy Tarreau81389672015-03-10 12:03:52 +01001758 struct channel *oc;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001759 struct stream_interface *si;
1760 struct stream *s;
1761 struct xref *peer;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001762 int missing_bytes;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001763
1764 /* Check if this lua stack is schedulable. */
1765 if (!hlua || !hlua->task)
1766 WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in "
1767 "'frontend', 'backend' or 'task'"));
1768
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001769 /* Check if we run on the same thread than the xreator thread.
1770 * We cannot access to the socket if the thread is different.
1771 */
1772 if (socket->tid != tid)
1773 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1774
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001775 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001776 peer = xref_get_peer_and_lock(&socket->xref);
1777 if (!peer)
1778 goto no_peer;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001779 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
1780 si = appctx->owner;
1781 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001782
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001783 oc = &s->res;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001784 if (wanted == HLSR_READ_LINE) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001785 /* Read line. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001786 nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001787 if (nblk < 0) /* Connection close. */
1788 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001789 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001790 goto connection_empty;
Thierry FOURNIER00543922015-03-09 18:35:06 +01001791
1792 /* remove final \r\n. */
1793 if (nblk == 1) {
1794 if (blk1[len1-1] == '\n') {
1795 len1--;
1796 skip_at_end++;
1797 if (blk1[len1-1] == '\r') {
1798 len1--;
1799 skip_at_end++;
1800 }
1801 }
1802 }
1803 else {
1804 if (blk2[len2-1] == '\n') {
1805 len2--;
1806 skip_at_end++;
1807 if (blk2[len2-1] == '\r') {
1808 len2--;
1809 skip_at_end++;
1810 }
1811 }
1812 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001813 }
1814
1815 else if (wanted == HLSR_READ_ALL) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001816 /* Read all the available data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001817 nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001818 if (nblk < 0) /* Connection close. */
1819 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001820 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001821 goto connection_empty;
1822 }
1823
1824 else {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001825 /* Read a block of data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001826 nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001827 if (nblk < 0) /* Connection close. */
1828 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001829 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001830 goto connection_empty;
1831
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001832 missing_bytes = wanted - socket->b.n;
1833 if (len1 > missing_bytes) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001834 nblk = 1;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001835 len1 = missing_bytes;
1836 } if (nblk == 2 && len1 + len2 > missing_bytes)
1837 len2 = missing_bytes - len1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001838 }
1839
1840 len = len1;
1841
1842 luaL_addlstring(&socket->b, blk1, len1);
1843 if (nblk == 2) {
1844 len += len2;
1845 luaL_addlstring(&socket->b, blk2, len2);
1846 }
1847
1848 /* Consume data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001849 co_skip(oc, len + skip_at_end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001850
1851 /* Don't wait anything. */
Thierry FOURNIER7e4ee472018-05-25 15:03:50 +02001852 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001853
1854 /* If the pattern reclaim to read all the data
1855 * in the connection, got out.
1856 */
1857 if (wanted == HLSR_READ_ALL)
1858 goto connection_empty;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001859 else if (wanted >= 0 && socket->b.n < wanted)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001860 goto connection_empty;
1861
1862 /* Return result. */
1863 luaL_pushresult(&socket->b);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001864 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001865 return 1;
1866
1867connection_closed:
1868
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001869 xref_unlock(&socket->xref, peer);
1870
1871no_peer:
1872
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001873 /* If the buffer containds data. */
1874 if (socket->b.n > 0) {
1875 luaL_pushresult(&socket->b);
1876 return 1;
1877 }
1878 lua_pushnil(L);
1879 lua_pushstring(L, "connection closed.");
1880 return 2;
1881
1882connection_empty:
1883
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001884 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) {
1885 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001886 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001887 }
1888 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02001889 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_receive_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001890 return 0;
1891}
1892
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001893/* This Lua function gets two parameters. The first one can be string
1894 * or a number. If the string is "*l", the user requires one line. If
1895 * the string is "*a", the user requires all the contents of the stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001896 * If the value is a number, the user require a number of bytes equal
1897 * to the value. The default value is "*l" (a line).
1898 *
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001899 * This parameter with a variable type is converted in integer. This
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001900 * integer takes this values:
1901 * -1 : read a line
1902 * -2 : read all the stream
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001903 * >0 : amount of bytes.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001904 *
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001905 * The second parameter is optional. It contains a string that must be
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001906 * concatenated with the read data.
1907 */
1908__LJMP static int hlua_socket_receive(struct lua_State *L)
1909{
1910 int wanted = HLSR_READ_LINE;
1911 const char *pattern;
1912 int type;
1913 char *error;
1914 size_t len;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001915 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001916
1917 if (lua_gettop(L) < 1 || lua_gettop(L) > 3)
1918 WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments."));
1919
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001920 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001921
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001922 /* Check if we run on the same thread than the xreator thread.
1923 * We cannot access to the socket if the thread is different.
1924 */
1925 if (socket->tid != tid)
1926 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1927
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001928 /* check for pattern. */
1929 if (lua_gettop(L) >= 2) {
1930 type = lua_type(L, 2);
1931 if (type == LUA_TSTRING) {
1932 pattern = lua_tostring(L, 2);
1933 if (strcmp(pattern, "*a") == 0)
1934 wanted = HLSR_READ_ALL;
1935 else if (strcmp(pattern, "*l") == 0)
1936 wanted = HLSR_READ_LINE;
1937 else {
1938 wanted = strtoll(pattern, &error, 10);
1939 if (*error != '\0')
1940 WILL_LJMP(luaL_error(L, "Unsupported pattern."));
1941 }
1942 }
1943 else if (type == LUA_TNUMBER) {
1944 wanted = lua_tointeger(L, 2);
1945 if (wanted < 0)
1946 WILL_LJMP(luaL_error(L, "Unsupported size."));
1947 }
1948 }
1949
1950 /* Set pattern. */
1951 lua_pushinteger(L, wanted);
Tim Duesterhusc6e377e2018-01-04 19:32:13 +01001952
1953 /* Check if we would replace the top by itself. */
1954 if (lua_gettop(L) != 2)
1955 lua_replace(L, 2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001956
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001957 /* init buffer, and fill it with prefix. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001958 luaL_buffinit(L, &socket->b);
1959
1960 /* Check prefix. */
1961 if (lua_gettop(L) >= 3) {
1962 if (lua_type(L, 3) != LUA_TSTRING)
1963 WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix"));
1964 pattern = lua_tolstring(L, 3, &len);
1965 luaL_addlstring(&socket->b, pattern, len);
1966 }
1967
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001968 return __LJMP(hlua_socket_receive_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001969}
1970
1971/* Write the Lua input string in the output buffer.
Mark Lakes22154b42018-01-29 14:38:40 -08001972 * This function returns a yield if no space is available.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001973 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001974static int hlua_socket_write_yield(struct lua_State *L,int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001975{
1976 struct hlua_socket *socket;
1977 struct hlua *hlua = hlua_gethlua(L);
1978 struct appctx *appctx;
1979 size_t buf_len;
1980 const char *buf;
1981 int len;
1982 int send_len;
1983 int sent;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001984 struct xref *peer;
1985 struct stream_interface *si;
1986 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001987
1988 /* Check if this lua stack is schedulable. */
1989 if (!hlua || !hlua->task)
1990 WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in "
1991 "'frontend', 'backend' or 'task'"));
1992
1993 /* Get object */
1994 socket = MAY_LJMP(hlua_checksocket(L, 1));
1995 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001996 sent = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001997
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001998 /* Check if we run on the same thread than the xreator thread.
1999 * We cannot access to the socket if the thread is different.
2000 */
2001 if (socket->tid != tid)
2002 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2003
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002004 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002005 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002006 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002007 lua_pushinteger(L, -1);
2008 return 1;
2009 }
2010 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2011 si = appctx->owner;
2012 s = si_strm(si);
2013
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002014 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002015 if (channel_output_closed(&s->req)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002016 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002017 lua_pushinteger(L, -1);
2018 return 1;
2019 }
2020
2021 /* Update the input buffer data. */
2022 buf += sent;
2023 send_len = buf_len - sent;
2024
2025 /* All the data are sent. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002026 if (sent >= buf_len) {
2027 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002028 return 1; /* Implicitly return the length sent. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002029 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002030
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002031 /* Check if the buffer is available because HAProxy doesn't allocate
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01002032 * the request buffer if its not required.
2033 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002034 if (s->req.buf.size == 0) {
Willy Tarreau581abd32018-10-25 10:21:41 +02002035 if (!si_alloc_ibuf(si, &appctx->buffer_wait))
Christopher Faulet33834b12016-12-19 09:29:06 +01002036 goto hlua_socket_write_yield_return;
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01002037 }
2038
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002039 /* Check for available space. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002040 len = b_room(&s->req.buf);
Christopher Faulet33834b12016-12-19 09:29:06 +01002041 if (len <= 0) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002042 goto hlua_socket_write_yield_return;
Christopher Faulet33834b12016-12-19 09:29:06 +01002043 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002044
2045 /* send data */
2046 if (len < send_len)
2047 send_len = len;
Thierry FOURNIER66b89192018-05-27 01:14:47 +02002048 len = ci_putblk(&s->req, buf, send_len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002049
2050 /* "Not enough space" (-1), "Buffer too little to contain
2051 * the data" (-2) are not expected because the available length
2052 * is tested.
2053 * Other unknown error are also not expected.
2054 */
2055 if (len <= 0) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01002056 if (len == -1)
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002057 s->req.flags |= CF_WAKE_WRITE;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002058
sada05ed3302018-05-11 11:48:18 -07002059 MAY_LJMP(hlua_socket_close_helper(L));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002060 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002061 lua_pushinteger(L, -1);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002062 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002063 return 1;
2064 }
2065
2066 /* update buffers. */
Thierry FOURNIER101b9762018-05-27 01:27:40 +02002067 appctx_wakeup(appctx);
Willy Tarreaude70fa12015-09-26 11:25:05 +02002068
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002069 s->req.rex = TICK_ETERNITY;
2070 s->res.wex = TICK_ETERNITY;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002071
2072 /* Update length sent. */
2073 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002074 lua_pushinteger(L, sent + len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002075
2076 /* All the data buffer is sent ? */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002077 if (sent + len >= buf_len) {
2078 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002079 return 1;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002080 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002081
2082hlua_socket_write_yield_return:
Thierry FOURNIERba42fcd2018-05-27 00:59:48 +02002083 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2084 xref_unlock(&socket->xref, peer);
2085 WILL_LJMP(luaL_error(L, "out of memory"));
2086 }
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002087 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002088 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_write_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002089 return 0;
2090}
2091
2092/* This function initiate the send of data. It just check the input
2093 * parameters and push an integer in the Lua stack that contain the
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002094 * amount of data written to the buffer. This is used by the function
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002095 * "hlua_socket_write_yield" that can yield.
2096 *
2097 * The Lua function gets between 3 and 4 parameters. The first one is
2098 * the associated object. The second is a string buffer. The third is
2099 * a facultative integer that represents where is the buffer position
2100 * of the start of the data that can send. The first byte is the
2101 * position "1". The default value is "1". The fourth argument is a
2102 * facultative integer that represents where is the buffer position
2103 * of the end of the data that can send. The default is the last byte.
2104 */
2105static int hlua_socket_send(struct lua_State *L)
2106{
2107 int i;
2108 int j;
2109 const char *buf;
2110 size_t buf_len;
2111
2112 /* Check number of arguments. */
2113 if (lua_gettop(L) < 2 || lua_gettop(L) > 4)
2114 WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments"));
2115
2116 /* Get the string. */
2117 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
2118
2119 /* Get and check j. */
2120 if (lua_gettop(L) == 4) {
2121 j = MAY_LJMP(luaL_checkinteger(L, 4));
2122 if (j < 0)
2123 j = buf_len + j + 1;
2124 if (j > buf_len)
2125 j = buf_len + 1;
2126 lua_pop(L, 1);
2127 }
2128 else
2129 j = buf_len;
2130
2131 /* Get and check i. */
2132 if (lua_gettop(L) == 3) {
2133 i = MAY_LJMP(luaL_checkinteger(L, 3));
2134 if (i < 0)
2135 i = buf_len + i + 1;
2136 if (i > buf_len)
2137 i = buf_len + 1;
2138 lua_pop(L, 1);
2139 } else
2140 i = 1;
2141
2142 /* Check bth i and j. */
2143 if (i > j) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002144 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002145 return 1;
2146 }
2147 if (i == 0 && j == 0) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002148 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002149 return 1;
2150 }
2151 if (i == 0)
2152 i = 1;
2153 if (j == 0)
2154 j = 1;
2155
2156 /* Pop the string. */
2157 lua_pop(L, 1);
2158
2159 /* Update the buffer length. */
2160 buf += i - 1;
2161 buf_len = j - i + 1;
2162 lua_pushlstring(L, buf, buf_len);
2163
2164 /* This unsigned is used to remember the amount of sent data. */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002165 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002166
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002167 return MAY_LJMP(hlua_socket_write_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002168}
2169
Willy Tarreau22b0a682015-06-17 19:43:49 +02002170#define SOCKET_INFO_MAX_LEN sizeof("[0000:0000:0000:0000:0000:0000:0000:0000]:12345")
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002171__LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr)
2172{
2173 static char buffer[SOCKET_INFO_MAX_LEN];
2174 int ret;
2175 int len;
2176 char *p;
2177
2178 ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1);
2179 if (ret <= 0) {
2180 lua_pushnil(L);
2181 return 1;
2182 }
2183
2184 if (ret == AF_UNIX) {
2185 lua_pushstring(L, buffer+1);
2186 return 1;
2187 }
2188 else if (ret == AF_INET6) {
2189 buffer[0] = '[';
2190 len = strlen(buffer);
2191 buffer[len] = ']';
2192 len++;
2193 buffer[len] = ':';
2194 len++;
2195 p = buffer;
2196 }
2197 else if (ret == AF_INET) {
2198 p = buffer + 1;
2199 len = strlen(p);
2200 p[len] = ':';
2201 len++;
2202 }
2203 else {
2204 lua_pushnil(L);
2205 return 1;
2206 }
2207
2208 if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) {
2209 lua_pushnil(L);
2210 return 1;
2211 }
2212
2213 lua_pushstring(L, p);
2214 return 1;
2215}
2216
2217/* Returns information about the peer of the connection. */
2218__LJMP static int hlua_socket_getpeername(struct lua_State *L)
2219{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002220 struct hlua_socket *socket;
2221 struct connection *conn;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002222 struct xref *peer;
2223 struct appctx *appctx;
2224 struct stream_interface *si;
2225 struct stream *s;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002226 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002227
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002228 MAY_LJMP(check_args(L, 1, "getpeername"));
2229
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002230 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002231
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002232 /* Check if we run on the same thread than the xreator thread.
2233 * We cannot access to the socket if the thread is different.
2234 */
2235 if (socket->tid != tid)
2236 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2237
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002238 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002239 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002240 if (!peer) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002241 lua_pushnil(L);
2242 return 1;
2243 }
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002244 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2245 si = appctx->owner;
2246 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002247
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002248 conn = cs_conn(objt_cs(s->si[1].end));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002249 if (!conn) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002250 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002251 lua_pushnil(L);
2252 return 1;
2253 }
2254
Willy Tarreaua71f6422016-11-16 17:00:14 +01002255 conn_get_to_addr(conn);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002256 if (!(conn->flags & CO_FL_ADDR_TO_SET)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002257 xref_unlock(&socket->xref, peer);
Willy Tarreaua71f6422016-11-16 17:00:14 +01002258 lua_pushnil(L);
2259 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002260 }
2261
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002262 ret = MAY_LJMP(hlua_socket_info(L, &conn->addr.to));
2263 xref_unlock(&socket->xref, peer);
2264 return ret;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002265}
2266
2267/* Returns information about my connection side. */
2268static int hlua_socket_getsockname(struct lua_State *L)
2269{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002270 struct hlua_socket *socket;
2271 struct connection *conn;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002272 struct appctx *appctx;
2273 struct xref *peer;
2274 struct stream_interface *si;
2275 struct stream *s;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002276 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002277
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002278 MAY_LJMP(check_args(L, 1, "getsockname"));
2279
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002280 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002281
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002282 /* Check if we run on the same thread than the xreator thread.
2283 * We cannot access to the socket if the thread is different.
2284 */
2285 if (socket->tid != tid)
2286 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2287
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002288 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002289 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002290 if (!peer) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002291 lua_pushnil(L);
2292 return 1;
2293 }
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002294 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2295 si = appctx->owner;
2296 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002297
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002298 conn = cs_conn(objt_cs(s->si[1].end));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002299 if (!conn) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002300 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002301 lua_pushnil(L);
2302 return 1;
2303 }
2304
Willy Tarreaua71f6422016-11-16 17:00:14 +01002305 conn_get_from_addr(conn);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002306 if (!(conn->flags & CO_FL_ADDR_FROM_SET)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002307 xref_unlock(&socket->xref, peer);
Willy Tarreaua71f6422016-11-16 17:00:14 +01002308 lua_pushnil(L);
2309 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002310 }
2311
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002312 ret = hlua_socket_info(L, &conn->addr.from);
2313 xref_unlock(&socket->xref, peer);
2314 return ret;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002315}
2316
2317/* This struct define the applet. */
Willy Tarreau30576452015-04-13 13:50:30 +02002318static struct applet update_applet = {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002319 .obj_type = OBJ_TYPE_APPLET,
2320 .name = "<LUA_TCP>",
2321 .fct = hlua_socket_handler,
2322 .release = hlua_socket_release,
2323};
2324
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002325__LJMP static int hlua_socket_connect_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002326{
2327 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
2328 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002329 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002330 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002331 struct stream_interface *si;
2332 struct stream *s;
2333
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002334 /* Check if we run on the same thread than the xreator thread.
2335 * We cannot access to the socket if the thread is different.
2336 */
2337 if (socket->tid != tid)
2338 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2339
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002340 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002341 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002342 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002343 lua_pushnil(L);
2344 lua_pushstring(L, "Can't connect");
2345 return 2;
2346 }
2347 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2348 si = appctx->owner;
2349 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002350
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002351 /* Check if we run on the same thread than the xreator thread.
2352 * We cannot access to the socket if the thread is different.
2353 */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002354 if (socket->tid != tid) {
2355 xref_unlock(&socket->xref, peer);
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002356 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002357 }
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002358
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002359 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002360 if (!hlua || channel_output_closed(&s->req)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002361 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002362 lua_pushnil(L);
2363 lua_pushstring(L, "Can't connect");
2364 return 2;
2365 }
2366
Willy Tarreaue09101e2018-10-16 17:37:12 +02002367 appctx = __objt_appctx(s->si[0].end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002368
2369 /* Check for connection established. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002370 if (appctx->ctx.hlua_cosocket.connected) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002371 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002372 lua_pushinteger(L, 1);
2373 return 1;
2374 }
2375
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002376 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2377 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002378 WILL_LJMP(luaL_error(L, "out of memory error"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002379 }
2380 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002381 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002382 return 0;
2383}
2384
2385/* This function fail or initite the connection. */
2386__LJMP static int hlua_socket_connect(struct lua_State *L)
2387{
2388 struct hlua_socket *socket;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002389 int port = -1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002390 const char *ip;
2391 struct connection *conn;
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002392 struct hlua *hlua;
2393 struct appctx *appctx;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002394 int low, high;
2395 struct sockaddr_storage *addr;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002396 struct xref *peer;
2397 struct stream_interface *si;
2398 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002399
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002400 if (lua_gettop(L) < 2)
2401 WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments"));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002402
2403 /* Get args. */
2404 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002405
2406 /* Check if we run on the same thread than the xreator thread.
2407 * We cannot access to the socket if the thread is different.
2408 */
2409 if (socket->tid != tid)
2410 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2411
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002412 ip = MAY_LJMP(luaL_checkstring(L, 2));
Tim Duesterhus6edab862018-01-06 19:04:45 +01002413 if (lua_gettop(L) >= 3) {
2414 luaL_Buffer b;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002415 port = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002416
Tim Duesterhus6edab862018-01-06 19:04:45 +01002417 /* Force the ip to end with a colon, to support IPv6 addresses
2418 * that are not enclosed within square brackets.
2419 */
2420 if (port > 0) {
2421 luaL_buffinit(L, &b);
2422 luaL_addstring(&b, ip);
2423 luaL_addchar(&b, ':');
2424 luaL_pushresult(&b);
2425 ip = lua_tolstring(L, lua_gettop(L), NULL);
2426 }
2427 }
2428
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002429 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002430 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002431 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002432 lua_pushnil(L);
2433 return 1;
2434 }
2435 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2436 si = appctx->owner;
2437 s = si_strm(si);
2438
2439 /* Initialise connection. */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002440 conn = cs_conn(si_alloc_cs(&s->si[1], NULL));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002441 if (!conn) {
2442 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002443 WILL_LJMP(luaL_error(L, "connect: internal error"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002444 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002445
Willy Tarreau3adac082015-09-26 17:51:09 +02002446 /* needed for the connection not to be closed */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002447 conn->target = s->target;
Willy Tarreau3adac082015-09-26 17:51:09 +02002448
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002449 /* Parse ip address. */
Willy Tarreau48ef4c92017-01-06 18:32:38 +01002450 addr = str2sa_range(ip, NULL, &low, &high, NULL, NULL, NULL, 0);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002451 if (!addr) {
2452 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002453 WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002454 }
2455 if (low != high) {
2456 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002457 WILL_LJMP(luaL_error(L, "connect: port ranges not supported : address '%s'", ip));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002458 }
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002459 memcpy(&conn->addr.to, addr, sizeof(struct sockaddr_storage));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002460
2461 /* Set port. */
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002462 if (low == 0) {
2463 if (conn->addr.to.ss_family == AF_INET) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002464 if (port == -1) {
2465 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002466 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002467 }
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002468 ((struct sockaddr_in *)&conn->addr.to)->sin_port = htons(port);
2469 } else if (conn->addr.to.ss_family == AF_INET6) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002470 if (port == -1) {
2471 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002472 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002473 }
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002474 ((struct sockaddr_in6 *)&conn->addr.to)->sin6_port = htons(port);
2475 }
2476 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002477
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002478 hlua = hlua_gethlua(L);
Willy Tarreaue09101e2018-10-16 17:37:12 +02002479 appctx = __objt_appctx(s->si[0].end);
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002480
2481 /* inform the stream that we want to be notified whenever the
2482 * connection completes.
2483 */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01002484 si_cant_get(&s->si[0]);
Willy Tarreau3367d412018-11-15 10:57:41 +01002485 si_rx_endp_more(&s->si[0]);
Thierry FOURNIER8c8fbbe2015-09-26 17:02:35 +02002486 appctx_wakeup(appctx);
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002487
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +02002488 hlua->flags |= HLUA_MUST_GC;
2489
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002490 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2491 xref_unlock(&socket->xref, peer);
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002492 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002493 }
2494 xref_unlock(&socket->xref, peer);
PiBa-NL706d5ee2018-05-05 23:51:42 +02002495
2496 task_wakeup(s->task, TASK_WOKEN_INIT);
2497 /* Return yield waiting for connection. */
2498
Willy Tarreau9635e032018-10-16 17:52:55 +02002499 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002500
2501 return 0;
2502}
2503
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002504#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002505__LJMP static int hlua_socket_connect_ssl(struct lua_State *L)
2506{
2507 struct hlua_socket *socket;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002508 struct xref *peer;
2509 struct appctx *appctx;
2510 struct stream_interface *si;
2511 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002512
2513 MAY_LJMP(check_args(L, 3, "connect_ssl"));
2514 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002515
2516 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002517 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002518 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002519 lua_pushnil(L);
2520 return 1;
2521 }
2522 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2523 si = appctx->owner;
2524 s = si_strm(si);
2525
2526 s->target = &socket_ssl.obj_type;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002527 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002528 return MAY_LJMP(hlua_socket_connect(L));
2529}
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002530#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002531
2532__LJMP static int hlua_socket_setoption(struct lua_State *L)
2533{
2534 return 0;
2535}
2536
2537__LJMP static int hlua_socket_settimeout(struct lua_State *L)
2538{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002539 struct hlua_socket *socket;
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002540 int tmout;
Mark Lakes56cc1252018-03-27 09:48:06 +02002541 double dtmout;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002542 struct xref *peer;
2543 struct appctx *appctx;
2544 struct stream_interface *si;
2545 struct stream *s;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002546
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002547 MAY_LJMP(check_args(L, 2, "settimeout"));
2548
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002549 socket = MAY_LJMP(hlua_checksocket(L, 1));
Mark Lakes56cc1252018-03-27 09:48:06 +02002550
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002551 /* convert the timeout to millis */
2552 dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002553
Thierry Fournier17a921b2018-03-08 09:59:02 +01002554 /* Check for negative values */
Mark Lakes56cc1252018-03-27 09:48:06 +02002555 if (dtmout < 0)
Thierry Fournier17a921b2018-03-08 09:59:02 +01002556 WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values"));
2557
Mark Lakes56cc1252018-03-27 09:48:06 +02002558 if (dtmout > INT_MAX) /* overflow check */
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002559 WILL_LJMP(luaL_error(L, "settimeout: cannot set values larger than %d ms", INT_MAX));
Mark Lakes56cc1252018-03-27 09:48:06 +02002560
2561 tmout = MS_TO_TICKS((int)dtmout);
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002562 if (tmout == 0)
2563 tmout++; /* very small timeouts are adjusted to a minium of 1ms */
Mark Lakes56cc1252018-03-27 09:48:06 +02002564
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002565 /* Check if we run on the same thread than the xreator thread.
2566 * We cannot access to the socket if the thread is different.
2567 */
2568 if (socket->tid != tid)
2569 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2570
Mark Lakes56cc1252018-03-27 09:48:06 +02002571 /* check for connection break. If some data were read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002572 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002573 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002574 hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts.");
2575 WILL_LJMP(lua_error(L));
2576 return 0;
2577 }
2578 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2579 si = appctx->owner;
2580 s = si_strm(si);
2581
Cyril Bonté7bb63452018-08-17 23:51:02 +02002582 s->sess->fe->timeout.connect = tmout;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002583 s->req.rto = tmout;
2584 s->req.wto = tmout;
2585 s->res.rto = tmout;
2586 s->res.wto = tmout;
Cyril Bonté7bb63452018-08-17 23:51:02 +02002587 s->req.rex = tick_add_ifset(now_ms, tmout);
2588 s->req.wex = tick_add_ifset(now_ms, tmout);
2589 s->res.rex = tick_add_ifset(now_ms, tmout);
2590 s->res.wex = tick_add_ifset(now_ms, tmout);
2591
2592 s->task->expire = tick_add_ifset(now_ms, tmout);
2593 task_queue(s->task);
2594
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002595 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002596
Thierry Fourniere9636f12018-03-08 09:54:32 +01002597 lua_pushinteger(L, 1);
Tim Duesterhus119a5f12018-01-06 19:16:25 +01002598 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002599}
2600
2601__LJMP static int hlua_socket_new(lua_State *L)
2602{
2603 struct hlua_socket *socket;
2604 struct appctx *appctx;
Willy Tarreau15b5e142015-04-04 14:38:25 +02002605 struct session *sess;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002606 struct stream *strm;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002607
2608 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002609 if (!lua_checkstack(L, 3)) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002610 hlua_pusherror(L, "socket: full stack");
2611 goto out_fail_conf;
2612 }
2613
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002614 /* Create the object: obj[0] = userdata. */
2615 lua_newtable(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002616 socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket)));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002617 lua_rawseti(L, -2, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002618 memset(socket, 0, sizeof(*socket));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002619 socket->tid = tid;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002620
Thierry FOURNIER4a6170c2015-03-09 17:07:10 +01002621 /* Check if the various memory pools are intialized. */
Willy Tarreaubafbe012017-11-24 17:34:44 +01002622 if (!pool_head_stream || !pool_head_buffer) {
Thierry FOURNIER4a6170c2015-03-09 17:07:10 +01002623 hlua_pusherror(L, "socket: uninitialized pools.");
2624 goto out_fail_conf;
2625 }
2626
Willy Tarreau87b09662015-04-03 00:22:06 +02002627 /* Pop a class stream metatable and affect it to the userdata. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002628 lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref);
2629 lua_setmetatable(L, -2);
2630
Willy Tarreaud420a972015-04-06 00:39:18 +02002631 /* Create the applet context */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01002632 appctx = appctx_new(&update_applet, tid_bit);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002633 if (!appctx) {
2634 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaufeb76402015-04-03 14:10:06 +02002635 goto out_fail_conf;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002636 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002637
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002638 appctx->ctx.hlua_cosocket.connected = 0;
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02002639 appctx->ctx.hlua_cosocket.die = 0;
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002640 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write);
2641 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read);
Willy Tarreaub2bf8332015-04-04 15:58:58 +02002642
Willy Tarreaud420a972015-04-06 00:39:18 +02002643 /* Now create a session, task and stream for this applet */
2644 sess = session_new(&socket_proxy, NULL, &appctx->obj_type);
2645 if (!sess) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002646 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002647 goto out_fail_sess;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002648 }
2649
Willy Tarreau87787ac2017-08-28 16:22:54 +02002650 strm = stream_new(sess, &appctx->obj_type);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002651 if (!strm) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002652 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002653 goto out_fail_stream;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002654 }
2655
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002656 /* Initialise cross reference between stream and Lua socket object. */
2657 xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002658
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002659 /* Configure "right" stream interface. this "si" is used to connect
2660 * and retrieve data from the server. The connection is initialized
2661 * with the "struct server".
2662 */
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002663 si_set_state(&strm->si[1], SI_ST_ASS);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002664
2665 /* Force destination server. */
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002666 strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET | SF_BE_ASSIGNED;
2667 strm->target = &socket_tcp.obj_type;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002668
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002669 return 1;
2670
Willy Tarreaud420a972015-04-06 00:39:18 +02002671 out_fail_stream:
Willy Tarreau11c36242015-04-04 15:54:03 +02002672 session_free(sess);
Willy Tarreaud420a972015-04-06 00:39:18 +02002673 out_fail_sess:
2674 appctx_free(appctx);
2675 out_fail_conf:
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002676 WILL_LJMP(lua_error(L));
2677 return 0;
2678}
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01002679
2680/*
2681 *
2682 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002683 * Class Channel
2684 *
2685 *
2686 */
2687
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002688/* This function is called before the Lua execution. It stores
2689 * the differents parsers state before executing some Lua code.
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002690 */
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002691static inline void consistency_set(struct stream *stream, int opt, struct hlua_consistency *c)
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002692{
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002693 c->mode = stream->be->mode;
2694 switch (c->mode) {
2695 case PR_MODE_HTTP:
2696 c->data.http.dir = opt & SMP_OPT_DIR;
2697 if (c->data.http.dir == SMP_OPT_DIR_REQ)
2698 c->data.http.state = stream->txn->req.msg_state;
2699 else
2700 c->data.http.state = stream->txn->rsp.msg_state;
2701 break;
2702 default:
2703 break;
2704 }
2705}
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002706
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002707/* This function is called after the Lua execution. it
2708 * returns true if the parser state is consistent, otherwise,
2709 * it return false.
2710 *
2711 * In HTTP mode, the parser state must be in the same state
2712 * or greater when we exit the function. Even if we do a
2713 * control yield. This prevent to break the HTTP message
2714 * from the Lua code.
2715 */
2716static inline int consistency_check(struct stream *stream, int opt, struct hlua_consistency *c)
2717{
2718 if (c->mode != stream->be->mode)
2719 return 0;
2720
2721 switch (c->mode) {
2722 case PR_MODE_HTTP:
2723 if (c->data.http.dir != (opt & SMP_OPT_DIR))
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002724 return 0;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002725 if (c->data.http.dir == SMP_OPT_DIR_REQ)
2726 return stream->txn->req.msg_state >= c->data.http.state;
2727 else
2728 return stream->txn->rsp.msg_state >= c->data.http.state;
2729 default:
2730 return 1;
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002731 }
2732 return 1;
2733}
2734
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002735/* Returns the struct hlua_channel join to the class channel in the
2736 * stack entry "ud" or throws an argument error.
2737 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002738__LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002739{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02002740 return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002741}
2742
Willy Tarreau47860ed2015-03-10 14:07:50 +01002743/* Pushes the channel onto the top of the stack. If the stask does not have a
2744 * free slots, the function fails and returns 0;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002745 */
Willy Tarreau2a71af42015-03-10 13:51:50 +01002746static int hlua_channel_new(lua_State *L, struct channel *channel)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002747{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002748 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002749 if (!lua_checkstack(L, 3))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002750 return 0;
2751
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002752 lua_newtable(L);
Willy Tarreau47860ed2015-03-10 14:07:50 +01002753 lua_pushlightuserdata(L, channel);
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002754 lua_rawseti(L, -2, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002755
2756 /* Pop a class sesison metatable and affect it to the userdata. */
2757 lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref);
2758 lua_setmetatable(L, -2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002759 return 1;
2760}
2761
2762/* Duplicate all the data present in the input channel and put it
2763 * in a string LUA variables. Returns -1 and push a nil value in
2764 * the stack if the channel is closed and all the data are consumed,
2765 * returns 0 if no data are available, otherwise it returns the length
2766 * of the builded string.
2767 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002768static inline int _hlua_channel_dup(struct channel *chn, lua_State *L)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002769{
2770 char *blk1;
2771 char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02002772 size_t len1;
2773 size_t len2;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002774 int ret;
2775 luaL_Buffer b;
2776
Willy Tarreau06d80a92017-10-19 14:32:15 +02002777 ret = ci_getblk_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002778 if (unlikely(ret == 0))
2779 return 0;
2780
2781 if (unlikely(ret < 0)) {
2782 lua_pushnil(L);
2783 return -1;
2784 }
2785
2786 luaL_buffinit(L, &b);
2787 luaL_addlstring(&b, blk1, len1);
2788 if (unlikely(ret == 2))
2789 luaL_addlstring(&b, blk2, len2);
2790 luaL_pushresult(&b);
2791
2792 if (unlikely(ret == 2))
2793 return len1 + len2;
2794 return len1;
2795}
2796
2797/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2798 * a yield. This function keep the data in the buffer.
2799 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002800__LJMP static int hlua_channel_dup_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002801{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002802 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002803
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002804 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2805
Christopher Faulet3f829a42018-12-13 21:56:45 +01002806 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2807 WILL_LJMP(lua_error(L));
2808
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002809 if (_hlua_channel_dup(chn, L) == 0)
Willy Tarreau9635e032018-10-16 17:52:55 +02002810 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_dup_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002811 return 1;
2812}
2813
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002814/* Check arguments for the function "hlua_channel_dup_yield". */
2815__LJMP static int hlua_channel_dup(lua_State *L)
2816{
2817 MAY_LJMP(check_args(L, 1, "dup"));
2818 MAY_LJMP(hlua_checkchannel(L, 1));
2819 return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0));
2820}
2821
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002822/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2823 * a yield. This function consumes the data in the buffer. It returns
2824 * a string containing the data or a nil pointer if no data are available
2825 * and the channel is closed.
2826 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002827__LJMP static int hlua_channel_get_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002828{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002829 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002830 int ret;
2831
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002832 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002833
Christopher Faulet3f829a42018-12-13 21:56:45 +01002834 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2835 WILL_LJMP(lua_error(L));
2836
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002837 ret = _hlua_channel_dup(chn, L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002838 if (unlikely(ret == 0))
Willy Tarreau9635e032018-10-16 17:52:55 +02002839 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002840
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002841 if (unlikely(ret == -1))
2842 return 1;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002843
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002844 b_sub(&chn->buf, ret);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002845 return 1;
2846}
2847
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002848/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002849__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
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002858 * without the final '\n'. When no more data are available, it returns nil
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002859 * 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;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02002865 size_t len1;
2866 size_t len2;
2867 size_t 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
Christopher Faulet3f829a42018-12-13 21:56:45 +01002874 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2875 WILL_LJMP(lua_error(L));
2876
Willy Tarreau06d80a92017-10-19 14:32:15 +02002877 ret = ci_getline_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002878 if (ret == 0)
Willy Tarreau9635e032018-10-16 17:52:55 +02002879 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_getline_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002880
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002881 if (ret == -1) {
2882 lua_pushnil(L);
2883 return 1;
2884 }
2885
2886 luaL_buffinit(L, &b);
2887 luaL_addlstring(&b, blk1, len1);
2888 len = len1;
2889 if (unlikely(ret == 2)) {
2890 luaL_addlstring(&b, blk2, len2);
2891 len += len2;
2892 }
2893 luaL_pushresult(&b);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002894 b_rep_blk(&chn->buf, ci_head(chn), ci_head(chn) + len, NULL, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002895 return 1;
2896}
2897
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002898/* Check arguments for the function "hlua_channel_getline_yield". */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002899__LJMP static int hlua_channel_getline(lua_State *L)
2900{
2901 MAY_LJMP(check_args(L, 1, "getline"));
2902 MAY_LJMP(hlua_checkchannel(L, 1));
2903 return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0));
2904}
2905
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002906/* This function takes a string as input, and append it at the
2907 * input side of channel. If the data is too big, but a space
2908 * is probably available after sending some data, the function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002909 * yields. If the data is bigger than the buffer, or if the
2910 * channel is closed, it returns -1. Otherwise, it returns the
2911 * amount of data written.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002912 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002913__LJMP static int hlua_channel_append_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002914{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002915 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002916 size_t len;
2917 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
2918 int l = MAY_LJMP(luaL_checkinteger(L, 3));
2919 int ret;
2920 int max;
2921
Christopher Faulet3f829a42018-12-13 21:56:45 +01002922 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2923 WILL_LJMP(lua_error(L));
2924
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002925 /* Check if the buffer is available because HAProxy doesn't allocate
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002926 * the request buffer if its not required.
2927 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002928 if (chn->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +01002929 si_rx_buff_blk(chn_prod(chn));
Willy Tarreau9635e032018-10-16 17:52:55 +02002930 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002931 }
2932
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002933 max = channel_recv_limit(chn) - b_data(&chn->buf);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002934 if (max > len - l)
2935 max = len - l;
2936
Willy Tarreau06d80a92017-10-19 14:32:15 +02002937 ret = ci_putblk(chn, str + l, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002938 if (ret == -2 || ret == -3) {
2939 lua_pushinteger(L, -1);
2940 return 1;
2941 }
Willy Tarreaubc18da12015-03-13 14:00:47 +01002942 if (ret == -1) {
2943 chn->flags |= CF_WAKE_WRITE;
Willy Tarreau9635e032018-10-16 17:52:55 +02002944 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Willy Tarreaubc18da12015-03-13 14:00:47 +01002945 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002946 l += ret;
2947 lua_pop(L, 1);
2948 lua_pushinteger(L, l);
2949
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002950 max = channel_recv_limit(chn) - b_data(&chn->buf);
Willy Tarreaua79021a2018-06-15 18:07:57 +02002951 if (max == 0 && co_data(chn) == 0) {
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002952 /* There are no space available, and the output buffer is empty.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002953 * in this case, we cannot add more data, so we cannot yield,
2954 * we return the amount of copyied data.
2955 */
2956 return 1;
2957 }
2958 if (l < len)
Willy Tarreau9635e032018-10-16 17:52:55 +02002959 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002960 return 1;
2961}
2962
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002963/* Just a wrapper of "hlua_channel_append_yield". It returns the length
2964 * of the written string, or -1 if the channel is closed or if the
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002965 * buffer size is too little for the data.
2966 */
2967__LJMP static int hlua_channel_append(lua_State *L)
2968{
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002969 size_t len;
2970
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002971 MAY_LJMP(check_args(L, 2, "append"));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002972 MAY_LJMP(hlua_checkchannel(L, 1));
2973 MAY_LJMP(luaL_checklstring(L, 2, &len));
2974 MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002975 lua_pushinteger(L, 0);
2976
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002977 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002978}
2979
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002980/* Just a wrapper of "hlua_channel_append_yield". This wrapper starts
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002981 * his process by cleaning the buffer. The result is a replacement
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002982 * of the current data. It returns the length of the written string,
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002983 * or -1 if the channel is closed or if the buffer size is too
2984 * little for the data.
2985 */
2986__LJMP static int hlua_channel_set(lua_State *L)
2987{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002988 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002989
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002990 MAY_LJMP(check_args(L, 2, "set"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002991 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002992 lua_pushinteger(L, 0);
2993
Christopher Faulet3f829a42018-12-13 21:56:45 +01002994 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2995 WILL_LJMP(lua_error(L));
2996
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002997 b_set_data(&chn->buf, co_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002998
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002999 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003000}
3001
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003002/* Append data in the output side of the buffer. This data is immediately
3003 * sent. The function returns the amount of data written. If the buffer
3004 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003005 * if the channel is closed.
3006 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003007__LJMP static int hlua_channel_send_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003008{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003009 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003010 size_t len;
3011 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
3012 int l = MAY_LJMP(luaL_checkinteger(L, 3));
3013 int max;
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003014 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003015
Christopher Faulet3f829a42018-12-13 21:56:45 +01003016 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
3017 WILL_LJMP(lua_error(L));
3018
Willy Tarreau47860ed2015-03-10 14:07:50 +01003019 if (unlikely(channel_output_closed(chn))) {
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003020 lua_pushinteger(L, -1);
3021 return 1;
3022 }
3023
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003024 /* Check if the buffer is available because HAProxy doesn't allocate
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01003025 * the request buffer if its not required.
3026 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003027 if (chn->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +01003028 si_rx_buff_blk(chn_prod(chn));
Willy Tarreau9635e032018-10-16 17:52:55 +02003029 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01003030 }
3031
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003032 /* The written data will be immediately sent, so we can check
3033 * the available space without taking in account the reserve.
3034 * The reserve is guaranteed for the processing of incoming
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003035 * data, because the buffer will be flushed.
3036 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003037 max = b_room(&chn->buf);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003038
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003039 /* If there is no space available, and the output buffer is empty.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003040 * in this case, we cannot add more data, so we cannot yield,
3041 * we return the amount of copyied data.
3042 */
Willy Tarreaua79021a2018-06-15 18:07:57 +02003043 if (max == 0 && co_data(chn) == 0)
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003044 return 1;
3045
3046 /* Adjust the real required length. */
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003047 if (max > len - l)
3048 max = len - l;
3049
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003050 /* The buffer available size may be not contiguous. This test
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003051 * detects a non contiguous buffer and realign it.
3052 */
Willy Tarreau3f679992018-06-15 15:06:42 +02003053 if (ci_space_for_replace(chn) < max)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003054 channel_slow_realign(chn, trash.area);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003055
3056 /* Copy input data in the buffer. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003057 max = b_rep_blk(&chn->buf, ci_head(chn), ci_head(chn), str + l, max);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003058
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003059 /* buffer replace considers that the input part is filled.
3060 * so, I must forward these new data in the output part.
3061 */
Willy Tarreaubcbd3932018-06-06 07:13:22 +02003062 c_adv(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003063
3064 l += max;
3065 lua_pop(L, 1);
3066 lua_pushinteger(L, l);
3067
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003068 /* If there is no space available, and the output buffer is empty.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003069 * in this case, we cannot add more data, so we cannot yield,
3070 * we return the amount of copyied data.
3071 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003072 max = b_room(&chn->buf);
Willy Tarreaua79021a2018-06-15 18:07:57 +02003073 if (max == 0 && co_data(chn) == 0)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003074 return 1;
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003075
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003076 if (l < len) {
3077 /* If we are waiting for space in the response buffer, we
3078 * must set the flag WAKERESWR. This flag required the task
3079 * wake up if any activity is detected on the response buffer.
3080 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003081 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003082 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01003083 else
3084 HLUA_SET_WAKEREQWR(hlua);
Willy Tarreau9635e032018-10-16 17:52:55 +02003085 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003086 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003087
3088 return 1;
3089}
3090
3091/* Just a wraper of "_hlua_channel_send". This wrapper permits
3092 * yield the LUA process, and resume it without checking the
3093 * input arguments.
3094 */
3095__LJMP static int hlua_channel_send(lua_State *L)
3096{
3097 MAY_LJMP(check_args(L, 2, "send"));
3098 lua_pushinteger(L, 0);
3099
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003100 return MAY_LJMP(hlua_channel_send_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003101}
3102
3103/* This function forward and amount of butes. The data pass from
3104 * the input side of the buffer to the output side, and can be
3105 * forwarded. This function never fails.
3106 *
3107 * The Lua function takes an amount of bytes to be forwarded in
3108 * imput. It returns the number of bytes forwarded.
3109 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003110__LJMP static int hlua_channel_forward_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003111{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003112 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003113 int len;
3114 int l;
3115 int max;
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003116 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003117
3118 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Faulet3f829a42018-12-13 21:56:45 +01003119
3120 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
3121 WILL_LJMP(lua_error(L));
3122
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003123 len = MAY_LJMP(luaL_checkinteger(L, 2));
3124 l = MAY_LJMP(luaL_checkinteger(L, -1));
3125
3126 max = len - l;
Willy Tarreaua79021a2018-06-15 18:07:57 +02003127 if (max > ci_data(chn))
3128 max = ci_data(chn);
Willy Tarreau47860ed2015-03-10 14:07:50 +01003129 channel_forward(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003130 l += max;
3131
3132 lua_pop(L, 1);
3133 lua_pushinteger(L, l);
3134
3135 /* Check if it miss bytes to forward. */
3136 if (l < len) {
3137 /* The the input channel or the output channel are closed, we
3138 * must return the amount of data forwarded.
3139 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003140 if (channel_input_closed(chn) || channel_output_closed(chn))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003141 return 1;
3142
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003143 /* If we are waiting for space data in the response buffer, we
3144 * must set the flag WAKERESWR. This flag required the task
3145 * wake up if any activity is detected on the response buffer.
3146 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003147 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003148 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01003149 else
3150 HLUA_SET_WAKEREQWR(hlua);
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003151
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003152 /* Otherwise, we can yield waiting for new data in the inpout side. */
Willy Tarreau9635e032018-10-16 17:52:55 +02003153 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_forward_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003154 }
3155
3156 return 1;
3157}
3158
3159/* Just check the input and prepare the stack for the previous
3160 * function "hlua_channel_forward_yield"
3161 */
3162__LJMP static int hlua_channel_forward(lua_State *L)
3163{
3164 MAY_LJMP(check_args(L, 2, "forward"));
3165 MAY_LJMP(hlua_checkchannel(L, 1));
3166 MAY_LJMP(luaL_checkinteger(L, 2));
3167
3168 lua_pushinteger(L, 0);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003169 return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003170}
3171
3172/* Just returns the number of bytes available in the input
3173 * side of the buffer. This function never fails.
3174 */
3175__LJMP static int hlua_channel_get_in_len(lua_State *L)
3176{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003177 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003178
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003179 MAY_LJMP(check_args(L, 1, "get_in_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003180 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Willy Tarreaua79021a2018-06-15 18:07:57 +02003181 lua_pushinteger(L, ci_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003182 return 1;
3183}
3184
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003185/* Returns true if the channel is full. */
3186__LJMP static int hlua_channel_is_full(lua_State *L)
3187{
3188 struct channel *chn;
3189 int rem;
3190
3191 MAY_LJMP(check_args(L, 1, "is_full"));
3192 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3193
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003194 rem = b_room(&chn->buf);
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003195 rem -= global.tune.maxrewrite; /* Rewrite reserved size */
3196
3197 lua_pushboolean(L, rem <= 0);
3198 return 1;
3199}
3200
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003201/* Just returns the number of bytes available in the output
3202 * side of the buffer. This function never fails.
3203 */
3204__LJMP static int hlua_channel_get_out_len(lua_State *L)
3205{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003206 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003207
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003208 MAY_LJMP(check_args(L, 1, "get_out_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003209 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Willy Tarreaua79021a2018-06-15 18:07:57 +02003210 lua_pushinteger(L, co_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003211 return 1;
3212}
3213
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003214/*
3215 *
3216 *
3217 * Class Fetches
3218 *
3219 *
3220 */
3221
3222/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003223 * a class stream, otherwise it throws an error.
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003224 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003225__LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003226{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003227 return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003228}
3229
3230/* This function creates and push in the stack a fetch object according
3231 * with a current TXN.
3232 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003233static int hlua_fetches_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003234{
Willy Tarreau7073c472015-04-06 11:15:40 +02003235 struct hlua_smp *hsmp;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003236
3237 /* Check stack size. */
3238 if (!lua_checkstack(L, 3))
3239 return 0;
3240
3241 /* Create the object: obj[0] = userdata.
3242 * Note that the base of the Fetches object is the
3243 * transaction object.
3244 */
3245 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003246 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003247 lua_rawseti(L, -2, 0);
3248
Willy Tarreau7073c472015-04-06 11:15:40 +02003249 hsmp->s = txn->s;
3250 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003251 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003252 hsmp->flags = flags;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003253
3254 /* Pop a class sesison metatable and affect it to the userdata. */
3255 lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref);
3256 lua_setmetatable(L, -2);
3257
3258 return 1;
3259}
3260
3261/* This function is an LUA binding. It is called with each sample-fetch.
3262 * It uses closure argument to store the associated sample-fetch. It
3263 * returns only one argument or throws an error. An error is thrown
3264 * only if an error is encountered during the argument parsing. If
3265 * the "sample-fetch" function fails, nil is returned.
3266 */
3267__LJMP static int hlua_run_sample_fetch(lua_State *L)
3268{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003269 struct hlua_smp *hsmp;
Willy Tarreau2ec22742015-03-10 14:27:20 +01003270 struct sample_fetch *f;
Frédéric Lécaillef874a832018-06-15 13:56:04 +02003271 struct arg args[ARGM_NBARGS + 1] = {{0}};
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003272 int i;
3273 struct sample smp;
3274
3275 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003276 f = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003277
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003278 /* Get traditional arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003279 hsmp = MAY_LJMP(hlua_checkfetches(L, 1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003280
Thierry FOURNIERca988662015-12-20 18:43:03 +01003281 /* Check execution authorization. */
3282 if (f->use & SMP_USE_HTTP_ANY &&
3283 !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) {
3284 lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which "
3285 "is not available in Lua services", f->kw);
3286 WILL_LJMP(lua_error(L));
3287 }
3288
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003289 /* Get extra arguments. */
3290 for (i = 0; i < lua_gettop(L) - 1; i++) {
3291 if (i >= ARGM_NBARGS)
3292 break;
3293 hlua_lua2arg(L, i + 2, &args[i]);
3294 }
3295 args[i].type = ARGT_STOP;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003296 args[i].data.str.area = NULL;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003297
3298 /* Check arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003299 MAY_LJMP(hlua_lua2arg_check(L, 2, args, f->arg_mask, hsmp->p));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003300
3301 /* Run the special args checker. */
Willy Tarreau2ec22742015-03-10 14:27:20 +01003302 if (f->val_args && !f->val_args(args, NULL)) {
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003303 lua_pushfstring(L, "error in arguments");
3304 WILL_LJMP(lua_error(L));
3305 }
3306
3307 /* Initialise the sample. */
3308 memset(&smp, 0, sizeof(smp));
3309
3310 /* Run the sample fetch process. */
Willy Tarreau1777ea62016-03-10 16:15:46 +01003311 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003312 if (!f->process(args, &smp, f->kw, f->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003313 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003314 lua_pushstring(L, "");
3315 else
3316 lua_pushnil(L);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003317 return 1;
3318 }
3319
3320 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003321 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003322 hlua_smp2lua_str(L, &smp);
3323 else
3324 hlua_smp2lua(L, &smp);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003325 return 1;
3326}
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003327
3328/*
3329 *
3330 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003331 * Class Converters
3332 *
3333 *
3334 */
3335
3336/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003337 * a class stream, otherwise it throws an error.
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003338 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003339__LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003340{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003341 return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003342}
3343
3344/* This function creates and push in the stack a Converters object
3345 * according with a current TXN.
3346 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003347static int hlua_converters_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003348{
Willy Tarreau7073c472015-04-06 11:15:40 +02003349 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003350
3351 /* Check stack size. */
3352 if (!lua_checkstack(L, 3))
3353 return 0;
3354
3355 /* Create the object: obj[0] = userdata.
3356 * Note that the base of the Converters object is the
3357 * same than the TXN object.
3358 */
3359 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003360 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003361 lua_rawseti(L, -2, 0);
3362
Willy Tarreau7073c472015-04-06 11:15:40 +02003363 hsmp->s = txn->s;
3364 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003365 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003366 hsmp->flags = flags;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003367
Willy Tarreau87b09662015-04-03 00:22:06 +02003368 /* Pop a class stream metatable and affect it to the table. */
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003369 lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref);
3370 lua_setmetatable(L, -2);
3371
3372 return 1;
3373}
3374
3375/* This function is an LUA binding. It is called with each converter.
3376 * It uses closure argument to store the associated converter. It
3377 * returns only one argument or throws an error. An error is thrown
3378 * only if an error is encountered during the argument parsing. If
3379 * the converter function function fails, nil is returned.
3380 */
3381__LJMP static int hlua_run_sample_conv(lua_State *L)
3382{
Willy Tarreauda5f1082015-04-06 11:17:13 +02003383 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003384 struct sample_conv *conv;
Frédéric Lécaillef874a832018-06-15 13:56:04 +02003385 struct arg args[ARGM_NBARGS + 1] = {{0}};
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003386 int i;
3387 struct sample smp;
3388
3389 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003390 conv = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003391
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003392 /* Get traditional arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003393 hsmp = MAY_LJMP(hlua_checkconverters(L, 1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003394
3395 /* Get extra arguments. */
3396 for (i = 0; i < lua_gettop(L) - 2; i++) {
3397 if (i >= ARGM_NBARGS)
3398 break;
3399 hlua_lua2arg(L, i + 3, &args[i]);
3400 }
3401 args[i].type = ARGT_STOP;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003402 args[i].data.str.area = NULL;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003403
3404 /* Check arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003405 MAY_LJMP(hlua_lua2arg_check(L, 3, args, conv->arg_mask, hsmp->p));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003406
3407 /* Run the special args checker. */
3408 if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) {
3409 hlua_pusherror(L, "error in arguments");
3410 WILL_LJMP(lua_error(L));
3411 }
3412
3413 /* Initialise the sample. */
3414 if (!hlua_lua2smp(L, 2, &smp)) {
3415 hlua_pusherror(L, "error in the input argument");
3416 WILL_LJMP(lua_error(L));
3417 }
3418
Willy Tarreau1777ea62016-03-10 16:15:46 +01003419 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
3420
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003421 /* Apply expected cast. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003422 if (!sample_casts[smp.data.type][conv->in_type]) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003423 hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'",
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003424 smp_to_type[smp.data.type], smp_to_type[conv->in_type]);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003425 WILL_LJMP(lua_error(L));
3426 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003427 if (sample_casts[smp.data.type][conv->in_type] != c_none &&
3428 !sample_casts[smp.data.type][conv->in_type](&smp)) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003429 hlua_pusherror(L, "error during the input argument casting");
3430 WILL_LJMP(lua_error(L));
3431 }
3432
3433 /* Run the sample conversion process. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003434 if (!conv->process(args, &smp, conv->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003435 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003436 lua_pushstring(L, "");
3437 else
Willy Tarreaua678b432015-08-28 10:14:59 +02003438 lua_pushnil(L);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003439 return 1;
3440 }
3441
3442 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003443 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003444 hlua_smp2lua_str(L, &smp);
3445 else
3446 hlua_smp2lua(L, &smp);
Willy Tarreaua678b432015-08-28 10:14:59 +02003447 return 1;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003448}
3449
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003450/*
3451 *
3452 *
3453 * Class AppletTCP
3454 *
3455 *
3456 */
3457
3458/* Returns a struct hlua_txn if the stack entry "ud" is
3459 * a class stream, otherwise it throws an error.
3460 */
3461__LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud)
3462{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003463 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003464}
3465
3466/* This function creates and push in the stack an Applet object
3467 * according with a current TXN.
3468 */
3469static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx)
3470{
3471 struct hlua_appctx *appctx;
3472 struct stream_interface *si = ctx->owner;
3473 struct stream *s = si_strm(si);
3474 struct proxy *p = s->be;
3475
3476 /* Check stack size. */
3477 if (!lua_checkstack(L, 3))
3478 return 0;
3479
3480 /* Create the object: obj[0] = userdata.
3481 * Note that the base of the Converters object is the
3482 * same than the TXN object.
3483 */
3484 lua_newtable(L);
3485 appctx = lua_newuserdata(L, sizeof(*appctx));
3486 lua_rawseti(L, -2, 0);
3487 appctx->appctx = ctx;
3488 appctx->htxn.s = s;
3489 appctx->htxn.p = p;
3490
3491 /* Create the "f" field that contains a list of fetches. */
3492 lua_pushstring(L, "f");
3493 if (!hlua_fetches_new(L, &appctx->htxn, 0))
3494 return 0;
3495 lua_settable(L, -3);
3496
3497 /* Create the "sf" field that contains a list of stringsafe fetches. */
3498 lua_pushstring(L, "sf");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003499 if (!hlua_fetches_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 /* Create the "c" field that contains a list of converters. */
3504 lua_pushstring(L, "c");
3505 if (!hlua_converters_new(L, &appctx->htxn, 0))
3506 return 0;
3507 lua_settable(L, -3);
3508
3509 /* Create the "sc" field that contains a list of stringsafe converters. */
3510 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003511 if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003512 return 0;
3513 lua_settable(L, -3);
3514
3515 /* Pop a class stream metatable and affect it to the table. */
3516 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref);
3517 lua_setmetatable(L, -2);
3518
3519 return 1;
3520}
3521
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003522__LJMP static int hlua_applet_tcp_set_var(lua_State *L)
3523{
3524 struct hlua_appctx *appctx;
3525 struct stream *s;
3526 const char *name;
3527 size_t len;
3528 struct sample smp;
3529
3530 MAY_LJMP(check_args(L, 3, "set_var"));
3531
3532 /* It is useles to retrieve the stream, but this function
3533 * runs only in a stream context.
3534 */
3535 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3536 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3537 s = appctx->htxn.s;
3538
3539 /* Converts the third argument in a sample. */
3540 hlua_lua2smp(L, 3, &smp);
3541
3542 /* Store the sample in a variable. */
3543 smp_set_owner(&smp, s->be, s->sess, s, 0);
3544 vars_set_by_name(name, len, &smp);
3545 return 0;
3546}
3547
3548__LJMP static int hlua_applet_tcp_unset_var(lua_State *L)
3549{
3550 struct hlua_appctx *appctx;
3551 struct stream *s;
3552 const char *name;
3553 size_t len;
3554 struct sample smp;
3555
3556 MAY_LJMP(check_args(L, 2, "unset_var"));
3557
3558 /* It is useles to retrieve the stream, but this function
3559 * runs only in a stream context.
3560 */
3561 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3562 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3563 s = appctx->htxn.s;
3564
3565 /* Unset the variable. */
3566 smp_set_owner(&smp, s->be, s->sess, s, 0);
3567 vars_unset_by_name(name, len, &smp);
3568 return 0;
3569}
3570
3571__LJMP static int hlua_applet_tcp_get_var(lua_State *L)
3572{
3573 struct hlua_appctx *appctx;
3574 struct stream *s;
3575 const char *name;
3576 size_t len;
3577 struct sample smp;
3578
3579 MAY_LJMP(check_args(L, 2, "get_var"));
3580
3581 /* It is useles to retrieve the stream, but this function
3582 * runs only in a stream context.
3583 */
3584 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3585 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3586 s = appctx->htxn.s;
3587
3588 smp_set_owner(&smp, s->be, s->sess, s, 0);
3589 if (!vars_get_by_name(name, len, &smp)) {
3590 lua_pushnil(L);
3591 return 1;
3592 }
3593
3594 return hlua_smp2lua(L, &smp);
3595}
3596
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003597__LJMP static int hlua_applet_tcp_set_priv(lua_State *L)
3598{
3599 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3600 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003601 struct hlua *hlua;
3602
3603 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003604 if (!s->hlua)
3605 return 0;
3606 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003607
3608 MAY_LJMP(check_args(L, 2, "set_priv"));
3609
3610 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02003611 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003612
3613 /* Get and store new value. */
3614 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
3615 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
3616
3617 return 0;
3618}
3619
3620__LJMP static int hlua_applet_tcp_get_priv(lua_State *L)
3621{
3622 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3623 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003624 struct hlua *hlua;
3625
3626 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003627 if (!s->hlua) {
3628 lua_pushnil(L);
3629 return 1;
3630 }
3631 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003632
3633 /* Push configuration index in the stack. */
3634 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
3635
3636 return 1;
3637}
3638
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003639/* If expected data not yet available, it returns a yield. This function
3640 * consumes the data in the buffer. It returns a string containing the
3641 * data. This string can be empty.
3642 */
3643__LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx)
3644{
3645 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3646 struct stream_interface *si = appctx->appctx->owner;
3647 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02003648 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003649 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02003650 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003651 size_t len2;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003652
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003653 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003654 ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003655
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003656 /* Data not yet available. return yield. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003657 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003658 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003659 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_getline_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003660 }
3661
3662 /* End of data: commit the total strings and return. */
3663 if (ret < 0) {
3664 luaL_pushresult(&appctx->b);
3665 return 1;
3666 }
3667
3668 /* Ensure that the block 2 length is usable. */
3669 if (ret == 1)
3670 len2 = 0;
3671
3672 /* dont check the max length read and dont check. */
3673 luaL_addlstring(&appctx->b, blk1, len1);
3674 luaL_addlstring(&appctx->b, blk2, len2);
3675
3676 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003677 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003678 luaL_pushresult(&appctx->b);
3679 return 1;
3680}
3681
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003682/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003683__LJMP static int hlua_applet_tcp_getline(lua_State *L)
3684{
3685 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3686
3687 /* Initialise the string catenation. */
3688 luaL_buffinit(L, &appctx->b);
3689
3690 return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0));
3691}
3692
3693/* If expected data not yet available, it returns a yield. This function
3694 * consumes the data in the buffer. It returns a string containing the
3695 * data. This string can be empty.
3696 */
3697__LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx)
3698{
3699 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3700 struct stream_interface *si = appctx->appctx->owner;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003701 size_t len = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003702 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02003703 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003704 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02003705 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003706 size_t len2;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003707
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003708 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003709 ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003710
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003711 /* Data not yet available. return yield. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003712 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003713 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003714 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003715 }
3716
3717 /* End of data: commit the total strings and return. */
3718 if (ret < 0) {
3719 luaL_pushresult(&appctx->b);
3720 return 1;
3721 }
3722
3723 /* Ensure that the block 2 length is usable. */
3724 if (ret == 1)
3725 len2 = 0;
3726
3727 if (len == -1) {
3728
3729 /* If len == -1, catenate all the data avalaile and
3730 * yield because we want to get all the data until
3731 * the end of data stream.
3732 */
3733 luaL_addlstring(&appctx->b, blk1, len1);
3734 luaL_addlstring(&appctx->b, blk2, len2);
Willy Tarreau06d80a92017-10-19 14:32:15 +02003735 co_skip(si_oc(si), len1 + len2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003736 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003737 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003738
3739 } else {
3740
3741 /* Copy the fisrt block caping to the length required. */
3742 if (len1 > len)
3743 len1 = len;
3744 luaL_addlstring(&appctx->b, blk1, len1);
3745 len -= len1;
3746
3747 /* Copy the second block. */
3748 if (len2 > len)
3749 len2 = len;
3750 luaL_addlstring(&appctx->b, blk2, len2);
3751 len -= len2;
3752
3753 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003754 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003755
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003756 /* If there is no other data available, yield waiting for new data. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003757 if (len > 0) {
3758 lua_pushinteger(L, len);
3759 lua_replace(L, 2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003760 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003761 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003762 }
3763
3764 /* return the result. */
3765 luaL_pushresult(&appctx->b);
3766 return 1;
3767 }
3768
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003769 /* we never execute this */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003770 hlua_pusherror(L, "Lua: internal error");
3771 WILL_LJMP(lua_error(L));
3772 return 0;
3773}
3774
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003775/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003776__LJMP static int hlua_applet_tcp_recv(lua_State *L)
3777{
3778 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3779 int len = -1;
3780
3781 if (lua_gettop(L) > 2)
3782 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
3783 if (lua_gettop(L) >= 2) {
3784 len = MAY_LJMP(luaL_checkinteger(L, 2));
3785 lua_pop(L, 1);
3786 }
3787
3788 /* Confirm or set the required length */
3789 lua_pushinteger(L, len);
3790
3791 /* Initialise the string catenation. */
3792 luaL_buffinit(L, &appctx->b);
3793
3794 return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0));
3795}
3796
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003797/* Append data in the output side of the buffer. This data is immediately
3798 * sent. The function returns the amount of data written. If the buffer
3799 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003800 * if the channel is closed.
3801 */
3802__LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx)
3803{
3804 size_t len;
3805 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3806 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
3807 int l = MAY_LJMP(luaL_checkinteger(L, 3));
3808 struct stream_interface *si = appctx->appctx->owner;
3809 struct channel *chn = si_ic(si);
3810 int max;
3811
3812 /* Get the max amount of data which can write as input in the channel. */
3813 max = channel_recv_max(chn);
3814 if (max > (len - l))
3815 max = len - l;
3816
3817 /* Copy data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003818 ci_putblk(chn, str + l, max);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003819
3820 /* update counters. */
3821 l += max;
3822 lua_pop(L, 1);
3823 lua_pushinteger(L, l);
3824
3825 /* If some data is not send, declares the situation to the
3826 * applet, and returns a yield.
3827 */
3828 if (l < len) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003829 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003830 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003831 }
3832
3833 return 1;
3834}
3835
3836/* Just a wraper of "hlua_applet_tcp_send_yield". This wrapper permits
3837 * yield the LUA process, and resume it without checking the
3838 * input arguments.
3839 */
3840__LJMP static int hlua_applet_tcp_send(lua_State *L)
3841{
3842 MAY_LJMP(check_args(L, 2, "send"));
3843 lua_pushinteger(L, 0);
3844
3845 return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0));
3846}
3847
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003848/*
3849 *
3850 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003851 * Class AppletHTTP
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003852 *
3853 *
3854 */
3855
3856/* Returns a struct hlua_txn if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003857 * a class stream, otherwise it throws an error.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003858 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003859__LJMP static struct hlua_appctx *hlua_checkapplet_http(lua_State *L, int ud)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003860{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003861 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003862}
3863
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003864/* This function creates and push in the stack an Applet object
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003865 * according with a current TXN.
3866 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003867static int hlua_applet_http_new(lua_State *L, struct appctx *ctx)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003868{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003869 struct hlua_appctx *appctx;
Thierry FOURNIER841475e2015-12-11 17:10:09 +01003870 struct hlua_txn htxn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003871 struct stream_interface *si = ctx->owner;
3872 struct stream *s = si_strm(si);
3873 struct proxy *px = s->be;
3874 struct http_txn *txn = s->txn;
3875 const char *path;
3876 const char *end;
3877 const char *p;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003878
3879 /* Check stack size. */
3880 if (!lua_checkstack(L, 3))
3881 return 0;
3882
3883 /* Create the object: obj[0] = userdata.
3884 * Note that the base of the Converters object is the
3885 * same than the TXN object.
3886 */
3887 lua_newtable(L);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003888 appctx = lua_newuserdata(L, sizeof(*appctx));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003889 lua_rawseti(L, -2, 0);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003890 appctx->appctx = ctx;
3891 appctx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */
Robin H. Johnson52f5db22017-01-01 13:10:52 -08003892 appctx->appctx->ctx.hlua_apphttp.reason = NULL; /* Use default reason based on status */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003893 appctx->htxn.s = s;
3894 appctx->htxn.p = px;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003895
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003896 /* Create the "f" field that contains a list of fetches. */
3897 lua_pushstring(L, "f");
3898 if (!hlua_fetches_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 "sf" field that contains a list of stringsafe fetches. */
3903 lua_pushstring(L, "sf");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003904 if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003905 return 0;
3906 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003907
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003908 /* Create the "c" field that contains a list of converters. */
3909 lua_pushstring(L, "c");
3910 if (!hlua_converters_new(L, &appctx->htxn, 0))
3911 return 0;
3912 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003913
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003914 /* Create the "sc" field that contains a list of stringsafe converters. */
3915 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003916 if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003917 return 0;
3918 lua_settable(L, -3);
Willy Tarreaueee5b512015-04-03 23:46:31 +02003919
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003920 /* Stores the request method. */
3921 lua_pushstring(L, "method");
Willy Tarreaua79021a2018-06-15 18:07:57 +02003922 lua_pushlstring(L, ci_head(txn->req.chn), txn->req.sl.rq.m_l);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003923 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003924
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003925 /* Stores the http version. */
3926 lua_pushstring(L, "version");
Willy Tarreaua79021a2018-06-15 18:07:57 +02003927 lua_pushlstring(L, ci_head(txn->req.chn) + txn->req.sl.rq.v, txn->req.sl.rq.v_l);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003928 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003929
Thierry FOURNIER841475e2015-12-11 17:10:09 +01003930 /* creates an array of headers. hlua_http_get_headers() crates and push
3931 * the array on the top of the stack.
3932 */
3933 lua_pushstring(L, "headers");
3934 htxn.s = s;
3935 htxn.p = px;
3936 htxn.dir = SMP_OPT_DIR_REQ;
3937 if (!hlua_http_get_headers(L, &htxn, &htxn.s->txn->req))
3938 return 0;
3939 lua_settable(L, -3);
3940
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003941 /* Get path and qs */
Willy Tarreau6b952c82018-09-10 17:45:34 +02003942 path = http_txn_get_path(txn);
Thierry FOURNIER7d388632017-02-22 02:06:16 +01003943 if (path) {
Willy Tarreaua79021a2018-06-15 18:07:57 +02003944 end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Thierry FOURNIER7d388632017-02-22 02:06:16 +01003945 p = path;
3946 while (p < end && *p != '?')
3947 p++;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003948
Thierry FOURNIER7d388632017-02-22 02:06:16 +01003949 /* Stores the request path. */
3950 lua_pushstring(L, "path");
3951 lua_pushlstring(L, path, p - path);
3952 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003953
Thierry FOURNIER7d388632017-02-22 02:06:16 +01003954 /* Stores the query string. */
3955 lua_pushstring(L, "qs");
3956 if (*p == '?')
3957 p++;
3958 lua_pushlstring(L, p, end - p);
3959 lua_settable(L, -3);
3960 }
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003961
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003962 /* Stores the request path. */
3963 lua_pushstring(L, "length");
3964 lua_pushinteger(L, txn->req.body_len);
3965 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003966
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003967 /* Create an array of HTTP request headers. */
3968 lua_pushstring(L, "headers");
3969 MAY_LJMP(hlua_http_get_headers(L, &appctx->htxn, &appctx->htxn.s->txn->req));
3970 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003971
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003972 /* Create an empty array of HTTP request headers. */
3973 lua_pushstring(L, "response");
3974 lua_newtable(L);
3975 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003976
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003977 /* Pop a class stream metatable and affect it to the table. */
3978 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref);
3979 lua_setmetatable(L, -2);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003980
3981 return 1;
3982}
3983
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003984__LJMP static int hlua_applet_http_set_var(lua_State *L)
3985{
3986 struct hlua_appctx *appctx;
3987 struct stream *s;
3988 const char *name;
3989 size_t len;
3990 struct sample smp;
3991
3992 MAY_LJMP(check_args(L, 3, "set_var"));
3993
3994 /* It is useles to retrieve the stream, but this function
3995 * runs only in a stream context.
3996 */
3997 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
3998 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3999 s = appctx->htxn.s;
4000
4001 /* Converts the third argument in a sample. */
4002 hlua_lua2smp(L, 3, &smp);
4003
4004 /* Store the sample in a variable. */
4005 smp_set_owner(&smp, s->be, s->sess, s, 0);
4006 vars_set_by_name(name, len, &smp);
4007 return 0;
4008}
4009
4010__LJMP static int hlua_applet_http_unset_var(lua_State *L)
4011{
4012 struct hlua_appctx *appctx;
4013 struct stream *s;
4014 const char *name;
4015 size_t len;
4016 struct sample smp;
4017
4018 MAY_LJMP(check_args(L, 2, "unset_var"));
4019
4020 /* It is useles to retrieve the stream, but this function
4021 * runs only in a stream context.
4022 */
4023 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4024 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4025 s = appctx->htxn.s;
4026
4027 /* Unset the variable. */
4028 smp_set_owner(&smp, s->be, s->sess, s, 0);
4029 vars_unset_by_name(name, len, &smp);
4030 return 0;
4031}
4032
4033__LJMP static int hlua_applet_http_get_var(lua_State *L)
4034{
4035 struct hlua_appctx *appctx;
4036 struct stream *s;
4037 const char *name;
4038 size_t len;
4039 struct sample smp;
4040
4041 MAY_LJMP(check_args(L, 2, "get_var"));
4042
4043 /* It is useles to retrieve the stream, but this function
4044 * runs only in a stream context.
4045 */
4046 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4047 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4048 s = appctx->htxn.s;
4049
4050 smp_set_owner(&smp, s->be, s->sess, s, 0);
4051 if (!vars_get_by_name(name, len, &smp)) {
4052 lua_pushnil(L);
4053 return 1;
4054 }
4055
4056 return hlua_smp2lua(L, &smp);
4057}
4058
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004059__LJMP static int hlua_applet_http_set_priv(lua_State *L)
4060{
4061 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4062 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004063 struct hlua *hlua;
4064
4065 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004066 if (!s->hlua)
4067 return 0;
4068 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004069
4070 MAY_LJMP(check_args(L, 2, "set_priv"));
4071
4072 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02004073 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004074
4075 /* Get and store new value. */
4076 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
4077 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
4078
4079 return 0;
4080}
4081
4082__LJMP static int hlua_applet_http_get_priv(lua_State *L)
4083{
4084 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4085 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004086 struct hlua *hlua;
4087
4088 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004089 if (!s->hlua) {
4090 lua_pushnil(L);
4091 return 1;
4092 }
4093 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004094
4095 /* Push configuration index in the stack. */
4096 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
4097
4098 return 1;
4099}
4100
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004101/* If expected data not yet available, it returns a yield. This function
4102 * consumes the data in the buffer. It returns a string containing the
4103 * data. This string can be empty.
4104 */
4105__LJMP static int hlua_applet_http_getline_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004106{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004107 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4108 struct stream_interface *si = appctx->appctx->owner;
4109 struct channel *chn = si_ic(si);
4110 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02004111 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02004112 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02004113 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02004114 size_t len2;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004115
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004116 /* Maybe we cant send a 100-continue ? */
4117 if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_100C) {
Willy Tarreau04f1e2d2018-09-10 18:04:24 +02004118 ret = ci_putblk(chn, HTTP_100.ptr, HTTP_100.len);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004119 /* if ret == -2 or -3 the channel closed or the message si too
4120 * big for the buffers. We cant send anything. So, we ignoring
4121 * the error, considers that the 100-continue is sent, and try
4122 * to receive.
4123 * If ret is -1, we dont have room in the buffer, so we yield.
4124 */
4125 if (ret == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01004126 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004127 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_getline_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004128 }
4129 appctx->appctx->ctx.hlua_apphttp.flags &= ~APPLET_100C;
4130 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004131
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004132 /* Check for the end of the data. */
4133 if (appctx->appctx->ctx.hlua_apphttp.left_bytes <= 0) {
4134 luaL_pushresult(&appctx->b);
4135 return 1;
4136 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004137
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004138 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004139 ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004140
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004141 /* Data not yet available. return yield. */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004142 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01004143 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004144 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_getline_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004145 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004146
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004147 /* End of data: commit the total strings and return. */
4148 if (ret < 0) {
4149 luaL_pushresult(&appctx->b);
4150 return 1;
4151 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004152
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004153 /* Ensure that the block 2 length is usable. */
4154 if (ret == 1)
4155 len2 = 0;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004156
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004157 /* Copy the fisrt block caping to the length required. */
4158 if (len1 > appctx->appctx->ctx.hlua_apphttp.left_bytes)
4159 len1 = appctx->appctx->ctx.hlua_apphttp.left_bytes;
4160 luaL_addlstring(&appctx->b, blk1, len1);
4161 appctx->appctx->ctx.hlua_apphttp.left_bytes -= len1;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004162
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004163 /* Copy the second block. */
4164 if (len2 > appctx->appctx->ctx.hlua_apphttp.left_bytes)
4165 len2 = appctx->appctx->ctx.hlua_apphttp.left_bytes;
4166 luaL_addlstring(&appctx->b, blk2, len2);
4167 appctx->appctx->ctx.hlua_apphttp.left_bytes -= len2;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004168
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004169 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004170 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004171 luaL_pushresult(&appctx->b);
4172 return 1;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004173}
4174
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004175/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004176__LJMP static int hlua_applet_http_getline(lua_State *L)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004177{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004178 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004179
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004180 /* Initialise the string catenation. */
4181 luaL_buffinit(L, &appctx->b);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004182
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004183 return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004184}
4185
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004186/* If expected data not yet available, it returns a yield. This function
4187 * consumes the data in the buffer. It returns a string containing the
4188 * data. This string can be empty.
4189 */
4190__LJMP static int hlua_applet_http_recv_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004191{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004192 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4193 struct stream_interface *si = appctx->appctx->owner;
4194 int len = MAY_LJMP(luaL_checkinteger(L, 2));
4195 struct channel *chn = si_ic(si);
4196 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02004197 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02004198 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02004199 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02004200 size_t len2;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004201
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004202 /* Maybe we cant send a 100-continue ? */
4203 if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_100C) {
Willy Tarreau04f1e2d2018-09-10 18:04:24 +02004204 ret = ci_putblk(chn, HTTP_100.ptr, HTTP_100.len);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004205 /* if ret == -2 or -3 the channel closed or the message si too
4206 * big for the buffers. We cant send anything. So, we ignoring
4207 * the error, considers that the 100-continue is sent, and try
4208 * to receive.
4209 * If ret is -1, we dont have room in the buffer, so we yield.
4210 */
4211 if (ret == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01004212 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004213 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004214 }
4215 appctx->appctx->ctx.hlua_apphttp.flags &= ~APPLET_100C;
4216 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004217
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004218 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004219 ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004220
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004221 /* Data not yet available. return yield. */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004222 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01004223 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004224 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004225 }
4226
4227 /* End of data: commit the total strings and return. */
4228 if (ret < 0) {
4229 luaL_pushresult(&appctx->b);
4230 return 1;
4231 }
4232
4233 /* Ensure that the block 2 length is usable. */
4234 if (ret == 1)
4235 len2 = 0;
4236
4237 /* Copy the fisrt block caping to the length required. */
4238 if (len1 > len)
4239 len1 = len;
4240 luaL_addlstring(&appctx->b, blk1, len1);
4241 len -= len1;
4242
4243 /* Copy the second block. */
4244 if (len2 > len)
4245 len2 = len;
4246 luaL_addlstring(&appctx->b, blk2, len2);
4247 len -= len2;
4248
4249 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004250 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004251 if (appctx->appctx->ctx.hlua_apphttp.left_bytes != -1)
4252 appctx->appctx->ctx.hlua_apphttp.left_bytes -= len;
4253
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004254 /* If we are no other data available, yield waiting for new data. */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004255 if (len > 0) {
4256 lua_pushinteger(L, len);
4257 lua_replace(L, 2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01004258 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004259 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004260 }
4261
4262 /* return the result. */
4263 luaL_pushresult(&appctx->b);
4264 return 1;
4265}
4266
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004267/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004268__LJMP static int hlua_applet_http_recv(lua_State *L)
4269{
4270 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4271 int len = -1;
4272
4273 /* Check arguments. */
4274 if (lua_gettop(L) > 2)
4275 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
4276 if (lua_gettop(L) >= 2) {
4277 len = MAY_LJMP(luaL_checkinteger(L, 2));
4278 lua_pop(L, 1);
4279 }
4280
4281 /* Check the required length */
4282 if (len == -1 || len > appctx->appctx->ctx.hlua_apphttp.left_bytes)
4283 len = appctx->appctx->ctx.hlua_apphttp.left_bytes;
4284 lua_pushinteger(L, len);
4285
4286 /* Initialise the string catenation. */
4287 luaL_buffinit(L, &appctx->b);
4288
4289 return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0));
4290}
4291
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004292/* Append data in the output side of the buffer. This data is immediately
4293 * sent. The function returns the amount of data written. If the buffer
4294 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004295 * if the channel is closed.
4296 */
4297__LJMP static int hlua_applet_http_send_yield(lua_State *L, int status, lua_KContext ctx)
4298{
4299 size_t len;
4300 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4301 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
4302 int l = MAY_LJMP(luaL_checkinteger(L, 3));
4303 struct stream_interface *si = appctx->appctx->owner;
4304 struct channel *chn = si_ic(si);
4305 int max;
4306
4307 /* Get the max amount of data which can write as input in the channel. */
4308 max = channel_recv_max(chn);
4309 if (max > (len - l))
4310 max = len - l;
4311
4312 /* Copy data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004313 ci_putblk(chn, str + l, max);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004314
4315 /* update counters. */
4316 l += max;
4317 lua_pop(L, 1);
4318 lua_pushinteger(L, l);
4319
4320 /* If some data is not send, declares the situation to the
4321 * applet, and returns a yield.
4322 */
4323 if (l < len) {
Willy Tarreaudb398432018-11-15 11:08:52 +01004324 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004325 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004326 }
4327
4328 return 1;
4329}
4330
4331/* Just a wraper of "hlua_applet_send_yield". This wrapper permits
4332 * yield the LUA process, and resume it without checking the
4333 * input arguments.
4334 */
4335__LJMP static int hlua_applet_http_send(lua_State *L)
4336{
4337 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4338 size_t len;
4339 char hex[10];
4340
4341 MAY_LJMP(luaL_checklstring(L, 2, &len));
4342
4343 /* If transfer encoding chunked is selected, we surround the data
4344 * by chunk data.
4345 */
4346 if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_CHUNKED) {
4347 snprintf(hex, 9, "%x", (unsigned int)len);
4348 lua_pushfstring(L, "%s\r\n", hex);
4349 lua_insert(L, 2); /* swap the last 2 entries. */
4350 lua_pushstring(L, "\r\n");
4351 lua_concat(L, 3);
4352 }
4353
4354 /* This interger is used for followinf the amount of data sent. */
4355 lua_pushinteger(L, 0);
4356
4357 /* We want to send some data. Headers must be sent. */
4358 if (!(appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
4359 hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data.");
4360 WILL_LJMP(lua_error(L));
4361 }
4362
4363 return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0));
4364}
4365
4366__LJMP static int hlua_applet_http_addheader(lua_State *L)
4367{
4368 const char *name;
4369 int ret;
4370
4371 MAY_LJMP(hlua_checkapplet_http(L, 1));
4372 name = MAY_LJMP(luaL_checkstring(L, 2));
4373 MAY_LJMP(luaL_checkstring(L, 3));
4374
4375 /* Push in the stack the "response" entry. */
4376 ret = lua_getfield(L, 1, "response");
4377 if (ret != LUA_TTABLE) {
4378 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] "
4379 "is expected as an array. %s found", lua_typename(L, ret));
4380 WILL_LJMP(lua_error(L));
4381 }
4382
4383 /* check if the header is already registered if it is not
4384 * the case, register it.
4385 */
4386 ret = lua_getfield(L, -1, name);
4387 if (ret == LUA_TNIL) {
4388
4389 /* Entry not found. */
4390 lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */
4391
4392 /* Insert the new header name in the array in the top of the stack.
4393 * It left the new array in the top of the stack.
4394 */
4395 lua_newtable(L);
4396 lua_pushvalue(L, 2);
4397 lua_pushvalue(L, -2);
4398 lua_settable(L, -4);
4399
4400 } else if (ret != LUA_TTABLE) {
4401
4402 /* corruption error. */
4403 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] "
4404 "is expected as an array. %s found", name, lua_typename(L, ret));
4405 WILL_LJMP(lua_error(L));
4406 }
4407
4408 /* Now the top od thestack is an array of values. We push
4409 * the header value as new entry.
4410 */
4411 lua_pushvalue(L, 3);
4412 ret = lua_rawlen(L, -2);
4413 lua_rawseti(L, -2, ret + 1);
4414 lua_pushboolean(L, 1);
4415 return 1;
4416}
4417
4418__LJMP static int hlua_applet_http_status(lua_State *L)
4419{
4420 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4421 int status = MAY_LJMP(luaL_checkinteger(L, 2));
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004422 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004423
4424 if (status < 100 || status > 599) {
4425 lua_pushboolean(L, 0);
4426 return 1;
4427 }
4428
4429 appctx->appctx->ctx.hlua_apphttp.status = status;
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004430 appctx->appctx->ctx.hlua_apphttp.reason = reason;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004431 lua_pushboolean(L, 1);
4432 return 1;
4433}
4434
4435/* We will build the status line and the headers of the HTTP response.
4436 * We will try send at once if its not possible, we give back the hand
4437 * waiting for more room.
4438 */
4439__LJMP static int hlua_applet_http_start_response_yield(lua_State *L, int status, lua_KContext ctx)
4440{
4441 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4442 struct stream_interface *si = appctx->appctx->owner;
4443 struct channel *chn = si_ic(si);
4444 int ret;
4445 size_t len;
4446 const char *msg;
4447
4448 /* Get the message as the first argument on the stack. */
4449 msg = MAY_LJMP(luaL_checklstring(L, 2, &len));
4450
4451 /* Send the message at once. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004452 ret = ci_putblk(chn, msg, len);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004453
4454 /* if ret == -2 or -3 the channel closed or the message si too
4455 * big for the buffers.
4456 */
4457 if (ret == -2 || ret == -3) {
4458 hlua_pusherror(L, "Lua: 'start_response': response header block too big");
4459 WILL_LJMP(lua_error(L));
4460 }
4461
4462 /* If ret is -1, we dont have room in the buffer, so we yield. */
4463 if (ret == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01004464 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004465 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_start_response_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004466 }
4467
4468 /* Headers sent, set the flag. */
4469 appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT;
4470 return 0;
4471}
4472
4473__LJMP static int hlua_applet_http_start_response(lua_State *L)
4474{
Willy Tarreau83061a82018-07-13 11:56:34 +02004475 struct buffer *tmp = get_trash_chunk();
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004476 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004477 const char *name;
Willy Tarreaua3294632017-08-23 11:24:47 +02004478 size_t name_len;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004479 const char *value;
Willy Tarreaua3294632017-08-23 11:24:47 +02004480 size_t value_len;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004481 int id;
Willy Tarreauc9f4ea02017-08-23 09:32:06 +02004482 long long hdr_contentlength = -1;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004483 int hdr_chunked = 0;
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004484 const char *reason = appctx->appctx->ctx.hlua_apphttp.reason;
4485
4486 if (reason == NULL)
Willy Tarreau04f1e2d2018-09-10 18:04:24 +02004487 reason = http_get_reason(appctx->appctx->ctx.hlua_apphttp.status);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004488
4489 /* Use the same http version than the request. */
4490 chunk_appendf(tmp, "HTTP/1.%c %d %s\r\n",
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +01004491 appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11 ? '1' : '0',
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004492 appctx->appctx->ctx.hlua_apphttp.status,
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004493 reason);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004494
4495 /* Get the array associated to the field "response" in the object AppletHTTP. */
4496 lua_pushvalue(L, 0);
4497 if (lua_getfield(L, 1, "response") != LUA_TTABLE) {
4498 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n",
4499 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4500 WILL_LJMP(lua_error(L));
4501 }
4502
4503 /* Browse the list of headers. */
4504 lua_pushnil(L);
4505 while(lua_next(L, -2) != 0) {
4506
4507 /* We expect a string as -2. */
4508 if (lua_type(L, -2) != LUA_TSTRING) {
4509 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n",
4510 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4511 lua_typename(L, lua_type(L, -2)));
4512 WILL_LJMP(lua_error(L));
4513 }
Willy Tarreaua3294632017-08-23 11:24:47 +02004514 name = lua_tolstring(L, -2, &name_len);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004515
4516 /* We expect an array as -1. */
4517 if (lua_type(L, -1) != LUA_TTABLE) {
4518 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n",
4519 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4520 name,
4521 lua_typename(L, lua_type(L, -1)));
4522 WILL_LJMP(lua_error(L));
4523 }
4524
4525 /* Browse the table who is on the top of the stack. */
4526 lua_pushnil(L);
4527 while(lua_next(L, -2) != 0) {
4528
4529 /* We expect a number as -2. */
4530 if (lua_type(L, -2) != LUA_TNUMBER) {
4531 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n",
4532 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4533 name,
4534 lua_typename(L, lua_type(L, -2)));
4535 WILL_LJMP(lua_error(L));
4536 }
4537 id = lua_tointeger(L, -2);
4538
4539 /* We expect a string as -2. */
4540 if (lua_type(L, -1) != LUA_TSTRING) {
4541 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n",
4542 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4543 name, id,
4544 lua_typename(L, lua_type(L, -1)));
4545 WILL_LJMP(lua_error(L));
4546 }
Willy Tarreaua3294632017-08-23 11:24:47 +02004547 value = lua_tolstring(L, -1, &value_len);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004548
4549 /* Catenate a new header. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004550 if (tmp->data + name_len + 2 + value_len + 2 < tmp->size) {
4551 memcpy(tmp->area + tmp->data, name, name_len);
4552 tmp->data += name_len;
4553 tmp->area[tmp->data++] = ':';
4554 tmp->area[tmp->data++] = ' ';
Willy Tarreaua3294632017-08-23 11:24:47 +02004555
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004556 memcpy(tmp->area + tmp->data, value,
4557 value_len);
4558 tmp->data += value_len;
4559 tmp->area[tmp->data++] = '\r';
4560 tmp->area[tmp->data++] = '\n';
Willy Tarreaua3294632017-08-23 11:24:47 +02004561 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004562
4563 /* Protocol checks. */
4564
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004565 /* Copy the header content length. The length conversion
Willy Tarreauc9f4ea02017-08-23 09:32:06 +02004566 * is done without control. If it contains a bad value,
4567 * the content-length remains negative so that we can
4568 * switch to either chunked encoding or close.
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004569 */
Willy Tarreaua3294632017-08-23 11:24:47 +02004570 if (name_len == 14 && strcasecmp("content-length", name) == 0)
Willy Tarreauc9f4ea02017-08-23 09:32:06 +02004571 strl2llrc(value, strlen(value), &hdr_contentlength);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004572
4573 /* Check if the client annouces a transfer-encoding chunked it self. */
Willy Tarreaua3294632017-08-23 11:24:47 +02004574 if (name_len == 17 && value_len == 7 &&
4575 strcasecmp("transfer-encoding", name) == 0 &&
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004576 strcasecmp("chunked", value) == 0)
4577 hdr_chunked = 1;
4578
4579 /* Remove the array from the stack, and get next element with a remaining string. */
4580 lua_pop(L, 1);
4581 }
4582
4583 /* Remove the array from the stack, and get next element with a remaining string. */
4584 lua_pop(L, 1);
4585 }
4586
Willy Tarreau06c75fe2017-08-23 09:10:38 +02004587 /* If we dont have a content-length set, and the HTTP version is 1.1
4588 * and the status code implies the presence of a message body, we must
4589 * announce a transfer encoding chunked. This is required by haproxy
4590 * for the keepalive compliance. If the applet annouces a transfer-encoding
4591 * chunked itslef, don't do anything.
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004592 */
Willy Tarreauc9f4ea02017-08-23 09:32:06 +02004593 if (hdr_contentlength < 0 && hdr_chunked == 0 &&
Willy Tarreau06c75fe2017-08-23 09:10:38 +02004594 (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) &&
4595 appctx->appctx->ctx.hlua_apphttp.status >= 200 &&
4596 appctx->appctx->ctx.hlua_apphttp.status != 204 &&
4597 appctx->appctx->ctx.hlua_apphttp.status != 304) {
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004598 chunk_appendf(tmp, "Transfer-encoding: chunked\r\n");
4599 appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_CHUNKED;
4600 }
4601
4602 /* Finalize headers. */
4603 chunk_appendf(tmp, "\r\n");
4604
4605 /* Remove the last entry and the array of headers */
4606 lua_pop(L, 2);
4607
4608 /* Push the headers block. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004609 lua_pushlstring(L, tmp->area, tmp->data);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004610
4611 return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0));
4612}
4613
4614/*
4615 *
4616 *
4617 * Class HTTP
4618 *
4619 *
4620 */
4621
4622/* Returns a struct hlua_txn if the stack entry "ud" is
4623 * a class stream, otherwise it throws an error.
4624 */
4625__LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud)
4626{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02004627 return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004628}
4629
4630/* This function creates and push in the stack a HTTP object
4631 * according with a current TXN.
4632 */
4633static int hlua_http_new(lua_State *L, struct hlua_txn *txn)
4634{
4635 struct hlua_txn *htxn;
4636
4637 /* Check stack size. */
4638 if (!lua_checkstack(L, 3))
4639 return 0;
4640
4641 /* Create the object: obj[0] = userdata.
4642 * Note that the base of the Converters object is the
4643 * same than the TXN object.
4644 */
4645 lua_newtable(L);
4646 htxn = lua_newuserdata(L, sizeof(*htxn));
4647 lua_rawseti(L, -2, 0);
4648
4649 htxn->s = txn->s;
4650 htxn->p = txn->p;
4651
4652 /* Pop a class stream metatable and affect it to the table. */
4653 lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref);
4654 lua_setmetatable(L, -2);
4655
4656 return 1;
4657}
4658
4659/* This function creates ans returns an array of HTTP headers.
4660 * This function does not fails. It is used as wrapper with the
4661 * 2 following functions.
4662 */
4663__LJMP static int hlua_http_get_headers(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg)
4664{
4665 const char *cur_ptr, *cur_next, *p;
4666 int old_idx, cur_idx;
4667 struct hdr_idx_elem *cur_hdr;
4668 const char *hn, *hv;
4669 int hnl, hvl;
4670 int type;
4671 const char *in;
4672 char *out;
4673 int len;
4674
4675 /* Create the table. */
4676 lua_newtable(L);
4677
4678 if (!htxn->s->txn)
4679 return 1;
4680
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004681 /* Check if a valid response is parsed */
4682 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4683 return 1;
4684
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004685 /* Build array of headers. */
4686 old_idx = 0;
Willy Tarreaua79021a2018-06-15 18:07:57 +02004687 cur_next = ci_head(msg->chn) + hdr_idx_first_pos(&htxn->s->txn->hdr_idx);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004688
4689 while (1) {
4690 cur_idx = htxn->s->txn->hdr_idx.v[old_idx].next;
4691 if (!cur_idx)
4692 break;
4693 old_idx = cur_idx;
4694
4695 cur_hdr = &htxn->s->txn->hdr_idx.v[cur_idx];
4696 cur_ptr = cur_next;
4697 cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1;
4698
4699 /* Now we have one full header at cur_ptr of len cur_hdr->len,
4700 * and the next header starts at cur_next. We'll check
4701 * this header in the list as well as against the default
4702 * rule.
4703 */
4704
4705 /* look for ': *'. */
4706 hn = cur_ptr;
4707 for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++);
4708 if (p >= cur_ptr+cur_hdr->len)
4709 continue;
4710 hnl = p - hn;
4711 p++;
4712 while (p < cur_ptr+cur_hdr->len && ( *p == ' ' || *p == '\t' ))
4713 p++;
4714 if (p >= cur_ptr+cur_hdr->len)
4715 continue;
4716 hv = p;
4717 hvl = cur_ptr+cur_hdr->len-p;
4718
4719 /* Lowercase the key. Don't check the size of trash, it have
4720 * the size of one buffer and the input data contains in one
4721 * buffer.
4722 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004723 out = trash.area;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004724 for (in=hn; in<hn+hnl; in++, out++)
4725 *out = tolower(*in);
4726 *out = '\0';
4727
4728 /* Check for existing entry:
4729 * assume that the table is on the top of the stack, and
4730 * push the key in the stack, the function lua_gettable()
4731 * perform the lookup.
4732 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004733 lua_pushlstring(L, trash.area, hnl);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004734 lua_gettable(L, -2);
4735 type = lua_type(L, -1);
4736
4737 switch (type) {
4738 case LUA_TNIL:
4739 /* Table not found, create it. */
4740 lua_pop(L, 1); /* remove the nil value. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004741 lua_pushlstring(L, trash.area, hnl); /* push the header name as key. */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004742 lua_newtable(L); /* create and push empty table. */
4743 lua_pushlstring(L, hv, hvl); /* push header value. */
4744 lua_rawseti(L, -2, 0); /* index header value (pop it). */
4745 lua_rawset(L, -3); /* index new table with header name (pop the values). */
4746 break;
4747
4748 case LUA_TTABLE:
4749 /* Entry found: push the value in the table. */
4750 len = lua_rawlen(L, -1);
4751 lua_pushlstring(L, hv, hvl); /* push header value. */
4752 lua_rawseti(L, -2, len+1); /* index header value (pop it). */
4753 lua_pop(L, 1); /* remove the table (it is stored in the main table). */
4754 break;
4755
4756 default:
4757 /* Other cases are errors. */
4758 hlua_pusherror(L, "internal error during the parsing of headers.");
4759 WILL_LJMP(lua_error(L));
4760 }
4761 }
4762
4763 return 1;
4764}
4765
4766__LJMP static int hlua_http_req_get_headers(lua_State *L)
4767{
4768 struct hlua_txn *htxn;
4769
4770 MAY_LJMP(check_args(L, 1, "req_get_headers"));
4771 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4772
4773 return hlua_http_get_headers(L, htxn, &htxn->s->txn->req);
4774}
4775
4776__LJMP static int hlua_http_res_get_headers(lua_State *L)
4777{
4778 struct hlua_txn *htxn;
4779
4780 MAY_LJMP(check_args(L, 1, "res_get_headers"));
4781 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4782
4783 return hlua_http_get_headers(L, htxn, &htxn->s->txn->rsp);
4784}
4785
4786/* This function replace full header, or just a value in
4787 * the request or in the response. It is a wrapper fir the
4788 * 4 following functions.
4789 */
4790__LJMP static inline int hlua_http_rep_hdr(lua_State *L, struct hlua_txn *htxn,
4791 struct http_msg *msg, int action)
4792{
4793 size_t name_len;
4794 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
4795 const char *reg = MAY_LJMP(luaL_checkstring(L, 3));
4796 const char *value = MAY_LJMP(luaL_checkstring(L, 4));
4797 struct my_regex re;
4798
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004799 /* Check if a valid response is parsed */
4800 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4801 return 0;
4802
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004803 if (!regex_comp(reg, &re, 1, 1, NULL))
4804 WILL_LJMP(luaL_argerror(L, 3, "invalid regex"));
4805
4806 http_transform_header_str(htxn->s, msg, name, name_len, value, &re, action);
4807 regex_free(&re);
4808 return 0;
4809}
4810
4811__LJMP static int hlua_http_req_rep_hdr(lua_State *L)
4812{
4813 struct hlua_txn *htxn;
4814
4815 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
4816 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4817
4818 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_HDR));
4819}
4820
4821__LJMP static int hlua_http_res_rep_hdr(lua_State *L)
4822{
4823 struct hlua_txn *htxn;
4824
4825 MAY_LJMP(check_args(L, 4, "res_rep_hdr"));
4826 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4827
4828 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->rsp, ACT_HTTP_REPLACE_HDR));
4829}
4830
4831__LJMP static int hlua_http_req_rep_val(lua_State *L)
4832{
4833 struct hlua_txn *htxn;
4834
4835 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
4836 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4837
4838 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_VAL));
4839}
4840
4841__LJMP static int hlua_http_res_rep_val(lua_State *L)
4842{
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004843 struct hlua_txn *htxn;
4844
4845 MAY_LJMP(check_args(L, 4, "res_rep_val"));
4846 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4847
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02004848 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->rsp, ACT_HTTP_REPLACE_VAL));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004849}
4850
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004851/* This function deletes all the occurrences of an header.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004852 * It is a wrapper for the 2 following functions.
4853 */
4854__LJMP static inline int hlua_http_del_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg)
4855{
4856 size_t len;
4857 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4858 struct hdr_ctx ctx;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004859 struct http_txn *txn = htxn->s->txn;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004860
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004861 /* Check if a valid response is parsed */
4862 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4863 return 0;
4864
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004865 ctx.idx = 0;
Willy Tarreaua79021a2018-06-15 18:07:57 +02004866 while (http_find_header2(name, len, ci_head(msg->chn), &txn->hdr_idx, &ctx))
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004867 http_remove_header2(msg, &txn->hdr_idx, &ctx);
4868 return 0;
4869}
4870
4871__LJMP static int hlua_http_req_del_hdr(lua_State *L)
4872{
4873 struct hlua_txn *htxn;
4874
4875 MAY_LJMP(check_args(L, 2, "req_del_hdr"));
4876 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4877
Willy Tarreaueee5b512015-04-03 23:46:31 +02004878 return hlua_http_del_hdr(L, htxn, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004879}
4880
4881__LJMP static int hlua_http_res_del_hdr(lua_State *L)
4882{
4883 struct hlua_txn *htxn;
4884
4885 MAY_LJMP(check_args(L, 2, "req_del_hdr"));
4886 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4887
Willy Tarreaueee5b512015-04-03 23:46:31 +02004888 return hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004889}
4890
4891/* This function adds an header. It is a wrapper used by
4892 * the 2 following functions.
4893 */
4894__LJMP static inline int hlua_http_add_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg)
4895{
4896 size_t name_len;
4897 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
4898 size_t value_len;
4899 const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len));
4900 char *p;
4901
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004902 /* Check if a valid message is parsed */
4903 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4904 return 0;
4905
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004906 /* Check length. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004907 trash.data = value_len + name_len + 2;
4908 if (trash.data > trash.size)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004909 return 0;
4910
4911 /* Creates the header string. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004912 p = trash.area;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004913 memcpy(p, name, name_len);
4914 p += name_len;
4915 *p = ':';
4916 p++;
4917 *p = ' ';
4918 p++;
4919 memcpy(p, value, value_len);
4920
Willy Tarreaueee5b512015-04-03 23:46:31 +02004921 lua_pushboolean(L, http_header_add_tail2(msg, &htxn->s->txn->hdr_idx,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004922 trash.area, trash.data) != 0);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004923
4924 return 0;
4925}
4926
4927__LJMP static int hlua_http_req_add_hdr(lua_State *L)
4928{
4929 struct hlua_txn *htxn;
4930
4931 MAY_LJMP(check_args(L, 3, "req_add_hdr"));
4932 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4933
Willy Tarreaueee5b512015-04-03 23:46:31 +02004934 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004935}
4936
4937__LJMP static int hlua_http_res_add_hdr(lua_State *L)
4938{
4939 struct hlua_txn *htxn;
4940
4941 MAY_LJMP(check_args(L, 3, "res_add_hdr"));
4942 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4943
Willy Tarreaueee5b512015-04-03 23:46:31 +02004944 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004945}
4946
4947static int hlua_http_req_set_hdr(lua_State *L)
4948{
4949 struct hlua_txn *htxn;
4950
4951 MAY_LJMP(check_args(L, 3, "req_set_hdr"));
4952 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4953
Willy Tarreaueee5b512015-04-03 23:46:31 +02004954 hlua_http_del_hdr(L, htxn, &htxn->s->txn->req);
4955 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004956}
4957
4958static int hlua_http_res_set_hdr(lua_State *L)
4959{
4960 struct hlua_txn *htxn;
4961
4962 MAY_LJMP(check_args(L, 3, "res_set_hdr"));
4963 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4964
Willy Tarreaueee5b512015-04-03 23:46:31 +02004965 hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp);
4966 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004967}
4968
4969/* This function set the method. */
4970static int hlua_http_req_set_meth(lua_State *L)
4971{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02004972 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004973 size_t name_len;
4974 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004975
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004976 /* Check if a valid request is parsed */
4977 if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) {
4978 lua_pushboolean(L, 0);
4979 return 1;
4980 }
4981
Willy Tarreau987e3fb2015-04-04 01:09:08 +02004982 lua_pushboolean(L, http_replace_req_line(0, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004983 return 1;
4984}
4985
4986/* This function set the method. */
4987static int hlua_http_req_set_path(lua_State *L)
4988{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02004989 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004990 size_t name_len;
4991 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004992
4993 /* Check if a valid request is parsed */
4994 if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) {
4995 lua_pushboolean(L, 0);
4996 return 1;
4997 }
4998
Willy Tarreau987e3fb2015-04-04 01:09:08 +02004999 lua_pushboolean(L, http_replace_req_line(1, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005000 return 1;
5001}
5002
5003/* This function set the query-string. */
5004static int hlua_http_req_set_query(lua_State *L)
5005{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005006 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005007 size_t name_len;
5008 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005009
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02005010 /* Check if a valid request is parsed */
5011 if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) {
5012 lua_pushboolean(L, 0);
5013 return 1;
5014 }
5015
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005016 /* Check length. */
5017 if (name_len > trash.size - 1) {
5018 lua_pushboolean(L, 0);
5019 return 1;
5020 }
5021
5022 /* Add the mark question as prefix. */
5023 chunk_reset(&trash);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005024 trash.area[trash.data++] = '?';
5025 memcpy(trash.area + trash.data, name, name_len);
5026 trash.data += name_len;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005027
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005028 lua_pushboolean(L,
5029 http_replace_req_line(2, trash.area, trash.data, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005030 return 1;
5031}
5032
5033/* This function set the uri. */
5034static int hlua_http_req_set_uri(lua_State *L)
5035{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005036 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005037 size_t name_len;
5038 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005039
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02005040 /* Check if a valid request is parsed */
5041 if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) {
5042 lua_pushboolean(L, 0);
5043 return 1;
5044 }
5045
Willy Tarreau987e3fb2015-04-04 01:09:08 +02005046 lua_pushboolean(L, http_replace_req_line(3, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005047 return 1;
5048}
5049
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005050/* This function set the response code & optionally reason. */
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005051static int hlua_http_res_set_status(lua_State *L)
5052{
5053 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5054 unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2));
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005055 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005056
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02005057 /* Check if a valid response is parsed */
5058 if (unlikely(htxn->s->txn->rsp.msg_state < HTTP_MSG_BODY))
5059 return 0;
5060
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005061 http_set_status(code, reason, htxn->s);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005062 return 0;
5063}
5064
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005065/*
5066 *
5067 *
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005068 * Class TXN
5069 *
5070 *
5071 */
5072
5073/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02005074 * a class stream, otherwise it throws an error.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005075 */
5076__LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud)
5077{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02005078 return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref));
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005079}
5080
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005081__LJMP static int hlua_set_var(lua_State *L)
5082{
5083 struct hlua_txn *htxn;
5084 const char *name;
5085 size_t len;
5086 struct sample smp;
5087
5088 MAY_LJMP(check_args(L, 3, "set_var"));
5089
5090 /* It is useles to retrieve the stream, but this function
5091 * runs only in a stream context.
5092 */
5093 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5094 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5095
5096 /* Converts the third argument in a sample. */
5097 hlua_lua2smp(L, 3, &smp);
5098
5099 /* Store the sample in a variable. */
Willy Tarreau7560dd42016-03-10 16:28:58 +01005100 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Willy Tarreau6204cd92016-03-10 16:33:04 +01005101 vars_set_by_name(name, len, &smp);
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005102 return 0;
5103}
5104
Christopher Faulet85d79c92016-11-09 16:54:56 +01005105__LJMP static int hlua_unset_var(lua_State *L)
5106{
5107 struct hlua_txn *htxn;
5108 const char *name;
5109 size_t len;
5110 struct sample smp;
5111
5112 MAY_LJMP(check_args(L, 2, "unset_var"));
5113
5114 /* It is useles to retrieve the stream, but this function
5115 * runs only in a stream context.
5116 */
5117 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5118 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5119
5120 /* Unset the variable. */
5121 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
5122 vars_unset_by_name(name, len, &smp);
5123 return 0;
5124}
5125
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005126__LJMP static int hlua_get_var(lua_State *L)
5127{
5128 struct hlua_txn *htxn;
5129 const char *name;
5130 size_t len;
5131 struct sample smp;
5132
5133 MAY_LJMP(check_args(L, 2, "get_var"));
5134
5135 /* It is useles to retrieve the stream, but this function
5136 * runs only in a stream context.
5137 */
5138 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5139 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5140
Willy Tarreau7560dd42016-03-10 16:28:58 +01005141 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Willy Tarreau6204cd92016-03-10 16:33:04 +01005142 if (!vars_get_by_name(name, len, &smp)) {
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005143 lua_pushnil(L);
5144 return 1;
5145 }
5146
5147 return hlua_smp2lua(L, &smp);
5148}
5149
Willy Tarreau59551662015-03-10 14:23:13 +01005150__LJMP static int hlua_set_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005151{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005152 struct hlua *hlua;
5153
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005154 MAY_LJMP(check_args(L, 2, "set_priv"));
5155
Willy Tarreau87b09662015-04-03 00:22:06 +02005156 /* It is useles to retrieve the stream, but this function
5157 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005158 */
5159 MAY_LJMP(hlua_checktxn(L, 1));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005160 hlua = hlua_gethlua(L);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005161
5162 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02005163 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005164
5165 /* Get and store new value. */
5166 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
5167 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
5168
5169 return 0;
5170}
5171
Willy Tarreau59551662015-03-10 14:23:13 +01005172__LJMP static int hlua_get_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005173{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005174 struct hlua *hlua;
5175
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005176 MAY_LJMP(check_args(L, 1, "get_priv"));
5177
Willy Tarreau87b09662015-04-03 00:22:06 +02005178 /* It is useles to retrieve the stream, but this function
5179 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005180 */
5181 MAY_LJMP(hlua_checktxn(L, 1));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005182 hlua = hlua_gethlua(L);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005183
5184 /* Push configuration index in the stack. */
5185 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
5186
5187 return 1;
5188}
5189
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005190/* Create stack entry containing a class TXN. This function
5191 * return 0 if the stack does not contains free slots,
5192 * otherwise it returns 1.
5193 */
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005194static int hlua_txn_new(lua_State *L, struct stream *s, struct proxy *p, int dir, int flags)
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005195{
Willy Tarreaude491382015-04-06 11:04:28 +02005196 struct hlua_txn *htxn;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005197
5198 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005199 if (!lua_checkstack(L, 3))
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005200 return 0;
5201
5202 /* NOTE: The allocation never fails. The failure
5203 * throw an error, and the function never returns.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08005204 * if the throw is not available, the process is aborted.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005205 */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005206 /* Create the object: obj[0] = userdata. */
5207 lua_newtable(L);
Willy Tarreaude491382015-04-06 11:04:28 +02005208 htxn = lua_newuserdata(L, sizeof(*htxn));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005209 lua_rawseti(L, -2, 0);
5210
Willy Tarreaude491382015-04-06 11:04:28 +02005211 htxn->s = s;
5212 htxn->p = p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01005213 htxn->dir = dir;
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005214 htxn->flags = flags;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005215
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005216 /* Create the "f" field that contains a list of fetches. */
5217 lua_pushstring(L, "f");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005218 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005219 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005220 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005221
5222 /* Create the "sf" field that contains a list of stringsafe fetches. */
5223 lua_pushstring(L, "sf");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005224 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP | HLUA_F_AS_STRING))
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005225 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005226 lua_rawset(L, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005227
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005228 /* Create the "c" field that contains a list of converters. */
5229 lua_pushstring(L, "c");
Willy Tarreaude491382015-04-06 11:04:28 +02005230 if (!hlua_converters_new(L, htxn, 0))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005231 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005232 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005233
5234 /* Create the "sc" field that contains a list of stringsafe converters. */
5235 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01005236 if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING))
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005237 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005238 lua_rawset(L, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005239
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005240 /* Create the "req" field that contains the request channel object. */
5241 lua_pushstring(L, "req");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005242 if (!hlua_channel_new(L, &s->req))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005243 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005244 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005245
5246 /* Create the "res" field that contains the response channel object. */
5247 lua_pushstring(L, "res");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005248 if (!hlua_channel_new(L, &s->res))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005249 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005250 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005251
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005252 /* Creates the HTTP object is the current proxy allows http. */
5253 lua_pushstring(L, "http");
5254 if (p->mode == PR_MODE_HTTP) {
Willy Tarreaude491382015-04-06 11:04:28 +02005255 if (!hlua_http_new(L, htxn))
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005256 return 0;
5257 }
5258 else
5259 lua_pushnil(L);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005260 lua_rawset(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005261
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005262 /* Pop a class sesison metatable and affect it to the userdata. */
5263 lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref);
5264 lua_setmetatable(L, -2);
5265
5266 return 1;
5267}
5268
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005269__LJMP static int hlua_txn_deflog(lua_State *L)
5270{
5271 const char *msg;
5272 struct hlua_txn *htxn;
5273
5274 MAY_LJMP(check_args(L, 2, "deflog"));
5275 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5276 msg = MAY_LJMP(luaL_checkstring(L, 2));
5277
5278 hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg);
5279 return 0;
5280}
5281
5282__LJMP static int hlua_txn_log(lua_State *L)
5283{
5284 int level;
5285 const char *msg;
5286 struct hlua_txn *htxn;
5287
5288 MAY_LJMP(check_args(L, 3, "log"));
5289 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5290 level = MAY_LJMP(luaL_checkinteger(L, 2));
5291 msg = MAY_LJMP(luaL_checkstring(L, 3));
5292
5293 if (level < 0 || level >= NB_LOG_LEVELS)
5294 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
5295
5296 hlua_sendlog(htxn->s->be, level, msg);
5297 return 0;
5298}
5299
5300__LJMP static int hlua_txn_log_debug(lua_State *L)
5301{
5302 const char *msg;
5303 struct hlua_txn *htxn;
5304
5305 MAY_LJMP(check_args(L, 2, "Debug"));
5306 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5307 msg = MAY_LJMP(luaL_checkstring(L, 2));
5308 hlua_sendlog(htxn->s->be, LOG_DEBUG, msg);
5309 return 0;
5310}
5311
5312__LJMP static int hlua_txn_log_info(lua_State *L)
5313{
5314 const char *msg;
5315 struct hlua_txn *htxn;
5316
5317 MAY_LJMP(check_args(L, 2, "Info"));
5318 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5319 msg = MAY_LJMP(luaL_checkstring(L, 2));
5320 hlua_sendlog(htxn->s->be, LOG_INFO, msg);
5321 return 0;
5322}
5323
5324__LJMP static int hlua_txn_log_warning(lua_State *L)
5325{
5326 const char *msg;
5327 struct hlua_txn *htxn;
5328
5329 MAY_LJMP(check_args(L, 2, "Warning"));
5330 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5331 msg = MAY_LJMP(luaL_checkstring(L, 2));
5332 hlua_sendlog(htxn->s->be, LOG_WARNING, msg);
5333 return 0;
5334}
5335
5336__LJMP static int hlua_txn_log_alert(lua_State *L)
5337{
5338 const char *msg;
5339 struct hlua_txn *htxn;
5340
5341 MAY_LJMP(check_args(L, 2, "Alert"));
5342 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5343 msg = MAY_LJMP(luaL_checkstring(L, 2));
5344 hlua_sendlog(htxn->s->be, LOG_ALERT, msg);
5345 return 0;
5346}
5347
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005348__LJMP static int hlua_txn_set_loglevel(lua_State *L)
5349{
5350 struct hlua_txn *htxn;
5351 int ll;
5352
5353 MAY_LJMP(check_args(L, 2, "set_loglevel"));
5354 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5355 ll = MAY_LJMP(luaL_checkinteger(L, 2));
5356
5357 if (ll < 0 || ll > 7)
5358 WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7"));
5359
5360 htxn->s->logs.level = ll;
5361 return 0;
5362}
5363
5364__LJMP static int hlua_txn_set_tos(lua_State *L)
5365{
5366 struct hlua_txn *htxn;
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005367 int tos;
5368
5369 MAY_LJMP(check_args(L, 2, "set_tos"));
5370 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5371 tos = MAY_LJMP(luaL_checkinteger(L, 2));
5372
Willy Tarreau1a18b542018-12-11 16:37:42 +01005373 conn_set_tos(objt_conn(htxn->s->sess->origin), tos);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005374 return 0;
5375}
5376
5377__LJMP static int hlua_txn_set_mark(lua_State *L)
5378{
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005379 struct hlua_txn *htxn;
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005380 int mark;
5381
5382 MAY_LJMP(check_args(L, 2, "set_mark"));
5383 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5384 mark = MAY_LJMP(luaL_checkinteger(L, 2));
5385
Willy Tarreau1a18b542018-12-11 16:37:42 +01005386 conn_set_tos(objt_conn(htxn->s->sess->origin), mark);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005387 return 0;
5388}
5389
Patrick Hemmer268a7072018-05-11 12:52:31 -04005390__LJMP static int hlua_txn_set_priority_class(lua_State *L)
5391{
5392 struct hlua_txn *htxn;
5393
5394 MAY_LJMP(check_args(L, 2, "set_priority_class"));
5395 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5396 htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2)));
5397 return 0;
5398}
5399
5400__LJMP static int hlua_txn_set_priority_offset(lua_State *L)
5401{
5402 struct hlua_txn *htxn;
5403
5404 MAY_LJMP(check_args(L, 2, "set_priority_offset"));
5405 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5406 htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2)));
5407 return 0;
5408}
5409
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005410/* This function is an Lua binding that send pending data
5411 * to the client, and close the stream interface.
5412 */
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005413__LJMP static int hlua_txn_done(lua_State *L)
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005414{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005415 struct hlua_txn *htxn;
Thierry FOURNIER9bd52d42016-07-14 11:45:33 +02005416 struct hlua *hlua;
Willy Tarreau81389672015-03-10 12:03:52 +01005417 struct channel *ic, *oc;
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005418
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005419 MAY_LJMP(check_args(L, 1, "close"));
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005420 htxn = MAY_LJMP(hlua_checktxn(L, 1));
Thierry FOURNIER9bd52d42016-07-14 11:45:33 +02005421 hlua = hlua_gethlua(L);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005422
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005423 /* If the flags NOTERM is set, we cannot terminate the http
5424 * session, so we just end the execution of the current
5425 * lua code.
5426 */
5427 if (htxn->flags & HLUA_TXN_NOTERM) {
5428 WILL_LJMP(hlua_done(L));
5429 return 0;
5430 }
5431
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005432 ic = &htxn->s->req;
5433 oc = &htxn->s->res;
Willy Tarreau81389672015-03-10 12:03:52 +01005434
Willy Tarreau630ef452015-08-28 10:06:15 +02005435 if (htxn->s->txn) {
5436 /* HTTP mode, let's stay in sync with the stream */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005437 b_del(&ic->buf, htxn->s->txn->req.sov);
Willy Tarreau630ef452015-08-28 10:06:15 +02005438 htxn->s->txn->req.next -= htxn->s->txn->req.sov;
5439 htxn->s->txn->req.sov = 0;
5440 ic->analysers &= AN_REQ_HTTP_XFER_BODY;
5441 oc->analysers = AN_RES_HTTP_XFER_BODY;
5442 htxn->s->txn->req.msg_state = HTTP_MSG_CLOSED;
5443 htxn->s->txn->rsp.msg_state = HTTP_MSG_DONE;
5444
Willy Tarreau630ef452015-08-28 10:06:15 +02005445 /* Note that if we want to support keep-alive, we need
5446 * to bypass the close/shutr_now calls below, but that
5447 * may only be done if the HTTP request was already
5448 * processed and the connection header is known (ie
5449 * not during TCP rules).
5450 */
5451 }
5452
Thierry FOURNIER10ec2142015-08-24 17:23:45 +02005453 channel_auto_read(ic);
Willy Tarreau81389672015-03-10 12:03:52 +01005454 channel_abort(ic);
5455 channel_auto_close(ic);
5456 channel_erase(ic);
Thierry FOURNIER10ec2142015-08-24 17:23:45 +02005457
5458 oc->wex = tick_add_ifset(now_ms, oc->wto);
Willy Tarreau81389672015-03-10 12:03:52 +01005459 channel_auto_read(oc);
5460 channel_auto_close(oc);
5461 channel_shutr_now(oc);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005462
Willy Tarreau0458b082015-08-28 09:40:04 +02005463 ic->analysers = 0;
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005464
Thierry FOURNIER9bd52d42016-07-14 11:45:33 +02005465 hlua->flags |= HLUA_STOP;
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005466 WILL_LJMP(hlua_done(L));
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005467 return 0;
5468}
5469
5470__LJMP static int hlua_log(lua_State *L)
5471{
5472 int level;
5473 const char *msg;
5474
5475 MAY_LJMP(check_args(L, 2, "log"));
5476 level = MAY_LJMP(luaL_checkinteger(L, 1));
5477 msg = MAY_LJMP(luaL_checkstring(L, 2));
5478
5479 if (level < 0 || level >= NB_LOG_LEVELS)
5480 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
5481
5482 hlua_sendlog(NULL, level, msg);
5483 return 0;
5484}
5485
5486__LJMP static int hlua_log_debug(lua_State *L)
5487{
5488 const char *msg;
5489
5490 MAY_LJMP(check_args(L, 1, "debug"));
5491 msg = MAY_LJMP(luaL_checkstring(L, 1));
5492 hlua_sendlog(NULL, LOG_DEBUG, msg);
5493 return 0;
5494}
5495
5496__LJMP static int hlua_log_info(lua_State *L)
5497{
5498 const char *msg;
5499
5500 MAY_LJMP(check_args(L, 1, "info"));
5501 msg = MAY_LJMP(luaL_checkstring(L, 1));
5502 hlua_sendlog(NULL, LOG_INFO, msg);
5503 return 0;
5504}
5505
5506__LJMP static int hlua_log_warning(lua_State *L)
5507{
5508 const char *msg;
5509
5510 MAY_LJMP(check_args(L, 1, "warning"));
5511 msg = MAY_LJMP(luaL_checkstring(L, 1));
5512 hlua_sendlog(NULL, LOG_WARNING, msg);
5513 return 0;
5514}
5515
5516__LJMP static int hlua_log_alert(lua_State *L)
5517{
5518 const char *msg;
5519
5520 MAY_LJMP(check_args(L, 1, "alert"));
5521 msg = MAY_LJMP(luaL_checkstring(L, 1));
5522 hlua_sendlog(NULL, LOG_ALERT, msg);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005523 return 0;
5524}
5525
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005526__LJMP static int hlua_sleep_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005527{
5528 int wakeup_ms = lua_tointeger(L, -1);
5529 if (now_ms < wakeup_ms)
Willy Tarreau9635e032018-10-16 17:52:55 +02005530 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005531 return 0;
5532}
5533
5534__LJMP static int hlua_sleep(lua_State *L)
5535{
5536 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005537 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005538
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005539 MAY_LJMP(check_args(L, 1, "sleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005540
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005541 delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005542 wakeup_ms = tick_add(now_ms, delay);
5543 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005544
Willy Tarreau9635e032018-10-16 17:52:55 +02005545 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005546 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005547}
5548
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005549__LJMP static int hlua_msleep(lua_State *L)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005550{
5551 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005552 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005553
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005554 MAY_LJMP(check_args(L, 1, "msleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005555
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005556 delay = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005557 wakeup_ms = tick_add(now_ms, delay);
5558 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005559
Willy Tarreau9635e032018-10-16 17:52:55 +02005560 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005561 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005562}
5563
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01005564/* This functionis an LUA binding. it permits to give back
5565 * the hand at the HAProxy scheduler. It is used when the
5566 * LUA processing consumes a lot of time.
5567 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005568__LJMP static int hlua_yield_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005569{
5570 return 0;
5571}
5572
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01005573__LJMP static int hlua_yield(lua_State *L)
5574{
Willy Tarreau9635e032018-10-16 17:52:55 +02005575 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_yield_yield, TICK_ETERNITY, HLUA_CTRLYIELD));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005576 return 0;
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01005577}
5578
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005579/* This function change the nice of the currently executed
5580 * task. It is used set low or high priority at the current
5581 * task.
5582 */
Willy Tarreau59551662015-03-10 14:23:13 +01005583__LJMP static int hlua_set_nice(lua_State *L)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005584{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005585 struct hlua *hlua;
5586 int nice;
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005587
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005588 MAY_LJMP(check_args(L, 1, "set_nice"));
5589 hlua = hlua_gethlua(L);
5590 nice = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005591
5592 /* If he task is not set, I'm in a start mode. */
5593 if (!hlua || !hlua->task)
5594 return 0;
5595
5596 if (nice < -1024)
5597 nice = -1024;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005598 else if (nice > 1024)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005599 nice = 1024;
5600
5601 hlua->task->nice = nice;
5602 return 0;
5603}
5604
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08005605/* This function is used as a callback of a task. It is called by the
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005606 * HAProxy task subsystem when the task is awaked. The LUA runtime can
5607 * return an E_AGAIN signal, the emmiter of this signal must set a
5608 * signal to wake the task.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005609 *
5610 * Task wrapper are longjmp safe because the only one Lua code
5611 * executed is the safe hlua_ctx_resume();
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005612 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02005613static struct task *hlua_process_task(struct task *task, void *context, unsigned short state)
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005614{
Olivier Houchard9f6af332018-05-25 14:04:04 +02005615 struct hlua *hlua = context;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005616 enum hlua_exec status;
5617
Christopher Faulet5bc99722018-04-25 10:34:45 +02005618 if (task->thread_mask == MAX_THREADS_MASK)
5619 task_set_affinity(task, tid_bit);
5620
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005621 /* If it is the first call to the task, we must initialize the
5622 * execution timeouts.
5623 */
5624 if (!HLUA_IS_RUNNING(hlua))
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02005625 hlua->max_time = hlua_timeout_task;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005626
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005627 /* Execute the Lua code. */
5628 status = hlua_ctx_resume(hlua, 1);
5629
5630 switch (status) {
5631 /* finished or yield */
5632 case HLUA_E_OK:
5633 hlua_ctx_destroy(hlua);
5634 task_delete(task);
5635 task_free(task);
Tim Duesterhuscd235c62018-04-24 13:56:01 +02005636 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005637 break;
5638
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01005639 case HLUA_E_AGAIN: /* co process or timeout wake me later. */
Thierry FOURNIERcb146882017-12-10 17:10:57 +01005640 notification_gc(&hlua->com);
PiBa-NLfe971b32018-05-02 22:27:14 +02005641 task->expire = hlua->wake_time;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005642 break;
5643
5644 /* finished with error. */
5645 case HLUA_E_ERRMSG:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005646 SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005647 hlua_ctx_destroy(hlua);
5648 task_delete(task);
5649 task_free(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02005650 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005651 break;
5652
5653 case HLUA_E_ERR:
5654 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005655 SEND_ERR(NULL, "Lua task: unknown error.\n");
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005656 hlua_ctx_destroy(hlua);
5657 task_delete(task);
5658 task_free(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02005659 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005660 break;
5661 }
Emeric Brun253e53e2017-10-17 18:58:40 +02005662 return task;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005663}
5664
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01005665/* This function is an LUA binding that register LUA function to be
5666 * executed after the HAProxy configuration parsing and before the
5667 * HAProxy scheduler starts. This function expect only one LUA
5668 * argument that is a function. This function returns nothing, but
5669 * throws if an error is encountered.
5670 */
5671__LJMP static int hlua_register_init(lua_State *L)
5672{
5673 struct hlua_init_function *init;
5674 int ref;
5675
5676 MAY_LJMP(check_args(L, 1, "register_init"));
5677
5678 ref = MAY_LJMP(hlua_checkfunction(L, 1));
5679
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02005680 init = calloc(1, sizeof(*init));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01005681 if (!init)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01005682 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01005683
5684 init->function_ref = ref;
5685 LIST_ADDQ(&hlua_init_functions, &init->l);
5686 return 0;
5687}
5688
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005689/* This functio is an LUA binding. It permits to register a task
5690 * executed in parallel of the main HAroxy activity. The task is
5691 * created and it is set in the HAProxy scheduler. It can be called
5692 * from the "init" section, "post init" or during the runtime.
5693 *
5694 * Lua prototype:
5695 *
5696 * <none> core.register_task(<function>)
5697 */
5698static int hlua_register_task(lua_State *L)
5699{
5700 struct hlua *hlua;
5701 struct task *task;
5702 int ref;
5703
5704 MAY_LJMP(check_args(L, 1, "register_task"));
5705
5706 ref = MAY_LJMP(hlua_checkfunction(L, 1));
5707
Willy Tarreaubafbe012017-11-24 17:34:44 +01005708 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005709 if (!hlua)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01005710 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005711
Emeric Brunc60def82017-09-27 14:59:38 +02005712 task = task_new(MAX_THREADS_MASK);
Willy Tarreaue09101e2018-10-16 17:37:12 +02005713 if (!task)
5714 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
5715
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005716 task->context = hlua;
5717 task->process = hlua_process_task;
5718
5719 if (!hlua_ctx_init(hlua, task))
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01005720 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005721
5722 /* Restore the function in the stack. */
5723 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref);
5724 hlua->nargs = 0;
5725
5726 /* Schedule task. */
5727 task_schedule(task, now_ms);
5728
5729 return 0;
5730}
5731
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005732/* Wrapper called by HAProxy to execute an LUA converter. This wrapper
5733 * doesn't allow "yield" functions because the HAProxy engine cannot
5734 * resume converters.
5735 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02005736static int hlua_sample_conv_wrapper(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005737{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02005738 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02005739 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01005740 const char *error;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005741
Willy Tarreaube508f12016-03-10 11:47:01 +01005742 if (!stream)
5743 return 0;
5744
Christopher Fauletafd8f102018-11-08 11:34:21 +01005745 if (IS_HTX_STRM(stream)) {
5746 SEND_ERR(stream->be, "Lua converter '%s': Lua fetches cannot be used when the"
5747 " HTX internal representation is enabled.\n", fcn->name);
5748 return 0;
5749 }
Willy Tarreau87b09662015-04-03 00:22:06 +02005750 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01005751 * Lua context can be not initialized. This behavior
5752 * permits to save performances because a systematic
5753 * Lua initialization cause 5% performances loss.
5754 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005755 if (!stream->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01005756 stream->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005757 if (!stream->hlua) {
5758 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
5759 return 0;
5760 }
5761 if (!hlua_ctx_init(stream->hlua, stream->task)) {
5762 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
5763 return 0;
5764 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01005765 }
5766
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005767 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005768 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005769
5770 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005771 if (!SET_SAFE_LJMP(stream->hlua->T)) {
5772 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
5773 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01005774 else
5775 error = "critical error";
5776 SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005777 return 0;
5778 }
5779
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005780 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005781 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005782 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005783 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005784 return 0;
5785 }
5786
5787 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005788 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005789
5790 /* convert input sample and pust-it in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005791 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005792 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005793 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005794 return 0;
5795 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005796 hlua_smp2lua(stream->hlua->T, smp);
5797 stream->hlua->nargs = 1;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005798
5799 /* push keywords in the stack. */
5800 if (arg_p) {
5801 for (; arg_p->type != ARGT_STOP; arg_p++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005802 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005803 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005804 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005805 return 0;
5806 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005807 hlua_arg2lua(stream->hlua->T, arg_p);
5808 stream->hlua->nargs++;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005809 }
5810 }
5811
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005812 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005813 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005814
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005815 /* At this point the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005816 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005817 }
5818
5819 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005820 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005821 /* finished. */
5822 case HLUA_E_OK:
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02005823 /* If the stack is empty, the function fails. */
5824 if (lua_gettop(stream->hlua->T) <= 0)
5825 return 0;
5826
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005827 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005828 hlua_lua2smp(stream->hlua->T, -1, smp);
5829 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005830 return 1;
5831
5832 /* yield. */
5833 case HLUA_E_AGAIN:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005834 SEND_ERR(stream->be, "Lua converter '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005835 return 0;
5836
5837 /* finished with error. */
5838 case HLUA_E_ERRMSG:
5839 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005840 SEND_ERR(stream->be, "Lua converter '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005841 fcn->name, lua_tostring(stream->hlua->T, -1));
5842 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005843 return 0;
5844
Thierry Fournierd5b073c2018-05-21 19:42:47 +02005845 case HLUA_E_ETMOUT:
5846 SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name);
5847 return 0;
5848
5849 case HLUA_E_NOMEM:
5850 SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name);
5851 return 0;
5852
5853 case HLUA_E_YIELD:
5854 SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name);
5855 return 0;
5856
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005857 case HLUA_E_ERR:
5858 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005859 SEND_ERR(stream->be, "Lua converter '%s' returns an unknown error.\n", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005860
5861 default:
5862 return 0;
5863 }
5864}
5865
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005866/* Wrapper called by HAProxy to execute a sample-fetch. this wrapper
5867 * doesn't allow "yield" functions because the HAProxy engine cannot
Willy Tarreaube508f12016-03-10 11:47:01 +01005868 * resume sample-fetches. This function will be called by the sample
5869 * fetch engine to call lua-based fetch operations.
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005870 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +02005871static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp,
5872 const char *kw, void *private)
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005873{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02005874 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02005875 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01005876 const char *error;
Willy Tarreau83061a82018-07-13 11:56:34 +02005877 const struct buffer msg = { };
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005878
Willy Tarreaube508f12016-03-10 11:47:01 +01005879 if (!stream)
5880 return 0;
Christopher Fauletafd8f102018-11-08 11:34:21 +01005881
5882 if (IS_HTX_STRM(stream)) {
5883 SEND_ERR(stream->be, "Lua sample-fetch '%s': Lua fetches cannot be used when the"
5884 " HTX internal representation is enabled.\n", fcn->name);
5885 return 0;
5886 }
Willy Tarreaube508f12016-03-10 11:47:01 +01005887
Willy Tarreau87b09662015-04-03 00:22:06 +02005888 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01005889 * Lua context can be not initialized. This behavior
5890 * permits to save performances because a systematic
5891 * Lua initialization cause 5% performances loss.
5892 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005893 if (!stream->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01005894 stream->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005895 if (!stream->hlua) {
5896 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
5897 return 0;
5898 }
5899 if (!hlua_ctx_init(stream->hlua, stream->task)) {
5900 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
5901 return 0;
5902 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01005903 }
5904
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005905 consistency_set(stream, smp->opt, &stream->hlua->cons);
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005906
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005907 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005908 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005909
5910 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005911 if (!SET_SAFE_LJMP(stream->hlua->T)) {
5912 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
5913 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01005914 else
5915 error = "critical error";
5916 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005917 return 0;
5918 }
5919
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005920 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005921 if (!lua_checkstack(stream->hlua->T, 2)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005922 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005923 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005924 return 0;
5925 }
5926
5927 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005928 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005929
5930 /* push arguments in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005931 if (!hlua_txn_new(stream->hlua->T, stream, smp->px, smp->opt & SMP_OPT_DIR,
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005932 HLUA_TXN_NOTERM)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005933 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005934 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005935 return 0;
5936 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005937 stream->hlua->nargs = 1;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005938
5939 /* push keywords in the stack. */
5940 for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) {
5941 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005942 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005943 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005944 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005945 return 0;
5946 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005947 hlua_arg2lua(stream->hlua->T, arg_p);
5948 stream->hlua->nargs++;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005949 }
5950
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005951 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005952 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005953
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005954 /* At this point the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005955 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005956 }
5957
5958 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005959 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005960 /* finished. */
5961 case HLUA_E_OK:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005962 if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) {
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005963 stream_int_retnclose(&stream->si[0], &msg);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02005964 return 0;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005965 }
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02005966 /* If the stack is empty, the function fails. */
5967 if (lua_gettop(stream->hlua->T) <= 0)
5968 return 0;
5969
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005970 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005971 hlua_lua2smp(stream->hlua->T, -1, smp);
5972 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005973
5974 /* Set the end of execution flag. */
5975 smp->flags &= ~SMP_F_MAY_CHANGE;
5976 return 1;
5977
5978 /* yield. */
5979 case HLUA_E_AGAIN:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005980 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005981 stream_int_retnclose(&stream->si[0], &msg);
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005982 SEND_ERR(smp->px, "Lua sample-fetch '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005983 return 0;
5984
5985 /* finished with error. */
5986 case HLUA_E_ERRMSG:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005987 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005988 stream_int_retnclose(&stream->si[0], &msg);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005989 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005990 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005991 fcn->name, lua_tostring(stream->hlua->T, -1));
5992 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005993 return 0;
5994
Thierry Fournierd5b073c2018-05-21 19:42:47 +02005995 case HLUA_E_ETMOUT:
5996 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
5997 stream_int_retnclose(&stream->si[0], &msg);
5998 SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name);
5999 return 0;
6000
6001 case HLUA_E_NOMEM:
6002 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
6003 stream_int_retnclose(&stream->si[0], &msg);
6004 SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name);
6005 return 0;
6006
6007 case HLUA_E_YIELD:
6008 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
6009 stream_int_retnclose(&stream->si[0], &msg);
6010 SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name);
6011 return 0;
6012
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006013 case HLUA_E_ERR:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006014 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006015 stream_int_retnclose(&stream->si[0], &msg);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006016 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006017 SEND_ERR(smp->px, "Lua sample-fetch '%s' returns an unknown error.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006018
6019 default:
6020 return 0;
6021 }
6022}
6023
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006024/* This function is an LUA binding used for registering
6025 * "sample-conv" functions. It expects a converter name used
6026 * in the haproxy configuration file, and an LUA function.
6027 */
6028__LJMP static int hlua_register_converters(lua_State *L)
6029{
6030 struct sample_conv_kw_list *sck;
6031 const char *name;
6032 int ref;
6033 int len;
6034 struct hlua_function *fcn;
6035
6036 MAY_LJMP(check_args(L, 2, "register_converters"));
6037
6038 /* First argument : converter name. */
6039 name = MAY_LJMP(luaL_checkstring(L, 1));
6040
6041 /* Second argument : lua function. */
6042 ref = MAY_LJMP(hlua_checkfunction(L, 2));
6043
6044 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006045 sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006046 if (!sck)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006047 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006048 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006049 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006050 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006051
6052 /* Fill fcn. */
6053 fcn->name = strdup(name);
6054 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006055 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006056 fcn->function_ref = ref;
6057
6058 /* List head */
6059 sck->list.n = sck->list.p = NULL;
6060
6061 /* converter keyword. */
6062 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006063 sck->kw[0].kw = calloc(1, len);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006064 if (!sck->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006065 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006066
6067 snprintf((char *)sck->kw[0].kw, len, "lua.%s", name);
6068 sck->kw[0].process = hlua_sample_conv_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01006069 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 +01006070 sck->kw[0].val_args = NULL;
6071 sck->kw[0].in_type = SMP_T_STR;
6072 sck->kw[0].out_type = SMP_T_STR;
6073 sck->kw[0].private = fcn;
6074
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006075 /* Register this new converter */
6076 sample_register_convs(sck);
6077
6078 return 0;
6079}
6080
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006081/* This function is an LUA binding used for registering
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006082 * "sample-fetch" functions. It expects a converter name used
6083 * in the haproxy configuration file, and an LUA function.
6084 */
6085__LJMP static int hlua_register_fetches(lua_State *L)
6086{
6087 const char *name;
6088 int ref;
6089 int len;
6090 struct sample_fetch_kw_list *sfk;
6091 struct hlua_function *fcn;
6092
6093 MAY_LJMP(check_args(L, 2, "register_fetches"));
6094
6095 /* First argument : sample-fetch name. */
6096 name = MAY_LJMP(luaL_checkstring(L, 1));
6097
6098 /* Second argument : lua function. */
6099 ref = MAY_LJMP(hlua_checkfunction(L, 2));
6100
6101 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006102 sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006103 if (!sfk)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006104 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006105 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006106 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006107 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006108
6109 /* Fill fcn. */
6110 fcn->name = strdup(name);
6111 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006112 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006113 fcn->function_ref = ref;
6114
6115 /* List head */
6116 sfk->list.n = sfk->list.p = NULL;
6117
6118 /* sample-fetch keyword. */
6119 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006120 sfk->kw[0].kw = calloc(1, len);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006121 if (!sfk->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006122 return luaL_error(L, "Lua out of memory error.");
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006123
6124 snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name);
6125 sfk->kw[0].process = hlua_sample_fetch_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01006126 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 +01006127 sfk->kw[0].val_args = NULL;
6128 sfk->kw[0].out_type = SMP_T_STR;
6129 sfk->kw[0].use = SMP_USE_HTTP_ANY;
6130 sfk->kw[0].val = 0;
6131 sfk->kw[0].private = fcn;
6132
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006133 /* Register this new fetch. */
6134 sample_register_fetches(sfk);
6135
6136 return 0;
6137}
6138
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006139/* This function is a wrapper to execute each LUA function declared
6140 * as an action wrapper during the initialisation period. This function
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006141 * return ACT_RET_CONT if the processing is finished (with or without
6142 * error) and return ACT_RET_YIELD if the function must be called again
6143 * because the LUA returns a yield.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006144 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006145static enum act_return hlua_action(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +02006146 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006147{
6148 char **arg;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006149 unsigned int analyzer;
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02006150 int dir;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006151 const char *error;
Willy Tarreau83061a82018-07-13 11:56:34 +02006152 const struct buffer msg = { };
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006153
6154 switch (rule->from) {
Thierry FOURNIER6e01f382015-11-02 09:52:54 +01006155 case ACT_F_TCP_REQ_CNT: analyzer = AN_REQ_INSPECT_FE ; dir = SMP_OPT_DIR_REQ; break;
6156 case ACT_F_TCP_RES_CNT: analyzer = AN_RES_INSPECT ; dir = SMP_OPT_DIR_RES; break;
6157 case ACT_F_HTTP_REQ: analyzer = AN_REQ_HTTP_PROCESS_FE; dir = SMP_OPT_DIR_REQ; break;
6158 case ACT_F_HTTP_RES: analyzer = AN_RES_HTTP_PROCESS_BE; dir = SMP_OPT_DIR_RES; break;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006159 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006160 SEND_ERR(px, "Lua: internal error while execute action.\n");
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006161 return ACT_RET_CONT;
6162 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006163
Willy Tarreau87b09662015-04-03 00:22:06 +02006164 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006165 * Lua context can be not initialized. This behavior
6166 * permits to save performances because a systematic
6167 * Lua initialization cause 5% performances loss.
6168 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006169 if (!s->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006170 s->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006171 if (!s->hlua) {
6172 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
6173 rule->arg.hlua_rule->fcn.name);
6174 return ACT_RET_CONT;
6175 }
6176 if (!hlua_ctx_init(s->hlua, s->task)) {
6177 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
6178 rule->arg.hlua_rule->fcn.name);
6179 return ACT_RET_CONT;
6180 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006181 }
6182
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006183 consistency_set(s, dir, &s->hlua->cons);
6184
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006185 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006186 if (!HLUA_IS_RUNNING(s->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006187
6188 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006189 if (!SET_SAFE_LJMP(s->hlua->T)) {
6190 if (lua_type(s->hlua->T, -1) == LUA_TSTRING)
6191 error = lua_tostring(s->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006192 else
6193 error = "critical error";
6194 SEND_ERR(px, "Lua function '%s': %s.\n",
6195 rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006196 return ACT_RET_CONT;
6197 }
6198
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006199 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006200 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006201 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006202 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006203 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006204 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006205 }
6206
6207 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006208 lua_rawgeti(s->hlua->T, LUA_REGISTRYINDEX, rule->arg.hlua_rule->fcn.function_ref);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006209
Willy Tarreau87b09662015-04-03 00:22:06 +02006210 /* Create and and push object stream in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006211 if (!hlua_txn_new(s->hlua->T, s, px, dir, 0)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006212 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006213 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006214 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006215 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006216 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006217 s->hlua->nargs = 1;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006218
6219 /* push keywords in the stack. */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006220 for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006221 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006222 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006223 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006224 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006225 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006226 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006227 lua_pushstring(s->hlua->T, *arg);
6228 s->hlua->nargs++;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006229 }
6230
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006231 /* Now the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006232 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006233
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006234 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006235 s->hlua->max_time = hlua_timeout_session;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006236 }
6237
6238 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006239 switch (hlua_ctx_resume(s->hlua, !(flags & ACT_FLAG_FINAL))) {
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006240 /* finished. */
6241 case HLUA_E_OK:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006242 if (!consistency_check(s, dir, &s->hlua->cons)) {
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006243 stream_int_retnclose(&s->si[0], &msg);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02006244 return ACT_RET_ERR;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006245 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006246 if (s->hlua->flags & HLUA_STOP)
Thierry FOURNIER9bd52d42016-07-14 11:45:33 +02006247 return ACT_RET_STOP;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006248 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006249
6250 /* yield. */
6251 case HLUA_E_AGAIN:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006252 /* Set timeout in the required channel. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006253 if (s->hlua->wake_time != TICK_ETERNITY) {
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006254 if (analyzer & (AN_REQ_INSPECT_FE|AN_REQ_HTTP_PROCESS_FE))
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006255 s->req.analyse_exp = s->hlua->wake_time;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006256 else if (analyzer & (AN_RES_INSPECT|AN_RES_HTTP_PROCESS_BE))
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006257 s->res.analyse_exp = s->hlua->wake_time;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006258 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006259 /* Some actions can be wake up when a "write" event
6260 * is detected on a response channel. This is useful
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006261 * only for actions targeted on the requests.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006262 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006263 if (HLUA_IS_WAKERESWR(s->hlua)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006264 s->res.flags |= CF_WAKE_WRITE;
Willy Tarreau76bd97f2015-03-10 17:16:10 +01006265 if ((analyzer & (AN_REQ_INSPECT_FE|AN_REQ_HTTP_PROCESS_FE)))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006266 s->res.analysers |= analyzer;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006267 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006268 if (HLUA_IS_WAKEREQWR(s->hlua))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006269 s->req.flags |= CF_WAKE_WRITE;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006270 /* We can quit the function without consistency check
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006271 * because HAProxy is not able to manipulate data, it
6272 * is only allowed to call me again. */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006273 return ACT_RET_YIELD;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006274
6275 /* finished with error. */
6276 case HLUA_E_ERRMSG:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006277 if (!consistency_check(s, dir, &s->hlua->cons)) {
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006278 stream_int_retnclose(&s->si[0], &msg);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02006279 return ACT_RET_ERR;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006280 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006281 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006282 SEND_ERR(px, "Lua function '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006283 rule->arg.hlua_rule->fcn.name, lua_tostring(s->hlua->T, -1));
6284 lua_pop(s->hlua->T, 1);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006285 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006286
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006287 case HLUA_E_ETMOUT:
6288 if (!consistency_check(s, dir, &s->hlua->cons)) {
6289 stream_int_retnclose(&s->si[0], &msg);
6290 return ACT_RET_ERR;
6291 }
6292 SEND_ERR(px, "Lua function '%s': execution timeout.\n", rule->arg.hlua_rule->fcn.name);
6293 return 0;
6294
6295 case HLUA_E_NOMEM:
6296 if (!consistency_check(s, dir, &s->hlua->cons)) {
6297 stream_int_retnclose(&s->si[0], &msg);
6298 return ACT_RET_ERR;
6299 }
6300 SEND_ERR(px, "Lua function '%s': out of memory error.\n", rule->arg.hlua_rule->fcn.name);
6301 return 0;
6302
6303 case HLUA_E_YIELD:
6304 if (!consistency_check(s, dir, &s->hlua->cons)) {
6305 stream_int_retnclose(&s->si[0], &msg);
6306 return ACT_RET_ERR;
6307 }
6308 SEND_ERR(px, "Lua function '%s': aborting Lua processing on expired timeout.\n",
6309 rule->arg.hlua_rule->fcn.name);
6310 return 0;
6311
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006312 case HLUA_E_ERR:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006313 if (!consistency_check(s, dir, &s->hlua->cons)) {
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006314 stream_int_retnclose(&s->si[0], &msg);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02006315 return ACT_RET_ERR;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006316 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006317 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006318 SEND_ERR(px, "Lua function '%s' return an unknown error.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006319 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006320
6321 default:
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006322 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006323 }
6324}
6325
Olivier Houchard9f6af332018-05-25 14:04:04 +02006326struct task *hlua_applet_wakeup(struct task *t, void *context, unsigned short state)
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006327{
Olivier Houchard9f6af332018-05-25 14:04:04 +02006328 struct appctx *ctx = context;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006329
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006330 appctx_wakeup(ctx);
Willy Tarreaud9587412017-08-23 16:07:33 +02006331 t->expire = TICK_ETERNITY;
Willy Tarreaud1aa41f2017-07-21 16:41:56 +02006332 return t;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006333}
6334
6335static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
6336{
6337 struct stream_interface *si = ctx->owner;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006338 struct hlua *hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006339 struct task *task;
6340 char **arg;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006341 const char *error;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006342
Willy Tarreaubafbe012017-11-24 17:34:44 +01006343 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006344 if (!hlua) {
6345 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
6346 ctx->rule->arg.hlua_rule->fcn.name);
6347 return 0;
6348 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006349 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006350 ctx->ctx.hlua_apptcp.hlua = hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006351 ctx->ctx.hlua_apptcp.flags = 0;
6352
6353 /* Create task used by signal to wakeup applets. */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01006354 task = task_new(tid_bit);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006355 if (!task) {
6356 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
6357 ctx->rule->arg.hlua_rule->fcn.name);
6358 return 0;
6359 }
6360 task->nice = 0;
6361 task->context = ctx;
6362 task->process = hlua_applet_wakeup;
6363 ctx->ctx.hlua_apptcp.task = task;
6364
6365 /* In the execution wrappers linked with a stream, the
6366 * Lua context can be not initialized. This behavior
6367 * permits to save performances because a systematic
6368 * Lua initialization cause 5% performances loss.
6369 */
6370 if (!hlua_ctx_init(hlua, task)) {
6371 SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n",
6372 ctx->rule->arg.hlua_rule->fcn.name);
6373 return 0;
6374 }
6375
6376 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02006377 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006378
6379 /* The following Lua calls can fail. */
6380 if (!SET_SAFE_LJMP(hlua->T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01006381 if (lua_type(hlua->T, -1) == LUA_TSTRING)
6382 error = lua_tostring(hlua->T, -1);
6383 else
6384 error = "critical error";
6385 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
6386 ctx->rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006387 return 0;
6388 }
6389
6390 /* Check stack available size. */
6391 if (!lua_checkstack(hlua->T, 1)) {
6392 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
6393 ctx->rule->arg.hlua_rule->fcn.name);
6394 RESET_SAFE_LJMP(hlua->T);
6395 return 0;
6396 }
6397
6398 /* Restore the function in the stack. */
6399 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref);
6400
6401 /* Create and and push object stream in the stack. */
6402 if (!hlua_applet_tcp_new(hlua->T, ctx)) {
6403 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
6404 ctx->rule->arg.hlua_rule->fcn.name);
6405 RESET_SAFE_LJMP(hlua->T);
6406 return 0;
6407 }
6408 hlua->nargs = 1;
6409
6410 /* push keywords in the stack. */
6411 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
6412 if (!lua_checkstack(hlua->T, 1)) {
6413 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
6414 ctx->rule->arg.hlua_rule->fcn.name);
6415 RESET_SAFE_LJMP(hlua->T);
6416 return 0;
6417 }
6418 lua_pushstring(hlua->T, *arg);
6419 hlua->nargs++;
6420 }
6421
6422 RESET_SAFE_LJMP(hlua->T);
6423
6424 /* Wakeup the applet ASAP. */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01006425 si_cant_get(si);
Willy Tarreau3367d412018-11-15 10:57:41 +01006426 si_rx_endp_more(si);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006427
6428 return 1;
6429}
6430
6431static void hlua_applet_tcp_fct(struct appctx *ctx)
6432{
6433 struct stream_interface *si = ctx->owner;
6434 struct stream *strm = si_strm(si);
6435 struct channel *res = si_ic(si);
6436 struct act_rule *rule = ctx->rule;
6437 struct proxy *px = strm->be;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006438 struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006439
6440 /* The applet execution is already done. */
Olivier Houchard594c8c52018-08-28 14:41:31 +02006441 if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) {
6442 /* eat the whole request */
6443 co_skip(si_oc(si), co_data(si_oc(si)));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006444 return;
Olivier Houchard594c8c52018-08-28 14:41:31 +02006445 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006446
6447 /* If the stream is disconnect or closed, ldo nothing. */
6448 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
6449 return;
6450
6451 /* Execute the function. */
6452 switch (hlua_ctx_resume(hlua, 1)) {
6453 /* finished. */
6454 case HLUA_E_OK:
6455 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
6456
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006457 /* eat the whole request */
Willy Tarreaua79021a2018-06-15 18:07:57 +02006458 co_skip(si_oc(si), co_data(si_oc(si)));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006459 res->flags |= CF_READ_NULL;
6460 si_shutr(si);
6461 return;
6462
6463 /* yield. */
6464 case HLUA_E_AGAIN:
Thierry Fournier0164f202016-02-20 17:47:43 +01006465 if (hlua->wake_time != TICK_ETERNITY)
6466 task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006467 return;
6468
6469 /* finished with error. */
6470 case HLUA_E_ERRMSG:
6471 /* Display log. */
6472 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
6473 rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1));
6474 lua_pop(hlua->T, 1);
6475 goto error;
6476
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006477 case HLUA_E_ETMOUT:
6478 SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n",
6479 rule->arg.hlua_rule->fcn.name);
6480 goto error;
6481
6482 case HLUA_E_NOMEM:
6483 SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n",
6484 rule->arg.hlua_rule->fcn.name);
6485 goto error;
6486
6487 case HLUA_E_YIELD: /* unexpected */
6488 SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n",
6489 rule->arg.hlua_rule->fcn.name);
6490 goto error;
6491
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006492 case HLUA_E_ERR:
6493 /* Display log. */
6494 SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n",
6495 rule->arg.hlua_rule->fcn.name);
6496 goto error;
6497
6498 default:
6499 goto error;
6500 }
6501
6502error:
6503
6504 /* For all other cases, just close the stream. */
6505 si_shutw(si);
6506 si_shutr(si);
6507 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
6508}
6509
6510static void hlua_applet_tcp_release(struct appctx *ctx)
6511{
Willy Tarreaubd7fc952017-07-24 17:35:27 +02006512 task_delete(ctx->ctx.hlua_apptcp.task);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006513 task_free(ctx->ctx.hlua_apptcp.task);
6514 ctx->ctx.hlua_apptcp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006515 hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006516 ctx->ctx.hlua_apptcp.hlua = NULL;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006517}
6518
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006519/* The function returns 1 if the initialisation is complete, 0 if
6520 * an errors occurs and -1 if more data are required for initializing
6521 * the applet.
6522 */
6523static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
6524{
6525 struct stream_interface *si = ctx->owner;
6526 struct channel *req = si_oc(si);
6527 struct http_msg *msg;
6528 struct http_txn *txn;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006529 struct hlua *hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006530 char **arg;
6531 struct hdr_ctx hdr;
6532 struct task *task;
6533 struct sample smp; /* just used for a valid call to smp_prefetch_http. */
Thierry Fournierfd107a22016-02-19 19:57:23 +01006534 const char *error;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006535
6536 /* Wait for a full HTTP request. */
6537 if (!smp_prefetch_http(px, strm, 0, NULL, &smp, 0)) {
6538 if (smp.flags & SMP_F_MAY_CHANGE)
6539 return -1;
6540 return 0;
6541 }
6542 txn = strm->txn;
6543 msg = &txn->req;
6544
Willy Tarreau0078bfc2015-10-07 20:20:28 +02006545 /* We want two things in HTTP mode :
6546 * - enforce server-close mode if we were in keep-alive, so that the
6547 * applet is released after each response ;
6548 * - enable request body transfer to the applet in order to resync
6549 * with the response body.
6550 */
6551 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)
6552 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreau0078bfc2015-10-07 20:20:28 +02006553
Willy Tarreaubafbe012017-11-24 17:34:44 +01006554 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006555 if (!hlua) {
6556 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
6557 ctx->rule->arg.hlua_rule->fcn.name);
6558 return 0;
6559 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006560 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006561 ctx->ctx.hlua_apphttp.hlua = hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006562 ctx->ctx.hlua_apphttp.left_bytes = -1;
6563 ctx->ctx.hlua_apphttp.flags = 0;
6564
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +01006565 if (txn->req.flags & HTTP_MSGF_VER_11)
6566 ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11;
6567
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006568 /* Create task used by signal to wakeup applets. */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01006569 task = task_new(tid_bit);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006570 if (!task) {
6571 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
6572 ctx->rule->arg.hlua_rule->fcn.name);
6573 return 0;
6574 }
6575 task->nice = 0;
6576 task->context = ctx;
6577 task->process = hlua_applet_wakeup;
6578 ctx->ctx.hlua_apphttp.task = task;
6579
6580 /* In the execution wrappers linked with a stream, the
6581 * Lua context can be not initialized. This behavior
6582 * permits to save performances because a systematic
6583 * Lua initialization cause 5% performances loss.
6584 */
6585 if (!hlua_ctx_init(hlua, task)) {
6586 SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n",
6587 ctx->rule->arg.hlua_rule->fcn.name);
6588 return 0;
6589 }
6590
6591 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02006592 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006593
6594 /* The following Lua calls can fail. */
6595 if (!SET_SAFE_LJMP(hlua->T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01006596 if (lua_type(hlua->T, -1) == LUA_TSTRING)
6597 error = lua_tostring(hlua->T, -1);
6598 else
6599 error = "critical error";
6600 SEND_ERR(px, "Lua applet http '%s': %s.\n",
6601 ctx->rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006602 return 0;
6603 }
6604
6605 /* Check stack available size. */
6606 if (!lua_checkstack(hlua->T, 1)) {
6607 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
6608 ctx->rule->arg.hlua_rule->fcn.name);
6609 RESET_SAFE_LJMP(hlua->T);
6610 return 0;
6611 }
6612
6613 /* Restore the function in the stack. */
6614 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref);
6615
6616 /* Create and and push object stream in the stack. */
6617 if (!hlua_applet_http_new(hlua->T, ctx)) {
6618 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
6619 ctx->rule->arg.hlua_rule->fcn.name);
6620 RESET_SAFE_LJMP(hlua->T);
6621 return 0;
6622 }
6623 hlua->nargs = 1;
6624
6625 /* Look for a 100-continue expected. */
6626 if (msg->flags & HTTP_MSGF_VER_11) {
6627 hdr.idx = 0;
Willy Tarreaua79021a2018-06-15 18:07:57 +02006628 if (http_find_header2("Expect", 6, ci_head(req), &txn->hdr_idx, &hdr) &&
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006629 unlikely(hdr.vlen == 12 && strncasecmp(hdr.line+hdr.val, "100-continue", 12) == 0))
6630 ctx->ctx.hlua_apphttp.flags |= APPLET_100C;
6631 }
6632
6633 /* push keywords in the stack. */
6634 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
6635 if (!lua_checkstack(hlua->T, 1)) {
6636 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
6637 ctx->rule->arg.hlua_rule->fcn.name);
6638 RESET_SAFE_LJMP(hlua->T);
6639 return 0;
6640 }
6641 lua_pushstring(hlua->T, *arg);
6642 hlua->nargs++;
6643 }
6644
6645 RESET_SAFE_LJMP(hlua->T);
6646
6647 /* Wakeup the applet when data is ready for read. */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01006648 si_cant_get(si);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006649
6650 return 1;
6651}
6652
6653static void hlua_applet_http_fct(struct appctx *ctx)
6654{
6655 struct stream_interface *si = ctx->owner;
6656 struct stream *strm = si_strm(si);
6657 struct channel *res = si_ic(si);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006658 struct act_rule *rule = ctx->rule;
6659 struct proxy *px = strm->be;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006660 struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua;
Willy Tarreau206ba832018-06-14 15:27:31 +02006661 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02006662 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02006663 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02006664 size_t len2;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006665 int ret;
6666
6667 /* If the stream is disconnect or closed, ldo nothing. */
6668 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
6669 return;
6670
6671 /* Set the currently running flag. */
6672 if (!HLUA_IS_RUNNING(hlua) &&
6673 !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
6674
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006675 /* Wait for full HTTP analysys. */
6676 if (unlikely(strm->txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01006677 si_cant_get(si);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006678 return;
6679 }
6680
6681 /* Store the max amount of bytes that we can read. */
6682 ctx->ctx.hlua_apphttp.left_bytes = strm->txn->req.body_len;
6683
6684 /* We need to flush the request header. This left the body
6685 * for the Lua.
6686 */
6687
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006688 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02006689 ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006690 if (ret == -1)
6691 return;
6692
6693 /* No data available, ask for more data. */
6694 if (ret == 1)
6695 len2 = 0;
6696 if (ret == 0)
6697 len1 = 0;
Thierry FOURNIER70d318c2018-06-30 10:37:33 +02006698 if (len1 + len2 < strm->txn->req.eoh + strm->txn->req.eol) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01006699 si_cant_get(si);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006700 return;
6701 }
6702
6703 /* skip the requests bytes. */
Thierry FOURNIER70d318c2018-06-30 10:37:33 +02006704 co_skip(si_oc(si), strm->txn->req.eoh + strm->txn->req.eol);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006705 }
6706
6707 /* Executes The applet if it is not done. */
6708 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
6709
6710 /* Execute the function. */
6711 switch (hlua_ctx_resume(hlua, 1)) {
6712 /* finished. */
6713 case HLUA_E_OK:
6714 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
6715 break;
6716
6717 /* yield. */
6718 case HLUA_E_AGAIN:
Thierry Fournier0164f202016-02-20 17:47:43 +01006719 if (hlua->wake_time != TICK_ETERNITY)
6720 task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006721 return;
6722
6723 /* finished with error. */
6724 case HLUA_E_ERRMSG:
6725 /* Display log. */
6726 SEND_ERR(px, "Lua applet http '%s': %s.\n",
6727 rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1));
6728 lua_pop(hlua->T, 1);
6729 goto error;
6730
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006731 case HLUA_E_ETMOUT:
6732 SEND_ERR(px, "Lua applet http '%s': execution timeout.\n",
6733 rule->arg.hlua_rule->fcn.name);
6734 goto error;
6735
6736 case HLUA_E_NOMEM:
6737 SEND_ERR(px, "Lua applet http '%s': out of memory error.\n",
6738 rule->arg.hlua_rule->fcn.name);
6739 goto error;
6740
6741 case HLUA_E_YIELD: /* unexpected */
6742 SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n",
6743 rule->arg.hlua_rule->fcn.name);
6744 goto error;
6745
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006746 case HLUA_E_ERR:
6747 /* Display log. */
6748 SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n",
6749 rule->arg.hlua_rule->fcn.name);
6750 goto error;
6751
6752 default:
6753 goto error;
6754 }
6755 }
6756
6757 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
6758
6759 /* We must send the final chunk. */
6760 if (ctx->ctx.hlua_apphttp.flags & APPLET_CHUNKED &&
6761 !(ctx->ctx.hlua_apphttp.flags & APPLET_LAST_CHK)) {
6762
6763 /* sent last chunk at once. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02006764 ret = ci_putblk(res, "0\r\n\r\n", 5);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006765
6766 /* critical error. */
6767 if (ret == -2 || ret == -3) {
6768 SEND_ERR(px, "Lua applet http '%s'cannont send last chunk.\n",
6769 rule->arg.hlua_rule->fcn.name);
6770 goto error;
6771 }
6772
6773 /* no enough space error. */
6774 if (ret == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01006775 si_rx_room_blk(si);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006776 return;
6777 }
6778
6779 /* set the last chunk sent. */
6780 ctx->ctx.hlua_apphttp.flags |= APPLET_LAST_CHK;
6781 }
6782
6783 /* close the connection. */
6784
Frédéric Lécaille83ed5d52018-07-18 14:25:26 +02006785 /* status */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006786 strm->txn->status = ctx->ctx.hlua_apphttp.status;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006787
6788 /* eat the whole request */
Willy Tarreaua79021a2018-06-15 18:07:57 +02006789 co_skip(si_oc(si), co_data(si_oc(si)));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006790 res->flags |= CF_READ_NULL;
6791 si_shutr(si);
6792
6793 return;
6794 }
6795
6796error:
6797
6798 /* If we are in HTTP mode, and we are not send any
6799 * data, return a 500 server error in best effort:
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006800 * if there is no room available in the buffer,
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006801 * just close the connection.
6802 */
Willy Tarreau06d80a92017-10-19 14:32:15 +02006803 ci_putblk(res, error_500, strlen(error_500));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006804 if (!(strm->flags & SF_ERR_MASK))
6805 strm->flags |= SF_ERR_RESOURCE;
6806 si_shutw(si);
6807 si_shutr(si);
6808 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
6809}
6810
6811static void hlua_applet_http_release(struct appctx *ctx)
6812{
Willy Tarreaubd7fc952017-07-24 17:35:27 +02006813 task_delete(ctx->ctx.hlua_apphttp.task);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006814 task_free(ctx->ctx.hlua_apphttp.task);
6815 ctx->ctx.hlua_apphttp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006816 hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006817 ctx->ctx.hlua_apphttp.hlua = NULL;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006818}
6819
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006820/* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in
6821 * succes case, else return ACT_RET_PRS_ERR.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006822 *
6823 * This function can fail with an abort() due to an Lua critical error.
6824 * We are in the configuration parsing process of HAProxy, this abort() is
6825 * tolerated.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006826 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006827static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px,
6828 struct act_rule *rule, char **err)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006829{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006830 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006831 int i;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006832
Christopher Fauletafd8f102018-11-08 11:34:21 +01006833 if (px->options2 & PR_O2_USE_HTX) {
6834 memprintf(err, "Lua actions cannot be used when the HTX internal representation is enabled");
6835 return ACT_RET_PRS_ERR;
6836 }
6837
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006838 /* Memory for the rule. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006839 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006840 if (!rule->arg.hlua_rule) {
6841 memprintf(err, "out of memory error");
Thierry FOURNIERafa80492015-08-19 09:04:15 +02006842 return ACT_RET_PRS_ERR;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006843 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006844
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006845 /* Memory for arguments. */
6846 rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, sizeof(char *));
6847 if (!rule->arg.hlua_rule->args) {
6848 memprintf(err, "out of memory error");
6849 return ACT_RET_PRS_ERR;
6850 }
6851
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006852 /* Reference the Lua function and store the reference. */
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006853 rule->arg.hlua_rule->fcn = *fcn;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006854
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006855 /* Expect some arguments */
6856 for (i = 0; i < fcn->nargs; i++) {
6857 if (*args[i+1] == '\0') {
6858 memprintf(err, "expect %d arguments", fcn->nargs);
6859 return ACT_RET_PRS_ERR;
6860 }
6861 rule->arg.hlua_rule->args[i] = strdup(args[i + 1]);
6862 if (!rule->arg.hlua_rule->args[i]) {
6863 memprintf(err, "out of memory error");
6864 return ACT_RET_PRS_ERR;
6865 }
6866 (*cur_arg)++;
6867 }
6868 rule->arg.hlua_rule->args[i] = NULL;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006869
Thierry FOURNIER42148732015-09-02 17:17:33 +02006870 rule->action = ACT_CUSTOM;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006871 rule->action_ptr = hlua_action;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02006872 return ACT_RET_PRS_OK;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006873}
6874
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006875static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px,
6876 struct act_rule *rule, char **err)
6877{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006878 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006879
Christopher Fauletafd8f102018-11-08 11:34:21 +01006880 if (px->options2 & PR_O2_USE_HTX) {
6881 memprintf(err, "Lua services cannot be used when the HTX internal representation is enabled");
6882 return ACT_RET_PRS_ERR;
6883 }
6884
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01006885 /* HTTP applets are forbidden in tcp-request rules.
6886 * HTTP applet request requires everything initilized by
6887 * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER).
6888 * The applet will be immediately initilized, but its before
6889 * the call of this analyzer.
6890 */
6891 if (rule->from != ACT_F_HTTP_REQ) {
6892 memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets");
6893 return ACT_RET_PRS_ERR;
6894 }
6895
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006896 /* Memory for the rule. */
6897 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
6898 if (!rule->arg.hlua_rule) {
6899 memprintf(err, "out of memory error");
6900 return ACT_RET_PRS_ERR;
6901 }
6902
6903 /* Reference the Lua function and store the reference. */
6904 rule->arg.hlua_rule->fcn = *fcn;
6905
6906 /* TODO: later accept arguments. */
6907 rule->arg.hlua_rule->args = NULL;
6908
6909 /* Add applet pointer in the rule. */
6910 rule->applet.obj_type = OBJ_TYPE_APPLET;
6911 rule->applet.name = fcn->name;
6912 rule->applet.init = hlua_applet_http_init;
6913 rule->applet.fct = hlua_applet_http_fct;
6914 rule->applet.release = hlua_applet_http_release;
6915 rule->applet.timeout = hlua_timeout_applet;
6916
6917 return ACT_RET_PRS_OK;
6918}
6919
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006920/* This function is an LUA binding used for registering
6921 * "sample-conv" functions. It expects a converter name used
6922 * in the haproxy configuration file, and an LUA function.
6923 */
6924__LJMP static int hlua_register_action(lua_State *L)
6925{
6926 struct action_kw_list *akl;
6927 const char *name;
6928 int ref;
6929 int len;
6930 struct hlua_function *fcn;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006931 int nargs;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006932
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006933 /* Initialise the number of expected arguments at 0. */
6934 nargs = 0;
6935
6936 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
6937 WILL_LJMP(luaL_error(L, "'register_action' needs between 3 and 4 arguments"));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006938
6939 /* First argument : converter name. */
6940 name = MAY_LJMP(luaL_checkstring(L, 1));
6941
6942 /* Second argument : environment. */
6943 if (lua_type(L, 2) != LUA_TTABLE)
6944 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
6945
6946 /* Third argument : lua function. */
6947 ref = MAY_LJMP(hlua_checkfunction(L, 3));
6948
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006949 /* Fourth argument : number of mandatory arguments expected on the configuration line. */
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006950 if (lua_gettop(L) >= 4)
6951 nargs = MAY_LJMP(luaL_checkinteger(L, 4));
6952
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006953 /* browse the second argument as an array. */
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006954 lua_pushnil(L);
6955 while (lua_next(L, 2) != 0) {
6956 if (lua_type(L, -1) != LUA_TSTRING)
6957 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
6958
6959 /* Check required environment. Only accepted "http" or "tcp". */
6960 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006961 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006962 if (!akl)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006963 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006964 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006965 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006966 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006967
6968 /* Fill fcn. */
6969 fcn->name = strdup(name);
6970 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006971 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006972 fcn->function_ref = ref;
6973
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006974 /* Set the expected number od arguments. */
6975 fcn->nargs = nargs;
6976
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006977 /* List head */
6978 akl->list.n = akl->list.p = NULL;
6979
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006980 /* action keyword. */
6981 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006982 akl->kw[0].kw = calloc(1, len);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006983 if (!akl->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006984 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006985
6986 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
6987
6988 akl->kw[0].match_pfx = 0;
6989 akl->kw[0].private = fcn;
6990 akl->kw[0].parse = action_register_lua;
6991
6992 /* select the action registering point. */
6993 if (strcmp(lua_tostring(L, -1), "tcp-req") == 0)
6994 tcp_req_cont_keywords_register(akl);
6995 else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0)
6996 tcp_res_cont_keywords_register(akl);
6997 else if (strcmp(lua_tostring(L, -1), "http-req") == 0)
6998 http_req_keywords_register(akl);
6999 else if (strcmp(lua_tostring(L, -1), "http-res") == 0)
7000 http_res_keywords_register(akl);
7001 else
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007002 WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. "
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007003 "'tcp-req', 'tcp-res', 'http-req' or 'http-res' "
7004 "are expected.", lua_tostring(L, -1)));
7005
7006 /* pop the environment string. */
7007 lua_pop(L, 1);
7008 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007009 return ACT_RET_PRS_OK;
7010}
7011
7012static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px,
7013 struct act_rule *rule, char **err)
7014{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007015 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007016
Christopher Fauletafd8f102018-11-08 11:34:21 +01007017 if (px->options2 & PR_O2_USE_HTX) {
7018 memprintf(err, "Lua services cannot be used when the HTX internal representation is enabled");
7019 return ACT_RET_PRS_ERR;
7020 }
7021
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007022 /* Memory for the rule. */
7023 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
7024 if (!rule->arg.hlua_rule) {
7025 memprintf(err, "out of memory error");
7026 return ACT_RET_PRS_ERR;
7027 }
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007028
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007029 /* Reference the Lua function and store the reference. */
7030 rule->arg.hlua_rule->fcn = *fcn;
7031
7032 /* TODO: later accept arguments. */
7033 rule->arg.hlua_rule->args = NULL;
7034
7035 /* Add applet pointer in the rule. */
7036 rule->applet.obj_type = OBJ_TYPE_APPLET;
7037 rule->applet.name = fcn->name;
7038 rule->applet.init = hlua_applet_tcp_init;
7039 rule->applet.fct = hlua_applet_tcp_fct;
7040 rule->applet.release = hlua_applet_tcp_release;
7041 rule->applet.timeout = hlua_timeout_applet;
7042
7043 return 0;
7044}
7045
7046/* This function is an LUA binding used for registering
7047 * "sample-conv" functions. It expects a converter name used
7048 * in the haproxy configuration file, and an LUA function.
7049 */
7050__LJMP static int hlua_register_service(lua_State *L)
7051{
7052 struct action_kw_list *akl;
7053 const char *name;
7054 const char *env;
7055 int ref;
7056 int len;
7057 struct hlua_function *fcn;
7058
7059 MAY_LJMP(check_args(L, 3, "register_service"));
7060
7061 /* First argument : converter name. */
7062 name = MAY_LJMP(luaL_checkstring(L, 1));
7063
7064 /* Second argument : environment. */
7065 env = MAY_LJMP(luaL_checkstring(L, 2));
7066
7067 /* Third argument : lua function. */
7068 ref = MAY_LJMP(hlua_checkfunction(L, 3));
7069
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007070 /* Allocate and fill the sample fetch keyword struct. */
7071 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
7072 if (!akl)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007073 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007074 fcn = calloc(1, sizeof(*fcn));
7075 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007076 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007077
7078 /* Fill fcn. */
7079 len = strlen("<lua.>") + strlen(name) + 1;
7080 fcn->name = calloc(1, len);
7081 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007082 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007083 snprintf((char *)fcn->name, len, "<lua.%s>", name);
7084 fcn->function_ref = ref;
7085
7086 /* List head */
7087 akl->list.n = akl->list.p = NULL;
7088
7089 /* converter keyword. */
7090 len = strlen("lua.") + strlen(name) + 1;
7091 akl->kw[0].kw = calloc(1, len);
7092 if (!akl->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007093 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007094
7095 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
7096
Thierry FOURNIER / OZON.IO02564fd2016-11-12 11:07:05 +01007097 /* Check required environment. Only accepted "http" or "tcp". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007098 if (strcmp(env, "tcp") == 0)
7099 akl->kw[0].parse = action_register_service_tcp;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007100 else if (strcmp(env, "http") == 0)
7101 akl->kw[0].parse = action_register_service_http;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007102 else
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007103 WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. "
Eric Salamafe7456f2017-12-21 14:30:07 +01007104 "'tcp' or 'http' are expected.", env));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007105
7106 akl->kw[0].match_pfx = 0;
7107 akl->kw[0].private = fcn;
7108
7109 /* End of array. */
7110 memset(&akl->kw[1], 0, sizeof(*akl->kw));
7111
7112 /* Register this new converter */
7113 service_keywords_register(akl);
7114
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007115 return 0;
7116}
7117
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007118/* This function initialises Lua cli handler. It copies the
7119 * arguments in the Lua stack and create channel IO objects.
7120 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02007121static int hlua_cli_parse_fct(char **args, char *payload, struct appctx *appctx, void *private)
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007122{
7123 struct hlua *hlua;
7124 struct hlua_function *fcn;
7125 int i;
7126 const char *error;
7127
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007128 fcn = private;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007129 appctx->ctx.hlua_cli.fcn = private;
7130
Willy Tarreaubafbe012017-11-24 17:34:44 +01007131 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007132 if (!hlua) {
7133 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007134 return 1;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007135 }
7136 HLUA_INIT(hlua);
7137 appctx->ctx.hlua_cli.hlua = hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007138
7139 /* Create task used by signal to wakeup applets.
7140 * We use the same wakeup fonction than the Lua applet_tcp and
7141 * applet_http. It is absolutely compatible.
7142 */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01007143 appctx->ctx.hlua_cli.task = task_new(tid_bit);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007144 if (!appctx->ctx.hlua_cli.task) {
Thierry FOURNIERffbf5692016-12-16 11:14:06 +01007145 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007146 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007147 }
7148 appctx->ctx.hlua_cli.task->nice = 0;
7149 appctx->ctx.hlua_cli.task->context = appctx;
7150 appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup;
7151
7152 /* Initialises the Lua context */
7153 if (!hlua_ctx_init(hlua, appctx->ctx.hlua_cli.task)) {
7154 SEND_ERR(NULL, "Lua cli '%s': can't initialize Lua context.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007155 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007156 }
7157
7158 /* The following Lua calls can fail. */
7159 if (!SET_SAFE_LJMP(hlua->T)) {
7160 if (lua_type(hlua->T, -1) == LUA_TSTRING)
7161 error = lua_tostring(hlua->T, -1);
7162 else
7163 error = "critical error";
7164 SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error);
7165 goto error;
7166 }
7167
7168 /* Check stack available size. */
7169 if (!lua_checkstack(hlua->T, 2)) {
7170 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7171 goto error;
7172 }
7173
7174 /* Restore the function in the stack. */
7175 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
7176
7177 /* Once the arguments parsed, the CLI is like an AppletTCP,
7178 * so push AppletTCP in the stack.
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007179 */
7180 if (!hlua_applet_tcp_new(hlua->T, appctx)) {
7181 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7182 goto error;
7183 }
7184 hlua->nargs = 1;
7185
7186 /* push keywords in the stack. */
7187 for (i = 0; *args[i]; i++) {
7188 /* Check stack available size. */
7189 if (!lua_checkstack(hlua->T, 1)) {
7190 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7191 goto error;
7192 }
7193 lua_pushstring(hlua->T, args[i]);
7194 hlua->nargs++;
7195 }
7196
7197 /* We must initialize the execution timeouts. */
7198 hlua->max_time = hlua_timeout_session;
7199
7200 /* At this point the execution is safe. */
7201 RESET_SAFE_LJMP(hlua->T);
7202
7203 /* It's ok */
7204 return 0;
7205
7206 /* It's not ok. */
7207error:
7208 RESET_SAFE_LJMP(hlua->T);
7209 hlua_ctx_destroy(hlua);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007210 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007211 return 1;
7212}
7213
7214static int hlua_cli_io_handler_fct(struct appctx *appctx)
7215{
7216 struct hlua *hlua;
7217 struct stream_interface *si;
7218 struct hlua_function *fcn;
7219
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007220 hlua = appctx->ctx.hlua_cli.hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007221 si = appctx->owner;
Willy Tarreau8ae4f752016-12-14 15:41:45 +01007222 fcn = appctx->ctx.hlua_cli.fcn;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007223
7224 /* If the stream is disconnect or closed, ldo nothing. */
7225 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
7226 return 1;
7227
7228 /* Execute the function. */
7229 switch (hlua_ctx_resume(hlua, 1)) {
7230
7231 /* finished. */
7232 case HLUA_E_OK:
7233 return 1;
7234
7235 /* yield. */
7236 case HLUA_E_AGAIN:
7237 /* We want write. */
7238 if (HLUA_IS_WAKERESWR(hlua))
Willy Tarreaudb398432018-11-15 11:08:52 +01007239 si_rx_room_blk(si);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007240 /* Set the timeout. */
7241 if (hlua->wake_time != TICK_ETERNITY)
7242 task_schedule(hlua->task, hlua->wake_time);
7243 return 0;
7244
7245 /* finished with error. */
7246 case HLUA_E_ERRMSG:
7247 /* Display log. */
7248 SEND_ERR(NULL, "Lua cli '%s': %s.\n",
7249 fcn->name, lua_tostring(hlua->T, -1));
7250 lua_pop(hlua->T, 1);
7251 return 1;
7252
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007253 case HLUA_E_ETMOUT:
7254 SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n",
7255 fcn->name);
7256 return 1;
7257
7258 case HLUA_E_NOMEM:
7259 SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n",
7260 fcn->name);
7261 return 1;
7262
7263 case HLUA_E_YIELD: /* unexpected */
7264 SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n",
7265 fcn->name);
7266 return 1;
7267
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007268 case HLUA_E_ERR:
7269 /* Display log. */
7270 SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n",
7271 fcn->name);
7272 return 1;
7273
7274 default:
7275 return 1;
7276 }
7277
7278 return 1;
7279}
7280
7281static void hlua_cli_io_release_fct(struct appctx *appctx)
7282{
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007283 hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007284 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007285}
7286
7287/* This function is an LUA binding used for registering
7288 * new keywords in the cli. It expects a list of keywords
7289 * which are the "path". It is limited to 5 keywords. A
7290 * description of the command, a function to be executed
7291 * for the parsing and a function for io handlers.
7292 */
7293__LJMP static int hlua_register_cli(lua_State *L)
7294{
7295 struct cli_kw_list *cli_kws;
7296 const char *message;
7297 int ref_io;
7298 int len;
7299 struct hlua_function *fcn;
7300 int index;
7301 int i;
7302
7303 MAY_LJMP(check_args(L, 3, "register_cli"));
7304
7305 /* First argument : an array of maximum 5 keywords. */
7306 if (!lua_istable(L, 1))
7307 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table"));
7308
7309 /* Second argument : string with contextual message. */
7310 message = MAY_LJMP(luaL_checkstring(L, 2));
7311
7312 /* Third and fourth argument : lua function. */
7313 ref_io = MAY_LJMP(hlua_checkfunction(L, 3));
7314
7315 /* Allocate and fill the sample fetch keyword struct. */
7316 cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2);
7317 if (!cli_kws)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007318 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007319 fcn = calloc(1, sizeof(*fcn));
7320 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007321 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007322
7323 /* Fill path. */
7324 index = 0;
7325 lua_pushnil(L);
7326 while(lua_next(L, 1) != 0) {
7327 if (index >= 5)
7328 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries"));
7329 if (lua_type(L, -1) != LUA_TSTRING)
7330 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings"));
7331 cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1));
7332 if (!cli_kws->kw[0].str_kw[index])
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007333 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007334 index++;
7335 lua_pop(L, 1);
7336 }
7337
7338 /* Copy help message. */
7339 cli_kws->kw[0].usage = strdup(message);
7340 if (!cli_kws->kw[0].usage)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007341 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007342
7343 /* Fill fcn io handler. */
7344 len = strlen("<lua.cli>") + 1;
7345 for (i = 0; i < index; i++)
7346 len += strlen(cli_kws->kw[0].str_kw[i]) + 1;
7347 fcn->name = calloc(1, len);
7348 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007349 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007350 strncat((char *)fcn->name, "<lua.cli", len);
7351 for (i = 0; i < index; i++) {
7352 strncat((char *)fcn->name, ".", len);
7353 strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len);
7354 }
7355 strncat((char *)fcn->name, ">", len);
7356 fcn->function_ref = ref_io;
7357
7358 /* Fill last entries. */
7359 cli_kws->kw[0].private = fcn;
7360 cli_kws->kw[0].parse = hlua_cli_parse_fct;
7361 cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct;
7362 cli_kws->kw[0].io_release = hlua_cli_io_release_fct;
7363
7364 /* Register this new converter */
7365 cli_register_kw(cli_kws);
7366
7367 return 0;
7368}
7369
Thierry FOURNIERbd413492015-03-03 16:52:26 +01007370static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx,
7371 struct proxy *defpx, const char *file, int line,
7372 char **err, unsigned int *timeout)
7373{
7374 const char *error;
7375
7376 error = parse_time_err(args[1], timeout, TIME_UNIT_MS);
7377 if (error && *error != '\0') {
7378 memprintf(err, "%s: invalid timeout", args[0]);
7379 return -1;
7380 }
7381 return 0;
7382}
7383
7384static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx,
7385 struct proxy *defpx, const char *file, int line,
7386 char **err)
7387{
7388 return hlua_read_timeout(args, section_type, curpx, defpx,
7389 file, line, err, &hlua_timeout_session);
7390}
7391
7392static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx,
7393 struct proxy *defpx, const char *file, int line,
7394 char **err)
7395{
7396 return hlua_read_timeout(args, section_type, curpx, defpx,
7397 file, line, err, &hlua_timeout_task);
7398}
7399
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007400static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx,
7401 struct proxy *defpx, const char *file, int line,
7402 char **err)
7403{
7404 return hlua_read_timeout(args, section_type, curpx, defpx,
7405 file, line, err, &hlua_timeout_applet);
7406}
7407
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01007408static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx,
7409 struct proxy *defpx, const char *file, int line,
7410 char **err)
7411{
7412 char *error;
7413
7414 hlua_nb_instruction = strtoll(args[1], &error, 10);
7415 if (*error != '\0') {
7416 memprintf(err, "%s: invalid number", args[0]);
7417 return -1;
7418 }
7419 return 0;
7420}
7421
Willy Tarreau32f61e22015-03-18 17:54:59 +01007422static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx,
7423 struct proxy *defpx, const char *file, int line,
7424 char **err)
7425{
7426 char *error;
7427
7428 if (*(args[1]) == 0) {
7429 memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]);
7430 return -1;
7431 }
7432 hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L;
7433 if (*error != '\0') {
7434 memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error);
7435 return -1;
7436 }
7437 return 0;
7438}
7439
7440
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007441/* This function is called by the main configuration key "lua-load". It loads and
7442 * execute an lua file during the parsing of the HAProxy configuration file. It is
7443 * the main lua entry point.
7444 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007445 * This function runs with the HAProxy keywords API. It returns -1 if an error
7446 * occurs, otherwise it returns 0.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007447 *
7448 * In some error case, LUA set an error message in top of the stack. This function
7449 * returns this error message in the HAProxy logs and pop it from the stack.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007450 *
7451 * This function can fail with an abort() due to an Lua critical error.
7452 * We are in the configuration parsing process of HAProxy, this abort() is
7453 * tolerated.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007454 */
7455static int hlua_load(char **args, int section_type, struct proxy *curpx,
7456 struct proxy *defpx, const char *file, int line,
7457 char **err)
7458{
7459 int error;
7460
7461 /* Just load and compile the file. */
7462 error = luaL_loadfile(gL.T, args[1]);
7463 if (error) {
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007464 memprintf(err, "error in Lua file '%s': %s", args[1], lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007465 lua_pop(gL.T, 1);
7466 return -1;
7467 }
7468
7469 /* If no syntax error where detected, execute the code. */
7470 error = lua_pcall(gL.T, 0, LUA_MULTRET, 0);
7471 switch (error) {
7472 case LUA_OK:
7473 break;
7474 case LUA_ERRRUN:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007475 memprintf(err, "Lua runtime error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007476 lua_pop(gL.T, 1);
7477 return -1;
7478 case LUA_ERRMEM:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007479 memprintf(err, "Lua out of memory error.n");
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007480 return -1;
7481 case LUA_ERRERR:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007482 memprintf(err, "Lua message handler error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007483 lua_pop(gL.T, 1);
7484 return -1;
7485 case LUA_ERRGCMM:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007486 memprintf(err, "Lua garbage collector error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007487 lua_pop(gL.T, 1);
7488 return -1;
7489 default:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007490 memprintf(err, "Lua unknonwn error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007491 lua_pop(gL.T, 1);
7492 return -1;
7493 }
7494
7495 return 0;
7496}
7497
7498/* configuration keywords declaration */
7499static struct cfg_kw_list cfg_kws = {{ },{
Thierry FOURNIERbd413492015-03-03 16:52:26 +01007500 { CFG_GLOBAL, "lua-load", hlua_load },
7501 { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout },
7502 { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout },
Thierry FOURNIER56da1012015-10-01 08:42:31 +02007503 { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout },
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01007504 { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield },
Willy Tarreau32f61e22015-03-18 17:54:59 +01007505 { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem },
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007506 { 0, NULL, NULL },
7507}};
7508
Willy Tarreau0108d902018-11-25 19:14:37 +01007509INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
7510
Christopher Fauletafd8f102018-11-08 11:34:21 +01007511static int hlua_check_config()
7512{
7513 struct proxy *px;
7514 struct acl *acl;
7515 struct acl_expr *expr;
7516 struct sample_fetch *fetch;
7517 int err = 0;
7518
7519 for (px = proxies_list; px; px = px->next) {
7520 if (!(px->options2 & PR_O2_USE_HTX))
7521 continue;
7522
7523 list_for_each_entry(acl, &px->acl, list) {
7524 list_for_each_entry(expr, &acl->expr, list) {
7525 fetch = expr->smp->fetch;
7526 if (fetch->process != hlua_sample_fetch_wrapper)
7527 continue;
7528
7529 ha_alert("config: %s '%s': sample-fetch '%s' cannot be used used "
7530 "when the HTX internal representation is enabled.\n",
7531 proxy_type_str(px), px->id, fetch->kw);
7532 err++;
7533 }
7534 }
7535 }
7536 return err;
7537}
7538
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007539/* This function can fail with an abort() due to an Lua critical error.
7540 * We are in the initialisation process of HAProxy, this abort() is
7541 * tolerated.
7542 */
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007543int hlua_post_init()
7544{
7545 struct hlua_init_function *init;
7546 const char *msg;
7547 enum hlua_exec ret;
Thierry Fournierfd107a22016-02-19 19:57:23 +01007548 const char *error;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007549
Thierry Fournier3d4a6752016-02-19 20:53:30 +01007550 /* Call post initialisation function in safe environement. */
7551 if (!SET_SAFE_LJMP(gL.T)) {
7552 if (lua_type(gL.T, -1) == LUA_TSTRING)
7553 error = lua_tostring(gL.T, -1);
7554 else
7555 error = "critical error";
7556 fprintf(stderr, "Lua post-init: %s.\n", error);
7557 exit(1);
7558 }
Frédéric Lécaille54f2bcf2018-08-29 13:46:24 +02007559
7560#if USE_OPENSSL
7561 /* Initialize SSL server. */
7562 if (socket_ssl.xprt->prepare_srv) {
7563 int saved_used_backed = global.ssl_used_backend;
7564 // don't affect maxconn automatic computation
7565 socket_ssl.xprt->prepare_srv(&socket_ssl);
7566 global.ssl_used_backend = saved_used_backed;
7567 }
7568#endif
7569
Thierry Fournier3d4a6752016-02-19 20:53:30 +01007570 hlua_fcn_post_init(gL.T);
7571 RESET_SAFE_LJMP(gL.T);
7572
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007573 list_for_each_entry(init, &hlua_init_functions, l) {
7574 lua_rawgeti(gL.T, LUA_REGISTRYINDEX, init->function_ref);
7575 ret = hlua_ctx_resume(&gL, 0);
7576 switch (ret) {
7577 case HLUA_E_OK:
7578 lua_pop(gL.T, -1);
7579 return 1;
7580 case HLUA_E_AGAIN:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007581 ha_alert("Lua init: yield not allowed.\n");
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007582 return 0;
7583 case HLUA_E_ERRMSG:
7584 msg = lua_tostring(gL.T, -1);
Christopher Faulet767a84b2017-11-24 16:50:31 +01007585 ha_alert("lua init: %s.\n", msg);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007586 return 0;
7587 case HLUA_E_ERR:
7588 default:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007589 ha_alert("Lua init: unknown runtime error.\n");
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007590 return 0;
7591 }
7592 }
7593 return 1;
7594}
7595
Willy Tarreau32f61e22015-03-18 17:54:59 +01007596/* The memory allocator used by the Lua stack. <ud> is a pointer to the
7597 * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize>
7598 * is the previously allocated size or the kind of object in case of a new
7599 * allocation. <nsize> is the requested new size.
7600 */
7601static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
7602{
7603 struct hlua_mem_allocator *zone = ud;
7604
7605 if (nsize == 0) {
7606 /* it's a free */
7607 if (ptr)
7608 zone->allocated -= osize;
7609 free(ptr);
7610 return NULL;
7611 }
7612
7613 if (!ptr) {
7614 /* it's a new allocation */
7615 if (zone->limit && zone->allocated + nsize > zone->limit)
7616 return NULL;
7617
7618 ptr = malloc(nsize);
7619 if (ptr)
7620 zone->allocated += nsize;
7621 return ptr;
7622 }
7623
7624 /* it's a realloc */
7625 if (zone->limit && zone->allocated + nsize - osize > zone->limit)
7626 return NULL;
7627
7628 ptr = realloc(ptr, nsize);
7629 if (ptr)
7630 zone->allocated += nsize - osize;
7631 return ptr;
7632}
7633
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007634/* Ithis function can fail with an abort() due to an Lua critical error.
7635 * We are in the initialisation process of HAProxy, this abort() is
7636 * tolerated.
7637 */
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01007638void hlua_init(void)
7639{
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007640 int i;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01007641 int idx;
7642 struct sample_fetch *sf;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007643 struct sample_conv *sc;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01007644 char *p;
Thierry Fournierfd107a22016-02-19 19:57:23 +01007645 const char *error_msg;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01007646#ifdef USE_OPENSSL
Willy Tarreau80f5fae2015-02-27 16:38:20 +01007647 struct srv_kw *kw;
7648 int tmp_error;
7649 char *error;
Thierry FOURNIER36d13742015-03-17 16:48:53 +01007650 char *args[] = { /* SSL client configuration. */
7651 "ssl",
7652 "verify",
7653 "none",
Thierry FOURNIER36d13742015-03-17 16:48:53 +01007654 NULL
7655 };
Willy Tarreau80f5fae2015-02-27 16:38:20 +01007656#endif
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007657
Thierry FOURNIER380d0932015-01-23 14:27:52 +01007658 /* Init main lua stack. */
7659 gL.Mref = LUA_REFNIL;
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01007660 gL.flags = 0;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01007661 LIST_INIT(&gL.com);
Willy Tarreau42ef75f2017-04-12 21:40:29 +02007662 gL.T = lua_newstate(hlua_alloc, &hlua_global_allocator);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01007663 hlua_sethlua(&gL);
7664 gL.Tref = LUA_REFNIL;
7665 gL.task = NULL;
7666
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007667 /* From this point, until the end of the initialisation function,
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007668 * the Lua function can fail with an abort. We are in the initialisation
7669 * process of HAProxy, this abort() is tolerated.
7670 */
7671
Thierry FOURNIER380d0932015-01-23 14:27:52 +01007672 /* Initialise lua. */
7673 luaL_openlibs(gL.T);
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007674
Thierry Fournier75933d42016-01-21 09:30:18 +01007675 /* Set safe environment for the initialisation. */
7676 if (!SET_SAFE_LJMP(gL.T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01007677 if (lua_type(gL.T, -1) == LUA_TSTRING)
7678 error_msg = lua_tostring(gL.T, -1);
7679 else
7680 error_msg = "critical error";
7681 fprintf(stderr, "Lua init: %s.\n", error_msg);
Thierry Fournier75933d42016-01-21 09:30:18 +01007682 exit(1);
7683 }
7684
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007685 /*
7686 *
7687 * Create "core" object.
7688 *
7689 */
7690
Thierry FOURNIERa2d8c652015-03-11 17:29:39 +01007691 /* This table entry is the object "core" base. */
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007692 lua_newtable(gL.T);
7693
7694 /* Push the loglevel constants. */
Willy Tarreau80f5fae2015-02-27 16:38:20 +01007695 for (i = 0; i < NB_LOG_LEVELS; i++)
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007696 hlua_class_const_int(gL.T, log_levels[i], i);
7697
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007698 /* Register special functions. */
7699 hlua_class_function(gL.T, "register_init", hlua_register_init);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01007700 hlua_class_function(gL.T, "register_task", hlua_register_task);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01007701 hlua_class_function(gL.T, "register_fetches", hlua_register_fetches);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01007702 hlua_class_function(gL.T, "register_converters", hlua_register_converters);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007703 hlua_class_function(gL.T, "register_action", hlua_register_action);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007704 hlua_class_function(gL.T, "register_service", hlua_register_service);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007705 hlua_class_function(gL.T, "register_cli", hlua_register_cli);
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01007706 hlua_class_function(gL.T, "yield", hlua_yield);
Willy Tarreau59551662015-03-10 14:23:13 +01007707 hlua_class_function(gL.T, "set_nice", hlua_set_nice);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01007708 hlua_class_function(gL.T, "sleep", hlua_sleep);
7709 hlua_class_function(gL.T, "msleep", hlua_msleep);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01007710 hlua_class_function(gL.T, "add_acl", hlua_add_acl);
7711 hlua_class_function(gL.T, "del_acl", hlua_del_acl);
7712 hlua_class_function(gL.T, "set_map", hlua_set_map);
7713 hlua_class_function(gL.T, "del_map", hlua_del_map);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007714 hlua_class_function(gL.T, "tcp", hlua_socket_new);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01007715 hlua_class_function(gL.T, "log", hlua_log);
7716 hlua_class_function(gL.T, "Debug", hlua_log_debug);
7717 hlua_class_function(gL.T, "Info", hlua_log_info);
7718 hlua_class_function(gL.T, "Warning", hlua_log_warning);
7719 hlua_class_function(gL.T, "Alert", hlua_log_alert);
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02007720 hlua_class_function(gL.T, "done", hlua_done);
Thierry Fournierfb0b5462016-01-21 09:28:58 +01007721 hlua_fcn_reg_core_fcn(gL.T);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007722
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007723 lua_setglobal(gL.T, "core");
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01007724
7725 /*
7726 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02007727 * Register class Map
7728 *
7729 */
7730
7731 /* This table entry is the object "Map" base. */
7732 lua_newtable(gL.T);
7733
7734 /* register pattern types. */
7735 for (i=0; i<PAT_MATCH_NUM; i++)
7736 hlua_class_const_int(gL.T, pat_match_names[i], i);
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01007737 for (i=0; i<PAT_MATCH_NUM; i++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007738 snprintf(trash.area, trash.size, "_%s", pat_match_names[i]);
7739 hlua_class_const_int(gL.T, trash.area, i);
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01007740 }
Thierry FOURNIER3def3932015-04-07 11:27:54 +02007741
7742 /* register constructor. */
7743 hlua_class_function(gL.T, "new", hlua_map_new);
7744
7745 /* Create and fill the metatable. */
7746 lua_newtable(gL.T);
7747
7748 /* Create and fille the __index entry. */
7749 lua_pushstring(gL.T, "__index");
7750 lua_newtable(gL.T);
7751
7752 /* Register . */
7753 hlua_class_function(gL.T, "lookup", hlua_map_lookup);
7754 hlua_class_function(gL.T, "slookup", hlua_map_slookup);
7755
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007756 lua_rawset(gL.T, -3);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02007757
Thierry Fournier45e78d72016-02-19 18:34:46 +01007758 /* Register previous table in the registry with reference and named entry.
7759 * The function hlua_register_metatable() pops the stack, so we
7760 * previously create a copy of the table.
7761 */
Thierry FOURNIER3def3932015-04-07 11:27:54 +02007762 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007763 class_map_ref = hlua_register_metatable(gL.T, CLASS_MAP);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02007764
7765 /* Assign the metatable to the mai Map object. */
7766 lua_setmetatable(gL.T, -2);
7767
7768 /* Set a name to the table. */
7769 lua_setglobal(gL.T, "Map");
7770
7771 /*
7772 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01007773 * Register class Channel
7774 *
7775 */
7776
7777 /* Create and fill the metatable. */
7778 lua_newtable(gL.T);
7779
7780 /* Create and fille the __index entry. */
7781 lua_pushstring(gL.T, "__index");
7782 lua_newtable(gL.T);
7783
7784 /* Register . */
7785 hlua_class_function(gL.T, "get", hlua_channel_get);
7786 hlua_class_function(gL.T, "dup", hlua_channel_dup);
7787 hlua_class_function(gL.T, "getline", hlua_channel_getline);
7788 hlua_class_function(gL.T, "set", hlua_channel_set);
7789 hlua_class_function(gL.T, "append", hlua_channel_append);
7790 hlua_class_function(gL.T, "send", hlua_channel_send);
7791 hlua_class_function(gL.T, "forward", hlua_channel_forward);
7792 hlua_class_function(gL.T, "get_in_len", hlua_channel_get_in_len);
7793 hlua_class_function(gL.T, "get_out_len", hlua_channel_get_out_len);
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01007794 hlua_class_function(gL.T, "is_full", hlua_channel_is_full);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01007795
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007796 lua_rawset(gL.T, -3);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01007797
7798 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007799 class_channel_ref = hlua_register_metatable(gL.T, CLASS_CHANNEL);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01007800
7801 /*
7802 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01007803 * Register class Fetches
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01007804 *
7805 */
7806
7807 /* Create and fill the metatable. */
7808 lua_newtable(gL.T);
7809
7810 /* Create and fille the __index entry. */
7811 lua_pushstring(gL.T, "__index");
7812 lua_newtable(gL.T);
7813
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01007814 /* Browse existing fetches and create the associated
7815 * object method.
7816 */
7817 sf = NULL;
7818 while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) {
7819
7820 /* Dont register the keywork if the arguments check function are
7821 * not safe during the runtime.
7822 */
7823 if ((sf->val_args != NULL) &&
7824 (sf->val_args != val_payload_lv) &&
7825 (sf->val_args != val_hdr))
7826 continue;
7827
7828 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
7829 * by an underscore.
7830 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007831 strncpy(trash.area, sf->kw, trash.size);
7832 trash.area[trash.size - 1] = '\0';
7833 for (p = trash.area; *p; p++)
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01007834 if (*p == '.' || *p == '-' || *p == '+')
7835 *p = '_';
7836
7837 /* Register the function. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007838 lua_pushstring(gL.T, trash.area);
Willy Tarreau2ec22742015-03-10 14:27:20 +01007839 lua_pushlightuserdata(gL.T, sf);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01007840 lua_pushcclosure(gL.T, hlua_run_sample_fetch, 1);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007841 lua_rawset(gL.T, -3);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01007842 }
7843
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007844 lua_rawset(gL.T, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01007845
7846 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007847 class_fetches_ref = hlua_register_metatable(gL.T, CLASS_FETCHES);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01007848
7849 /*
7850 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007851 * Register class Converters
7852 *
7853 */
7854
7855 /* Create and fill the metatable. */
7856 lua_newtable(gL.T);
7857
7858 /* Create and fill the __index entry. */
7859 lua_pushstring(gL.T, "__index");
7860 lua_newtable(gL.T);
7861
7862 /* Browse existing converters and create the associated
7863 * object method.
7864 */
7865 sc = NULL;
7866 while ((sc = sample_conv_getnext(sc, &idx)) != NULL) {
7867 /* Dont register the keywork if the arguments check function are
7868 * not safe during the runtime.
7869 */
7870 if (sc->val_args != NULL)
7871 continue;
7872
7873 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
7874 * by an underscore.
7875 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007876 strncpy(trash.area, sc->kw, trash.size);
7877 trash.area[trash.size - 1] = '\0';
7878 for (p = trash.area; *p; p++)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007879 if (*p == '.' || *p == '-' || *p == '+')
7880 *p = '_';
7881
7882 /* Register the function. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007883 lua_pushstring(gL.T, trash.area);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007884 lua_pushlightuserdata(gL.T, sc);
7885 lua_pushcclosure(gL.T, hlua_run_sample_conv, 1);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007886 lua_rawset(gL.T, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007887 }
7888
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007889 lua_rawset(gL.T, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007890
7891 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007892 class_converters_ref = hlua_register_metatable(gL.T, CLASS_CONVERTERS);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007893
7894 /*
7895 *
Thierry FOURNIER08504f42015-03-16 14:17:08 +01007896 * Register class HTTP
7897 *
7898 */
7899
7900 /* Create and fill the metatable. */
7901 lua_newtable(gL.T);
7902
7903 /* Create and fille the __index entry. */
7904 lua_pushstring(gL.T, "__index");
7905 lua_newtable(gL.T);
7906
7907 /* Register Lua functions. */
7908 hlua_class_function(gL.T, "req_get_headers",hlua_http_req_get_headers);
7909 hlua_class_function(gL.T, "req_del_header", hlua_http_req_del_hdr);
7910 hlua_class_function(gL.T, "req_rep_header", hlua_http_req_rep_hdr);
7911 hlua_class_function(gL.T, "req_rep_value", hlua_http_req_rep_val);
7912 hlua_class_function(gL.T, "req_add_header", hlua_http_req_add_hdr);
7913 hlua_class_function(gL.T, "req_set_header", hlua_http_req_set_hdr);
7914 hlua_class_function(gL.T, "req_set_method", hlua_http_req_set_meth);
7915 hlua_class_function(gL.T, "req_set_path", hlua_http_req_set_path);
7916 hlua_class_function(gL.T, "req_set_query", hlua_http_req_set_query);
7917 hlua_class_function(gL.T, "req_set_uri", hlua_http_req_set_uri);
7918
7919 hlua_class_function(gL.T, "res_get_headers",hlua_http_res_get_headers);
7920 hlua_class_function(gL.T, "res_del_header", hlua_http_res_del_hdr);
7921 hlua_class_function(gL.T, "res_rep_header", hlua_http_res_rep_hdr);
7922 hlua_class_function(gL.T, "res_rep_value", hlua_http_res_rep_val);
7923 hlua_class_function(gL.T, "res_add_header", hlua_http_res_add_hdr);
7924 hlua_class_function(gL.T, "res_set_header", hlua_http_res_set_hdr);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02007925 hlua_class_function(gL.T, "res_set_status", hlua_http_res_set_status);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01007926
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007927 lua_rawset(gL.T, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01007928
7929 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007930 class_http_ref = hlua_register_metatable(gL.T, CLASS_HTTP);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01007931
7932 /*
7933 *
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007934 * Register class AppletTCP
7935 *
7936 */
7937
7938 /* Create and fill the metatable. */
7939 lua_newtable(gL.T);
7940
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007941 /* Create and fille the __index entry. */
7942 lua_pushstring(gL.T, "__index");
7943 lua_newtable(gL.T);
7944
7945 /* Register Lua functions. */
Thierry FOURNIER / OZON.IO3e1d7912016-12-12 12:29:34 +01007946 hlua_class_function(gL.T, "getline", hlua_applet_tcp_getline);
7947 hlua_class_function(gL.T, "receive", hlua_applet_tcp_recv);
7948 hlua_class_function(gL.T, "send", hlua_applet_tcp_send);
7949 hlua_class_function(gL.T, "set_priv", hlua_applet_tcp_set_priv);
7950 hlua_class_function(gL.T, "get_priv", hlua_applet_tcp_get_priv);
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01007951 hlua_class_function(gL.T, "set_var", hlua_applet_tcp_set_var);
7952 hlua_class_function(gL.T, "unset_var", hlua_applet_tcp_unset_var);
7953 hlua_class_function(gL.T, "get_var", hlua_applet_tcp_get_var);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007954
7955 lua_settable(gL.T, -3);
7956
7957 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007958 class_applet_tcp_ref = hlua_register_metatable(gL.T, CLASS_APPLET_TCP);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007959
7960 /*
7961 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007962 * Register class AppletHTTP
7963 *
7964 */
7965
7966 /* Create and fill the metatable. */
7967 lua_newtable(gL.T);
7968
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007969 /* Create and fille the __index entry. */
7970 lua_pushstring(gL.T, "__index");
7971 lua_newtable(gL.T);
7972
7973 /* Register Lua functions. */
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01007974 hlua_class_function(gL.T, "set_priv", hlua_applet_http_set_priv);
7975 hlua_class_function(gL.T, "get_priv", hlua_applet_http_get_priv);
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01007976 hlua_class_function(gL.T, "set_var", hlua_applet_http_set_var);
7977 hlua_class_function(gL.T, "unset_var", hlua_applet_http_unset_var);
7978 hlua_class_function(gL.T, "get_var", hlua_applet_http_get_var);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007979 hlua_class_function(gL.T, "getline", hlua_applet_http_getline);
7980 hlua_class_function(gL.T, "receive", hlua_applet_http_recv);
7981 hlua_class_function(gL.T, "send", hlua_applet_http_send);
7982 hlua_class_function(gL.T, "add_header", hlua_applet_http_addheader);
7983 hlua_class_function(gL.T, "set_status", hlua_applet_http_status);
7984 hlua_class_function(gL.T, "start_response", hlua_applet_http_start_response);
7985
7986 lua_settable(gL.T, -3);
7987
7988 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007989 class_applet_http_ref = hlua_register_metatable(gL.T, CLASS_APPLET_HTTP);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007990
7991 /*
7992 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01007993 * Register class TXN
7994 *
7995 */
7996
7997 /* Create and fill the metatable. */
7998 lua_newtable(gL.T);
7999
8000 /* Create and fille the __index entry. */
8001 lua_pushstring(gL.T, "__index");
8002 lua_newtable(gL.T);
8003
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01008004 /* Register Lua functions. */
Patrick Hemmer268a7072018-05-11 12:52:31 -04008005 hlua_class_function(gL.T, "set_priv", hlua_set_priv);
8006 hlua_class_function(gL.T, "get_priv", hlua_get_priv);
8007 hlua_class_function(gL.T, "set_var", hlua_set_var);
8008 hlua_class_function(gL.T, "unset_var", hlua_unset_var);
8009 hlua_class_function(gL.T, "get_var", hlua_get_var);
8010 hlua_class_function(gL.T, "done", hlua_txn_done);
8011 hlua_class_function(gL.T, "set_loglevel", hlua_txn_set_loglevel);
8012 hlua_class_function(gL.T, "set_tos", hlua_txn_set_tos);
8013 hlua_class_function(gL.T, "set_mark", hlua_txn_set_mark);
8014 hlua_class_function(gL.T, "set_priority_class", hlua_txn_set_priority_class);
8015 hlua_class_function(gL.T, "set_priority_offset", hlua_txn_set_priority_offset);
8016 hlua_class_function(gL.T, "deflog", hlua_txn_deflog);
8017 hlua_class_function(gL.T, "log", hlua_txn_log);
8018 hlua_class_function(gL.T, "Debug", hlua_txn_log_debug);
8019 hlua_class_function(gL.T, "Info", hlua_txn_log_info);
8020 hlua_class_function(gL.T, "Warning", hlua_txn_log_warning);
8021 hlua_class_function(gL.T, "Alert", hlua_txn_log_alert);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01008022
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008023 lua_rawset(gL.T, -3);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008024
8025 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008026 class_txn_ref = hlua_register_metatable(gL.T, CLASS_TXN);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008027
8028 /*
8029 *
8030 * Register class Socket
8031 *
8032 */
8033
8034 /* Create and fill the metatable. */
8035 lua_newtable(gL.T);
8036
8037 /* Create and fille the __index entry. */
8038 lua_pushstring(gL.T, "__index");
8039 lua_newtable(gL.T);
8040
Baptiste Assmann84bb4932015-03-02 21:40:06 +01008041#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008042 hlua_class_function(gL.T, "connect_ssl", hlua_socket_connect_ssl);
Baptiste Assmann84bb4932015-03-02 21:40:06 +01008043#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008044 hlua_class_function(gL.T, "connect", hlua_socket_connect);
8045 hlua_class_function(gL.T, "send", hlua_socket_send);
8046 hlua_class_function(gL.T, "receive", hlua_socket_receive);
8047 hlua_class_function(gL.T, "close", hlua_socket_close);
8048 hlua_class_function(gL.T, "getpeername", hlua_socket_getpeername);
8049 hlua_class_function(gL.T, "getsockname", hlua_socket_getsockname);
8050 hlua_class_function(gL.T, "setoption", hlua_socket_setoption);
8051 hlua_class_function(gL.T, "settimeout", hlua_socket_settimeout);
8052
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008053 lua_rawset(gL.T, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008054
8055 /* Register the garbage collector entry. */
8056 lua_pushstring(gL.T, "__gc");
8057 lua_pushcclosure(gL.T, hlua_socket_gc, 0);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008058 lua_rawset(gL.T, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008059
8060 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008061 class_socket_ref = hlua_register_metatable(gL.T, CLASS_SOCKET);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008062
8063 /* Proxy and server configuration initialisation. */
8064 memset(&socket_proxy, 0, sizeof(socket_proxy));
8065 init_new_proxy(&socket_proxy);
8066 socket_proxy.parent = NULL;
8067 socket_proxy.last_change = now.tv_sec;
8068 socket_proxy.id = "LUA-SOCKET";
8069 socket_proxy.cap = PR_CAP_FE | PR_CAP_BE;
8070 socket_proxy.maxconn = 0;
8071 socket_proxy.accept = NULL;
8072 socket_proxy.options2 |= PR_O2_INDEPSTR;
8073 socket_proxy.srv = NULL;
8074 socket_proxy.conn_retries = 0;
8075 socket_proxy.timeout.connect = 5000; /* By default the timeout connection is 5s. */
8076
8077 /* Init TCP server: unchanged parameters */
8078 memset(&socket_tcp, 0, sizeof(socket_tcp));
8079 socket_tcp.next = NULL;
8080 socket_tcp.proxy = &socket_proxy;
8081 socket_tcp.obj_type = OBJ_TYPE_SERVER;
8082 LIST_INIT(&socket_tcp.actconns);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04008083 socket_tcp.pendconns = EB_ROOT;
Christopher Faulet40a007c2017-07-03 15:41:01 +02008084 socket_tcp.priv_conns = NULL;
8085 socket_tcp.idle_conns = NULL;
8086 socket_tcp.safe_conns = NULL;
Emeric Brun52a91d32017-08-31 14:41:55 +02008087 socket_tcp.next_state = SRV_ST_RUNNING; /* early server setup */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008088 socket_tcp.last_change = 0;
8089 socket_tcp.id = "LUA-TCP-CONN";
8090 socket_tcp.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8091 socket_tcp.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8092 socket_tcp.pp_opts = 0; /* Remove proxy protocol. */
8093
8094 /* XXX: Copy default parameter from default server,
8095 * but the default server is not initialized.
8096 */
8097 socket_tcp.maxqueue = socket_proxy.defsrv.maxqueue;
8098 socket_tcp.minconn = socket_proxy.defsrv.minconn;
8099 socket_tcp.maxconn = socket_proxy.defsrv.maxconn;
8100 socket_tcp.slowstart = socket_proxy.defsrv.slowstart;
8101 socket_tcp.onerror = socket_proxy.defsrv.onerror;
8102 socket_tcp.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
8103 socket_tcp.onmarkedup = socket_proxy.defsrv.onmarkedup;
8104 socket_tcp.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
8105 socket_tcp.uweight = socket_proxy.defsrv.iweight;
8106 socket_tcp.iweight = socket_proxy.defsrv.iweight;
8107
8108 socket_tcp.check.status = HCHK_STATUS_INI;
8109 socket_tcp.check.rise = socket_proxy.defsrv.check.rise;
8110 socket_tcp.check.fall = socket_proxy.defsrv.check.fall;
8111 socket_tcp.check.health = socket_tcp.check.rise; /* socket, but will fall down at first failure */
8112 socket_tcp.check.server = &socket_tcp;
8113
8114 socket_tcp.agent.status = HCHK_STATUS_INI;
8115 socket_tcp.agent.rise = socket_proxy.defsrv.agent.rise;
8116 socket_tcp.agent.fall = socket_proxy.defsrv.agent.fall;
8117 socket_tcp.agent.health = socket_tcp.agent.rise; /* socket, but will fall down at first failure */
8118 socket_tcp.agent.server = &socket_tcp;
8119
Willy Tarreaua261e9b2016-12-22 20:44:00 +01008120 socket_tcp.xprt = xprt_get(XPRT_RAW);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008121
8122#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008123 /* Init TCP server: unchanged parameters */
8124 memset(&socket_ssl, 0, sizeof(socket_ssl));
8125 socket_ssl.next = NULL;
8126 socket_ssl.proxy = &socket_proxy;
8127 socket_ssl.obj_type = OBJ_TYPE_SERVER;
8128 LIST_INIT(&socket_ssl.actconns);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04008129 socket_ssl.pendconns = EB_ROOT;
Christopher Faulet40a007c2017-07-03 15:41:01 +02008130 socket_tcp.priv_conns = NULL;
8131 socket_tcp.idle_conns = NULL;
8132 socket_tcp.safe_conns = NULL;
Emeric Brun52a91d32017-08-31 14:41:55 +02008133 socket_ssl.next_state = SRV_ST_RUNNING; /* early server setup */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008134 socket_ssl.last_change = 0;
8135 socket_ssl.id = "LUA-SSL-CONN";
8136 socket_ssl.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8137 socket_ssl.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8138 socket_ssl.pp_opts = 0; /* Remove proxy protocol. */
8139
8140 /* XXX: Copy default parameter from default server,
8141 * but the default server is not initialized.
8142 */
8143 socket_ssl.maxqueue = socket_proxy.defsrv.maxqueue;
8144 socket_ssl.minconn = socket_proxy.defsrv.minconn;
8145 socket_ssl.maxconn = socket_proxy.defsrv.maxconn;
8146 socket_ssl.slowstart = socket_proxy.defsrv.slowstart;
8147 socket_ssl.onerror = socket_proxy.defsrv.onerror;
8148 socket_ssl.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
8149 socket_ssl.onmarkedup = socket_proxy.defsrv.onmarkedup;
8150 socket_ssl.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
8151 socket_ssl.uweight = socket_proxy.defsrv.iweight;
8152 socket_ssl.iweight = socket_proxy.defsrv.iweight;
8153
8154 socket_ssl.check.status = HCHK_STATUS_INI;
8155 socket_ssl.check.rise = socket_proxy.defsrv.check.rise;
8156 socket_ssl.check.fall = socket_proxy.defsrv.check.fall;
8157 socket_ssl.check.health = socket_ssl.check.rise; /* socket, but will fall down at first failure */
8158 socket_ssl.check.server = &socket_ssl;
8159
8160 socket_ssl.agent.status = HCHK_STATUS_INI;
8161 socket_ssl.agent.rise = socket_proxy.defsrv.agent.rise;
8162 socket_ssl.agent.fall = socket_proxy.defsrv.agent.fall;
8163 socket_ssl.agent.health = socket_ssl.agent.rise; /* socket, but will fall down at first failure */
8164 socket_ssl.agent.server = &socket_ssl;
8165
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008166 socket_ssl.use_ssl = 1;
Willy Tarreaua261e9b2016-12-22 20:44:00 +01008167 socket_ssl.xprt = xprt_get(XPRT_SSL);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008168
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008169 for (idx = 0; args[idx] != NULL; idx++) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008170 if ((kw = srv_find_kw(args[idx])) != NULL) { /* Maybe it's registered server keyword */
8171 /*
8172 *
8173 * If the keyword is not known, we can search in the registered
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08008174 * server keywords. This is useful to configure special SSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008175 * features like client certificates and ssl_verify.
8176 *
8177 */
8178 tmp_error = kw->parse(args, &idx, &socket_proxy, &socket_ssl, &error);
8179 if (tmp_error != 0) {
8180 fprintf(stderr, "INTERNAL ERROR: %s\n", error);
8181 abort(); /* This must be never arrives because the command line
8182 not editable by the user. */
8183 }
8184 idx += kw->skip;
8185 }
8186 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008187#endif
Thierry Fournier75933d42016-01-21 09:30:18 +01008188
8189 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01008190}
Willy Tarreaubb57d942016-12-21 19:04:56 +01008191
Willy Tarreau80713382018-11-26 10:19:54 +01008192static void hlua_register_build_options(void)
8193{
Willy Tarreaubb57d942016-12-21 19:04:56 +01008194 char *ptr = NULL;
Willy Tarreau80713382018-11-26 10:19:54 +01008195
Willy Tarreaubb57d942016-12-21 19:04:56 +01008196 memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE);
8197 hap_register_build_opts(ptr, 1);
8198}
Willy Tarreau80713382018-11-26 10:19:54 +01008199
8200INITCALL0(STG_REGISTER, hlua_register_build_options);
Willy Tarreaue6552512018-11-26 11:33:13 +01008201REGISTER_CONFIG_POSTPARSER("hlua", hlua_check_config);