blob: 1be6271f024da35effe66bcaf1cc96c4e054f1a7 [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>
Christopher Faulet724a12c2018-12-13 22:12:15 +010032#include <common/h1.h>
Thierry FOURNIER380d0932015-01-23 14:27:52 +010033
William Lallemand9ed62032016-11-21 17:49:11 +010034#include <types/cli.h>
Thierry FOURNIER380d0932015-01-23 14:27:52 +010035#include <types/hlua.h>
36#include <types/proxy.h>
William Lallemand9ed62032016-11-21 17:49:11 +010037#include <types/stats.h>
Thierry FOURNIER380d0932015-01-23 14:27:52 +010038
Thierry FOURNIER55da1652015-01-23 11:36:30 +010039#include <proto/arg.h>
Willy Tarreau8a8d83b2015-04-13 13:24:54 +020040#include <proto/applet.h>
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010041#include <proto/channel.h>
William Lallemand9ed62032016-11-21 17:49:11 +010042#include <proto/cli.h>
Willy Tarreaua71f6422016-11-16 17:00:14 +010043#include <proto/connection.h>
William Lallemand9ed62032016-11-21 17:49:11 +010044#include <proto/stats.h>
Thierry FOURNIER9a819e72015-02-16 20:22:55 +010045#include <proto/hdr_idx.h>
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +010046#include <proto/hlua.h>
Thierry Fournierfb0b5462016-01-21 09:28:58 +010047#include <proto/hlua_fcn.h>
Willy Tarreau79e57332018-10-02 16:01:16 +020048#include <proto/http_fetch.h>
Christopher Faulet724a12c2018-12-13 22:12:15 +010049#include <proto/http_htx.h>
Willy Tarreau61c112a2018-10-02 16:43:32 +020050#include <proto/http_rules.h>
Thierry FOURNIER3def3932015-04-07 11:27:54 +020051#include <proto/map.h>
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010052#include <proto/obj_type.h>
Patrick Hemmer268a7072018-05-11 12:52:31 -040053#include <proto/queue.h>
Thierry FOURNIER83758bb2015-02-04 13:21:04 +010054#include <proto/pattern.h>
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +010055#include <proto/payload.h>
56#include <proto/proto_http.h>
57#include <proto/sample.h>
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010058#include <proto/server.h>
Willy Tarreaufeb76402015-04-03 14:10:06 +020059#include <proto/session.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020060#include <proto/stream.h>
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010061#include <proto/stream_interface.h>
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +010062#include <proto/task.h>
Willy Tarreau39713102016-11-25 15:49:32 +010063#include <proto/tcp_rules.h>
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +020064#include <proto/vars.h>
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +010065
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +010066/* Lua uses longjmp to perform yield or throwing errors. This
67 * macro is used only for identifying the function that can
68 * not return because a longjmp is executed.
69 * __LJMP marks a prototype of hlua file that can use longjmp.
70 * WILL_LJMP() marks an lua function that will use longjmp.
71 * MAY_LJMP() marks an lua function that may use longjmp.
72 */
73#define __LJMP
Willy Tarreau4e7cc332018-10-20 17:45:48 +020074#define WILL_LJMP(func) do { func; my_unreachable(); } while(0)
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +010075#define MAY_LJMP(func) func
76
Thierry FOURNIERbabae282015-09-17 11:36:37 +020077/* This couple of function executes securely some Lua calls outside of
78 * the lua runtime environment. Each Lua call can return a longjmp
79 * if it encounter a memory error.
80 *
81 * Lua documentation extract:
82 *
83 * If an error happens outside any protected environment, Lua calls
84 * a panic function (see lua_atpanic) and then calls abort, thus
85 * exiting the host application. Your panic function can avoid this
86 * exit by never returning (e.g., doing a long jump to your own
87 * recovery point outside Lua).
88 *
89 * The panic function runs as if it were a message handler (see
90 * §2.3); in particular, the error message is at the top of the
91 * stack. However, there is no guarantee about stack space. To push
92 * anything on the stack, the panic function must first check the
93 * available space (see §4.2).
94 *
95 * We must check all the Lua entry point. This includes:
96 * - The include/proto/hlua.h exported functions
97 * - the task wrapper function
98 * - The action wrapper function
99 * - The converters wrapper function
100 * - The sample-fetch wrapper functions
101 *
102 * It is tolerated that the initilisation function returns an abort.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800103 * Before each Lua abort, an error message is written on stderr.
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200104 *
105 * The macro SET_SAFE_LJMP initialise the longjmp. The Macro
106 * RESET_SAFE_LJMP reset the longjmp. These function must be macro
107 * because they must be exists in the program stack when the longjmp
108 * is called.
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +0200109 *
110 * Note that the Lua processing is not really thread safe. It provides
111 * heavy system which consists to add our own lock function in the Lua
112 * code and recompile the library. This system will probably not accepted
113 * by maintainers of various distribs.
114 *
115 * Our main excution point of the Lua is the function lua_resume(). A
116 * quick looking on the Lua sources displays a lua_lock() a the start
117 * of function and a lua_unlock() at the end of the function. So I
118 * conclude that the Lua thread safe mode just perform a mutex around
119 * all execution. So I prefer to do this in the HAProxy code, it will be
120 * easier for distro maintainers.
121 *
122 * Note that the HAProxy lua functions rounded by the macro SET_SAFE_LJMP
123 * and RESET_SAFE_LJMP manipulates the Lua stack, so it will be careful
124 * to set mutex around these functions.
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200125 */
Willy Tarreau86abe442018-11-25 20:12:18 +0100126__decl_spinlock(hlua_global_lock);
Thierry FOURNIERffbad792017-07-12 11:39:04 +0200127THREAD_LOCAL jmp_buf safe_ljmp_env;
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200128static int hlua_panic_safe(lua_State *L) { return 0; }
129static int hlua_panic_ljmp(lua_State *L) { longjmp(safe_ljmp_env, 1); }
130
131#define SET_SAFE_LJMP(__L) \
132 ({ \
133 int ret; \
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100134 HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200135 if (setjmp(safe_ljmp_env) != 0) { \
136 lua_atpanic(__L, hlua_panic_safe); \
137 ret = 0; \
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100138 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200139 } else { \
140 lua_atpanic(__L, hlua_panic_ljmp); \
141 ret = 1; \
142 } \
143 ret; \
144 })
145
146/* If we are the last function catching Lua errors, we
147 * must reset the panic function.
148 */
149#define RESET_SAFE_LJMP(__L) \
150 do { \
151 lua_atpanic(__L, hlua_panic_safe); \
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100152 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200153 } while(0)
154
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200155/* Applet status flags */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200156#define APPLET_DONE 0x01 /* applet processing is done. */
157#define APPLET_100C 0x02 /* 100 continue expected. */
158#define APPLET_HDR_SENT 0x04 /* Response header sent. */
159#define APPLET_CHUNKED 0x08 /* Use transfer encoding chunked. */
160#define APPLET_LAST_CHK 0x10 /* Last chunk sent. */
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +0100161#define APPLET_HTTP11 0x20 /* Last chunk sent. */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200162
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100163/* The main Lua execution context. */
164struct hlua gL;
165
Thierry FOURNIERebed6e92016-12-16 11:54:07 +0100166/* This is the memory pool containing struct lua for applets
167 * (including cli).
168 */
Willy Tarreau8ceae722018-11-26 11:58:30 +0100169DECLARE_STATIC_POOL(pool_head_hlua, "hlua", sizeof(struct hlua));
Thierry FOURNIERebed6e92016-12-16 11:54:07 +0100170
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100171/* Used for Socket connection. */
172static struct proxy socket_proxy;
173static struct server socket_tcp;
174#ifdef USE_OPENSSL
175static struct server socket_ssl;
176#endif
177
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +0100178/* List head of the function called at the initialisation time. */
179struct list hlua_init_functions = LIST_HEAD_INIT(hlua_init_functions);
180
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +0100181/* The following variables contains the reference of the different
182 * Lua classes. These references are useful for identify metadata
183 * associated with an object.
184 */
Thierry FOURNIER65f34c62015-02-16 20:11:43 +0100185static int class_txn_ref;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100186static int class_socket_ref;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +0100187static int class_channel_ref;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +0100188static int class_fetches_ref;
Thierry FOURNIER594afe72015-03-10 23:58:30 +0100189static int class_converters_ref;
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100190static int class_http_ref;
Thierry FOURNIER3def3932015-04-07 11:27:54 +0200191static int class_map_ref;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200192static int class_applet_tcp_ref;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200193static int class_applet_http_ref;
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +0100194
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100195/* Global Lua execution timeout. By default Lua, execution linked
Willy Tarreau87b09662015-04-03 00:22:06 +0200196 * with stream (actions, sample-fetches and converters) have a
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100197 * short timeout. Lua linked with tasks doesn't have a timeout
198 * because a task may remain alive during all the haproxy execution.
199 */
200static unsigned int hlua_timeout_session = 4000; /* session timeout. */
201static unsigned int hlua_timeout_task = TICK_ETERNITY; /* task timeout. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200202static unsigned int hlua_timeout_applet = 4000; /* applet timeout. */
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100203
Thierry FOURNIERee9f8022015-03-03 17:37:37 +0100204/* Interrupts the Lua processing each "hlua_nb_instruction" instructions.
205 * it is used for preventing infinite loops.
206 *
207 * I test the scheer with an infinite loop containing one incrementation
208 * and one test. I run this loop between 10 seconds, I raise a ceil of
209 * 710M loops from one interrupt each 9000 instructions, so I fix the value
210 * to one interrupt each 10 000 instructions.
211 *
212 * configured | Number of
213 * instructions | loops executed
214 * between two | in milions
215 * forced yields |
216 * ---------------+---------------
217 * 10 | 160
218 * 500 | 670
219 * 1000 | 680
220 * 5000 | 700
221 * 7000 | 700
222 * 8000 | 700
223 * 9000 | 710 <- ceil
224 * 10000 | 710
225 * 100000 | 710
226 * 1000000 | 710
227 *
228 */
229static unsigned int hlua_nb_instruction = 10000;
230
Willy Tarreau32f61e22015-03-18 17:54:59 +0100231/* Descriptor for the memory allocation state. If limit is not null, it will
232 * be enforced on any memory allocation.
233 */
234struct hlua_mem_allocator {
235 size_t allocated;
236 size_t limit;
237};
238
239static struct hlua_mem_allocator hlua_global_allocator;
240
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200241static const char error_500[] =
Jarno Huuskonen16ad94a2017-01-09 14:17:10 +0200242 "HTTP/1.0 500 Internal Server Error\r\n"
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200243 "Cache-Control: no-cache\r\n"
244 "Connection: close\r\n"
245 "Content-Type: text/html\r\n"
246 "\r\n"
Joseph Herlant7fe15772018-11-15 10:07:32 -0800247 "<html><body><h1>500 Internal Server Error</h1>\nAn internal server error occurred.\n</body></html>\n";
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200248
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100249/* These functions converts types between HAProxy internal args or
250 * sample and LUA types. Another function permits to check if the
251 * LUA stack contains arguments according with an required ARG_T
252 * format.
253 */
254static int hlua_arg2lua(lua_State *L, const struct arg *arg);
255static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100256__LJMP static int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp,
David Carlier0c437f42016-04-27 16:21:56 +0100257 uint64_t mask, struct proxy *p);
Willy Tarreau5eadada2015-03-10 17:28:54 +0100258static int hlua_smp2lua(lua_State *L, struct sample *smp);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100259static int hlua_smp2lua_str(lua_State *L, struct sample *smp);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100260static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp);
261
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200262__LJMP static int hlua_http_get_headers(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg);
263
Thierry FOURNIER23bc3752015-09-11 19:15:43 +0200264#define SEND_ERR(__be, __fmt, __args...) \
265 do { \
266 send_log(__be, LOG_ERR, __fmt, ## __args); \
267 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) \
Christopher Faulet767a84b2017-11-24 16:50:31 +0100268 ha_alert(__fmt, ## __args); \
Thierry FOURNIER23bc3752015-09-11 19:15:43 +0200269 } while (0)
270
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100271/* Used to check an Lua function type in the stack. It creates and
272 * returns a reference of the function. This function throws an
273 * error if the rgument is not a "function".
274 */
275__LJMP unsigned int hlua_checkfunction(lua_State *L, int argno)
276{
277 if (!lua_isfunction(L, argno)) {
Thierry FOURNIERfd1e9552018-02-23 18:41:18 +0100278 const char *msg = lua_pushfstring(L, "function expected, got %s", luaL_typename(L, argno));
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100279 WILL_LJMP(luaL_argerror(L, argno, msg));
280 }
281 lua_pushvalue(L, argno);
282 return luaL_ref(L, LUA_REGISTRYINDEX);
283}
284
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200285/* Return the string that is of the top of the stack. */
286const char *hlua_get_top_error_string(lua_State *L)
287{
288 if (lua_gettop(L) < 1)
289 return "unknown error";
290 if (lua_type(L, -1) != LUA_TSTRING)
291 return "unknown error";
292 return lua_tostring(L, -1);
293}
294
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200295__LJMP static const char *hlua_traceback(lua_State *L)
296{
297 lua_Debug ar;
298 int level = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +0200299 struct buffer *msg = get_trash_chunk();
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200300 int filled = 0;
301
302 while (lua_getstack(L, level++, &ar)) {
303
304 /* Add separator */
305 if (filled)
306 chunk_appendf(msg, ", ");
307 filled = 1;
308
309 /* Fill fields:
310 * 'S': fills in the fields source, short_src, linedefined, lastlinedefined, and what;
311 * 'l': fills in the field currentline;
312 * 'n': fills in the field name and namewhat;
313 * 't': fills in the field istailcall;
314 */
315 lua_getinfo(L, "Slnt", &ar);
316
317 /* Append code localisation */
318 if (ar.currentline > 0)
319 chunk_appendf(msg, "%s:%d ", ar.short_src, ar.currentline);
320 else
321 chunk_appendf(msg, "%s ", ar.short_src);
322
323 /*
324 * Get function name
325 *
326 * if namewhat is no empty, name is defined.
327 * what contains "Lua" for Lua function, "C" for C function,
328 * or "main" for main code.
329 */
330 if (*ar.namewhat != '\0' && ar.name != NULL) /* is there a name from code? */
331 chunk_appendf(msg, "%s '%s'", ar.namewhat, ar.name); /* use it */
332
333 else if (*ar.what == 'm') /* "main", the code is not executed in a function */
334 chunk_appendf(msg, "main chunk");
335
336 else if (*ar.what != 'C') /* for Lua functions, use <file:line> */
337 chunk_appendf(msg, "C function line %d", ar.linedefined);
338
339 else /* nothing left... */
340 chunk_appendf(msg, "?");
341
342
343 /* Display tailed call */
344 if (ar.istailcall)
345 chunk_appendf(msg, " ...");
346 }
347
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200348 return msg->area;
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200349}
350
351
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100352/* This function check the number of arguments available in the
353 * stack. If the number of arguments available is not the same
354 * then <nb> an error is throwed.
355 */
356__LJMP static inline void check_args(lua_State *L, int nb, char *fcn)
357{
358 if (lua_gettop(L) == nb)
359 return;
360 WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb));
361}
362
Mark Lakes22154b42018-01-29 14:38:40 -0800363/* This function pushes an error string prefixed by the file name
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100364 * and the line number where the error is encountered.
365 */
366static int hlua_pusherror(lua_State *L, const char *fmt, ...)
367{
368 va_list argp;
369 va_start(argp, fmt);
370 luaL_where(L, 1);
371 lua_pushvfstring(L, fmt, argp);
372 va_end(argp);
373 lua_concat(L, 2);
374 return 1;
375}
376
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100377/* This functions is used with sample fetch and converters. It
378 * converts the HAProxy configuration argument in a lua stack
379 * values.
380 *
381 * It takes an array of "arg", and each entry of the array is
382 * converted and pushed in the LUA stack.
383 */
384static int hlua_arg2lua(lua_State *L, const struct arg *arg)
385{
386 switch (arg->type) {
387 case ARGT_SINT:
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100388 case ARGT_TIME:
389 case ARGT_SIZE:
Thierry FOURNIERf90838b2015-03-06 13:48:32 +0100390 lua_pushinteger(L, arg->data.sint);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100391 break;
392
393 case ARGT_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200394 lua_pushlstring(L, arg->data.str.area, arg->data.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100395 break;
396
397 case ARGT_IPV4:
398 case ARGT_IPV6:
399 case ARGT_MSK4:
400 case ARGT_MSK6:
401 case ARGT_FE:
402 case ARGT_BE:
403 case ARGT_TAB:
404 case ARGT_SRV:
405 case ARGT_USR:
406 case ARGT_MAP:
407 default:
408 lua_pushnil(L);
409 break;
410 }
411 return 1;
412}
413
414/* This function take one entrie in an LUA stack at the index "ud",
415 * and try to convert it in an HAProxy argument entry. This is useful
416 * with sample fetch wrappers. The input arguments are gived to the
417 * lua wrapper and converted as arg list by thi function.
418 */
419static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg)
420{
421 switch (lua_type(L, ud)) {
422
423 case LUA_TNUMBER:
424 case LUA_TBOOLEAN:
425 arg->type = ARGT_SINT;
426 arg->data.sint = lua_tointeger(L, ud);
427 break;
428
429 case LUA_TSTRING:
430 arg->type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200431 arg->data.str.area = (char *)lua_tolstring(L, ud, (size_t *)&arg->data.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100432 break;
433
434 case LUA_TUSERDATA:
435 case LUA_TNIL:
436 case LUA_TTABLE:
437 case LUA_TFUNCTION:
438 case LUA_TTHREAD:
439 case LUA_TLIGHTUSERDATA:
440 arg->type = ARGT_SINT;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +0200441 arg->data.sint = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100442 break;
443 }
444 return 1;
445}
446
447/* the following functions are used to convert a struct sample
448 * in Lua type. This useful to convert the return of the
449 * fetchs or converters.
450 */
Willy Tarreau5eadada2015-03-10 17:28:54 +0100451static int hlua_smp2lua(lua_State *L, struct sample *smp)
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100452{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200453 switch (smp->data.type) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100454 case SMP_T_SINT:
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100455 case SMP_T_BOOL:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200456 lua_pushinteger(L, smp->data.u.sint);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100457 break;
458
459 case SMP_T_BIN:
460 case SMP_T_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200461 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100462 break;
463
464 case SMP_T_METH:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200465 switch (smp->data.u.meth.meth) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100466 case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break;
467 case HTTP_METH_GET: lua_pushstring(L, "GET"); break;
468 case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break;
469 case HTTP_METH_POST: lua_pushstring(L, "POST"); break;
470 case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break;
471 case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break;
472 case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break;
473 case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break;
474 case HTTP_METH_OTHER:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200475 lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100476 break;
477 default:
478 lua_pushnil(L);
479 break;
480 }
481 break;
482
483 case SMP_T_IPV4:
484 case SMP_T_IPV6:
485 case SMP_T_ADDR: /* This type is never used to qualify a sample. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200486 if (sample_casts[smp->data.type][SMP_T_STR] &&
487 sample_casts[smp->data.type][SMP_T_STR](smp))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200488 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Willy Tarreau5eadada2015-03-10 17:28:54 +0100489 else
490 lua_pushnil(L);
491 break;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100492 default:
493 lua_pushnil(L);
494 break;
495 }
496 return 1;
497}
498
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100499/* the following functions are used to convert a struct sample
500 * in Lua strings. This is useful to convert the return of the
501 * fetchs or converters.
502 */
503static int hlua_smp2lua_str(lua_State *L, struct sample *smp)
504{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200505 switch (smp->data.type) {
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100506
507 case SMP_T_BIN:
508 case SMP_T_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200509 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100510 break;
511
512 case SMP_T_METH:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200513 switch (smp->data.u.meth.meth) {
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100514 case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break;
515 case HTTP_METH_GET: lua_pushstring(L, "GET"); break;
516 case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break;
517 case HTTP_METH_POST: lua_pushstring(L, "POST"); break;
518 case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break;
519 case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break;
520 case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break;
521 case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break;
522 case HTTP_METH_OTHER:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200523 lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100524 break;
525 default:
526 lua_pushstring(L, "");
527 break;
528 }
529 break;
530
531 case SMP_T_SINT:
532 case SMP_T_BOOL:
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100533 case SMP_T_IPV4:
534 case SMP_T_IPV6:
535 case SMP_T_ADDR: /* This type is never used to qualify a sample. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200536 if (sample_casts[smp->data.type][SMP_T_STR] &&
537 sample_casts[smp->data.type][SMP_T_STR](smp))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200538 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100539 else
540 lua_pushstring(L, "");
541 break;
542 default:
543 lua_pushstring(L, "");
544 break;
545 }
546 return 1;
547}
548
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100549/* the following functions are used to convert an Lua type in a
550 * struct sample. This is useful to provide data from a converter
551 * to the LUA code.
552 */
553static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp)
554{
555 switch (lua_type(L, ud)) {
556
557 case LUA_TNUMBER:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200558 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200559 smp->data.u.sint = lua_tointeger(L, ud);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100560 break;
561
562
563 case LUA_TBOOLEAN:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200564 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200565 smp->data.u.sint = lua_toboolean(L, ud);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100566 break;
567
568 case LUA_TSTRING:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200569 smp->data.type = SMP_T_STR;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100570 smp->flags |= SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200571 smp->data.u.str.area = (char *)lua_tolstring(L, ud, (size_t *)&smp->data.u.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100572 break;
573
574 case LUA_TUSERDATA:
575 case LUA_TNIL:
576 case LUA_TTABLE:
577 case LUA_TFUNCTION:
578 case LUA_TTHREAD:
579 case LUA_TLIGHTUSERDATA:
Thierry FOURNIER93405e12015-08-26 14:19:03 +0200580 case LUA_TNONE:
581 default:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200582 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200583 smp->data.u.sint = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100584 break;
585 }
586 return 1;
587}
588
589/* This function check the "argp" builded by another conversion function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800590 * is in accord with the expected argp defined by the "mask". The function
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100591 * returns true or false. It can be adjust the types if there compatibles.
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100592 *
593 * This function assumes thant the argp argument contains ARGM_NBARGS + 1
594 * entries.
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100595 */
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100596__LJMP int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp,
David Carlier0c437f42016-04-27 16:21:56 +0100597 uint64_t mask, struct proxy *p)
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100598{
599 int min_arg;
600 int idx;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100601 struct proxy *px;
602 char *sname, *pname;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100603
604 idx = 0;
605 min_arg = ARGM(mask);
606 mask >>= ARGM_BITS;
607
608 while (1) {
609
610 /* Check oversize. */
611 if (idx >= ARGM_NBARGS && argp[idx].type != ARGT_STOP) {
Cyril Bonté577a36a2015-03-02 00:08:38 +0100612 WILL_LJMP(luaL_argerror(L, first + idx, "Malformed argument mask"));
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100613 }
614
615 /* Check for mandatory arguments. */
616 if (argp[idx].type == ARGT_STOP) {
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100617 if (idx < min_arg) {
618
619 /* If miss other argument than the first one, we return an error. */
620 if (idx > 0)
621 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
622
623 /* If first argument have a certain type, some default values
624 * may be used. See the function smp_resolve_args().
625 */
626 switch (mask & ARGT_MASK) {
627
628 case ARGT_FE:
629 if (!(p->cap & PR_CAP_FE))
630 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
631 argp[idx].data.prx = p;
632 argp[idx].type = ARGT_FE;
633 argp[idx+1].type = ARGT_STOP;
634 break;
635
636 case ARGT_BE:
637 if (!(p->cap & PR_CAP_BE))
638 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
639 argp[idx].data.prx = p;
640 argp[idx].type = ARGT_BE;
641 argp[idx+1].type = ARGT_STOP;
642 break;
643
644 case ARGT_TAB:
645 argp[idx].data.prx = p;
646 argp[idx].type = ARGT_TAB;
647 argp[idx+1].type = ARGT_STOP;
648 break;
649
650 default:
651 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
652 break;
653 }
654 }
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100655 return 0;
656 }
657
658 /* Check for exceed the number of requiered argument. */
659 if ((mask & ARGT_MASK) == ARGT_STOP &&
660 argp[idx].type != ARGT_STOP) {
661 WILL_LJMP(luaL_argerror(L, first + idx, "Last argument expected"));
662 }
663
664 if ((mask & ARGT_MASK) == ARGT_STOP &&
665 argp[idx].type == ARGT_STOP) {
666 return 0;
667 }
668
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100669 /* Convert some argument types. */
670 switch (mask & ARGT_MASK) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100671 case ARGT_SINT:
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100672 if (argp[idx].type != ARGT_SINT)
673 WILL_LJMP(luaL_argerror(L, first + idx, "integer expected"));
674 argp[idx].type = ARGT_SINT;
675 break;
676
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100677 case ARGT_TIME:
678 if (argp[idx].type != ARGT_SINT)
679 WILL_LJMP(luaL_argerror(L, first + idx, "integer expected"));
Thierry FOURNIER29176f32015-07-07 00:41:29 +0200680 argp[idx].type = ARGT_TIME;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100681 break;
682
683 case ARGT_SIZE:
684 if (argp[idx].type != ARGT_SINT)
685 WILL_LJMP(luaL_argerror(L, first + idx, "integer expected"));
Thierry FOURNIER29176f32015-07-07 00:41:29 +0200686 argp[idx].type = ARGT_SIZE;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100687 break;
688
689 case ARGT_FE:
690 if (argp[idx].type != ARGT_STR)
691 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200692 memcpy(trash.area, argp[idx].data.str.area,
693 argp[idx].data.str.data);
694 trash.area[argp[idx].data.str.data] = 0;
695 argp[idx].data.prx = proxy_fe_by_name(trash.area);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100696 if (!argp[idx].data.prx)
697 WILL_LJMP(luaL_argerror(L, first + idx, "frontend doesn't exist"));
698 argp[idx].type = ARGT_FE;
699 break;
700
701 case ARGT_BE:
702 if (argp[idx].type != ARGT_STR)
703 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200704 memcpy(trash.area, argp[idx].data.str.area,
705 argp[idx].data.str.data);
706 trash.area[argp[idx].data.str.data] = 0;
707 argp[idx].data.prx = proxy_be_by_name(trash.area);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100708 if (!argp[idx].data.prx)
709 WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist"));
710 argp[idx].type = ARGT_BE;
711 break;
712
713 case ARGT_TAB:
714 if (argp[idx].type != ARGT_STR)
715 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200716 memcpy(trash.area, argp[idx].data.str.area,
717 argp[idx].data.str.data);
718 trash.area[argp[idx].data.str.data] = 0;
719 argp[idx].data.prx = proxy_tbl_by_name(trash.area);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100720 if (!argp[idx].data.prx)
721 WILL_LJMP(luaL_argerror(L, first + idx, "table doesn't exist"));
722 argp[idx].type = ARGT_TAB;
723 break;
724
725 case ARGT_SRV:
726 if (argp[idx].type != ARGT_STR)
727 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200728 memcpy(trash.area, argp[idx].data.str.area,
729 argp[idx].data.str.data);
730 trash.area[argp[idx].data.str.data] = 0;
731 sname = strrchr(trash.area, '/');
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100732 if (sname) {
733 *sname++ = '\0';
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200734 pname = trash.area;
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200735 px = proxy_be_by_name(pname);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100736 if (!px)
737 WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist"));
738 }
739 else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200740 sname = trash.area;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100741 px = p;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100742 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100743 argp[idx].data.srv = findserver(px, sname);
744 if (!argp[idx].data.srv)
745 WILL_LJMP(luaL_argerror(L, first + idx, "server doesn't exist"));
746 argp[idx].type = ARGT_SRV;
747 break;
748
749 case ARGT_IPV4:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200750 memcpy(trash.area, argp[idx].data.str.area,
751 argp[idx].data.str.data);
752 trash.area[argp[idx].data.str.data] = 0;
753 if (inet_pton(AF_INET, trash.area, &argp[idx].data.ipv4))
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100754 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 address"));
755 argp[idx].type = ARGT_IPV4;
756 break;
757
758 case ARGT_MSK4:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200759 memcpy(trash.area, argp[idx].data.str.area,
760 argp[idx].data.str.data);
761 trash.area[argp[idx].data.str.data] = 0;
762 if (!str2mask(trash.area, &argp[idx].data.ipv4))
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100763 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 mask"));
764 argp[idx].type = ARGT_MSK4;
765 break;
766
767 case ARGT_IPV6:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200768 memcpy(trash.area, argp[idx].data.str.area,
769 argp[idx].data.str.data);
770 trash.area[argp[idx].data.str.data] = 0;
771 if (inet_pton(AF_INET6, trash.area, &argp[idx].data.ipv6))
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100772 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 address"));
773 argp[idx].type = ARGT_IPV6;
774 break;
775
776 case ARGT_MSK6:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200777 memcpy(trash.area, argp[idx].data.str.area,
778 argp[idx].data.str.data);
779 trash.area[argp[idx].data.str.data] = 0;
780 if (!str2mask6(trash.area, &argp[idx].data.ipv6))
Tim Duesterhusb814da62018-01-25 16:24:50 +0100781 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 mask"));
782 argp[idx].type = ARGT_MSK6;
783 break;
784
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100785 case ARGT_MAP:
786 case ARGT_REG:
787 case ARGT_USR:
788 WILL_LJMP(luaL_argerror(L, first + idx, "type not yet supported"));
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100789 break;
790 }
791
792 /* Check for type of argument. */
793 if ((mask & ARGT_MASK) != argp[idx].type) {
794 const char *msg = lua_pushfstring(L, "'%s' expected, got '%s'",
795 arg_type_names[(mask & ARGT_MASK)],
796 arg_type_names[argp[idx].type & ARGT_MASK]);
797 WILL_LJMP(luaL_argerror(L, first + idx, msg));
798 }
799
800 /* Next argument. */
801 mask >>= ARGT_BITS;
802 idx++;
803 }
804}
805
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100806/*
807 * The following functions are used to make correspondance between the the
808 * executed lua pointer and the "struct hlua *" that contain the context.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100809 *
810 * - hlua_gethlua : return the hlua context associated with an lua_State.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100811 * - hlua_sethlua : create the association between hlua context and lua_state.
812 */
813static inline struct hlua *hlua_gethlua(lua_State *L)
814{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +0100815 struct hlua **hlua = lua_getextraspace(L);
816 return *hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100817}
818static inline void hlua_sethlua(struct hlua *hlua)
819{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +0100820 struct hlua **hlua_store = lua_getextraspace(hlua->T);
821 *hlua_store = hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100822}
823
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100824/* This function is used to send logs. It try to send on screen (stderr)
825 * and on the default syslog server.
826 */
827static inline void hlua_sendlog(struct proxy *px, int level, const char *msg)
828{
829 struct tm tm;
830 char *p;
831
832 /* Cleanup the log message. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200833 p = trash.area;
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100834 for (; *msg != '\0'; msg++, p++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200835 if (p >= trash.area + trash.size - 1) {
Thierry FOURNIERccf00632015-09-16 12:47:03 +0200836 /* Break the message if exceed the buffer size. */
837 *(p-4) = ' ';
838 *(p-3) = '.';
839 *(p-2) = '.';
840 *(p-1) = '.';
841 break;
842 }
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100843 if (isprint(*msg))
844 *p = *msg;
845 else
846 *p = '.';
847 }
848 *p = '\0';
849
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200850 send_log(px, level, "%s\n", trash.area);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100851 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
Willy Tarreaua678b432015-08-28 10:14:59 +0200852 get_localtime(date.tv_sec, &tm);
853 fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n",
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100854 log_levels[level], tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec,
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200855 (int)getpid(), trash.area);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100856 fflush(stderr);
857 }
858}
859
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100860/* This function just ensure that the yield will be always
861 * returned with a timeout and permit to set some flags
862 */
863__LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx,
Thierry FOURNIERf90838b2015-03-06 13:48:32 +0100864 lua_KFunction k, int timeout, unsigned int flags)
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100865{
866 struct hlua *hlua = hlua_gethlua(L);
867
868 /* Set the wake timeout. If timeout is required, we set
869 * the expiration time.
870 */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +0200871 hlua->wake_time = timeout;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100872
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +0100873 hlua->flags |= flags;
874
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100875 /* Process the yield. */
Willy Tarreau9635e032018-10-16 17:52:55 +0200876 MAY_LJMP(lua_yieldk(L, nresults, ctx, k));
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100877}
878
Willy Tarreau87b09662015-04-03 00:22:06 +0200879/* This function initialises the Lua environment stored in the stream.
880 * It must be called at the start of the stream. This function creates
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100881 * an LUA coroutine. It can not be use to crete the main LUA context.
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200882 *
883 * This function is particular. it initialises a new Lua thread. If the
884 * initialisation fails (example: out of memory error), the lua function
885 * throws an error (longjmp).
886 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800887 * This function manipulates two Lua stacks: the main and the thread. Only
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200888 * the main stack can fail. The thread is not manipulated. This function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800889 * MUST NOT manipulate the created thread stack state, because it is not
890 * proctected against errors thrown by the thread stack.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100891 */
892int hlua_ctx_init(struct hlua *lua, struct task *task)
893{
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200894 if (!SET_SAFE_LJMP(gL.T)) {
895 lua->Tref = LUA_REFNIL;
896 return 0;
897 }
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100898 lua->Mref = LUA_REFNIL;
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +0100899 lua->flags = 0;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100900 LIST_INIT(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100901 lua->T = lua_newthread(gL.T);
902 if (!lua->T) {
903 lua->Tref = LUA_REFNIL;
Thierry FOURNIER0a976202017-07-12 11:18:00 +0200904 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100905 return 0;
906 }
907 hlua_sethlua(lua);
908 lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
909 lua->task = task;
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200910 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100911 return 1;
912}
913
Willy Tarreau87b09662015-04-03 00:22:06 +0200914/* Used to destroy the Lua coroutine when the attached stream or task
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100915 * is destroyed. The destroy also the memory context. The struct "lua"
916 * is not freed.
917 */
918void hlua_ctx_destroy(struct hlua *lua)
919{
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100920 if (!lua)
Thierry FOURNIERa718b292015-03-04 16:48:34 +0100921 return;
922
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100923 if (!lua->T)
924 goto end;
925
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100926 /* Purge all the pending signals. */
Thierry FOURNIERd6975962017-07-12 14:31:10 +0200927 notification_purge(&lua->com);
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100928
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200929 if (!SET_SAFE_LJMP(lua->T))
930 return;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100931 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200932 RESET_SAFE_LJMP(lua->T);
Thierry FOURNIER5a50a852015-09-23 16:59:28 +0200933
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200934 if (!SET_SAFE_LJMP(gL.T))
935 return;
936 luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref);
937 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER5a50a852015-09-23 16:59:28 +0200938 /* Forces a garbage collecting process. If the Lua program is finished
939 * without error, we run the GC on the thread pointer. Its freed all
940 * the unused memory.
941 * If the thread is finnish with an error or is currently yielded,
942 * it seems that the GC applied on the thread doesn't clean anything,
943 * so e run the GC on the main thread.
944 * NOTE: maybe this action locks all the Lua threads untiml the en of
945 * the garbage collection.
946 */
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +0200947 if (lua->flags & HLUA_MUST_GC) {
Thierry FOURNIER7bd10d52017-07-17 00:44:40 +0200948 if (!SET_SAFE_LJMP(gL.T))
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200949 return;
Thierry FOURNIER7bd10d52017-07-17 00:44:40 +0200950 lua_gc(gL.T, LUA_GCCOLLECT, 0);
951 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +0200952 }
Thierry FOURNIER5a50a852015-09-23 16:59:28 +0200953
Thierry FOURNIERa7b536b2015-09-21 22:50:24 +0200954 lua->T = NULL;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100955
956end:
Willy Tarreaubafbe012017-11-24 17:34:44 +0100957 pool_free(pool_head_hlua, lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100958}
959
960/* This function is used to restore the Lua context when a coroutine
961 * fails. This function copy the common memory between old coroutine
962 * and the new coroutine. The old coroutine is destroyed, and its
963 * replaced by the new coroutine.
964 * If the flag "keep_msg" is set, the last entry of the old is assumed
965 * as string error message and it is copied in the new stack.
966 */
967static int hlua_ctx_renew(struct hlua *lua, int keep_msg)
968{
969 lua_State *T;
970 int new_ref;
971
972 /* Renew the main LUA stack doesn't have sense. */
973 if (lua == &gL)
974 return 0;
975
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100976 /* New Lua coroutine. */
977 T = lua_newthread(gL.T);
978 if (!T)
979 return 0;
980
981 /* Copy last error message. */
982 if (keep_msg)
983 lua_xmove(lua->T, T, 1);
984
985 /* Copy data between the coroutines. */
986 lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref);
987 lua_xmove(lua->T, T, 1);
988 new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Valur poped. */
989
990 /* Destroy old data. */
991 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
992
993 /* The thread is garbage collected by Lua. */
994 luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref);
995
996 /* Fill the struct with the new coroutine values. */
997 lua->Mref = new_ref;
998 lua->T = T;
999 lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
1000
1001 /* Set context. */
1002 hlua_sethlua(lua);
1003
1004 return 1;
1005}
1006
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001007void hlua_hook(lua_State *L, lua_Debug *ar)
1008{
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001009 struct hlua *hlua = hlua_gethlua(L);
1010
1011 /* Lua cannot yield when its returning from a function,
1012 * so, we can fix the interrupt hook to 1 instruction,
1013 * expecting that the function is finnished.
1014 */
1015 if (lua_gethookmask(L) & LUA_MASKRET) {
1016 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1);
1017 return;
1018 }
1019
1020 /* restore the interrupt condition. */
1021 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
1022
1023 /* If we interrupt the Lua processing in yieldable state, we yield.
1024 * If the state is not yieldable, trying yield causes an error.
1025 */
1026 if (lua_isyieldable(L))
Willy Tarreau9635e032018-10-16 17:52:55 +02001027 MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD));
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001028
Thierry FOURNIERa85cfb12015-03-13 14:50:06 +01001029 /* If we cannot yield, update the clock and check the timeout. */
1030 tv_update_date(0, 1);
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001031 hlua->run_time += now_ms - hlua->start_time;
1032 if (hlua->max_time && hlua->run_time >= hlua->max_time) {
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001033 lua_pushfstring(L, "execution timeout");
1034 WILL_LJMP(lua_error(L));
1035 }
1036
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001037 /* Update the start time. */
1038 hlua->start_time = now_ms;
1039
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001040 /* Try to interrupt the process at the end of the current
1041 * unyieldable function.
1042 */
1043 lua_sethook(hlua->T, hlua_hook, LUA_MASKRET|LUA_MASKCOUNT, hlua_nb_instruction);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001044}
1045
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001046/* This function start or resumes the Lua stack execution. If the flag
1047 * "yield_allowed" if no set and the LUA stack execution returns a yield
1048 * The function return an error.
1049 *
1050 * The function can returns 4 values:
1051 * - HLUA_E_OK : The execution is terminated without any errors.
1052 * - HLUA_E_AGAIN : The execution must continue at the next associated
1053 * task wakeup.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001054 * - HLUA_E_ERRMSG : An error has occurred, an error message is set in
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001055 * the top of the stack.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001056 * - HLUA_E_ERR : An error has occurred without error message.
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001057 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001058 * If an error occurred, the stack is renewed and it is ready to run new
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001059 * LUA code.
1060 */
1061static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed)
1062{
1063 int ret;
1064 const char *msg;
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001065 const char *trace;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001066
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001067 /* Initialise run time counter. */
1068 if (!HLUA_IS_RUNNING(lua))
1069 lua->run_time = 0;
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001070
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001071 /* Lock the whole Lua execution. This lock must be before the
1072 * label "resume_execution".
1073 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001074 HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock);
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001075
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001076resume_execution:
1077
1078 /* This hook interrupts the Lua processing each 'hlua_nb_instruction'
1079 * instructions. it is used for preventing infinite loops.
1080 */
1081 lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
1082
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +01001083 /* Remove all flags except the running flags. */
Thierry FOURNIER2f3867f2015-09-28 01:02:01 +02001084 HLUA_SET_RUN(lua);
1085 HLUA_CLR_CTRLYIELD(lua);
1086 HLUA_CLR_WAKERESWR(lua);
1087 HLUA_CLR_WAKEREQWR(lua);
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +01001088
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001089 /* Update the start time. */
1090 lua->start_time = now_ms;
1091
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001092 /* Call the function. */
1093 ret = lua_resume(lua->T, gL.T, lua->nargs);
1094 switch (ret) {
1095
1096 case LUA_OK:
1097 ret = HLUA_E_OK;
1098 break;
1099
1100 case LUA_YIELD:
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001101 /* Check if the execution timeout is expired. It it is the case, we
1102 * break the Lua execution.
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001103 */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001104 tv_update_date(0, 1);
1105 lua->run_time += now_ms - lua->start_time;
1106 if (lua->max_time && lua->run_time > lua->max_time) {
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001107 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001108 ret = HLUA_E_ETMOUT;
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001109 break;
1110 }
1111 /* Process the forced yield. if the general yield is not allowed or
1112 * if no task were associated this the current Lua execution
1113 * coroutine, we resume the execution. Else we want to return in the
1114 * scheduler and we want to be waked up again, to continue the
1115 * current Lua execution. So we schedule our own task.
1116 */
1117 if (HLUA_IS_CTRLYIELDING(lua)) {
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001118 if (!yield_allowed || !lua->task)
1119 goto resume_execution;
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001120 task_wakeup(lua->task, TASK_WOKEN_MSG);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001121 }
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001122 if (!yield_allowed) {
1123 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001124 ret = HLUA_E_YIELD;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001125 break;
1126 }
1127 ret = HLUA_E_AGAIN;
1128 break;
1129
1130 case LUA_ERRRUN:
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001131
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001132 /* Special exit case. The traditional exit is returned as an error
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001133 * because the errors ares the only one mean to return immediately
1134 * from and lua execution.
1135 */
1136 if (lua->flags & HLUA_EXIT) {
1137 ret = HLUA_E_OK;
Thierry FOURNIERe1587b32015-08-28 09:54:13 +02001138 hlua_ctx_renew(lua, 0);
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001139 break;
1140 }
1141
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001142 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001143 if (!lua_checkstack(lua->T, 1)) {
1144 ret = HLUA_E_ERR;
1145 break;
1146 }
1147 msg = lua_tostring(lua->T, -1);
1148 lua_settop(lua->T, 0); /* Empty the stack. */
1149 lua_pop(lua->T, 1);
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001150 trace = hlua_traceback(lua->T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001151 if (msg)
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001152 lua_pushfstring(lua->T, "runtime error: %s from %s", msg, trace);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001153 else
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001154 lua_pushfstring(lua->T, "unknown runtime error from %s", trace);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001155 ret = HLUA_E_ERRMSG;
1156 break;
1157
1158 case LUA_ERRMEM:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001159 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001160 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001161 ret = HLUA_E_NOMEM;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001162 break;
1163
1164 case LUA_ERRERR:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001165 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001166 if (!lua_checkstack(lua->T, 1)) {
1167 ret = HLUA_E_ERR;
1168 break;
1169 }
1170 msg = lua_tostring(lua->T, -1);
1171 lua_settop(lua->T, 0); /* Empty the stack. */
1172 lua_pop(lua->T, 1);
1173 if (msg)
1174 lua_pushfstring(lua->T, "message handler error: %s", msg);
1175 else
1176 lua_pushfstring(lua->T, "message handler error");
1177 ret = HLUA_E_ERRMSG;
1178 break;
1179
1180 default:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001181 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001182 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001183 ret = HLUA_E_ERR;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001184 break;
1185 }
1186
Thierry FOURNIER6ab4d8e2015-09-27 22:17:19 +02001187 /* This GC permits to destroy some object when a Lua timeout strikes. */
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +02001188 if (lua->flags & HLUA_MUST_GC &&
1189 ret != HLUA_E_AGAIN)
Thierry FOURNIER6ab4d8e2015-09-27 22:17:19 +02001190 lua_gc(lua->T, LUA_GCCOLLECT, 0);
1191
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001192 switch (ret) {
1193 case HLUA_E_AGAIN:
1194 break;
1195
1196 case HLUA_E_ERRMSG:
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001197 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001198 hlua_ctx_renew(lua, 1);
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001199 HLUA_CLR_RUN(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001200 break;
1201
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001202 case HLUA_E_ETMOUT:
1203 case HLUA_E_NOMEM:
1204 case HLUA_E_YIELD:
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001205 case HLUA_E_ERR:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001206 HLUA_CLR_RUN(lua);
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001207 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001208 hlua_ctx_renew(lua, 0);
1209 break;
1210
1211 case HLUA_E_OK:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001212 HLUA_CLR_RUN(lua);
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001213 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001214 break;
1215 }
1216
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001217 /* This is the main exit point, remove the Lua lock. */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001218 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock);
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001219
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001220 return ret;
1221}
1222
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001223/* This function exit the current code. */
1224__LJMP static int hlua_done(lua_State *L)
1225{
1226 struct hlua *hlua = hlua_gethlua(L);
1227
1228 hlua->flags |= HLUA_EXIT;
1229 WILL_LJMP(lua_error(L));
1230
1231 return 0;
1232}
1233
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001234/* This function is an LUA binding. It provides a function
1235 * for deleting ACL from a referenced ACL file.
1236 */
1237__LJMP static int hlua_del_acl(lua_State *L)
1238{
1239 const char *name;
1240 const char *key;
1241 struct pat_ref *ref;
1242
1243 MAY_LJMP(check_args(L, 2, "del_acl"));
1244
1245 name = MAY_LJMP(luaL_checkstring(L, 1));
1246 key = MAY_LJMP(luaL_checkstring(L, 2));
1247
1248 ref = pat_ref_lookup(name);
1249 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001250 WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001251
1252 pat_ref_delete(ref, key);
1253 return 0;
1254}
1255
1256/* This function is an LUA binding. It provides a function
1257 * for deleting map entry from a referenced map file.
1258 */
1259static int hlua_del_map(lua_State *L)
1260{
1261 const char *name;
1262 const char *key;
1263 struct pat_ref *ref;
1264
1265 MAY_LJMP(check_args(L, 2, "del_map"));
1266
1267 name = MAY_LJMP(luaL_checkstring(L, 1));
1268 key = MAY_LJMP(luaL_checkstring(L, 2));
1269
1270 ref = pat_ref_lookup(name);
1271 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001272 WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001273
1274 pat_ref_delete(ref, key);
1275 return 0;
1276}
1277
1278/* This function is an LUA binding. It provides a function
1279 * for adding ACL pattern from a referenced ACL file.
1280 */
1281static int hlua_add_acl(lua_State *L)
1282{
1283 const char *name;
1284 const char *key;
1285 struct pat_ref *ref;
1286
1287 MAY_LJMP(check_args(L, 2, "add_acl"));
1288
1289 name = MAY_LJMP(luaL_checkstring(L, 1));
1290 key = MAY_LJMP(luaL_checkstring(L, 2));
1291
1292 ref = pat_ref_lookup(name);
1293 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001294 WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001295
1296 if (pat_ref_find_elt(ref, key) == NULL)
1297 pat_ref_add(ref, key, NULL, NULL);
1298 return 0;
1299}
1300
1301/* This function is an LUA binding. It provides a function
1302 * for setting map pattern and sample from a referenced map
1303 * file.
1304 */
1305static int hlua_set_map(lua_State *L)
1306{
1307 const char *name;
1308 const char *key;
1309 const char *value;
1310 struct pat_ref *ref;
1311
1312 MAY_LJMP(check_args(L, 3, "set_map"));
1313
1314 name = MAY_LJMP(luaL_checkstring(L, 1));
1315 key = MAY_LJMP(luaL_checkstring(L, 2));
1316 value = MAY_LJMP(luaL_checkstring(L, 3));
1317
1318 ref = pat_ref_lookup(name);
1319 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001320 WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001321
1322 if (pat_ref_find_elt(ref, key) != NULL)
1323 pat_ref_set(ref, key, value, NULL);
1324 else
1325 pat_ref_add(ref, key, value, NULL);
1326 return 0;
1327}
1328
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001329/* A class is a lot of memory that contain data. This data can be a table,
1330 * an integer or user data. This data is associated with a metatable. This
1331 * metatable have an original version registred in the global context with
1332 * the name of the object (_G[<name>] = <metable> ).
1333 *
1334 * A metable is a table that modify the standard behavior of a standard
1335 * access to the associated data. The entries of this new metatable are
1336 * defined as is:
1337 *
1338 * http://lua-users.org/wiki/MetatableEvents
1339 *
1340 * __index
1341 *
1342 * we access an absent field in a table, the result is nil. This is
1343 * true, but it is not the whole truth. Actually, such access triggers
1344 * the interpreter to look for an __index metamethod: If there is no
1345 * such method, as usually happens, then the access results in nil;
1346 * otherwise, the metamethod will provide the result.
1347 *
1348 * Control 'prototype' inheritance. When accessing "myTable[key]" and
1349 * the key does not appear in the table, but the metatable has an __index
1350 * property:
1351 *
1352 * - if the value is a function, the function is called, passing in the
1353 * table and the key; the return value of that function is returned as
1354 * the result.
1355 *
1356 * - if the value is another table, the value of the key in that table is
1357 * asked for and returned (and if it doesn't exist in that table, but that
1358 * table's metatable has an __index property, then it continues on up)
1359 *
1360 * - Use "rawget(myTable,key)" to skip this metamethod.
1361 *
1362 * http://www.lua.org/pil/13.4.1.html
1363 *
1364 * __newindex
1365 *
1366 * Like __index, but control property assignment.
1367 *
1368 * __mode - Control weak references. A string value with one or both
1369 * of the characters 'k' and 'v' which specifies that the the
1370 * keys and/or values in the table are weak references.
1371 *
1372 * __call - Treat a table like a function. When a table is followed by
1373 * parenthesis such as "myTable( 'foo' )" and the metatable has
1374 * a __call key pointing to a function, that function is invoked
1375 * (passing any specified arguments) and the return value is
1376 * returned.
1377 *
1378 * __metatable - Hide the metatable. When "getmetatable( myTable )" is
1379 * called, if the metatable for myTable has a __metatable
1380 * key, the value of that key is returned instead of the
1381 * actual metatable.
1382 *
1383 * __tostring - Control string representation. When the builtin
1384 * "tostring( myTable )" function is called, if the metatable
1385 * for myTable has a __tostring property set to a function,
1386 * that function is invoked (passing myTable to it) and the
1387 * return value is used as the string representation.
1388 *
1389 * __len - Control table length. When the table length is requested using
1390 * the length operator ( '#' ), if the metatable for myTable has
1391 * a __len key pointing to a function, that function is invoked
1392 * (passing myTable to it) and the return value used as the value
1393 * of "#myTable".
1394 *
1395 * __gc - Userdata finalizer code. When userdata is set to be garbage
1396 * collected, if the metatable has a __gc field pointing to a
1397 * function, that function is first invoked, passing the userdata
1398 * to it. The __gc metamethod is not called for tables.
1399 * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html)
1400 *
1401 * Special metamethods for redefining standard operators:
1402 * http://www.lua.org/pil/13.1.html
1403 *
1404 * __add "+"
1405 * __sub "-"
1406 * __mul "*"
1407 * __div "/"
1408 * __unm "!"
1409 * __pow "^"
1410 * __concat ".."
1411 *
1412 * Special methods for redfining standar relations
1413 * http://www.lua.org/pil/13.2.html
1414 *
1415 * __eq "=="
1416 * __lt "<"
1417 * __le "<="
1418 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001419
1420/*
1421 *
1422 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001423 * Class Map
1424 *
1425 *
1426 */
1427
1428/* Returns a struct hlua_map if the stack entry "ud" is
1429 * a class session, otherwise it throws an error.
1430 */
1431__LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud)
1432{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001433 return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001434}
1435
1436/* This function is the map constructor. It don't need
1437 * the class Map object. It creates and return a new Map
1438 * object. It must be called only during "body" or "init"
1439 * context because it process some filesystem accesses.
1440 */
1441__LJMP static int hlua_map_new(struct lua_State *L)
1442{
1443 const char *fn;
1444 int match = PAT_MATCH_STR;
1445 struct sample_conv conv;
1446 const char *file = "";
1447 int line = 0;
1448 lua_Debug ar;
1449 char *err = NULL;
1450 struct arg args[2];
1451
1452 if (lua_gettop(L) < 1 || lua_gettop(L) > 2)
1453 WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument."));
1454
1455 fn = MAY_LJMP(luaL_checkstring(L, 1));
1456
1457 if (lua_gettop(L) >= 2) {
1458 match = MAY_LJMP(luaL_checkinteger(L, 2));
1459 if (match < 0 || match >= PAT_MATCH_NUM)
1460 WILL_LJMP(luaL_error(L, "'new' needs a valid match method."));
1461 }
1462
1463 /* Get Lua filename and line number. */
1464 if (lua_getstack(L, 1, &ar)) { /* check function at level */
1465 lua_getinfo(L, "Sl", &ar); /* get info about it */
1466 if (ar.currentline > 0) { /* is there info? */
1467 file = ar.short_src;
1468 line = ar.currentline;
1469 }
1470 }
1471
1472 /* fill fake sample_conv struct. */
1473 conv.kw = ""; /* unused. */
1474 conv.process = NULL; /* unused. */
1475 conv.arg_mask = 0; /* unused. */
1476 conv.val_args = NULL; /* unused. */
1477 conv.out_type = SMP_T_STR;
1478 conv.private = (void *)(long)match;
1479 switch (match) {
1480 case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break;
1481 case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break;
1482 case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break;
1483 case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break;
1484 case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break;
1485 case PAT_MATCH_END: conv.in_type = SMP_T_STR; break;
1486 case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001487 case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001488 case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break;
1489 default:
1490 WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode."));
1491 }
1492
1493 /* fill fake args. */
1494 args[0].type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001495 args[0].data.str.area = (char *)fn;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001496 args[1].type = ARGT_STOP;
1497
1498 /* load the map. */
1499 if (!sample_load_map(args, &conv, file, line, &err)) {
1500 /* error case: we cant use luaL_error because we must
1501 * free the err variable.
1502 */
1503 luaL_where(L, 1);
1504 lua_pushfstring(L, "'new': %s.", err);
1505 lua_concat(L, 2);
1506 free(err);
1507 WILL_LJMP(lua_error(L));
1508 }
1509
1510 /* create the lua object. */
1511 lua_newtable(L);
1512 lua_pushlightuserdata(L, args[0].data.map);
1513 lua_rawseti(L, -2, 0);
1514
1515 /* Pop a class Map metatable and affect it to the userdata. */
1516 lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref);
1517 lua_setmetatable(L, -2);
1518
1519
1520 return 1;
1521}
1522
1523__LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str)
1524{
1525 struct map_descriptor *desc;
1526 struct pattern *pat;
1527 struct sample smp;
1528
1529 MAY_LJMP(check_args(L, 2, "lookup"));
1530 desc = MAY_LJMP(hlua_checkmap(L, 1));
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001531 if (desc->pat.expect_type == SMP_T_SINT) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001532 smp.data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001533 smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001534 }
1535 else {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001536 smp.data.type = SMP_T_STR;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001537 smp.flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001538 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 +02001539 }
1540
1541 pat = pattern_exec_match(&desc->pat, &smp, 1);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001542 if (!pat || !pat->data) {
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001543 if (str)
1544 lua_pushstring(L, "");
1545 else
1546 lua_pushnil(L);
1547 return 1;
1548 }
1549
1550 /* The Lua pattern must return a string, so we can't check the returned type */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001551 lua_pushlstring(L, pat->data->u.str.area, pat->data->u.str.data);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001552 return 1;
1553}
1554
1555__LJMP static int hlua_map_lookup(struct lua_State *L)
1556{
1557 return _hlua_map_lookup(L, 0);
1558}
1559
1560__LJMP static int hlua_map_slookup(struct lua_State *L)
1561{
1562 return _hlua_map_lookup(L, 1);
1563}
1564
1565/*
1566 *
1567 *
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001568 * Class Socket
1569 *
1570 *
1571 */
1572
1573__LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud)
1574{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001575 return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001576}
1577
1578/* This function is the handler called for each I/O on the established
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001579 * connection. It is used for notify space available to send or data
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001580 * received.
1581 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001582static void hlua_socket_handler(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001583{
Willy Tarreau00a37f02015-04-13 12:05:19 +02001584 struct stream_interface *si = appctx->owner;
Olivier Houchard9aaf7782017-09-13 18:30:23 +02001585 struct connection *c = cs_conn(objt_cs(si_opposite(si)->end));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001586
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001587 if (appctx->ctx.hlua_cosocket.die) {
1588 si_shutw(si);
1589 si_shutr(si);
1590 si_ic(si)->flags |= CF_READ_NULL;
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001591 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1592 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001593 stream_shutdown(si_strm(si), SF_ERR_KILLED);
1594 }
1595
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001596 /* If the connection object is not available, close all the
1597 * streams and wakeup everything waiting for.
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001598 */
1599 if (!c) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001600 si_shutw(si);
1601 si_shutr(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01001602 si_ic(si)->flags |= CF_READ_NULL;
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001603 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1604 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Willy Tarreaud4da1962015-04-20 01:31:23 +02001605 return;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001606 }
1607
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001608 /* If we cant write, wakeup the pending write signals. */
1609 if (channel_output_closed(si_ic(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001610 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001611
1612 /* If we cant read, wakeup the pending read signals. */
1613 if (channel_input_closed(si_oc(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001614 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001615
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001616 /* if the connection is not estabkished, inform the stream that we want
1617 * to be notified whenever the connection completes.
1618 */
1619 if (!(c->flags & CO_FL_CONNECTED)) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01001620 si_cant_get(si);
Willy Tarreau12c24232018-12-06 15:29:50 +01001621 si_rx_conn_blk(si);
Willy Tarreau3367d412018-11-15 10:57:41 +01001622 si_rx_endp_more(si);
Willy Tarreaud4da1962015-04-20 01:31:23 +02001623 return;
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001624 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001625
1626 /* This function is called after the connect. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01001627 appctx->ctx.hlua_cosocket.connected = 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001628
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001629 /* Wake the tasks which wants to write if the buffer have available space. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001630 if (channel_may_recv(si_ic(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001631 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001632
1633 /* Wake the tasks which wants to read if the buffer contains data. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001634 if (!channel_is_empty(si_oc(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001635 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001636
1637 /* Some data were injected in the buffer, notify the stream
1638 * interface.
1639 */
1640 if (!channel_is_empty(si_ic(si)))
1641 stream_int_update(si);
1642
1643 /* If write notifications are registered, we considers we want
Willy Tarreau3367d412018-11-15 10:57:41 +01001644 * to write, so we clear the blocking flag.
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001645 */
1646 if (notification_registered(&appctx->ctx.hlua_cosocket.wake_on_write))
Willy Tarreau3367d412018-11-15 10:57:41 +01001647 si_rx_endp_more(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001648}
1649
Willy Tarreau87b09662015-04-03 00:22:06 +02001650/* This function is called when the "struct stream" is destroyed.
1651 * Remove the link from the object to this stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001652 * Wake all the pending signals.
1653 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001654static void hlua_socket_release(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001655{
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001656 struct xref *peer;
1657
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001658 /* Remove my link in the original object. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001659 peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref);
1660 if (peer)
1661 xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001662
1663 /* Wake all the task waiting for me. */
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001664 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1665 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001666}
1667
1668/* If the garbage collectio of the object is launch, nobody
Willy Tarreau87b09662015-04-03 00:22:06 +02001669 * uses this object. If the stream does not exists, just quit.
1670 * Send the shutdown signal to the stream. In some cases,
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001671 * pending signal can rest in the read and write lists. destroy
1672 * it.
1673 */
1674__LJMP static int hlua_socket_gc(lua_State *L)
1675{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001676 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001677 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001678 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001679
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001680 MAY_LJMP(check_args(L, 1, "__gc"));
1681
1682 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001683 peer = xref_get_peer_and_lock(&socket->xref);
1684 if (!peer)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001685 return 0;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001686 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001687
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001688 /* Set the flag which destroy the session. */
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001689 appctx->ctx.hlua_cosocket.die = 1;
1690 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001691
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001692 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001693 xref_disconnect(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001694 return 0;
1695}
1696
1697/* The close function send shutdown signal and break the
Willy Tarreau87b09662015-04-03 00:22:06 +02001698 * links between the stream and the object.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001699 */
sada05ed3302018-05-11 11:48:18 -07001700__LJMP static int hlua_socket_close_helper(lua_State *L)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001701{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001702 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001703 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001704 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001705
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001706 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001707
1708 /* Check if we run on the same thread than the xreator thread.
1709 * We cannot access to the socket if the thread is different.
1710 */
1711 if (socket->tid != tid)
1712 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1713
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001714 peer = xref_get_peer_and_lock(&socket->xref);
1715 if (!peer)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001716 return 0;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001717 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001718
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001719 /* Set the flag which destroy the session. */
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001720 appctx->ctx.hlua_cosocket.die = 1;
1721 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001722
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001723 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001724 xref_disconnect(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001725 return 0;
1726}
1727
sada05ed3302018-05-11 11:48:18 -07001728/* The close function calls close_helper.
1729 */
1730__LJMP static int hlua_socket_close(lua_State *L)
1731{
1732 MAY_LJMP(check_args(L, 1, "close"));
1733 return hlua_socket_close_helper(L);
1734}
1735
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001736/* This Lua function assumes that the stack contain three parameters.
1737 * 1 - USERDATA containing a struct socket
1738 * 2 - INTEGER with values of the macro defined below
1739 * If the integer is -1, we must read at most one line.
1740 * If the integer is -2, we ust read all the data until the
1741 * end of the stream.
1742 * If the integer is positive value, we must read a number of
1743 * bytes corresponding to this value.
1744 */
1745#define HLSR_READ_LINE (-1)
1746#define HLSR_READ_ALL (-2)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001747__LJMP static int hlua_socket_receive_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001748{
1749 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
1750 int wanted = lua_tointeger(L, 2);
1751 struct hlua *hlua = hlua_gethlua(L);
1752 struct appctx *appctx;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001753 size_t len;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001754 int nblk;
Willy Tarreau206ba832018-06-14 15:27:31 +02001755 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001756 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02001757 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001758 size_t len2;
Thierry FOURNIER00543922015-03-09 18:35:06 +01001759 int skip_at_end = 0;
Willy Tarreau81389672015-03-10 12:03:52 +01001760 struct channel *oc;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001761 struct stream_interface *si;
1762 struct stream *s;
1763 struct xref *peer;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001764 int missing_bytes;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001765
1766 /* Check if this lua stack is schedulable. */
1767 if (!hlua || !hlua->task)
1768 WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in "
1769 "'frontend', 'backend' or 'task'"));
1770
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001771 /* Check if we run on the same thread than the xreator thread.
1772 * We cannot access to the socket if the thread is different.
1773 */
1774 if (socket->tid != tid)
1775 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1776
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001777 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001778 peer = xref_get_peer_and_lock(&socket->xref);
1779 if (!peer)
1780 goto no_peer;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001781 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
1782 si = appctx->owner;
1783 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001784
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001785 oc = &s->res;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001786 if (wanted == HLSR_READ_LINE) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001787 /* Read line. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001788 nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001789 if (nblk < 0) /* Connection close. */
1790 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001791 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001792 goto connection_empty;
Thierry FOURNIER00543922015-03-09 18:35:06 +01001793
1794 /* remove final \r\n. */
1795 if (nblk == 1) {
1796 if (blk1[len1-1] == '\n') {
1797 len1--;
1798 skip_at_end++;
1799 if (blk1[len1-1] == '\r') {
1800 len1--;
1801 skip_at_end++;
1802 }
1803 }
1804 }
1805 else {
1806 if (blk2[len2-1] == '\n') {
1807 len2--;
1808 skip_at_end++;
1809 if (blk2[len2-1] == '\r') {
1810 len2--;
1811 skip_at_end++;
1812 }
1813 }
1814 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001815 }
1816
1817 else if (wanted == HLSR_READ_ALL) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001818 /* Read all the available data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001819 nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001820 if (nblk < 0) /* Connection close. */
1821 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001822 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001823 goto connection_empty;
1824 }
1825
1826 else {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001827 /* Read a block of data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001828 nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001829 if (nblk < 0) /* Connection close. */
1830 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001831 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001832 goto connection_empty;
1833
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001834 missing_bytes = wanted - socket->b.n;
1835 if (len1 > missing_bytes) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001836 nblk = 1;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001837 len1 = missing_bytes;
1838 } if (nblk == 2 && len1 + len2 > missing_bytes)
1839 len2 = missing_bytes - len1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001840 }
1841
1842 len = len1;
1843
1844 luaL_addlstring(&socket->b, blk1, len1);
1845 if (nblk == 2) {
1846 len += len2;
1847 luaL_addlstring(&socket->b, blk2, len2);
1848 }
1849
1850 /* Consume data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001851 co_skip(oc, len + skip_at_end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001852
1853 /* Don't wait anything. */
Thierry FOURNIER7e4ee472018-05-25 15:03:50 +02001854 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001855
1856 /* If the pattern reclaim to read all the data
1857 * in the connection, got out.
1858 */
1859 if (wanted == HLSR_READ_ALL)
1860 goto connection_empty;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001861 else if (wanted >= 0 && socket->b.n < wanted)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001862 goto connection_empty;
1863
1864 /* Return result. */
1865 luaL_pushresult(&socket->b);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001866 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001867 return 1;
1868
1869connection_closed:
1870
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001871 xref_unlock(&socket->xref, peer);
1872
1873no_peer:
1874
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001875 /* If the buffer containds data. */
1876 if (socket->b.n > 0) {
1877 luaL_pushresult(&socket->b);
1878 return 1;
1879 }
1880 lua_pushnil(L);
1881 lua_pushstring(L, "connection closed.");
1882 return 2;
1883
1884connection_empty:
1885
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001886 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) {
1887 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001888 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001889 }
1890 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02001891 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_receive_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001892 return 0;
1893}
1894
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001895/* This Lua function gets two parameters. The first one can be string
1896 * or a number. If the string is "*l", the user requires one line. If
1897 * the string is "*a", the user requires all the contents of the stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001898 * If the value is a number, the user require a number of bytes equal
1899 * to the value. The default value is "*l" (a line).
1900 *
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001901 * This parameter with a variable type is converted in integer. This
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001902 * integer takes this values:
1903 * -1 : read a line
1904 * -2 : read all the stream
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001905 * >0 : amount of bytes.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001906 *
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001907 * The second parameter is optional. It contains a string that must be
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001908 * concatenated with the read data.
1909 */
1910__LJMP static int hlua_socket_receive(struct lua_State *L)
1911{
1912 int wanted = HLSR_READ_LINE;
1913 const char *pattern;
1914 int type;
1915 char *error;
1916 size_t len;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001917 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001918
1919 if (lua_gettop(L) < 1 || lua_gettop(L) > 3)
1920 WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments."));
1921
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001922 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001923
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001924 /* Check if we run on the same thread than the xreator thread.
1925 * We cannot access to the socket if the thread is different.
1926 */
1927 if (socket->tid != tid)
1928 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1929
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001930 /* check for pattern. */
1931 if (lua_gettop(L) >= 2) {
1932 type = lua_type(L, 2);
1933 if (type == LUA_TSTRING) {
1934 pattern = lua_tostring(L, 2);
1935 if (strcmp(pattern, "*a") == 0)
1936 wanted = HLSR_READ_ALL;
1937 else if (strcmp(pattern, "*l") == 0)
1938 wanted = HLSR_READ_LINE;
1939 else {
1940 wanted = strtoll(pattern, &error, 10);
1941 if (*error != '\0')
1942 WILL_LJMP(luaL_error(L, "Unsupported pattern."));
1943 }
1944 }
1945 else if (type == LUA_TNUMBER) {
1946 wanted = lua_tointeger(L, 2);
1947 if (wanted < 0)
1948 WILL_LJMP(luaL_error(L, "Unsupported size."));
1949 }
1950 }
1951
1952 /* Set pattern. */
1953 lua_pushinteger(L, wanted);
Tim Duesterhusc6e377e2018-01-04 19:32:13 +01001954
1955 /* Check if we would replace the top by itself. */
1956 if (lua_gettop(L) != 2)
1957 lua_replace(L, 2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001958
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001959 /* init buffer, and fill it with prefix. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001960 luaL_buffinit(L, &socket->b);
1961
1962 /* Check prefix. */
1963 if (lua_gettop(L) >= 3) {
1964 if (lua_type(L, 3) != LUA_TSTRING)
1965 WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix"));
1966 pattern = lua_tolstring(L, 3, &len);
1967 luaL_addlstring(&socket->b, pattern, len);
1968 }
1969
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001970 return __LJMP(hlua_socket_receive_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001971}
1972
1973/* Write the Lua input string in the output buffer.
Mark Lakes22154b42018-01-29 14:38:40 -08001974 * This function returns a yield if no space is available.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001975 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001976static int hlua_socket_write_yield(struct lua_State *L,int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001977{
1978 struct hlua_socket *socket;
1979 struct hlua *hlua = hlua_gethlua(L);
1980 struct appctx *appctx;
1981 size_t buf_len;
1982 const char *buf;
1983 int len;
1984 int send_len;
1985 int sent;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001986 struct xref *peer;
1987 struct stream_interface *si;
1988 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001989
1990 /* Check if this lua stack is schedulable. */
1991 if (!hlua || !hlua->task)
1992 WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in "
1993 "'frontend', 'backend' or 'task'"));
1994
1995 /* Get object */
1996 socket = MAY_LJMP(hlua_checksocket(L, 1));
1997 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001998 sent = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001999
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002000 /* Check if we run on the same thread than the xreator thread.
2001 * We cannot access to the socket if the thread is different.
2002 */
2003 if (socket->tid != tid)
2004 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2005
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002006 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002007 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002008 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002009 lua_pushinteger(L, -1);
2010 return 1;
2011 }
2012 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2013 si = appctx->owner;
2014 s = si_strm(si);
2015
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002016 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002017 if (channel_output_closed(&s->req)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002018 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002019 lua_pushinteger(L, -1);
2020 return 1;
2021 }
2022
2023 /* Update the input buffer data. */
2024 buf += sent;
2025 send_len = buf_len - sent;
2026
2027 /* All the data are sent. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002028 if (sent >= buf_len) {
2029 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002030 return 1; /* Implicitly return the length sent. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002031 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002032
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002033 /* Check if the buffer is available because HAProxy doesn't allocate
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01002034 * the request buffer if its not required.
2035 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002036 if (s->req.buf.size == 0) {
Willy Tarreau581abd32018-10-25 10:21:41 +02002037 if (!si_alloc_ibuf(si, &appctx->buffer_wait))
Christopher Faulet33834b12016-12-19 09:29:06 +01002038 goto hlua_socket_write_yield_return;
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01002039 }
2040
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002041 /* Check for available space. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002042 len = b_room(&s->req.buf);
Christopher Faulet33834b12016-12-19 09:29:06 +01002043 if (len <= 0) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002044 goto hlua_socket_write_yield_return;
Christopher Faulet33834b12016-12-19 09:29:06 +01002045 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002046
2047 /* send data */
2048 if (len < send_len)
2049 send_len = len;
Thierry FOURNIER66b89192018-05-27 01:14:47 +02002050 len = ci_putblk(&s->req, buf, send_len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002051
2052 /* "Not enough space" (-1), "Buffer too little to contain
2053 * the data" (-2) are not expected because the available length
2054 * is tested.
2055 * Other unknown error are also not expected.
2056 */
2057 if (len <= 0) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01002058 if (len == -1)
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002059 s->req.flags |= CF_WAKE_WRITE;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002060
sada05ed3302018-05-11 11:48:18 -07002061 MAY_LJMP(hlua_socket_close_helper(L));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002062 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002063 lua_pushinteger(L, -1);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002064 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002065 return 1;
2066 }
2067
2068 /* update buffers. */
Thierry FOURNIER101b9762018-05-27 01:27:40 +02002069 appctx_wakeup(appctx);
Willy Tarreaude70fa12015-09-26 11:25:05 +02002070
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002071 s->req.rex = TICK_ETERNITY;
2072 s->res.wex = TICK_ETERNITY;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002073
2074 /* Update length sent. */
2075 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002076 lua_pushinteger(L, sent + len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002077
2078 /* All the data buffer is sent ? */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002079 if (sent + len >= buf_len) {
2080 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002081 return 1;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002082 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002083
2084hlua_socket_write_yield_return:
Thierry FOURNIERba42fcd2018-05-27 00:59:48 +02002085 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2086 xref_unlock(&socket->xref, peer);
2087 WILL_LJMP(luaL_error(L, "out of memory"));
2088 }
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002089 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002090 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_write_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002091 return 0;
2092}
2093
2094/* This function initiate the send of data. It just check the input
2095 * parameters and push an integer in the Lua stack that contain the
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002096 * amount of data written to the buffer. This is used by the function
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002097 * "hlua_socket_write_yield" that can yield.
2098 *
2099 * The Lua function gets between 3 and 4 parameters. The first one is
2100 * the associated object. The second is a string buffer. The third is
2101 * a facultative integer that represents where is the buffer position
2102 * of the start of the data that can send. The first byte is the
2103 * position "1". The default value is "1". The fourth argument is a
2104 * facultative integer that represents where is the buffer position
2105 * of the end of the data that can send. The default is the last byte.
2106 */
2107static int hlua_socket_send(struct lua_State *L)
2108{
2109 int i;
2110 int j;
2111 const char *buf;
2112 size_t buf_len;
2113
2114 /* Check number of arguments. */
2115 if (lua_gettop(L) < 2 || lua_gettop(L) > 4)
2116 WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments"));
2117
2118 /* Get the string. */
2119 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
2120
2121 /* Get and check j. */
2122 if (lua_gettop(L) == 4) {
2123 j = MAY_LJMP(luaL_checkinteger(L, 4));
2124 if (j < 0)
2125 j = buf_len + j + 1;
2126 if (j > buf_len)
2127 j = buf_len + 1;
2128 lua_pop(L, 1);
2129 }
2130 else
2131 j = buf_len;
2132
2133 /* Get and check i. */
2134 if (lua_gettop(L) == 3) {
2135 i = MAY_LJMP(luaL_checkinteger(L, 3));
2136 if (i < 0)
2137 i = buf_len + i + 1;
2138 if (i > buf_len)
2139 i = buf_len + 1;
2140 lua_pop(L, 1);
2141 } else
2142 i = 1;
2143
2144 /* Check bth i and j. */
2145 if (i > j) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002146 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002147 return 1;
2148 }
2149 if (i == 0 && j == 0) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002150 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002151 return 1;
2152 }
2153 if (i == 0)
2154 i = 1;
2155 if (j == 0)
2156 j = 1;
2157
2158 /* Pop the string. */
2159 lua_pop(L, 1);
2160
2161 /* Update the buffer length. */
2162 buf += i - 1;
2163 buf_len = j - i + 1;
2164 lua_pushlstring(L, buf, buf_len);
2165
2166 /* This unsigned is used to remember the amount of sent data. */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002167 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002168
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002169 return MAY_LJMP(hlua_socket_write_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002170}
2171
Willy Tarreau22b0a682015-06-17 19:43:49 +02002172#define SOCKET_INFO_MAX_LEN sizeof("[0000:0000:0000:0000:0000:0000:0000:0000]:12345")
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002173__LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr)
2174{
2175 static char buffer[SOCKET_INFO_MAX_LEN];
2176 int ret;
2177 int len;
2178 char *p;
2179
2180 ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1);
2181 if (ret <= 0) {
2182 lua_pushnil(L);
2183 return 1;
2184 }
2185
2186 if (ret == AF_UNIX) {
2187 lua_pushstring(L, buffer+1);
2188 return 1;
2189 }
2190 else if (ret == AF_INET6) {
2191 buffer[0] = '[';
2192 len = strlen(buffer);
2193 buffer[len] = ']';
2194 len++;
2195 buffer[len] = ':';
2196 len++;
2197 p = buffer;
2198 }
2199 else if (ret == AF_INET) {
2200 p = buffer + 1;
2201 len = strlen(p);
2202 p[len] = ':';
2203 len++;
2204 }
2205 else {
2206 lua_pushnil(L);
2207 return 1;
2208 }
2209
2210 if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) {
2211 lua_pushnil(L);
2212 return 1;
2213 }
2214
2215 lua_pushstring(L, p);
2216 return 1;
2217}
2218
2219/* Returns information about the peer of the connection. */
2220__LJMP static int hlua_socket_getpeername(struct lua_State *L)
2221{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002222 struct hlua_socket *socket;
2223 struct connection *conn;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002224 struct xref *peer;
2225 struct appctx *appctx;
2226 struct stream_interface *si;
2227 struct stream *s;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002228 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002229
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002230 MAY_LJMP(check_args(L, 1, "getpeername"));
2231
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002232 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002233
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002234 /* Check if we run on the same thread than the xreator thread.
2235 * We cannot access to the socket if the thread is different.
2236 */
2237 if (socket->tid != tid)
2238 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2239
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002240 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002241 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002242 if (!peer) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002243 lua_pushnil(L);
2244 return 1;
2245 }
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002246 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2247 si = appctx->owner;
2248 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002249
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002250 conn = cs_conn(objt_cs(s->si[1].end));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002251 if (!conn) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002252 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002253 lua_pushnil(L);
2254 return 1;
2255 }
2256
Willy Tarreaua71f6422016-11-16 17:00:14 +01002257 conn_get_to_addr(conn);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002258 if (!(conn->flags & CO_FL_ADDR_TO_SET)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002259 xref_unlock(&socket->xref, peer);
Willy Tarreaua71f6422016-11-16 17:00:14 +01002260 lua_pushnil(L);
2261 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002262 }
2263
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002264 ret = MAY_LJMP(hlua_socket_info(L, &conn->addr.to));
2265 xref_unlock(&socket->xref, peer);
2266 return ret;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002267}
2268
2269/* Returns information about my connection side. */
2270static int hlua_socket_getsockname(struct lua_State *L)
2271{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002272 struct hlua_socket *socket;
2273 struct connection *conn;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002274 struct appctx *appctx;
2275 struct xref *peer;
2276 struct stream_interface *si;
2277 struct stream *s;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002278 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002279
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002280 MAY_LJMP(check_args(L, 1, "getsockname"));
2281
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002282 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002283
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002284 /* Check if we run on the same thread than the xreator thread.
2285 * We cannot access to the socket if the thread is different.
2286 */
2287 if (socket->tid != tid)
2288 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2289
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002290 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002291 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002292 if (!peer) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002293 lua_pushnil(L);
2294 return 1;
2295 }
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002296 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2297 si = appctx->owner;
2298 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002299
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002300 conn = cs_conn(objt_cs(s->si[1].end));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002301 if (!conn) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002302 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002303 lua_pushnil(L);
2304 return 1;
2305 }
2306
Willy Tarreaua71f6422016-11-16 17:00:14 +01002307 conn_get_from_addr(conn);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002308 if (!(conn->flags & CO_FL_ADDR_FROM_SET)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002309 xref_unlock(&socket->xref, peer);
Willy Tarreaua71f6422016-11-16 17:00:14 +01002310 lua_pushnil(L);
2311 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002312 }
2313
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002314 ret = hlua_socket_info(L, &conn->addr.from);
2315 xref_unlock(&socket->xref, peer);
2316 return ret;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002317}
2318
2319/* This struct define the applet. */
Willy Tarreau30576452015-04-13 13:50:30 +02002320static struct applet update_applet = {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002321 .obj_type = OBJ_TYPE_APPLET,
2322 .name = "<LUA_TCP>",
2323 .fct = hlua_socket_handler,
2324 .release = hlua_socket_release,
2325};
2326
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002327__LJMP static int hlua_socket_connect_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002328{
2329 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
2330 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002331 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002332 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002333 struct stream_interface *si;
2334 struct stream *s;
2335
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002336 /* Check if we run on the same thread than the xreator thread.
2337 * We cannot access to the socket if the thread is different.
2338 */
2339 if (socket->tid != tid)
2340 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2341
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002342 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002343 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002344 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002345 lua_pushnil(L);
2346 lua_pushstring(L, "Can't connect");
2347 return 2;
2348 }
2349 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2350 si = appctx->owner;
2351 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002352
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002353 /* Check if we run on the same thread than the xreator thread.
2354 * We cannot access to the socket if the thread is different.
2355 */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002356 if (socket->tid != tid) {
2357 xref_unlock(&socket->xref, peer);
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002358 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002359 }
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002360
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002361 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002362 if (!hlua || channel_output_closed(&s->req)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002363 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002364 lua_pushnil(L);
2365 lua_pushstring(L, "Can't connect");
2366 return 2;
2367 }
2368
Willy Tarreaue09101e2018-10-16 17:37:12 +02002369 appctx = __objt_appctx(s->si[0].end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002370
2371 /* Check for connection established. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002372 if (appctx->ctx.hlua_cosocket.connected) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002373 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002374 lua_pushinteger(L, 1);
2375 return 1;
2376 }
2377
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002378 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2379 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002380 WILL_LJMP(luaL_error(L, "out of memory error"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002381 }
2382 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002383 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002384 return 0;
2385}
2386
2387/* This function fail or initite the connection. */
2388__LJMP static int hlua_socket_connect(struct lua_State *L)
2389{
2390 struct hlua_socket *socket;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002391 int port = -1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002392 const char *ip;
2393 struct connection *conn;
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002394 struct hlua *hlua;
2395 struct appctx *appctx;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002396 int low, high;
2397 struct sockaddr_storage *addr;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002398 struct xref *peer;
2399 struct stream_interface *si;
2400 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002401
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002402 if (lua_gettop(L) < 2)
2403 WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments"));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002404
2405 /* Get args. */
2406 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002407
2408 /* Check if we run on the same thread than the xreator thread.
2409 * We cannot access to the socket if the thread is different.
2410 */
2411 if (socket->tid != tid)
2412 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2413
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002414 ip = MAY_LJMP(luaL_checkstring(L, 2));
Tim Duesterhus6edab862018-01-06 19:04:45 +01002415 if (lua_gettop(L) >= 3) {
2416 luaL_Buffer b;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002417 port = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002418
Tim Duesterhus6edab862018-01-06 19:04:45 +01002419 /* Force the ip to end with a colon, to support IPv6 addresses
2420 * that are not enclosed within square brackets.
2421 */
2422 if (port > 0) {
2423 luaL_buffinit(L, &b);
2424 luaL_addstring(&b, ip);
2425 luaL_addchar(&b, ':');
2426 luaL_pushresult(&b);
2427 ip = lua_tolstring(L, lua_gettop(L), NULL);
2428 }
2429 }
2430
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002431 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002432 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002433 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002434 lua_pushnil(L);
2435 return 1;
2436 }
2437 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2438 si = appctx->owner;
2439 s = si_strm(si);
2440
2441 /* Initialise connection. */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002442 conn = cs_conn(si_alloc_cs(&s->si[1], NULL));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002443 if (!conn) {
2444 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002445 WILL_LJMP(luaL_error(L, "connect: internal error"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002446 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002447
Willy Tarreau3adac082015-09-26 17:51:09 +02002448 /* needed for the connection not to be closed */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002449 conn->target = s->target;
Willy Tarreau3adac082015-09-26 17:51:09 +02002450
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002451 /* Parse ip address. */
Willy Tarreau48ef4c92017-01-06 18:32:38 +01002452 addr = str2sa_range(ip, NULL, &low, &high, NULL, NULL, NULL, 0);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002453 if (!addr) {
2454 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002455 WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002456 }
2457 if (low != high) {
2458 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002459 WILL_LJMP(luaL_error(L, "connect: port ranges not supported : address '%s'", ip));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002460 }
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002461 memcpy(&conn->addr.to, addr, sizeof(struct sockaddr_storage));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002462
2463 /* Set port. */
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002464 if (low == 0) {
2465 if (conn->addr.to.ss_family == AF_INET) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002466 if (port == -1) {
2467 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002468 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002469 }
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002470 ((struct sockaddr_in *)&conn->addr.to)->sin_port = htons(port);
2471 } else if (conn->addr.to.ss_family == AF_INET6) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002472 if (port == -1) {
2473 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002474 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002475 }
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002476 ((struct sockaddr_in6 *)&conn->addr.to)->sin6_port = htons(port);
2477 }
2478 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002479
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002480 hlua = hlua_gethlua(L);
Willy Tarreaue09101e2018-10-16 17:37:12 +02002481 appctx = __objt_appctx(s->si[0].end);
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002482
2483 /* inform the stream that we want to be notified whenever the
2484 * connection completes.
2485 */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01002486 si_cant_get(&s->si[0]);
Willy Tarreau3367d412018-11-15 10:57:41 +01002487 si_rx_endp_more(&s->si[0]);
Thierry FOURNIER8c8fbbe2015-09-26 17:02:35 +02002488 appctx_wakeup(appctx);
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002489
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +02002490 hlua->flags |= HLUA_MUST_GC;
2491
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002492 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2493 xref_unlock(&socket->xref, peer);
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002494 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002495 }
2496 xref_unlock(&socket->xref, peer);
PiBa-NL706d5ee2018-05-05 23:51:42 +02002497
2498 task_wakeup(s->task, TASK_WOKEN_INIT);
2499 /* Return yield waiting for connection. */
2500
Willy Tarreau9635e032018-10-16 17:52:55 +02002501 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002502
2503 return 0;
2504}
2505
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002506#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002507__LJMP static int hlua_socket_connect_ssl(struct lua_State *L)
2508{
2509 struct hlua_socket *socket;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002510 struct xref *peer;
2511 struct appctx *appctx;
2512 struct stream_interface *si;
2513 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002514
2515 MAY_LJMP(check_args(L, 3, "connect_ssl"));
2516 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002517
2518 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002519 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002520 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002521 lua_pushnil(L);
2522 return 1;
2523 }
2524 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2525 si = appctx->owner;
2526 s = si_strm(si);
2527
2528 s->target = &socket_ssl.obj_type;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002529 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002530 return MAY_LJMP(hlua_socket_connect(L));
2531}
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002532#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002533
2534__LJMP static int hlua_socket_setoption(struct lua_State *L)
2535{
2536 return 0;
2537}
2538
2539__LJMP static int hlua_socket_settimeout(struct lua_State *L)
2540{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002541 struct hlua_socket *socket;
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002542 int tmout;
Mark Lakes56cc1252018-03-27 09:48:06 +02002543 double dtmout;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002544 struct xref *peer;
2545 struct appctx *appctx;
2546 struct stream_interface *si;
2547 struct stream *s;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002548
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002549 MAY_LJMP(check_args(L, 2, "settimeout"));
2550
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002551 socket = MAY_LJMP(hlua_checksocket(L, 1));
Mark Lakes56cc1252018-03-27 09:48:06 +02002552
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002553 /* convert the timeout to millis */
2554 dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002555
Thierry Fournier17a921b2018-03-08 09:59:02 +01002556 /* Check for negative values */
Mark Lakes56cc1252018-03-27 09:48:06 +02002557 if (dtmout < 0)
Thierry Fournier17a921b2018-03-08 09:59:02 +01002558 WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values"));
2559
Mark Lakes56cc1252018-03-27 09:48:06 +02002560 if (dtmout > INT_MAX) /* overflow check */
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002561 WILL_LJMP(luaL_error(L, "settimeout: cannot set values larger than %d ms", INT_MAX));
Mark Lakes56cc1252018-03-27 09:48:06 +02002562
2563 tmout = MS_TO_TICKS((int)dtmout);
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002564 if (tmout == 0)
2565 tmout++; /* very small timeouts are adjusted to a minium of 1ms */
Mark Lakes56cc1252018-03-27 09:48:06 +02002566
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002567 /* Check if we run on the same thread than the xreator thread.
2568 * We cannot access to the socket if the thread is different.
2569 */
2570 if (socket->tid != tid)
2571 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2572
Mark Lakes56cc1252018-03-27 09:48:06 +02002573 /* check for connection break. If some data were read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002574 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002575 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002576 hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts.");
2577 WILL_LJMP(lua_error(L));
2578 return 0;
2579 }
2580 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2581 si = appctx->owner;
2582 s = si_strm(si);
2583
Cyril Bonté7bb63452018-08-17 23:51:02 +02002584 s->sess->fe->timeout.connect = tmout;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002585 s->req.rto = tmout;
2586 s->req.wto = tmout;
2587 s->res.rto = tmout;
2588 s->res.wto = tmout;
Cyril Bonté7bb63452018-08-17 23:51:02 +02002589 s->req.rex = tick_add_ifset(now_ms, tmout);
2590 s->req.wex = tick_add_ifset(now_ms, tmout);
2591 s->res.rex = tick_add_ifset(now_ms, tmout);
2592 s->res.wex = tick_add_ifset(now_ms, tmout);
2593
2594 s->task->expire = tick_add_ifset(now_ms, tmout);
2595 task_queue(s->task);
2596
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002597 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002598
Thierry Fourniere9636f12018-03-08 09:54:32 +01002599 lua_pushinteger(L, 1);
Tim Duesterhus119a5f12018-01-06 19:16:25 +01002600 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002601}
2602
2603__LJMP static int hlua_socket_new(lua_State *L)
2604{
2605 struct hlua_socket *socket;
2606 struct appctx *appctx;
Willy Tarreau15b5e142015-04-04 14:38:25 +02002607 struct session *sess;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002608 struct stream *strm;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002609
2610 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002611 if (!lua_checkstack(L, 3)) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002612 hlua_pusherror(L, "socket: full stack");
2613 goto out_fail_conf;
2614 }
2615
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002616 /* Create the object: obj[0] = userdata. */
2617 lua_newtable(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002618 socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket)));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002619 lua_rawseti(L, -2, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002620 memset(socket, 0, sizeof(*socket));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002621 socket->tid = tid;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002622
Thierry FOURNIER4a6170c2015-03-09 17:07:10 +01002623 /* Check if the various memory pools are intialized. */
Willy Tarreaubafbe012017-11-24 17:34:44 +01002624 if (!pool_head_stream || !pool_head_buffer) {
Thierry FOURNIER4a6170c2015-03-09 17:07:10 +01002625 hlua_pusherror(L, "socket: uninitialized pools.");
2626 goto out_fail_conf;
2627 }
2628
Willy Tarreau87b09662015-04-03 00:22:06 +02002629 /* Pop a class stream metatable and affect it to the userdata. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002630 lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref);
2631 lua_setmetatable(L, -2);
2632
Willy Tarreaud420a972015-04-06 00:39:18 +02002633 /* Create the applet context */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01002634 appctx = appctx_new(&update_applet, tid_bit);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002635 if (!appctx) {
2636 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaufeb76402015-04-03 14:10:06 +02002637 goto out_fail_conf;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002638 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002639
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002640 appctx->ctx.hlua_cosocket.connected = 0;
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02002641 appctx->ctx.hlua_cosocket.die = 0;
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002642 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write);
2643 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read);
Willy Tarreaub2bf8332015-04-04 15:58:58 +02002644
Willy Tarreaud420a972015-04-06 00:39:18 +02002645 /* Now create a session, task and stream for this applet */
2646 sess = session_new(&socket_proxy, NULL, &appctx->obj_type);
2647 if (!sess) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002648 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002649 goto out_fail_sess;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002650 }
2651
Willy Tarreau87787ac2017-08-28 16:22:54 +02002652 strm = stream_new(sess, &appctx->obj_type);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002653 if (!strm) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002654 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002655 goto out_fail_stream;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002656 }
2657
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002658 /* Initialise cross reference between stream and Lua socket object. */
2659 xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002660
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002661 /* Configure "right" stream interface. this "si" is used to connect
2662 * and retrieve data from the server. The connection is initialized
2663 * with the "struct server".
2664 */
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002665 si_set_state(&strm->si[1], SI_ST_ASS);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002666
2667 /* Force destination server. */
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002668 strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET | SF_BE_ASSIGNED;
2669 strm->target = &socket_tcp.obj_type;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002670
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002671 return 1;
2672
Willy Tarreaud420a972015-04-06 00:39:18 +02002673 out_fail_stream:
Willy Tarreau11c36242015-04-04 15:54:03 +02002674 session_free(sess);
Willy Tarreaud420a972015-04-06 00:39:18 +02002675 out_fail_sess:
2676 appctx_free(appctx);
2677 out_fail_conf:
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002678 WILL_LJMP(lua_error(L));
2679 return 0;
2680}
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01002681
2682/*
2683 *
2684 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002685 * Class Channel
2686 *
2687 *
2688 */
2689
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002690/* This function is called before the Lua execution. It stores
2691 * the differents parsers state before executing some Lua code.
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002692 */
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002693static inline void consistency_set(struct stream *stream, int opt, struct hlua_consistency *c)
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002694{
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002695 c->mode = stream->be->mode;
2696 switch (c->mode) {
2697 case PR_MODE_HTTP:
2698 c->data.http.dir = opt & SMP_OPT_DIR;
2699 if (c->data.http.dir == SMP_OPT_DIR_REQ)
2700 c->data.http.state = stream->txn->req.msg_state;
2701 else
2702 c->data.http.state = stream->txn->rsp.msg_state;
2703 break;
2704 default:
2705 break;
2706 }
2707}
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002708
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002709/* This function is called after the Lua execution. it
2710 * returns true if the parser state is consistent, otherwise,
2711 * it return false.
2712 *
2713 * In HTTP mode, the parser state must be in the same state
2714 * or greater when we exit the function. Even if we do a
2715 * control yield. This prevent to break the HTTP message
2716 * from the Lua code.
2717 */
2718static inline int consistency_check(struct stream *stream, int opt, struct hlua_consistency *c)
2719{
2720 if (c->mode != stream->be->mode)
2721 return 0;
2722
2723 switch (c->mode) {
2724 case PR_MODE_HTTP:
2725 if (c->data.http.dir != (opt & SMP_OPT_DIR))
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002726 return 0;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002727 if (c->data.http.dir == SMP_OPT_DIR_REQ)
2728 return stream->txn->req.msg_state >= c->data.http.state;
2729 else
2730 return stream->txn->rsp.msg_state >= c->data.http.state;
2731 default:
2732 return 1;
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002733 }
2734 return 1;
2735}
2736
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002737/* Returns the struct hlua_channel join to the class channel in the
2738 * stack entry "ud" or throws an argument error.
2739 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002740__LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002741{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02002742 return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002743}
2744
Willy Tarreau47860ed2015-03-10 14:07:50 +01002745/* Pushes the channel onto the top of the stack. If the stask does not have a
2746 * free slots, the function fails and returns 0;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002747 */
Willy Tarreau2a71af42015-03-10 13:51:50 +01002748static int hlua_channel_new(lua_State *L, struct channel *channel)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002749{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002750 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002751 if (!lua_checkstack(L, 3))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002752 return 0;
2753
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002754 lua_newtable(L);
Willy Tarreau47860ed2015-03-10 14:07:50 +01002755 lua_pushlightuserdata(L, channel);
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002756 lua_rawseti(L, -2, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002757
2758 /* Pop a class sesison metatable and affect it to the userdata. */
2759 lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref);
2760 lua_setmetatable(L, -2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002761 return 1;
2762}
2763
2764/* Duplicate all the data present in the input channel and put it
2765 * in a string LUA variables. Returns -1 and push a nil value in
2766 * the stack if the channel is closed and all the data are consumed,
2767 * returns 0 if no data are available, otherwise it returns the length
2768 * of the builded string.
2769 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002770static inline int _hlua_channel_dup(struct channel *chn, lua_State *L)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002771{
2772 char *blk1;
2773 char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02002774 size_t len1;
2775 size_t len2;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002776 int ret;
2777 luaL_Buffer b;
2778
Willy Tarreau06d80a92017-10-19 14:32:15 +02002779 ret = ci_getblk_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002780 if (unlikely(ret == 0))
2781 return 0;
2782
2783 if (unlikely(ret < 0)) {
2784 lua_pushnil(L);
2785 return -1;
2786 }
2787
2788 luaL_buffinit(L, &b);
2789 luaL_addlstring(&b, blk1, len1);
2790 if (unlikely(ret == 2))
2791 luaL_addlstring(&b, blk2, len2);
2792 luaL_pushresult(&b);
2793
2794 if (unlikely(ret == 2))
2795 return len1 + len2;
2796 return len1;
2797}
2798
2799/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2800 * a yield. This function keep the data in the buffer.
2801 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002802__LJMP static int hlua_channel_dup_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002803{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002804 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002805
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002806 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2807
Christopher Faulet3f829a42018-12-13 21:56:45 +01002808 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2809 WILL_LJMP(lua_error(L));
2810
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002811 if (_hlua_channel_dup(chn, L) == 0)
Willy Tarreau9635e032018-10-16 17:52:55 +02002812 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_dup_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002813 return 1;
2814}
2815
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002816/* Check arguments for the function "hlua_channel_dup_yield". */
2817__LJMP static int hlua_channel_dup(lua_State *L)
2818{
2819 MAY_LJMP(check_args(L, 1, "dup"));
2820 MAY_LJMP(hlua_checkchannel(L, 1));
2821 return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0));
2822}
2823
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002824/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2825 * a yield. This function consumes the data in the buffer. It returns
2826 * a string containing the data or a nil pointer if no data are available
2827 * and the channel is closed.
2828 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002829__LJMP static int hlua_channel_get_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002830{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002831 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002832 int ret;
2833
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002834 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002835
Christopher Faulet3f829a42018-12-13 21:56:45 +01002836 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2837 WILL_LJMP(lua_error(L));
2838
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002839 ret = _hlua_channel_dup(chn, L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002840 if (unlikely(ret == 0))
Willy Tarreau9635e032018-10-16 17:52:55 +02002841 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002842
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002843 if (unlikely(ret == -1))
2844 return 1;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002845
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002846 b_sub(&chn->buf, ret);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002847 return 1;
2848}
2849
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002850/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002851__LJMP static int hlua_channel_get(lua_State *L)
2852{
2853 MAY_LJMP(check_args(L, 1, "get"));
2854 MAY_LJMP(hlua_checkchannel(L, 1));
2855 return MAY_LJMP(hlua_channel_get_yield(L, 0, 0));
2856}
2857
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002858/* This functions consumes and returns one line. If the channel is closed,
2859 * and the last data does not contains a final '\n', the data are returned
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002860 * without the final '\n'. When no more data are available, it returns nil
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002861 * value.
2862 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002863__LJMP static int hlua_channel_getline_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002864{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002865 char *blk1;
2866 char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02002867 size_t len1;
2868 size_t len2;
2869 size_t len;
Willy Tarreau47860ed2015-03-10 14:07:50 +01002870 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002871 int ret;
2872 luaL_Buffer b;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002873
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002874 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2875
Christopher Faulet3f829a42018-12-13 21:56:45 +01002876 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2877 WILL_LJMP(lua_error(L));
2878
Willy Tarreau06d80a92017-10-19 14:32:15 +02002879 ret = ci_getline_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002880 if (ret == 0)
Willy Tarreau9635e032018-10-16 17:52:55 +02002881 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_getline_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002882
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002883 if (ret == -1) {
2884 lua_pushnil(L);
2885 return 1;
2886 }
2887
2888 luaL_buffinit(L, &b);
2889 luaL_addlstring(&b, blk1, len1);
2890 len = len1;
2891 if (unlikely(ret == 2)) {
2892 luaL_addlstring(&b, blk2, len2);
2893 len += len2;
2894 }
2895 luaL_pushresult(&b);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002896 b_rep_blk(&chn->buf, ci_head(chn), ci_head(chn) + len, NULL, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002897 return 1;
2898}
2899
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002900/* Check arguments for the function "hlua_channel_getline_yield". */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002901__LJMP static int hlua_channel_getline(lua_State *L)
2902{
2903 MAY_LJMP(check_args(L, 1, "getline"));
2904 MAY_LJMP(hlua_checkchannel(L, 1));
2905 return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0));
2906}
2907
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002908/* This function takes a string as input, and append it at the
2909 * input side of channel. If the data is too big, but a space
2910 * is probably available after sending some data, the function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002911 * yields. If the data is bigger than the buffer, or if the
2912 * channel is closed, it returns -1. Otherwise, it returns the
2913 * amount of data written.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002914 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002915__LJMP static int hlua_channel_append_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002916{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002917 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002918 size_t len;
2919 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
2920 int l = MAY_LJMP(luaL_checkinteger(L, 3));
2921 int ret;
2922 int max;
2923
Christopher Faulet3f829a42018-12-13 21:56:45 +01002924 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2925 WILL_LJMP(lua_error(L));
2926
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002927 /* Check if the buffer is available because HAProxy doesn't allocate
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002928 * the request buffer if its not required.
2929 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002930 if (chn->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +01002931 si_rx_buff_blk(chn_prod(chn));
Willy Tarreau9635e032018-10-16 17:52:55 +02002932 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002933 }
2934
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002935 max = channel_recv_limit(chn) - b_data(&chn->buf);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002936 if (max > len - l)
2937 max = len - l;
2938
Willy Tarreau06d80a92017-10-19 14:32:15 +02002939 ret = ci_putblk(chn, str + l, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002940 if (ret == -2 || ret == -3) {
2941 lua_pushinteger(L, -1);
2942 return 1;
2943 }
Willy Tarreaubc18da12015-03-13 14:00:47 +01002944 if (ret == -1) {
2945 chn->flags |= CF_WAKE_WRITE;
Willy Tarreau9635e032018-10-16 17:52:55 +02002946 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Willy Tarreaubc18da12015-03-13 14:00:47 +01002947 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002948 l += ret;
2949 lua_pop(L, 1);
2950 lua_pushinteger(L, l);
2951
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002952 max = channel_recv_limit(chn) - b_data(&chn->buf);
Willy Tarreaua79021a2018-06-15 18:07:57 +02002953 if (max == 0 && co_data(chn) == 0) {
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002954 /* There are no space available, and the output buffer is empty.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002955 * in this case, we cannot add more data, so we cannot yield,
2956 * we return the amount of copyied data.
2957 */
2958 return 1;
2959 }
2960 if (l < len)
Willy Tarreau9635e032018-10-16 17:52:55 +02002961 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002962 return 1;
2963}
2964
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002965/* Just a wrapper of "hlua_channel_append_yield". It returns the length
2966 * of the written string, or -1 if the channel is closed or if the
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002967 * buffer size is too little for the data.
2968 */
2969__LJMP static int hlua_channel_append(lua_State *L)
2970{
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002971 size_t len;
2972
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002973 MAY_LJMP(check_args(L, 2, "append"));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002974 MAY_LJMP(hlua_checkchannel(L, 1));
2975 MAY_LJMP(luaL_checklstring(L, 2, &len));
2976 MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002977 lua_pushinteger(L, 0);
2978
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002979 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002980}
2981
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002982/* Just a wrapper of "hlua_channel_append_yield". This wrapper starts
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002983 * his process by cleaning the buffer. The result is a replacement
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002984 * of the current data. It returns the length of the written string,
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002985 * or -1 if the channel is closed or if the buffer size is too
2986 * little for the data.
2987 */
2988__LJMP static int hlua_channel_set(lua_State *L)
2989{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002990 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002991
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002992 MAY_LJMP(check_args(L, 2, "set"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002993 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002994 lua_pushinteger(L, 0);
2995
Christopher Faulet3f829a42018-12-13 21:56:45 +01002996 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2997 WILL_LJMP(lua_error(L));
2998
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002999 b_set_data(&chn->buf, co_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003000
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003001 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003002}
3003
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003004/* Append data in the output side of the buffer. This data is immediately
3005 * sent. The function returns the amount of data written. If the buffer
3006 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003007 * if the channel is closed.
3008 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003009__LJMP static int hlua_channel_send_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003010{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003011 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003012 size_t len;
3013 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
3014 int l = MAY_LJMP(luaL_checkinteger(L, 3));
3015 int max;
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003016 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003017
Christopher Faulet3f829a42018-12-13 21:56:45 +01003018 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
3019 WILL_LJMP(lua_error(L));
3020
Willy Tarreau47860ed2015-03-10 14:07:50 +01003021 if (unlikely(channel_output_closed(chn))) {
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003022 lua_pushinteger(L, -1);
3023 return 1;
3024 }
3025
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003026 /* Check if the buffer is available because HAProxy doesn't allocate
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01003027 * the request buffer if its not required.
3028 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003029 if (chn->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +01003030 si_rx_buff_blk(chn_prod(chn));
Willy Tarreau9635e032018-10-16 17:52:55 +02003031 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01003032 }
3033
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003034 /* The written data will be immediately sent, so we can check
3035 * the available space without taking in account the reserve.
3036 * The reserve is guaranteed for the processing of incoming
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003037 * data, because the buffer will be flushed.
3038 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003039 max = b_room(&chn->buf);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003040
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003041 /* If there is no space available, and the output buffer is empty.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003042 * in this case, we cannot add more data, so we cannot yield,
3043 * we return the amount of copyied data.
3044 */
Willy Tarreaua79021a2018-06-15 18:07:57 +02003045 if (max == 0 && co_data(chn) == 0)
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003046 return 1;
3047
3048 /* Adjust the real required length. */
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003049 if (max > len - l)
3050 max = len - l;
3051
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003052 /* The buffer available size may be not contiguous. This test
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003053 * detects a non contiguous buffer and realign it.
3054 */
Willy Tarreau3f679992018-06-15 15:06:42 +02003055 if (ci_space_for_replace(chn) < max)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003056 channel_slow_realign(chn, trash.area);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003057
3058 /* Copy input data in the buffer. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003059 max = b_rep_blk(&chn->buf, ci_head(chn), ci_head(chn), str + l, max);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003060
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003061 /* buffer replace considers that the input part is filled.
3062 * so, I must forward these new data in the output part.
3063 */
Willy Tarreaubcbd3932018-06-06 07:13:22 +02003064 c_adv(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003065
3066 l += max;
3067 lua_pop(L, 1);
3068 lua_pushinteger(L, l);
3069
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003070 /* If there is no space available, and the output buffer is empty.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003071 * in this case, we cannot add more data, so we cannot yield,
3072 * we return the amount of copyied data.
3073 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003074 max = b_room(&chn->buf);
Willy Tarreaua79021a2018-06-15 18:07:57 +02003075 if (max == 0 && co_data(chn) == 0)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003076 return 1;
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003077
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003078 if (l < len) {
3079 /* If we are waiting for space in the response buffer, we
3080 * must set the flag WAKERESWR. This flag required the task
3081 * wake up if any activity is detected on the response buffer.
3082 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003083 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003084 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01003085 else
3086 HLUA_SET_WAKEREQWR(hlua);
Willy Tarreau9635e032018-10-16 17:52:55 +02003087 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003088 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003089
3090 return 1;
3091}
3092
3093/* Just a wraper of "_hlua_channel_send". This wrapper permits
3094 * yield the LUA process, and resume it without checking the
3095 * input arguments.
3096 */
3097__LJMP static int hlua_channel_send(lua_State *L)
3098{
3099 MAY_LJMP(check_args(L, 2, "send"));
3100 lua_pushinteger(L, 0);
3101
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003102 return MAY_LJMP(hlua_channel_send_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003103}
3104
3105/* This function forward and amount of butes. The data pass from
3106 * the input side of the buffer to the output side, and can be
3107 * forwarded. This function never fails.
3108 *
3109 * The Lua function takes an amount of bytes to be forwarded in
3110 * imput. It returns the number of bytes forwarded.
3111 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003112__LJMP static int hlua_channel_forward_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003113{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003114 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003115 int len;
3116 int l;
3117 int max;
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003118 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003119
3120 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Faulet3f829a42018-12-13 21:56:45 +01003121
3122 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
3123 WILL_LJMP(lua_error(L));
3124
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003125 len = MAY_LJMP(luaL_checkinteger(L, 2));
3126 l = MAY_LJMP(luaL_checkinteger(L, -1));
3127
3128 max = len - l;
Willy Tarreaua79021a2018-06-15 18:07:57 +02003129 if (max > ci_data(chn))
3130 max = ci_data(chn);
Willy Tarreau47860ed2015-03-10 14:07:50 +01003131 channel_forward(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003132 l += max;
3133
3134 lua_pop(L, 1);
3135 lua_pushinteger(L, l);
3136
3137 /* Check if it miss bytes to forward. */
3138 if (l < len) {
3139 /* The the input channel or the output channel are closed, we
3140 * must return the amount of data forwarded.
3141 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003142 if (channel_input_closed(chn) || channel_output_closed(chn))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003143 return 1;
3144
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003145 /* If we are waiting for space data in the response buffer, we
3146 * must set the flag WAKERESWR. This flag required the task
3147 * wake up if any activity is detected on the response buffer.
3148 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003149 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003150 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01003151 else
3152 HLUA_SET_WAKEREQWR(hlua);
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003153
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003154 /* Otherwise, we can yield waiting for new data in the inpout side. */
Willy Tarreau9635e032018-10-16 17:52:55 +02003155 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_forward_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003156 }
3157
3158 return 1;
3159}
3160
3161/* Just check the input and prepare the stack for the previous
3162 * function "hlua_channel_forward_yield"
3163 */
3164__LJMP static int hlua_channel_forward(lua_State *L)
3165{
3166 MAY_LJMP(check_args(L, 2, "forward"));
3167 MAY_LJMP(hlua_checkchannel(L, 1));
3168 MAY_LJMP(luaL_checkinteger(L, 2));
3169
3170 lua_pushinteger(L, 0);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003171 return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003172}
3173
3174/* Just returns the number of bytes available in the input
3175 * side of the buffer. This function never fails.
3176 */
3177__LJMP static int hlua_channel_get_in_len(lua_State *L)
3178{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003179 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003180
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003181 MAY_LJMP(check_args(L, 1, "get_in_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003182 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Willy Tarreaua79021a2018-06-15 18:07:57 +02003183 lua_pushinteger(L, ci_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003184 return 1;
3185}
3186
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003187/* Returns true if the channel is full. */
3188__LJMP static int hlua_channel_is_full(lua_State *L)
3189{
3190 struct channel *chn;
3191 int rem;
3192
3193 MAY_LJMP(check_args(L, 1, "is_full"));
3194 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3195
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003196 rem = b_room(&chn->buf);
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003197 rem -= global.tune.maxrewrite; /* Rewrite reserved size */
3198
3199 lua_pushboolean(L, rem <= 0);
3200 return 1;
3201}
3202
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003203/* Just returns the number of bytes available in the output
3204 * side of the buffer. This function never fails.
3205 */
3206__LJMP static int hlua_channel_get_out_len(lua_State *L)
3207{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003208 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003209
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003210 MAY_LJMP(check_args(L, 1, "get_out_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003211 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Willy Tarreaua79021a2018-06-15 18:07:57 +02003212 lua_pushinteger(L, co_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003213 return 1;
3214}
3215
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003216/*
3217 *
3218 *
3219 * Class Fetches
3220 *
3221 *
3222 */
3223
3224/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003225 * a class stream, otherwise it throws an error.
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003226 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003227__LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003228{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003229 return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003230}
3231
3232/* This function creates and push in the stack a fetch object according
3233 * with a current TXN.
3234 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003235static int hlua_fetches_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003236{
Willy Tarreau7073c472015-04-06 11:15:40 +02003237 struct hlua_smp *hsmp;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003238
3239 /* Check stack size. */
3240 if (!lua_checkstack(L, 3))
3241 return 0;
3242
3243 /* Create the object: obj[0] = userdata.
3244 * Note that the base of the Fetches object is the
3245 * transaction object.
3246 */
3247 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003248 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003249 lua_rawseti(L, -2, 0);
3250
Willy Tarreau7073c472015-04-06 11:15:40 +02003251 hsmp->s = txn->s;
3252 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003253 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003254 hsmp->flags = flags;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003255
3256 /* Pop a class sesison metatable and affect it to the userdata. */
3257 lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref);
3258 lua_setmetatable(L, -2);
3259
3260 return 1;
3261}
3262
3263/* This function is an LUA binding. It is called with each sample-fetch.
3264 * It uses closure argument to store the associated sample-fetch. It
3265 * returns only one argument or throws an error. An error is thrown
3266 * only if an error is encountered during the argument parsing. If
3267 * the "sample-fetch" function fails, nil is returned.
3268 */
3269__LJMP static int hlua_run_sample_fetch(lua_State *L)
3270{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003271 struct hlua_smp *hsmp;
Willy Tarreau2ec22742015-03-10 14:27:20 +01003272 struct sample_fetch *f;
Frédéric Lécaillef874a832018-06-15 13:56:04 +02003273 struct arg args[ARGM_NBARGS + 1] = {{0}};
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003274 int i;
3275 struct sample smp;
3276
3277 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003278 f = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003279
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003280 /* Get traditional arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003281 hsmp = MAY_LJMP(hlua_checkfetches(L, 1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003282
Thierry FOURNIERca988662015-12-20 18:43:03 +01003283 /* Check execution authorization. */
3284 if (f->use & SMP_USE_HTTP_ANY &&
3285 !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) {
3286 lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which "
3287 "is not available in Lua services", f->kw);
3288 WILL_LJMP(lua_error(L));
3289 }
3290
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003291 /* Get extra arguments. */
3292 for (i = 0; i < lua_gettop(L) - 1; i++) {
3293 if (i >= ARGM_NBARGS)
3294 break;
3295 hlua_lua2arg(L, i + 2, &args[i]);
3296 }
3297 args[i].type = ARGT_STOP;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003298 args[i].data.str.area = NULL;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003299
3300 /* Check arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003301 MAY_LJMP(hlua_lua2arg_check(L, 2, args, f->arg_mask, hsmp->p));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003302
3303 /* Run the special args checker. */
Willy Tarreau2ec22742015-03-10 14:27:20 +01003304 if (f->val_args && !f->val_args(args, NULL)) {
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003305 lua_pushfstring(L, "error in arguments");
3306 WILL_LJMP(lua_error(L));
3307 }
3308
3309 /* Initialise the sample. */
3310 memset(&smp, 0, sizeof(smp));
3311
3312 /* Run the sample fetch process. */
Willy Tarreau1777ea62016-03-10 16:15:46 +01003313 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003314 if (!f->process(args, &smp, f->kw, f->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003315 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003316 lua_pushstring(L, "");
3317 else
3318 lua_pushnil(L);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003319 return 1;
3320 }
3321
3322 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003323 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003324 hlua_smp2lua_str(L, &smp);
3325 else
3326 hlua_smp2lua(L, &smp);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003327 return 1;
3328}
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003329
3330/*
3331 *
3332 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003333 * Class Converters
3334 *
3335 *
3336 */
3337
3338/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003339 * a class stream, otherwise it throws an error.
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003340 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003341__LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003342{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003343 return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003344}
3345
3346/* This function creates and push in the stack a Converters object
3347 * according with a current TXN.
3348 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003349static int hlua_converters_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003350{
Willy Tarreau7073c472015-04-06 11:15:40 +02003351 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003352
3353 /* Check stack size. */
3354 if (!lua_checkstack(L, 3))
3355 return 0;
3356
3357 /* Create the object: obj[0] = userdata.
3358 * Note that the base of the Converters object is the
3359 * same than the TXN object.
3360 */
3361 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003362 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003363 lua_rawseti(L, -2, 0);
3364
Willy Tarreau7073c472015-04-06 11:15:40 +02003365 hsmp->s = txn->s;
3366 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003367 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003368 hsmp->flags = flags;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003369
Willy Tarreau87b09662015-04-03 00:22:06 +02003370 /* Pop a class stream metatable and affect it to the table. */
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003371 lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref);
3372 lua_setmetatable(L, -2);
3373
3374 return 1;
3375}
3376
3377/* This function is an LUA binding. It is called with each converter.
3378 * It uses closure argument to store the associated converter. It
3379 * returns only one argument or throws an error. An error is thrown
3380 * only if an error is encountered during the argument parsing. If
3381 * the converter function function fails, nil is returned.
3382 */
3383__LJMP static int hlua_run_sample_conv(lua_State *L)
3384{
Willy Tarreauda5f1082015-04-06 11:17:13 +02003385 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003386 struct sample_conv *conv;
Frédéric Lécaillef874a832018-06-15 13:56:04 +02003387 struct arg args[ARGM_NBARGS + 1] = {{0}};
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003388 int i;
3389 struct sample smp;
3390
3391 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003392 conv = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003393
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003394 /* Get traditional arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003395 hsmp = MAY_LJMP(hlua_checkconverters(L, 1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003396
3397 /* Get extra arguments. */
3398 for (i = 0; i < lua_gettop(L) - 2; i++) {
3399 if (i >= ARGM_NBARGS)
3400 break;
3401 hlua_lua2arg(L, i + 3, &args[i]);
3402 }
3403 args[i].type = ARGT_STOP;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003404 args[i].data.str.area = NULL;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003405
3406 /* Check arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003407 MAY_LJMP(hlua_lua2arg_check(L, 3, args, conv->arg_mask, hsmp->p));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003408
3409 /* Run the special args checker. */
3410 if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) {
3411 hlua_pusherror(L, "error in arguments");
3412 WILL_LJMP(lua_error(L));
3413 }
3414
3415 /* Initialise the sample. */
3416 if (!hlua_lua2smp(L, 2, &smp)) {
3417 hlua_pusherror(L, "error in the input argument");
3418 WILL_LJMP(lua_error(L));
3419 }
3420
Willy Tarreau1777ea62016-03-10 16:15:46 +01003421 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
3422
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003423 /* Apply expected cast. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003424 if (!sample_casts[smp.data.type][conv->in_type]) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003425 hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'",
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003426 smp_to_type[smp.data.type], smp_to_type[conv->in_type]);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003427 WILL_LJMP(lua_error(L));
3428 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003429 if (sample_casts[smp.data.type][conv->in_type] != c_none &&
3430 !sample_casts[smp.data.type][conv->in_type](&smp)) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003431 hlua_pusherror(L, "error during the input argument casting");
3432 WILL_LJMP(lua_error(L));
3433 }
3434
3435 /* Run the sample conversion process. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003436 if (!conv->process(args, &smp, conv->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003437 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003438 lua_pushstring(L, "");
3439 else
Willy Tarreaua678b432015-08-28 10:14:59 +02003440 lua_pushnil(L);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003441 return 1;
3442 }
3443
3444 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003445 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003446 hlua_smp2lua_str(L, &smp);
3447 else
3448 hlua_smp2lua(L, &smp);
Willy Tarreaua678b432015-08-28 10:14:59 +02003449 return 1;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003450}
3451
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003452/*
3453 *
3454 *
3455 * Class AppletTCP
3456 *
3457 *
3458 */
3459
3460/* Returns a struct hlua_txn if the stack entry "ud" is
3461 * a class stream, otherwise it throws an error.
3462 */
3463__LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud)
3464{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003465 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003466}
3467
3468/* This function creates and push in the stack an Applet object
3469 * according with a current TXN.
3470 */
3471static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx)
3472{
3473 struct hlua_appctx *appctx;
3474 struct stream_interface *si = ctx->owner;
3475 struct stream *s = si_strm(si);
3476 struct proxy *p = s->be;
3477
3478 /* Check stack size. */
3479 if (!lua_checkstack(L, 3))
3480 return 0;
3481
3482 /* Create the object: obj[0] = userdata.
3483 * Note that the base of the Converters object is the
3484 * same than the TXN object.
3485 */
3486 lua_newtable(L);
3487 appctx = lua_newuserdata(L, sizeof(*appctx));
3488 lua_rawseti(L, -2, 0);
3489 appctx->appctx = ctx;
3490 appctx->htxn.s = s;
3491 appctx->htxn.p = p;
3492
3493 /* Create the "f" field that contains a list of fetches. */
3494 lua_pushstring(L, "f");
3495 if (!hlua_fetches_new(L, &appctx->htxn, 0))
3496 return 0;
3497 lua_settable(L, -3);
3498
3499 /* Create the "sf" field that contains a list of stringsafe fetches. */
3500 lua_pushstring(L, "sf");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003501 if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003502 return 0;
3503 lua_settable(L, -3);
3504
3505 /* Create the "c" field that contains a list of converters. */
3506 lua_pushstring(L, "c");
3507 if (!hlua_converters_new(L, &appctx->htxn, 0))
3508 return 0;
3509 lua_settable(L, -3);
3510
3511 /* Create the "sc" field that contains a list of stringsafe converters. */
3512 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003513 if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003514 return 0;
3515 lua_settable(L, -3);
3516
3517 /* Pop a class stream metatable and affect it to the table. */
3518 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref);
3519 lua_setmetatable(L, -2);
3520
3521 return 1;
3522}
3523
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003524__LJMP static int hlua_applet_tcp_set_var(lua_State *L)
3525{
3526 struct hlua_appctx *appctx;
3527 struct stream *s;
3528 const char *name;
3529 size_t len;
3530 struct sample smp;
3531
3532 MAY_LJMP(check_args(L, 3, "set_var"));
3533
3534 /* It is useles to retrieve the stream, but this function
3535 * runs only in a stream context.
3536 */
3537 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3538 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3539 s = appctx->htxn.s;
3540
3541 /* Converts the third argument in a sample. */
3542 hlua_lua2smp(L, 3, &smp);
3543
3544 /* Store the sample in a variable. */
3545 smp_set_owner(&smp, s->be, s->sess, s, 0);
3546 vars_set_by_name(name, len, &smp);
3547 return 0;
3548}
3549
3550__LJMP static int hlua_applet_tcp_unset_var(lua_State *L)
3551{
3552 struct hlua_appctx *appctx;
3553 struct stream *s;
3554 const char *name;
3555 size_t len;
3556 struct sample smp;
3557
3558 MAY_LJMP(check_args(L, 2, "unset_var"));
3559
3560 /* It is useles to retrieve the stream, but this function
3561 * runs only in a stream context.
3562 */
3563 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3564 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3565 s = appctx->htxn.s;
3566
3567 /* Unset the variable. */
3568 smp_set_owner(&smp, s->be, s->sess, s, 0);
3569 vars_unset_by_name(name, len, &smp);
3570 return 0;
3571}
3572
3573__LJMP static int hlua_applet_tcp_get_var(lua_State *L)
3574{
3575 struct hlua_appctx *appctx;
3576 struct stream *s;
3577 const char *name;
3578 size_t len;
3579 struct sample smp;
3580
3581 MAY_LJMP(check_args(L, 2, "get_var"));
3582
3583 /* It is useles to retrieve the stream, but this function
3584 * runs only in a stream context.
3585 */
3586 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3587 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3588 s = appctx->htxn.s;
3589
3590 smp_set_owner(&smp, s->be, s->sess, s, 0);
3591 if (!vars_get_by_name(name, len, &smp)) {
3592 lua_pushnil(L);
3593 return 1;
3594 }
3595
3596 return hlua_smp2lua(L, &smp);
3597}
3598
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003599__LJMP static int hlua_applet_tcp_set_priv(lua_State *L)
3600{
3601 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3602 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003603 struct hlua *hlua;
3604
3605 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003606 if (!s->hlua)
3607 return 0;
3608 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003609
3610 MAY_LJMP(check_args(L, 2, "set_priv"));
3611
3612 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02003613 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003614
3615 /* Get and store new value. */
3616 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
3617 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
3618
3619 return 0;
3620}
3621
3622__LJMP static int hlua_applet_tcp_get_priv(lua_State *L)
3623{
3624 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3625 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003626 struct hlua *hlua;
3627
3628 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003629 if (!s->hlua) {
3630 lua_pushnil(L);
3631 return 1;
3632 }
3633 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003634
3635 /* Push configuration index in the stack. */
3636 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
3637
3638 return 1;
3639}
3640
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003641/* If expected data not yet available, it returns a yield. This function
3642 * consumes the data in the buffer. It returns a string containing the
3643 * data. This string can be empty.
3644 */
3645__LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx)
3646{
3647 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3648 struct stream_interface *si = appctx->appctx->owner;
3649 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02003650 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003651 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02003652 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003653 size_t len2;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003654
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003655 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003656 ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003657
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003658 /* Data not yet available. return yield. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003659 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003660 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003661 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_getline_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003662 }
3663
3664 /* End of data: commit the total strings and return. */
3665 if (ret < 0) {
3666 luaL_pushresult(&appctx->b);
3667 return 1;
3668 }
3669
3670 /* Ensure that the block 2 length is usable. */
3671 if (ret == 1)
3672 len2 = 0;
3673
3674 /* dont check the max length read and dont check. */
3675 luaL_addlstring(&appctx->b, blk1, len1);
3676 luaL_addlstring(&appctx->b, blk2, len2);
3677
3678 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003679 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003680 luaL_pushresult(&appctx->b);
3681 return 1;
3682}
3683
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003684/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003685__LJMP static int hlua_applet_tcp_getline(lua_State *L)
3686{
3687 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3688
3689 /* Initialise the string catenation. */
3690 luaL_buffinit(L, &appctx->b);
3691
3692 return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0));
3693}
3694
3695/* If expected data not yet available, it returns a yield. This function
3696 * consumes the data in the buffer. It returns a string containing the
3697 * data. This string can be empty.
3698 */
3699__LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx)
3700{
3701 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3702 struct stream_interface *si = appctx->appctx->owner;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003703 size_t len = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003704 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02003705 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003706 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02003707 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003708 size_t len2;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003709
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003710 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003711 ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003712
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003713 /* Data not yet available. return yield. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003714 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003715 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003716 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003717 }
3718
3719 /* End of data: commit the total strings and return. */
3720 if (ret < 0) {
3721 luaL_pushresult(&appctx->b);
3722 return 1;
3723 }
3724
3725 /* Ensure that the block 2 length is usable. */
3726 if (ret == 1)
3727 len2 = 0;
3728
3729 if (len == -1) {
3730
3731 /* If len == -1, catenate all the data avalaile and
3732 * yield because we want to get all the data until
3733 * the end of data stream.
3734 */
3735 luaL_addlstring(&appctx->b, blk1, len1);
3736 luaL_addlstring(&appctx->b, blk2, len2);
Willy Tarreau06d80a92017-10-19 14:32:15 +02003737 co_skip(si_oc(si), len1 + len2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003738 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003739 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003740
3741 } else {
3742
3743 /* Copy the fisrt block caping to the length required. */
3744 if (len1 > len)
3745 len1 = len;
3746 luaL_addlstring(&appctx->b, blk1, len1);
3747 len -= len1;
3748
3749 /* Copy the second block. */
3750 if (len2 > len)
3751 len2 = len;
3752 luaL_addlstring(&appctx->b, blk2, len2);
3753 len -= len2;
3754
3755 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003756 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003757
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003758 /* If there is no other data available, yield waiting for new data. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003759 if (len > 0) {
3760 lua_pushinteger(L, len);
3761 lua_replace(L, 2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003762 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003763 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003764 }
3765
3766 /* return the result. */
3767 luaL_pushresult(&appctx->b);
3768 return 1;
3769 }
3770
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003771 /* we never execute this */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003772 hlua_pusherror(L, "Lua: internal error");
3773 WILL_LJMP(lua_error(L));
3774 return 0;
3775}
3776
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003777/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003778__LJMP static int hlua_applet_tcp_recv(lua_State *L)
3779{
3780 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3781 int len = -1;
3782
3783 if (lua_gettop(L) > 2)
3784 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
3785 if (lua_gettop(L) >= 2) {
3786 len = MAY_LJMP(luaL_checkinteger(L, 2));
3787 lua_pop(L, 1);
3788 }
3789
3790 /* Confirm or set the required length */
3791 lua_pushinteger(L, len);
3792
3793 /* Initialise the string catenation. */
3794 luaL_buffinit(L, &appctx->b);
3795
3796 return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0));
3797}
3798
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003799/* Append data in the output side of the buffer. This data is immediately
3800 * sent. The function returns the amount of data written. If the buffer
3801 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003802 * if the channel is closed.
3803 */
3804__LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx)
3805{
3806 size_t len;
3807 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3808 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
3809 int l = MAY_LJMP(luaL_checkinteger(L, 3));
3810 struct stream_interface *si = appctx->appctx->owner;
3811 struct channel *chn = si_ic(si);
3812 int max;
3813
3814 /* Get the max amount of data which can write as input in the channel. */
3815 max = channel_recv_max(chn);
3816 if (max > (len - l))
3817 max = len - l;
3818
3819 /* Copy data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003820 ci_putblk(chn, str + l, max);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003821
3822 /* update counters. */
3823 l += max;
3824 lua_pop(L, 1);
3825 lua_pushinteger(L, l);
3826
3827 /* If some data is not send, declares the situation to the
3828 * applet, and returns a yield.
3829 */
3830 if (l < len) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003831 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003832 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003833 }
3834
3835 return 1;
3836}
3837
3838/* Just a wraper of "hlua_applet_tcp_send_yield". This wrapper permits
3839 * yield the LUA process, and resume it without checking the
3840 * input arguments.
3841 */
3842__LJMP static int hlua_applet_tcp_send(lua_State *L)
3843{
3844 MAY_LJMP(check_args(L, 2, "send"));
3845 lua_pushinteger(L, 0);
3846
3847 return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0));
3848}
3849
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003850/*
3851 *
3852 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003853 * Class AppletHTTP
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003854 *
3855 *
3856 */
3857
3858/* Returns a struct hlua_txn if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003859 * a class stream, otherwise it throws an error.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003860 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003861__LJMP static struct hlua_appctx *hlua_checkapplet_http(lua_State *L, int ud)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003862{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003863 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003864}
3865
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003866/* This function creates and push in the stack an Applet object
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003867 * according with a current TXN.
3868 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003869static int hlua_applet_http_new(lua_State *L, struct appctx *ctx)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003870{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003871 struct hlua_appctx *appctx;
Thierry FOURNIER841475e2015-12-11 17:10:09 +01003872 struct hlua_txn htxn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003873 struct stream_interface *si = ctx->owner;
3874 struct stream *s = si_strm(si);
3875 struct proxy *px = s->be;
3876 struct http_txn *txn = s->txn;
3877 const char *path;
3878 const char *end;
3879 const char *p;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003880
3881 /* Check stack size. */
3882 if (!lua_checkstack(L, 3))
3883 return 0;
3884
3885 /* Create the object: obj[0] = userdata.
3886 * Note that the base of the Converters object is the
3887 * same than the TXN object.
3888 */
3889 lua_newtable(L);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003890 appctx = lua_newuserdata(L, sizeof(*appctx));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003891 lua_rawseti(L, -2, 0);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003892 appctx->appctx = ctx;
3893 appctx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */
Robin H. Johnson52f5db22017-01-01 13:10:52 -08003894 appctx->appctx->ctx.hlua_apphttp.reason = NULL; /* Use default reason based on status */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003895 appctx->htxn.s = s;
3896 appctx->htxn.p = px;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003897
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003898 /* Create the "f" field that contains a list of fetches. */
3899 lua_pushstring(L, "f");
3900 if (!hlua_fetches_new(L, &appctx->htxn, 0))
3901 return 0;
3902 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003903
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003904 /* Create the "sf" field that contains a list of stringsafe fetches. */
3905 lua_pushstring(L, "sf");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003906 if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003907 return 0;
3908 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003909
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003910 /* Create the "c" field that contains a list of converters. */
3911 lua_pushstring(L, "c");
3912 if (!hlua_converters_new(L, &appctx->htxn, 0))
3913 return 0;
3914 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003915
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003916 /* Create the "sc" field that contains a list of stringsafe converters. */
3917 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003918 if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003919 return 0;
3920 lua_settable(L, -3);
Willy Tarreaueee5b512015-04-03 23:46:31 +02003921
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003922 /* Stores the request method. */
3923 lua_pushstring(L, "method");
Willy Tarreaua79021a2018-06-15 18:07:57 +02003924 lua_pushlstring(L, ci_head(txn->req.chn), txn->req.sl.rq.m_l);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003925 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003926
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003927 /* Stores the http version. */
3928 lua_pushstring(L, "version");
Willy Tarreaua79021a2018-06-15 18:07:57 +02003929 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 +02003930 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003931
Thierry FOURNIER841475e2015-12-11 17:10:09 +01003932 /* creates an array of headers. hlua_http_get_headers() crates and push
3933 * the array on the top of the stack.
3934 */
3935 lua_pushstring(L, "headers");
3936 htxn.s = s;
3937 htxn.p = px;
3938 htxn.dir = SMP_OPT_DIR_REQ;
3939 if (!hlua_http_get_headers(L, &htxn, &htxn.s->txn->req))
3940 return 0;
3941 lua_settable(L, -3);
3942
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003943 /* Get path and qs */
Willy Tarreau6b952c82018-09-10 17:45:34 +02003944 path = http_txn_get_path(txn);
Thierry FOURNIER7d388632017-02-22 02:06:16 +01003945 if (path) {
Willy Tarreaua79021a2018-06-15 18:07:57 +02003946 end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Thierry FOURNIER7d388632017-02-22 02:06:16 +01003947 p = path;
3948 while (p < end && *p != '?')
3949 p++;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003950
Thierry FOURNIER7d388632017-02-22 02:06:16 +01003951 /* Stores the request path. */
3952 lua_pushstring(L, "path");
3953 lua_pushlstring(L, path, p - path);
3954 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003955
Thierry FOURNIER7d388632017-02-22 02:06:16 +01003956 /* Stores the query string. */
3957 lua_pushstring(L, "qs");
3958 if (*p == '?')
3959 p++;
3960 lua_pushlstring(L, p, end - p);
3961 lua_settable(L, -3);
3962 }
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003963
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003964 /* Stores the request path. */
3965 lua_pushstring(L, "length");
3966 lua_pushinteger(L, txn->req.body_len);
3967 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003968
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003969 /* Create an array of HTTP request headers. */
3970 lua_pushstring(L, "headers");
3971 MAY_LJMP(hlua_http_get_headers(L, &appctx->htxn, &appctx->htxn.s->txn->req));
3972 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003973
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003974 /* Create an empty array of HTTP request headers. */
3975 lua_pushstring(L, "response");
3976 lua_newtable(L);
3977 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003978
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003979 /* Pop a class stream metatable and affect it to the table. */
3980 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref);
3981 lua_setmetatable(L, -2);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003982
3983 return 1;
3984}
3985
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003986__LJMP static int hlua_applet_http_set_var(lua_State *L)
3987{
3988 struct hlua_appctx *appctx;
3989 struct stream *s;
3990 const char *name;
3991 size_t len;
3992 struct sample smp;
3993
3994 MAY_LJMP(check_args(L, 3, "set_var"));
3995
3996 /* It is useles to retrieve the stream, but this function
3997 * runs only in a stream context.
3998 */
3999 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4000 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4001 s = appctx->htxn.s;
4002
4003 /* Converts the third argument in a sample. */
4004 hlua_lua2smp(L, 3, &smp);
4005
4006 /* Store the sample in a variable. */
4007 smp_set_owner(&smp, s->be, s->sess, s, 0);
4008 vars_set_by_name(name, len, &smp);
4009 return 0;
4010}
4011
4012__LJMP static int hlua_applet_http_unset_var(lua_State *L)
4013{
4014 struct hlua_appctx *appctx;
4015 struct stream *s;
4016 const char *name;
4017 size_t len;
4018 struct sample smp;
4019
4020 MAY_LJMP(check_args(L, 2, "unset_var"));
4021
4022 /* It is useles to retrieve the stream, but this function
4023 * runs only in a stream context.
4024 */
4025 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4026 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4027 s = appctx->htxn.s;
4028
4029 /* Unset the variable. */
4030 smp_set_owner(&smp, s->be, s->sess, s, 0);
4031 vars_unset_by_name(name, len, &smp);
4032 return 0;
4033}
4034
4035__LJMP static int hlua_applet_http_get_var(lua_State *L)
4036{
4037 struct hlua_appctx *appctx;
4038 struct stream *s;
4039 const char *name;
4040 size_t len;
4041 struct sample smp;
4042
4043 MAY_LJMP(check_args(L, 2, "get_var"));
4044
4045 /* It is useles to retrieve the stream, but this function
4046 * runs only in a stream context.
4047 */
4048 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4049 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4050 s = appctx->htxn.s;
4051
4052 smp_set_owner(&smp, s->be, s->sess, s, 0);
4053 if (!vars_get_by_name(name, len, &smp)) {
4054 lua_pushnil(L);
4055 return 1;
4056 }
4057
4058 return hlua_smp2lua(L, &smp);
4059}
4060
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004061__LJMP static int hlua_applet_http_set_priv(lua_State *L)
4062{
4063 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4064 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004065 struct hlua *hlua;
4066
4067 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004068 if (!s->hlua)
4069 return 0;
4070 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004071
4072 MAY_LJMP(check_args(L, 2, "set_priv"));
4073
4074 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02004075 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004076
4077 /* Get and store new value. */
4078 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
4079 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
4080
4081 return 0;
4082}
4083
4084__LJMP static int hlua_applet_http_get_priv(lua_State *L)
4085{
4086 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4087 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004088 struct hlua *hlua;
4089
4090 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004091 if (!s->hlua) {
4092 lua_pushnil(L);
4093 return 1;
4094 }
4095 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004096
4097 /* Push configuration index in the stack. */
4098 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
4099
4100 return 1;
4101}
4102
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004103/* If expected data not yet available, it returns a yield. This function
4104 * consumes the data in the buffer. It returns a string containing the
4105 * data. This string can be empty.
4106 */
4107__LJMP static int hlua_applet_http_getline_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004108{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004109 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4110 struct stream_interface *si = appctx->appctx->owner;
4111 struct channel *chn = si_ic(si);
4112 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02004113 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02004114 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02004115 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02004116 size_t len2;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004117
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004118 /* Maybe we cant send a 100-continue ? */
4119 if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_100C) {
Willy Tarreau04f1e2d2018-09-10 18:04:24 +02004120 ret = ci_putblk(chn, HTTP_100.ptr, HTTP_100.len);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004121 /* if ret == -2 or -3 the channel closed or the message si too
4122 * big for the buffers. We cant send anything. So, we ignoring
4123 * the error, considers that the 100-continue is sent, and try
4124 * to receive.
4125 * If ret is -1, we dont have room in the buffer, so we yield.
4126 */
4127 if (ret == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01004128 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004129 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_getline_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004130 }
4131 appctx->appctx->ctx.hlua_apphttp.flags &= ~APPLET_100C;
4132 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004133
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004134 /* Check for the end of the data. */
4135 if (appctx->appctx->ctx.hlua_apphttp.left_bytes <= 0) {
4136 luaL_pushresult(&appctx->b);
4137 return 1;
4138 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004139
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004140 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004141 ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004142
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004143 /* Data not yet available. return yield. */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004144 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01004145 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004146 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_getline_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004147 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004148
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004149 /* End of data: commit the total strings and return. */
4150 if (ret < 0) {
4151 luaL_pushresult(&appctx->b);
4152 return 1;
4153 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004154
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004155 /* Ensure that the block 2 length is usable. */
4156 if (ret == 1)
4157 len2 = 0;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004158
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004159 /* Copy the fisrt block caping to the length required. */
4160 if (len1 > appctx->appctx->ctx.hlua_apphttp.left_bytes)
4161 len1 = appctx->appctx->ctx.hlua_apphttp.left_bytes;
4162 luaL_addlstring(&appctx->b, blk1, len1);
4163 appctx->appctx->ctx.hlua_apphttp.left_bytes -= len1;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004164
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004165 /* Copy the second block. */
4166 if (len2 > appctx->appctx->ctx.hlua_apphttp.left_bytes)
4167 len2 = appctx->appctx->ctx.hlua_apphttp.left_bytes;
4168 luaL_addlstring(&appctx->b, blk2, len2);
4169 appctx->appctx->ctx.hlua_apphttp.left_bytes -= len2;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004170
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004171 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004172 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004173 luaL_pushresult(&appctx->b);
4174 return 1;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004175}
4176
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004177/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004178__LJMP static int hlua_applet_http_getline(lua_State *L)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004179{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004180 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004181
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004182 /* Initialise the string catenation. */
4183 luaL_buffinit(L, &appctx->b);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004184
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004185 return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004186}
4187
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004188/* If expected data not yet available, it returns a yield. This function
4189 * consumes the data in the buffer. It returns a string containing the
4190 * data. This string can be empty.
4191 */
4192__LJMP static int hlua_applet_http_recv_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004193{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004194 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4195 struct stream_interface *si = appctx->appctx->owner;
4196 int len = MAY_LJMP(luaL_checkinteger(L, 2));
4197 struct channel *chn = si_ic(si);
4198 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02004199 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02004200 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02004201 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02004202 size_t len2;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004203
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004204 /* Maybe we cant send a 100-continue ? */
4205 if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_100C) {
Willy Tarreau04f1e2d2018-09-10 18:04:24 +02004206 ret = ci_putblk(chn, HTTP_100.ptr, HTTP_100.len);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004207 /* if ret == -2 or -3 the channel closed or the message si too
4208 * big for the buffers. We cant send anything. So, we ignoring
4209 * the error, considers that the 100-continue is sent, and try
4210 * to receive.
4211 * If ret is -1, we dont have room in the buffer, so we yield.
4212 */
4213 if (ret == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01004214 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004215 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004216 }
4217 appctx->appctx->ctx.hlua_apphttp.flags &= ~APPLET_100C;
4218 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004219
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004220 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004221 ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004222
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004223 /* Data not yet available. return yield. */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004224 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01004225 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004226 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004227 }
4228
4229 /* End of data: commit the total strings and return. */
4230 if (ret < 0) {
4231 luaL_pushresult(&appctx->b);
4232 return 1;
4233 }
4234
4235 /* Ensure that the block 2 length is usable. */
4236 if (ret == 1)
4237 len2 = 0;
4238
4239 /* Copy the fisrt block caping to the length required. */
4240 if (len1 > len)
4241 len1 = len;
4242 luaL_addlstring(&appctx->b, blk1, len1);
4243 len -= len1;
4244
4245 /* Copy the second block. */
4246 if (len2 > len)
4247 len2 = len;
4248 luaL_addlstring(&appctx->b, blk2, len2);
4249 len -= len2;
4250
4251 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004252 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004253 if (appctx->appctx->ctx.hlua_apphttp.left_bytes != -1)
4254 appctx->appctx->ctx.hlua_apphttp.left_bytes -= len;
4255
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004256 /* If we are no other data available, yield waiting for new data. */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004257 if (len > 0) {
4258 lua_pushinteger(L, len);
4259 lua_replace(L, 2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01004260 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004261 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004262 }
4263
4264 /* return the result. */
4265 luaL_pushresult(&appctx->b);
4266 return 1;
4267}
4268
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004269/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004270__LJMP static int hlua_applet_http_recv(lua_State *L)
4271{
4272 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4273 int len = -1;
4274
4275 /* Check arguments. */
4276 if (lua_gettop(L) > 2)
4277 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
4278 if (lua_gettop(L) >= 2) {
4279 len = MAY_LJMP(luaL_checkinteger(L, 2));
4280 lua_pop(L, 1);
4281 }
4282
4283 /* Check the required length */
4284 if (len == -1 || len > appctx->appctx->ctx.hlua_apphttp.left_bytes)
4285 len = appctx->appctx->ctx.hlua_apphttp.left_bytes;
4286 lua_pushinteger(L, len);
4287
4288 /* Initialise the string catenation. */
4289 luaL_buffinit(L, &appctx->b);
4290
4291 return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0));
4292}
4293
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004294/* Append data in the output side of the buffer. This data is immediately
4295 * sent. The function returns the amount of data written. If the buffer
4296 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004297 * if the channel is closed.
4298 */
4299__LJMP static int hlua_applet_http_send_yield(lua_State *L, int status, lua_KContext ctx)
4300{
4301 size_t len;
4302 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4303 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
4304 int l = MAY_LJMP(luaL_checkinteger(L, 3));
4305 struct stream_interface *si = appctx->appctx->owner;
4306 struct channel *chn = si_ic(si);
4307 int max;
4308
4309 /* Get the max amount of data which can write as input in the channel. */
4310 max = channel_recv_max(chn);
4311 if (max > (len - l))
4312 max = len - l;
4313
4314 /* Copy data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004315 ci_putblk(chn, str + l, max);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004316
4317 /* update counters. */
4318 l += max;
4319 lua_pop(L, 1);
4320 lua_pushinteger(L, l);
4321
4322 /* If some data is not send, declares the situation to the
4323 * applet, and returns a yield.
4324 */
4325 if (l < len) {
Willy Tarreaudb398432018-11-15 11:08:52 +01004326 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004327 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004328 }
4329
4330 return 1;
4331}
4332
4333/* Just a wraper of "hlua_applet_send_yield". This wrapper permits
4334 * yield the LUA process, and resume it without checking the
4335 * input arguments.
4336 */
4337__LJMP static int hlua_applet_http_send(lua_State *L)
4338{
4339 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4340 size_t len;
4341 char hex[10];
4342
4343 MAY_LJMP(luaL_checklstring(L, 2, &len));
4344
4345 /* If transfer encoding chunked is selected, we surround the data
4346 * by chunk data.
4347 */
4348 if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_CHUNKED) {
4349 snprintf(hex, 9, "%x", (unsigned int)len);
4350 lua_pushfstring(L, "%s\r\n", hex);
4351 lua_insert(L, 2); /* swap the last 2 entries. */
4352 lua_pushstring(L, "\r\n");
4353 lua_concat(L, 3);
4354 }
4355
4356 /* This interger is used for followinf the amount of data sent. */
4357 lua_pushinteger(L, 0);
4358
4359 /* We want to send some data. Headers must be sent. */
4360 if (!(appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
4361 hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data.");
4362 WILL_LJMP(lua_error(L));
4363 }
4364
4365 return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0));
4366}
4367
4368__LJMP static int hlua_applet_http_addheader(lua_State *L)
4369{
4370 const char *name;
4371 int ret;
4372
4373 MAY_LJMP(hlua_checkapplet_http(L, 1));
4374 name = MAY_LJMP(luaL_checkstring(L, 2));
4375 MAY_LJMP(luaL_checkstring(L, 3));
4376
4377 /* Push in the stack the "response" entry. */
4378 ret = lua_getfield(L, 1, "response");
4379 if (ret != LUA_TTABLE) {
4380 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] "
4381 "is expected as an array. %s found", lua_typename(L, ret));
4382 WILL_LJMP(lua_error(L));
4383 }
4384
4385 /* check if the header is already registered if it is not
4386 * the case, register it.
4387 */
4388 ret = lua_getfield(L, -1, name);
4389 if (ret == LUA_TNIL) {
4390
4391 /* Entry not found. */
4392 lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */
4393
4394 /* Insert the new header name in the array in the top of the stack.
4395 * It left the new array in the top of the stack.
4396 */
4397 lua_newtable(L);
4398 lua_pushvalue(L, 2);
4399 lua_pushvalue(L, -2);
4400 lua_settable(L, -4);
4401
4402 } else if (ret != LUA_TTABLE) {
4403
4404 /* corruption error. */
4405 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] "
4406 "is expected as an array. %s found", name, lua_typename(L, ret));
4407 WILL_LJMP(lua_error(L));
4408 }
4409
4410 /* Now the top od thestack is an array of values. We push
4411 * the header value as new entry.
4412 */
4413 lua_pushvalue(L, 3);
4414 ret = lua_rawlen(L, -2);
4415 lua_rawseti(L, -2, ret + 1);
4416 lua_pushboolean(L, 1);
4417 return 1;
4418}
4419
4420__LJMP static int hlua_applet_http_status(lua_State *L)
4421{
4422 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4423 int status = MAY_LJMP(luaL_checkinteger(L, 2));
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004424 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004425
4426 if (status < 100 || status > 599) {
4427 lua_pushboolean(L, 0);
4428 return 1;
4429 }
4430
4431 appctx->appctx->ctx.hlua_apphttp.status = status;
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004432 appctx->appctx->ctx.hlua_apphttp.reason = reason;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004433 lua_pushboolean(L, 1);
4434 return 1;
4435}
4436
4437/* We will build the status line and the headers of the HTTP response.
4438 * We will try send at once if its not possible, we give back the hand
4439 * waiting for more room.
4440 */
4441__LJMP static int hlua_applet_http_start_response_yield(lua_State *L, int status, lua_KContext ctx)
4442{
4443 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4444 struct stream_interface *si = appctx->appctx->owner;
4445 struct channel *chn = si_ic(si);
4446 int ret;
4447 size_t len;
4448 const char *msg;
4449
4450 /* Get the message as the first argument on the stack. */
4451 msg = MAY_LJMP(luaL_checklstring(L, 2, &len));
4452
4453 /* Send the message at once. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004454 ret = ci_putblk(chn, msg, len);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004455
4456 /* if ret == -2 or -3 the channel closed or the message si too
4457 * big for the buffers.
4458 */
4459 if (ret == -2 || ret == -3) {
4460 hlua_pusherror(L, "Lua: 'start_response': response header block too big");
4461 WILL_LJMP(lua_error(L));
4462 }
4463
4464 /* If ret is -1, we dont have room in the buffer, so we yield. */
4465 if (ret == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01004466 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004467 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_start_response_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004468 }
4469
4470 /* Headers sent, set the flag. */
4471 appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT;
4472 return 0;
4473}
4474
4475__LJMP static int hlua_applet_http_start_response(lua_State *L)
4476{
Willy Tarreau83061a82018-07-13 11:56:34 +02004477 struct buffer *tmp = get_trash_chunk();
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004478 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004479 const char *name;
Willy Tarreaua3294632017-08-23 11:24:47 +02004480 size_t name_len;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004481 const char *value;
Willy Tarreaua3294632017-08-23 11:24:47 +02004482 size_t value_len;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004483 int id;
Willy Tarreauc9f4ea02017-08-23 09:32:06 +02004484 long long hdr_contentlength = -1;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004485 int hdr_chunked = 0;
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004486 const char *reason = appctx->appctx->ctx.hlua_apphttp.reason;
4487
4488 if (reason == NULL)
Willy Tarreau04f1e2d2018-09-10 18:04:24 +02004489 reason = http_get_reason(appctx->appctx->ctx.hlua_apphttp.status);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004490
4491 /* Use the same http version than the request. */
4492 chunk_appendf(tmp, "HTTP/1.%c %d %s\r\n",
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +01004493 appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11 ? '1' : '0',
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004494 appctx->appctx->ctx.hlua_apphttp.status,
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004495 reason);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004496
4497 /* Get the array associated to the field "response" in the object AppletHTTP. */
4498 lua_pushvalue(L, 0);
4499 if (lua_getfield(L, 1, "response") != LUA_TTABLE) {
4500 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n",
4501 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4502 WILL_LJMP(lua_error(L));
4503 }
4504
4505 /* Browse the list of headers. */
4506 lua_pushnil(L);
4507 while(lua_next(L, -2) != 0) {
4508
4509 /* We expect a string as -2. */
4510 if (lua_type(L, -2) != LUA_TSTRING) {
4511 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n",
4512 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4513 lua_typename(L, lua_type(L, -2)));
4514 WILL_LJMP(lua_error(L));
4515 }
Willy Tarreaua3294632017-08-23 11:24:47 +02004516 name = lua_tolstring(L, -2, &name_len);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004517
4518 /* We expect an array as -1. */
4519 if (lua_type(L, -1) != LUA_TTABLE) {
4520 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n",
4521 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4522 name,
4523 lua_typename(L, lua_type(L, -1)));
4524 WILL_LJMP(lua_error(L));
4525 }
4526
4527 /* Browse the table who is on the top of the stack. */
4528 lua_pushnil(L);
4529 while(lua_next(L, -2) != 0) {
4530
4531 /* We expect a number as -2. */
4532 if (lua_type(L, -2) != LUA_TNUMBER) {
4533 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n",
4534 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4535 name,
4536 lua_typename(L, lua_type(L, -2)));
4537 WILL_LJMP(lua_error(L));
4538 }
4539 id = lua_tointeger(L, -2);
4540
4541 /* We expect a string as -2. */
4542 if (lua_type(L, -1) != LUA_TSTRING) {
4543 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n",
4544 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4545 name, id,
4546 lua_typename(L, lua_type(L, -1)));
4547 WILL_LJMP(lua_error(L));
4548 }
Willy Tarreaua3294632017-08-23 11:24:47 +02004549 value = lua_tolstring(L, -1, &value_len);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004550
4551 /* Catenate a new header. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004552 if (tmp->data + name_len + 2 + value_len + 2 < tmp->size) {
4553 memcpy(tmp->area + tmp->data, name, name_len);
4554 tmp->data += name_len;
4555 tmp->area[tmp->data++] = ':';
4556 tmp->area[tmp->data++] = ' ';
Willy Tarreaua3294632017-08-23 11:24:47 +02004557
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004558 memcpy(tmp->area + tmp->data, value,
4559 value_len);
4560 tmp->data += value_len;
4561 tmp->area[tmp->data++] = '\r';
4562 tmp->area[tmp->data++] = '\n';
Willy Tarreaua3294632017-08-23 11:24:47 +02004563 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004564
4565 /* Protocol checks. */
4566
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004567 /* Copy the header content length. The length conversion
Willy Tarreauc9f4ea02017-08-23 09:32:06 +02004568 * is done without control. If it contains a bad value,
4569 * the content-length remains negative so that we can
4570 * switch to either chunked encoding or close.
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004571 */
Willy Tarreaua3294632017-08-23 11:24:47 +02004572 if (name_len == 14 && strcasecmp("content-length", name) == 0)
Willy Tarreauc9f4ea02017-08-23 09:32:06 +02004573 strl2llrc(value, strlen(value), &hdr_contentlength);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004574
4575 /* Check if the client annouces a transfer-encoding chunked it self. */
Willy Tarreaua3294632017-08-23 11:24:47 +02004576 if (name_len == 17 && value_len == 7 &&
4577 strcasecmp("transfer-encoding", name) == 0 &&
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004578 strcasecmp("chunked", value) == 0)
4579 hdr_chunked = 1;
4580
4581 /* Remove the array from the stack, and get next element with a remaining string. */
4582 lua_pop(L, 1);
4583 }
4584
4585 /* Remove the array from the stack, and get next element with a remaining string. */
4586 lua_pop(L, 1);
4587 }
4588
Willy Tarreau06c75fe2017-08-23 09:10:38 +02004589 /* If we dont have a content-length set, and the HTTP version is 1.1
4590 * and the status code implies the presence of a message body, we must
4591 * announce a transfer encoding chunked. This is required by haproxy
4592 * for the keepalive compliance. If the applet annouces a transfer-encoding
4593 * chunked itslef, don't do anything.
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004594 */
Willy Tarreauc9f4ea02017-08-23 09:32:06 +02004595 if (hdr_contentlength < 0 && hdr_chunked == 0 &&
Willy Tarreau06c75fe2017-08-23 09:10:38 +02004596 (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) &&
4597 appctx->appctx->ctx.hlua_apphttp.status >= 200 &&
4598 appctx->appctx->ctx.hlua_apphttp.status != 204 &&
4599 appctx->appctx->ctx.hlua_apphttp.status != 304) {
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004600 chunk_appendf(tmp, "Transfer-encoding: chunked\r\n");
4601 appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_CHUNKED;
4602 }
4603
4604 /* Finalize headers. */
4605 chunk_appendf(tmp, "\r\n");
4606
4607 /* Remove the last entry and the array of headers */
4608 lua_pop(L, 2);
4609
4610 /* Push the headers block. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004611 lua_pushlstring(L, tmp->area, tmp->data);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004612
4613 return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0));
4614}
4615
4616/*
4617 *
4618 *
4619 * Class HTTP
4620 *
4621 *
4622 */
4623
4624/* Returns a struct hlua_txn if the stack entry "ud" is
4625 * a class stream, otherwise it throws an error.
4626 */
4627__LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud)
4628{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02004629 return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004630}
4631
4632/* This function creates and push in the stack a HTTP object
4633 * according with a current TXN.
4634 */
4635static int hlua_http_new(lua_State *L, struct hlua_txn *txn)
4636{
4637 struct hlua_txn *htxn;
4638
4639 /* Check stack size. */
4640 if (!lua_checkstack(L, 3))
4641 return 0;
4642
4643 /* Create the object: obj[0] = userdata.
4644 * Note that the base of the Converters object is the
4645 * same than the TXN object.
4646 */
4647 lua_newtable(L);
4648 htxn = lua_newuserdata(L, sizeof(*htxn));
4649 lua_rawseti(L, -2, 0);
4650
4651 htxn->s = txn->s;
4652 htxn->p = txn->p;
4653
4654 /* Pop a class stream metatable and affect it to the table. */
4655 lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref);
4656 lua_setmetatable(L, -2);
4657
4658 return 1;
4659}
4660
4661/* This function creates ans returns an array of HTTP headers.
4662 * This function does not fails. It is used as wrapper with the
4663 * 2 following functions.
4664 */
4665__LJMP static int hlua_http_get_headers(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg)
4666{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004667 /* Create the table. */
4668 lua_newtable(L);
4669
4670 if (!htxn->s->txn)
4671 return 1;
4672
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004673 /* Check if a valid response is parsed */
4674 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4675 return 1;
4676
Christopher Faulet724a12c2018-12-13 22:12:15 +01004677 if (IS_HTX_STRM(htxn->s)) {
4678 /* HTX version */
4679 struct htx *htx = htxbuf(&msg->chn->buf);
4680 int32_t pos;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004681
Christopher Faulet724a12c2018-12-13 22:12:15 +01004682 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
4683 struct htx_blk *blk = htx_get_blk(htx, pos);
4684 enum htx_blk_type type = htx_get_blk_type(blk);
4685 struct ist n, v;
4686 int len;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004687
Christopher Faulet724a12c2018-12-13 22:12:15 +01004688 if (type == HTX_BLK_HDR) {
4689 n = htx_get_blk_name(htx,blk);
4690 v = htx_get_blk_value(htx, blk);
4691 }
4692 else if (type == HTX_BLK_EOH)
4693 break;
4694 else
4695 continue;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004696
Christopher Faulet724a12c2018-12-13 22:12:15 +01004697 /* Check for existing entry:
4698 * assume that the table is on the top of the stack, and
4699 * push the key in the stack, the function lua_gettable()
4700 * perform the lookup.
4701 */
4702 lua_pushlstring(L, n.ptr, n.len);
4703 lua_gettable(L, -2);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004704
Christopher Faulet724a12c2018-12-13 22:12:15 +01004705 switch (lua_type(L, -1)) {
4706 case LUA_TNIL:
4707 /* Table not found, create it. */
4708 lua_pop(L, 1); /* remove the nil value. */
4709 lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */
4710 lua_newtable(L); /* create and push empty table. */
4711 lua_pushlstring(L, v.ptr, v.len); /* push header value. */
4712 lua_rawseti(L, -2, 0); /* index header value (pop it). */
4713 lua_rawset(L, -3); /* index new table with header name (pop the values). */
4714 break;
4715
4716 case LUA_TTABLE:
4717 /* Entry found: push the value in the table. */
4718 len = lua_rawlen(L, -1);
4719 lua_pushlstring(L, v.ptr, v.len); /* push header value. */
4720 lua_rawseti(L, -2, len+1); /* index header value (pop it). */
4721 lua_pop(L, 1); /* remove the table (it is stored in the main table). */
4722 break;
4723
4724 default:
4725 /* Other cases are errors. */
4726 hlua_pusherror(L, "internal error during the parsing of headers.");
4727 WILL_LJMP(lua_error(L));
4728 }
4729 }
4730 }
4731 else {
4732 /* Legacy HTTP version */
4733 const char *cur_ptr, *cur_next, *p;
4734 int old_idx, cur_idx;
4735 struct hdr_idx_elem *cur_hdr;
4736 const char *hn, *hv;
4737 int hnl, hvl;
4738 const char *in;
4739 char *out;
4740 int len;
4741
4742 /* Build array of headers. */
4743 old_idx = 0;
4744 cur_next = ci_head(msg->chn) + hdr_idx_first_pos(&htxn->s->txn->hdr_idx);
4745
4746 while (1) {
4747 cur_idx = htxn->s->txn->hdr_idx.v[old_idx].next;
4748 if (!cur_idx)
4749 break;
4750 old_idx = cur_idx;
4751
4752 cur_hdr = &htxn->s->txn->hdr_idx.v[cur_idx];
4753 cur_ptr = cur_next;
4754 cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1;
4755
4756 /* Now we have one full header at cur_ptr of len cur_hdr->len,
4757 * and the next header starts at cur_next. We'll check
4758 * this header in the list as well as against the default
4759 * rule.
4760 */
4761
4762 /* look for ': *'. */
4763 hn = cur_ptr;
4764 for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++);
4765 if (p >= cur_ptr+cur_hdr->len)
4766 continue;
4767 hnl = p - hn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004768 p++;
Christopher Faulet724a12c2018-12-13 22:12:15 +01004769 while (p < cur_ptr+cur_hdr->len && ( *p == ' ' || *p == '\t' ))
4770 p++;
4771 if (p >= cur_ptr+cur_hdr->len)
4772 continue;
4773 hv = p;
4774 hvl = cur_ptr+cur_hdr->len-p;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004775
Christopher Faulet724a12c2018-12-13 22:12:15 +01004776 /* Lowercase the key. Don't check the size of trash, it have
4777 * the size of one buffer and the input data contains in one
4778 * buffer.
4779 */
4780 out = trash.area;
4781 for (in=hn; in<hn+hnl; in++, out++)
4782 *out = tolower(*in);
4783 *out = '\0';
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004784
Christopher Faulet724a12c2018-12-13 22:12:15 +01004785 /* Check for existing entry:
4786 * assume that the table is on the top of the stack, and
4787 * push the key in the stack, the function lua_gettable()
4788 * perform the lookup.
4789 */
4790 lua_pushlstring(L, trash.area, hnl);
4791 lua_gettable(L, -2);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004792
Christopher Faulet724a12c2018-12-13 22:12:15 +01004793 switch (lua_type(L, -1)) {
4794 case LUA_TNIL:
4795 /* Table not found, create it. */
4796 lua_pop(L, 1); /* remove the nil value. */
4797 lua_pushlstring(L, trash.area, hnl); /* push the header name as key. */
4798 lua_newtable(L); /* create and push empty table. */
4799 lua_pushlstring(L, hv, hvl); /* push header value. */
4800 lua_rawseti(L, -2, 0); /* index header value (pop it). */
4801 lua_rawset(L, -3); /* index new table with header name (pop the values). */
4802 break;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004803
Christopher Faulet724a12c2018-12-13 22:12:15 +01004804 case LUA_TTABLE:
4805 /* Entry found: push the value in the table. */
4806 len = lua_rawlen(L, -1);
4807 lua_pushlstring(L, hv, hvl); /* push header value. */
4808 lua_rawseti(L, -2, len+1); /* index header value (pop it). */
4809 lua_pop(L, 1); /* remove the table (it is stored in the main table). */
4810 break;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004811
Christopher Faulet724a12c2018-12-13 22:12:15 +01004812 default:
4813 /* Other cases are errors. */
4814 hlua_pusherror(L, "internal error during the parsing of headers.");
4815 WILL_LJMP(lua_error(L));
4816 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004817 }
4818 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004819 return 1;
4820}
4821
4822__LJMP static int hlua_http_req_get_headers(lua_State *L)
4823{
4824 struct hlua_txn *htxn;
4825
4826 MAY_LJMP(check_args(L, 1, "req_get_headers"));
4827 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4828
4829 return hlua_http_get_headers(L, htxn, &htxn->s->txn->req);
4830}
4831
4832__LJMP static int hlua_http_res_get_headers(lua_State *L)
4833{
4834 struct hlua_txn *htxn;
4835
4836 MAY_LJMP(check_args(L, 1, "res_get_headers"));
4837 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4838
4839 return hlua_http_get_headers(L, htxn, &htxn->s->txn->rsp);
4840}
4841
4842/* This function replace full header, or just a value in
4843 * the request or in the response. It is a wrapper fir the
4844 * 4 following functions.
4845 */
4846__LJMP static inline int hlua_http_rep_hdr(lua_State *L, struct hlua_txn *htxn,
4847 struct http_msg *msg, int action)
4848{
4849 size_t name_len;
4850 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
4851 const char *reg = MAY_LJMP(luaL_checkstring(L, 3));
4852 const char *value = MAY_LJMP(luaL_checkstring(L, 4));
4853 struct my_regex re;
4854
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004855 /* Check if a valid response is parsed */
4856 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4857 return 0;
4858
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004859 if (!regex_comp(reg, &re, 1, 1, NULL))
4860 WILL_LJMP(luaL_argerror(L, 3, "invalid regex"));
4861
4862 http_transform_header_str(htxn->s, msg, name, name_len, value, &re, action);
4863 regex_free(&re);
4864 return 0;
4865}
4866
4867__LJMP static int hlua_http_req_rep_hdr(lua_State *L)
4868{
4869 struct hlua_txn *htxn;
4870
4871 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
4872 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4873
4874 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_HDR));
4875}
4876
4877__LJMP static int hlua_http_res_rep_hdr(lua_State *L)
4878{
4879 struct hlua_txn *htxn;
4880
4881 MAY_LJMP(check_args(L, 4, "res_rep_hdr"));
4882 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4883
4884 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->rsp, ACT_HTTP_REPLACE_HDR));
4885}
4886
4887__LJMP static int hlua_http_req_rep_val(lua_State *L)
4888{
4889 struct hlua_txn *htxn;
4890
4891 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
4892 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4893
4894 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_VAL));
4895}
4896
4897__LJMP static int hlua_http_res_rep_val(lua_State *L)
4898{
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004899 struct hlua_txn *htxn;
4900
4901 MAY_LJMP(check_args(L, 4, "res_rep_val"));
4902 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4903
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02004904 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->rsp, ACT_HTTP_REPLACE_VAL));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004905}
4906
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004907/* This function deletes all the occurrences of an header.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004908 * It is a wrapper for the 2 following functions.
4909 */
4910__LJMP static inline int hlua_http_del_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg)
4911{
4912 size_t len;
4913 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004914
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004915 /* Check if a valid response is parsed */
4916 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4917 return 0;
4918
Christopher Faulet724a12c2018-12-13 22:12:15 +01004919 if (IS_HTX_STRM(htxn->s)) {
4920 /* HTX version */
4921 struct htx *htx = htxbuf(&msg->chn->buf);
4922 struct http_hdr_ctx ctx;
4923
4924 ctx.blk = NULL;
4925 while (http_find_header(htx, ist2(name, len), &ctx, 1))
4926 http_remove_header(htx, &ctx);
4927 }
4928 else {
4929 /* Legacy HTTP version */
4930 struct hdr_ctx ctx;
4931 struct http_txn *txn = htxn->s->txn;
4932
4933 ctx.idx = 0;
4934 while (http_find_header2(name, len, ci_head(msg->chn), &txn->hdr_idx, &ctx))
4935 http_remove_header2(msg, &txn->hdr_idx, &ctx);
4936 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004937 return 0;
4938}
4939
4940__LJMP static int hlua_http_req_del_hdr(lua_State *L)
4941{
4942 struct hlua_txn *htxn;
4943
4944 MAY_LJMP(check_args(L, 2, "req_del_hdr"));
4945 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4946
Willy Tarreaueee5b512015-04-03 23:46:31 +02004947 return hlua_http_del_hdr(L, htxn, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004948}
4949
4950__LJMP static int hlua_http_res_del_hdr(lua_State *L)
4951{
4952 struct hlua_txn *htxn;
4953
4954 MAY_LJMP(check_args(L, 2, "req_del_hdr"));
4955 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4956
Willy Tarreaueee5b512015-04-03 23:46:31 +02004957 return hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004958}
4959
4960/* This function adds an header. It is a wrapper used by
4961 * the 2 following functions.
4962 */
4963__LJMP static inline int hlua_http_add_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg)
4964{
4965 size_t name_len;
4966 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
4967 size_t value_len;
4968 const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len));
4969 char *p;
4970
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004971 /* Check if a valid message is parsed */
4972 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4973 return 0;
4974
Christopher Faulet724a12c2018-12-13 22:12:15 +01004975 if (IS_HTX_STRM(htxn->s)) {
4976 /* HTX version */
4977 struct htx *htx = htxbuf(&msg->chn->buf);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004978
Christopher Faulet724a12c2018-12-13 22:12:15 +01004979 lua_pushboolean(L, http_add_header(htx, ist2(name, name_len),
4980 ist2(value, value_len)));
4981 }
4982 else {
4983 /* Legacy HTTP version */
4984 /* Check length. */
4985 trash.data = value_len + name_len + 2;
4986 if (trash.data > trash.size)
4987 return 0;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004988
Christopher Faulet724a12c2018-12-13 22:12:15 +01004989 /* Creates the header string. */
4990 p = trash.area;
4991 memcpy(p, name, name_len);
4992 p += name_len;
4993 *p = ':';
4994 p++;
4995 *p = ' ';
4996 p++;
4997 memcpy(p, value, value_len);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004998
Christopher Faulet724a12c2018-12-13 22:12:15 +01004999 lua_pushboolean(L, http_header_add_tail2(msg, &htxn->s->txn->hdr_idx,
5000 trash.area, trash.data) != 0);
5001 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005002 return 0;
5003}
5004
5005__LJMP static int hlua_http_req_add_hdr(lua_State *L)
5006{
5007 struct hlua_txn *htxn;
5008
5009 MAY_LJMP(check_args(L, 3, "req_add_hdr"));
5010 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5011
Willy Tarreaueee5b512015-04-03 23:46:31 +02005012 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005013}
5014
5015__LJMP static int hlua_http_res_add_hdr(lua_State *L)
5016{
5017 struct hlua_txn *htxn;
5018
5019 MAY_LJMP(check_args(L, 3, "res_add_hdr"));
5020 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5021
Willy Tarreaueee5b512015-04-03 23:46:31 +02005022 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005023}
5024
5025static int hlua_http_req_set_hdr(lua_State *L)
5026{
5027 struct hlua_txn *htxn;
5028
5029 MAY_LJMP(check_args(L, 3, "req_set_hdr"));
5030 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5031
Willy Tarreaueee5b512015-04-03 23:46:31 +02005032 hlua_http_del_hdr(L, htxn, &htxn->s->txn->req);
5033 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005034}
5035
5036static int hlua_http_res_set_hdr(lua_State *L)
5037{
5038 struct hlua_txn *htxn;
5039
5040 MAY_LJMP(check_args(L, 3, "res_set_hdr"));
5041 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5042
Willy Tarreaueee5b512015-04-03 23:46:31 +02005043 hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp);
5044 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005045}
5046
5047/* This function set the method. */
5048static int hlua_http_req_set_meth(lua_State *L)
5049{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005050 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005051 size_t name_len;
5052 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005053
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02005054 /* Check if a valid request is parsed */
5055 if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) {
5056 lua_pushboolean(L, 0);
5057 return 1;
5058 }
5059
Willy Tarreau987e3fb2015-04-04 01:09:08 +02005060 lua_pushboolean(L, http_replace_req_line(0, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005061 return 1;
5062}
5063
5064/* This function set the method. */
5065static int hlua_http_req_set_path(lua_State *L)
5066{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005067 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005068 size_t name_len;
5069 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02005070
5071 /* Check if a valid request is parsed */
5072 if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) {
5073 lua_pushboolean(L, 0);
5074 return 1;
5075 }
5076
Willy Tarreau987e3fb2015-04-04 01:09:08 +02005077 lua_pushboolean(L, http_replace_req_line(1, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005078 return 1;
5079}
5080
5081/* This function set the query-string. */
5082static int hlua_http_req_set_query(lua_State *L)
5083{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005084 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005085 size_t name_len;
5086 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005087
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02005088 /* Check if a valid request is parsed */
5089 if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) {
5090 lua_pushboolean(L, 0);
5091 return 1;
5092 }
5093
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005094 /* Check length. */
5095 if (name_len > trash.size - 1) {
5096 lua_pushboolean(L, 0);
5097 return 1;
5098 }
5099
5100 /* Add the mark question as prefix. */
5101 chunk_reset(&trash);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005102 trash.area[trash.data++] = '?';
5103 memcpy(trash.area + trash.data, name, name_len);
5104 trash.data += name_len;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005105
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005106 lua_pushboolean(L,
5107 http_replace_req_line(2, trash.area, trash.data, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005108 return 1;
5109}
5110
5111/* This function set the uri. */
5112static int hlua_http_req_set_uri(lua_State *L)
5113{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005114 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005115 size_t name_len;
5116 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005117
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02005118 /* Check if a valid request is parsed */
5119 if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) {
5120 lua_pushboolean(L, 0);
5121 return 1;
5122 }
5123
Willy Tarreau987e3fb2015-04-04 01:09:08 +02005124 lua_pushboolean(L, http_replace_req_line(3, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005125 return 1;
5126}
5127
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005128/* This function set the response code & optionally reason. */
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005129static int hlua_http_res_set_status(lua_State *L)
5130{
5131 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5132 unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2));
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005133 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005134
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02005135 /* Check if a valid response is parsed */
5136 if (unlikely(htxn->s->txn->rsp.msg_state < HTTP_MSG_BODY))
5137 return 0;
5138
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005139 http_set_status(code, reason, htxn->s);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005140 return 0;
5141}
5142
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005143/*
5144 *
5145 *
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005146 * Class TXN
5147 *
5148 *
5149 */
5150
5151/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02005152 * a class stream, otherwise it throws an error.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005153 */
5154__LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud)
5155{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02005156 return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref));
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005157}
5158
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005159__LJMP static int hlua_set_var(lua_State *L)
5160{
5161 struct hlua_txn *htxn;
5162 const char *name;
5163 size_t len;
5164 struct sample smp;
5165
5166 MAY_LJMP(check_args(L, 3, "set_var"));
5167
5168 /* It is useles to retrieve the stream, but this function
5169 * runs only in a stream context.
5170 */
5171 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5172 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5173
5174 /* Converts the third argument in a sample. */
5175 hlua_lua2smp(L, 3, &smp);
5176
5177 /* Store the sample in a variable. */
Willy Tarreau7560dd42016-03-10 16:28:58 +01005178 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Willy Tarreau6204cd92016-03-10 16:33:04 +01005179 vars_set_by_name(name, len, &smp);
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005180 return 0;
5181}
5182
Christopher Faulet85d79c92016-11-09 16:54:56 +01005183__LJMP static int hlua_unset_var(lua_State *L)
5184{
5185 struct hlua_txn *htxn;
5186 const char *name;
5187 size_t len;
5188 struct sample smp;
5189
5190 MAY_LJMP(check_args(L, 2, "unset_var"));
5191
5192 /* It is useles to retrieve the stream, but this function
5193 * runs only in a stream context.
5194 */
5195 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5196 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5197
5198 /* Unset the variable. */
5199 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
5200 vars_unset_by_name(name, len, &smp);
5201 return 0;
5202}
5203
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005204__LJMP static int hlua_get_var(lua_State *L)
5205{
5206 struct hlua_txn *htxn;
5207 const char *name;
5208 size_t len;
5209 struct sample smp;
5210
5211 MAY_LJMP(check_args(L, 2, "get_var"));
5212
5213 /* It is useles to retrieve the stream, but this function
5214 * runs only in a stream context.
5215 */
5216 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5217 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5218
Willy Tarreau7560dd42016-03-10 16:28:58 +01005219 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Willy Tarreau6204cd92016-03-10 16:33:04 +01005220 if (!vars_get_by_name(name, len, &smp)) {
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005221 lua_pushnil(L);
5222 return 1;
5223 }
5224
5225 return hlua_smp2lua(L, &smp);
5226}
5227
Willy Tarreau59551662015-03-10 14:23:13 +01005228__LJMP static int hlua_set_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005229{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005230 struct hlua *hlua;
5231
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005232 MAY_LJMP(check_args(L, 2, "set_priv"));
5233
Willy Tarreau87b09662015-04-03 00:22:06 +02005234 /* It is useles to retrieve the stream, but this function
5235 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005236 */
5237 MAY_LJMP(hlua_checktxn(L, 1));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005238 hlua = hlua_gethlua(L);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005239
5240 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02005241 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005242
5243 /* Get and store new value. */
5244 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
5245 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
5246
5247 return 0;
5248}
5249
Willy Tarreau59551662015-03-10 14:23:13 +01005250__LJMP static int hlua_get_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005251{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005252 struct hlua *hlua;
5253
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005254 MAY_LJMP(check_args(L, 1, "get_priv"));
5255
Willy Tarreau87b09662015-04-03 00:22:06 +02005256 /* It is useles to retrieve the stream, but this function
5257 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005258 */
5259 MAY_LJMP(hlua_checktxn(L, 1));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005260 hlua = hlua_gethlua(L);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005261
5262 /* Push configuration index in the stack. */
5263 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
5264
5265 return 1;
5266}
5267
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005268/* Create stack entry containing a class TXN. This function
5269 * return 0 if the stack does not contains free slots,
5270 * otherwise it returns 1.
5271 */
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005272static int hlua_txn_new(lua_State *L, struct stream *s, struct proxy *p, int dir, int flags)
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005273{
Willy Tarreaude491382015-04-06 11:04:28 +02005274 struct hlua_txn *htxn;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005275
5276 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005277 if (!lua_checkstack(L, 3))
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005278 return 0;
5279
5280 /* NOTE: The allocation never fails. The failure
5281 * throw an error, and the function never returns.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08005282 * if the throw is not available, the process is aborted.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005283 */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005284 /* Create the object: obj[0] = userdata. */
5285 lua_newtable(L);
Willy Tarreaude491382015-04-06 11:04:28 +02005286 htxn = lua_newuserdata(L, sizeof(*htxn));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005287 lua_rawseti(L, -2, 0);
5288
Willy Tarreaude491382015-04-06 11:04:28 +02005289 htxn->s = s;
5290 htxn->p = p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01005291 htxn->dir = dir;
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005292 htxn->flags = flags;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005293
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005294 /* Create the "f" field that contains a list of fetches. */
5295 lua_pushstring(L, "f");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005296 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005297 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005298 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005299
5300 /* Create the "sf" field that contains a list of stringsafe fetches. */
5301 lua_pushstring(L, "sf");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005302 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP | HLUA_F_AS_STRING))
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005303 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005304 lua_rawset(L, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005305
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005306 /* Create the "c" field that contains a list of converters. */
5307 lua_pushstring(L, "c");
Willy Tarreaude491382015-04-06 11:04:28 +02005308 if (!hlua_converters_new(L, htxn, 0))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005309 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005310 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005311
5312 /* Create the "sc" field that contains a list of stringsafe converters. */
5313 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01005314 if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING))
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005315 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005316 lua_rawset(L, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005317
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005318 /* Create the "req" field that contains the request channel object. */
5319 lua_pushstring(L, "req");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005320 if (!hlua_channel_new(L, &s->req))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005321 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005322 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005323
5324 /* Create the "res" field that contains the response channel object. */
5325 lua_pushstring(L, "res");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005326 if (!hlua_channel_new(L, &s->res))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005327 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005328 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005329
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005330 /* Creates the HTTP object is the current proxy allows http. */
5331 lua_pushstring(L, "http");
5332 if (p->mode == PR_MODE_HTTP) {
Willy Tarreaude491382015-04-06 11:04:28 +02005333 if (!hlua_http_new(L, htxn))
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005334 return 0;
5335 }
5336 else
5337 lua_pushnil(L);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005338 lua_rawset(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005339
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005340 /* Pop a class sesison metatable and affect it to the userdata. */
5341 lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref);
5342 lua_setmetatable(L, -2);
5343
5344 return 1;
5345}
5346
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005347__LJMP static int hlua_txn_deflog(lua_State *L)
5348{
5349 const char *msg;
5350 struct hlua_txn *htxn;
5351
5352 MAY_LJMP(check_args(L, 2, "deflog"));
5353 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5354 msg = MAY_LJMP(luaL_checkstring(L, 2));
5355
5356 hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg);
5357 return 0;
5358}
5359
5360__LJMP static int hlua_txn_log(lua_State *L)
5361{
5362 int level;
5363 const char *msg;
5364 struct hlua_txn *htxn;
5365
5366 MAY_LJMP(check_args(L, 3, "log"));
5367 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5368 level = MAY_LJMP(luaL_checkinteger(L, 2));
5369 msg = MAY_LJMP(luaL_checkstring(L, 3));
5370
5371 if (level < 0 || level >= NB_LOG_LEVELS)
5372 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
5373
5374 hlua_sendlog(htxn->s->be, level, msg);
5375 return 0;
5376}
5377
5378__LJMP static int hlua_txn_log_debug(lua_State *L)
5379{
5380 const char *msg;
5381 struct hlua_txn *htxn;
5382
5383 MAY_LJMP(check_args(L, 2, "Debug"));
5384 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5385 msg = MAY_LJMP(luaL_checkstring(L, 2));
5386 hlua_sendlog(htxn->s->be, LOG_DEBUG, msg);
5387 return 0;
5388}
5389
5390__LJMP static int hlua_txn_log_info(lua_State *L)
5391{
5392 const char *msg;
5393 struct hlua_txn *htxn;
5394
5395 MAY_LJMP(check_args(L, 2, "Info"));
5396 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5397 msg = MAY_LJMP(luaL_checkstring(L, 2));
5398 hlua_sendlog(htxn->s->be, LOG_INFO, msg);
5399 return 0;
5400}
5401
5402__LJMP static int hlua_txn_log_warning(lua_State *L)
5403{
5404 const char *msg;
5405 struct hlua_txn *htxn;
5406
5407 MAY_LJMP(check_args(L, 2, "Warning"));
5408 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5409 msg = MAY_LJMP(luaL_checkstring(L, 2));
5410 hlua_sendlog(htxn->s->be, LOG_WARNING, msg);
5411 return 0;
5412}
5413
5414__LJMP static int hlua_txn_log_alert(lua_State *L)
5415{
5416 const char *msg;
5417 struct hlua_txn *htxn;
5418
5419 MAY_LJMP(check_args(L, 2, "Alert"));
5420 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5421 msg = MAY_LJMP(luaL_checkstring(L, 2));
5422 hlua_sendlog(htxn->s->be, LOG_ALERT, msg);
5423 return 0;
5424}
5425
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005426__LJMP static int hlua_txn_set_loglevel(lua_State *L)
5427{
5428 struct hlua_txn *htxn;
5429 int ll;
5430
5431 MAY_LJMP(check_args(L, 2, "set_loglevel"));
5432 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5433 ll = MAY_LJMP(luaL_checkinteger(L, 2));
5434
5435 if (ll < 0 || ll > 7)
5436 WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7"));
5437
5438 htxn->s->logs.level = ll;
5439 return 0;
5440}
5441
5442__LJMP static int hlua_txn_set_tos(lua_State *L)
5443{
5444 struct hlua_txn *htxn;
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005445 int tos;
5446
5447 MAY_LJMP(check_args(L, 2, "set_tos"));
5448 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5449 tos = MAY_LJMP(luaL_checkinteger(L, 2));
5450
Willy Tarreau1a18b542018-12-11 16:37:42 +01005451 conn_set_tos(objt_conn(htxn->s->sess->origin), tos);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005452 return 0;
5453}
5454
5455__LJMP static int hlua_txn_set_mark(lua_State *L)
5456{
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005457 struct hlua_txn *htxn;
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005458 int mark;
5459
5460 MAY_LJMP(check_args(L, 2, "set_mark"));
5461 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5462 mark = MAY_LJMP(luaL_checkinteger(L, 2));
5463
Willy Tarreau1a18b542018-12-11 16:37:42 +01005464 conn_set_tos(objt_conn(htxn->s->sess->origin), mark);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005465 return 0;
5466}
5467
Patrick Hemmer268a7072018-05-11 12:52:31 -04005468__LJMP static int hlua_txn_set_priority_class(lua_State *L)
5469{
5470 struct hlua_txn *htxn;
5471
5472 MAY_LJMP(check_args(L, 2, "set_priority_class"));
5473 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5474 htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2)));
5475 return 0;
5476}
5477
5478__LJMP static int hlua_txn_set_priority_offset(lua_State *L)
5479{
5480 struct hlua_txn *htxn;
5481
5482 MAY_LJMP(check_args(L, 2, "set_priority_offset"));
5483 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5484 htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2)));
5485 return 0;
5486}
5487
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005488/* This function is an Lua binding that send pending data
5489 * to the client, and close the stream interface.
5490 */
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005491__LJMP static int hlua_txn_done(lua_State *L)
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005492{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005493 struct hlua_txn *htxn;
Thierry FOURNIER9bd52d42016-07-14 11:45:33 +02005494 struct hlua *hlua;
Willy Tarreau81389672015-03-10 12:03:52 +01005495 struct channel *ic, *oc;
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005496
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005497 MAY_LJMP(check_args(L, 1, "close"));
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005498 htxn = MAY_LJMP(hlua_checktxn(L, 1));
Thierry FOURNIER9bd52d42016-07-14 11:45:33 +02005499 hlua = hlua_gethlua(L);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005500
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005501 /* If the flags NOTERM is set, we cannot terminate the http
5502 * session, so we just end the execution of the current
5503 * lua code.
5504 */
5505 if (htxn->flags & HLUA_TXN_NOTERM) {
5506 WILL_LJMP(hlua_done(L));
5507 return 0;
5508 }
5509
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005510 ic = &htxn->s->req;
5511 oc = &htxn->s->res;
Willy Tarreau81389672015-03-10 12:03:52 +01005512
Willy Tarreau630ef452015-08-28 10:06:15 +02005513 if (htxn->s->txn) {
5514 /* HTTP mode, let's stay in sync with the stream */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005515 b_del(&ic->buf, htxn->s->txn->req.sov);
Willy Tarreau630ef452015-08-28 10:06:15 +02005516 htxn->s->txn->req.next -= htxn->s->txn->req.sov;
5517 htxn->s->txn->req.sov = 0;
5518 ic->analysers &= AN_REQ_HTTP_XFER_BODY;
5519 oc->analysers = AN_RES_HTTP_XFER_BODY;
5520 htxn->s->txn->req.msg_state = HTTP_MSG_CLOSED;
5521 htxn->s->txn->rsp.msg_state = HTTP_MSG_DONE;
5522
Willy Tarreau630ef452015-08-28 10:06:15 +02005523 /* Note that if we want to support keep-alive, we need
5524 * to bypass the close/shutr_now calls below, but that
5525 * may only be done if the HTTP request was already
5526 * processed and the connection header is known (ie
5527 * not during TCP rules).
5528 */
5529 }
5530
Thierry FOURNIER10ec2142015-08-24 17:23:45 +02005531 channel_auto_read(ic);
Willy Tarreau81389672015-03-10 12:03:52 +01005532 channel_abort(ic);
5533 channel_auto_close(ic);
5534 channel_erase(ic);
Thierry FOURNIER10ec2142015-08-24 17:23:45 +02005535
5536 oc->wex = tick_add_ifset(now_ms, oc->wto);
Willy Tarreau81389672015-03-10 12:03:52 +01005537 channel_auto_read(oc);
5538 channel_auto_close(oc);
5539 channel_shutr_now(oc);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005540
Willy Tarreau0458b082015-08-28 09:40:04 +02005541 ic->analysers = 0;
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005542
Thierry FOURNIER9bd52d42016-07-14 11:45:33 +02005543 hlua->flags |= HLUA_STOP;
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005544 WILL_LJMP(hlua_done(L));
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005545 return 0;
5546}
5547
5548__LJMP static int hlua_log(lua_State *L)
5549{
5550 int level;
5551 const char *msg;
5552
5553 MAY_LJMP(check_args(L, 2, "log"));
5554 level = MAY_LJMP(luaL_checkinteger(L, 1));
5555 msg = MAY_LJMP(luaL_checkstring(L, 2));
5556
5557 if (level < 0 || level >= NB_LOG_LEVELS)
5558 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
5559
5560 hlua_sendlog(NULL, level, msg);
5561 return 0;
5562}
5563
5564__LJMP static int hlua_log_debug(lua_State *L)
5565{
5566 const char *msg;
5567
5568 MAY_LJMP(check_args(L, 1, "debug"));
5569 msg = MAY_LJMP(luaL_checkstring(L, 1));
5570 hlua_sendlog(NULL, LOG_DEBUG, msg);
5571 return 0;
5572}
5573
5574__LJMP static int hlua_log_info(lua_State *L)
5575{
5576 const char *msg;
5577
5578 MAY_LJMP(check_args(L, 1, "info"));
5579 msg = MAY_LJMP(luaL_checkstring(L, 1));
5580 hlua_sendlog(NULL, LOG_INFO, msg);
5581 return 0;
5582}
5583
5584__LJMP static int hlua_log_warning(lua_State *L)
5585{
5586 const char *msg;
5587
5588 MAY_LJMP(check_args(L, 1, "warning"));
5589 msg = MAY_LJMP(luaL_checkstring(L, 1));
5590 hlua_sendlog(NULL, LOG_WARNING, msg);
5591 return 0;
5592}
5593
5594__LJMP static int hlua_log_alert(lua_State *L)
5595{
5596 const char *msg;
5597
5598 MAY_LJMP(check_args(L, 1, "alert"));
5599 msg = MAY_LJMP(luaL_checkstring(L, 1));
5600 hlua_sendlog(NULL, LOG_ALERT, msg);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005601 return 0;
5602}
5603
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005604__LJMP static int hlua_sleep_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005605{
5606 int wakeup_ms = lua_tointeger(L, -1);
5607 if (now_ms < wakeup_ms)
Willy Tarreau9635e032018-10-16 17:52:55 +02005608 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005609 return 0;
5610}
5611
5612__LJMP static int hlua_sleep(lua_State *L)
5613{
5614 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005615 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005616
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005617 MAY_LJMP(check_args(L, 1, "sleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005618
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005619 delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005620 wakeup_ms = tick_add(now_ms, delay);
5621 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005622
Willy Tarreau9635e032018-10-16 17:52:55 +02005623 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005624 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005625}
5626
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005627__LJMP static int hlua_msleep(lua_State *L)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005628{
5629 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005630 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005631
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005632 MAY_LJMP(check_args(L, 1, "msleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005633
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005634 delay = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005635 wakeup_ms = tick_add(now_ms, delay);
5636 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005637
Willy Tarreau9635e032018-10-16 17:52:55 +02005638 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005639 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005640}
5641
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01005642/* This functionis an LUA binding. it permits to give back
5643 * the hand at the HAProxy scheduler. It is used when the
5644 * LUA processing consumes a lot of time.
5645 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005646__LJMP static int hlua_yield_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005647{
5648 return 0;
5649}
5650
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01005651__LJMP static int hlua_yield(lua_State *L)
5652{
Willy Tarreau9635e032018-10-16 17:52:55 +02005653 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_yield_yield, TICK_ETERNITY, HLUA_CTRLYIELD));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005654 return 0;
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01005655}
5656
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005657/* This function change the nice of the currently executed
5658 * task. It is used set low or high priority at the current
5659 * task.
5660 */
Willy Tarreau59551662015-03-10 14:23:13 +01005661__LJMP static int hlua_set_nice(lua_State *L)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005662{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005663 struct hlua *hlua;
5664 int nice;
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005665
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005666 MAY_LJMP(check_args(L, 1, "set_nice"));
5667 hlua = hlua_gethlua(L);
5668 nice = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005669
5670 /* If he task is not set, I'm in a start mode. */
5671 if (!hlua || !hlua->task)
5672 return 0;
5673
5674 if (nice < -1024)
5675 nice = -1024;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005676 else if (nice > 1024)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005677 nice = 1024;
5678
5679 hlua->task->nice = nice;
5680 return 0;
5681}
5682
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08005683/* This function is used as a callback of a task. It is called by the
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005684 * HAProxy task subsystem when the task is awaked. The LUA runtime can
5685 * return an E_AGAIN signal, the emmiter of this signal must set a
5686 * signal to wake the task.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005687 *
5688 * Task wrapper are longjmp safe because the only one Lua code
5689 * executed is the safe hlua_ctx_resume();
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005690 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02005691static struct task *hlua_process_task(struct task *task, void *context, unsigned short state)
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005692{
Olivier Houchard9f6af332018-05-25 14:04:04 +02005693 struct hlua *hlua = context;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005694 enum hlua_exec status;
5695
Christopher Faulet5bc99722018-04-25 10:34:45 +02005696 if (task->thread_mask == MAX_THREADS_MASK)
5697 task_set_affinity(task, tid_bit);
5698
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005699 /* If it is the first call to the task, we must initialize the
5700 * execution timeouts.
5701 */
5702 if (!HLUA_IS_RUNNING(hlua))
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02005703 hlua->max_time = hlua_timeout_task;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005704
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005705 /* Execute the Lua code. */
5706 status = hlua_ctx_resume(hlua, 1);
5707
5708 switch (status) {
5709 /* finished or yield */
5710 case HLUA_E_OK:
5711 hlua_ctx_destroy(hlua);
5712 task_delete(task);
5713 task_free(task);
Tim Duesterhuscd235c62018-04-24 13:56:01 +02005714 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005715 break;
5716
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01005717 case HLUA_E_AGAIN: /* co process or timeout wake me later. */
Thierry FOURNIERcb146882017-12-10 17:10:57 +01005718 notification_gc(&hlua->com);
PiBa-NLfe971b32018-05-02 22:27:14 +02005719 task->expire = hlua->wake_time;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005720 break;
5721
5722 /* finished with error. */
5723 case HLUA_E_ERRMSG:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005724 SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005725 hlua_ctx_destroy(hlua);
5726 task_delete(task);
5727 task_free(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02005728 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005729 break;
5730
5731 case HLUA_E_ERR:
5732 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005733 SEND_ERR(NULL, "Lua task: unknown error.\n");
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005734 hlua_ctx_destroy(hlua);
5735 task_delete(task);
5736 task_free(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02005737 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005738 break;
5739 }
Emeric Brun253e53e2017-10-17 18:58:40 +02005740 return task;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005741}
5742
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01005743/* This function is an LUA binding that register LUA function to be
5744 * executed after the HAProxy configuration parsing and before the
5745 * HAProxy scheduler starts. This function expect only one LUA
5746 * argument that is a function. This function returns nothing, but
5747 * throws if an error is encountered.
5748 */
5749__LJMP static int hlua_register_init(lua_State *L)
5750{
5751 struct hlua_init_function *init;
5752 int ref;
5753
5754 MAY_LJMP(check_args(L, 1, "register_init"));
5755
5756 ref = MAY_LJMP(hlua_checkfunction(L, 1));
5757
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02005758 init = calloc(1, sizeof(*init));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01005759 if (!init)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01005760 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01005761
5762 init->function_ref = ref;
5763 LIST_ADDQ(&hlua_init_functions, &init->l);
5764 return 0;
5765}
5766
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005767/* This functio is an LUA binding. It permits to register a task
5768 * executed in parallel of the main HAroxy activity. The task is
5769 * created and it is set in the HAProxy scheduler. It can be called
5770 * from the "init" section, "post init" or during the runtime.
5771 *
5772 * Lua prototype:
5773 *
5774 * <none> core.register_task(<function>)
5775 */
5776static int hlua_register_task(lua_State *L)
5777{
5778 struct hlua *hlua;
5779 struct task *task;
5780 int ref;
5781
5782 MAY_LJMP(check_args(L, 1, "register_task"));
5783
5784 ref = MAY_LJMP(hlua_checkfunction(L, 1));
5785
Willy Tarreaubafbe012017-11-24 17:34:44 +01005786 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005787 if (!hlua)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01005788 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005789
Emeric Brunc60def82017-09-27 14:59:38 +02005790 task = task_new(MAX_THREADS_MASK);
Willy Tarreaue09101e2018-10-16 17:37:12 +02005791 if (!task)
5792 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
5793
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005794 task->context = hlua;
5795 task->process = hlua_process_task;
5796
5797 if (!hlua_ctx_init(hlua, task))
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01005798 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005799
5800 /* Restore the function in the stack. */
5801 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref);
5802 hlua->nargs = 0;
5803
5804 /* Schedule task. */
5805 task_schedule(task, now_ms);
5806
5807 return 0;
5808}
5809
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005810/* Wrapper called by HAProxy to execute an LUA converter. This wrapper
5811 * doesn't allow "yield" functions because the HAProxy engine cannot
5812 * resume converters.
5813 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02005814static int hlua_sample_conv_wrapper(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005815{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02005816 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02005817 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01005818 const char *error;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005819
Willy Tarreaube508f12016-03-10 11:47:01 +01005820 if (!stream)
5821 return 0;
5822
Willy Tarreau87b09662015-04-03 00:22:06 +02005823 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01005824 * Lua context can be not initialized. This behavior
5825 * permits to save performances because a systematic
5826 * Lua initialization cause 5% performances loss.
5827 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005828 if (!stream->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01005829 stream->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005830 if (!stream->hlua) {
5831 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
5832 return 0;
5833 }
5834 if (!hlua_ctx_init(stream->hlua, stream->task)) {
5835 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
5836 return 0;
5837 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01005838 }
5839
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005840 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005841 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005842
5843 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005844 if (!SET_SAFE_LJMP(stream->hlua->T)) {
5845 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
5846 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01005847 else
5848 error = "critical error";
5849 SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005850 return 0;
5851 }
5852
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005853 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005854 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005855 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005856 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005857 return 0;
5858 }
5859
5860 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005861 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005862
5863 /* convert input sample and pust-it in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005864 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005865 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005866 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005867 return 0;
5868 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005869 hlua_smp2lua(stream->hlua->T, smp);
5870 stream->hlua->nargs = 1;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005871
5872 /* push keywords in the stack. */
5873 if (arg_p) {
5874 for (; arg_p->type != ARGT_STOP; arg_p++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005875 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005876 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005877 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005878 return 0;
5879 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005880 hlua_arg2lua(stream->hlua->T, arg_p);
5881 stream->hlua->nargs++;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005882 }
5883 }
5884
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005885 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005886 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005887
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005888 /* At this point the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005889 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005890 }
5891
5892 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005893 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005894 /* finished. */
5895 case HLUA_E_OK:
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02005896 /* If the stack is empty, the function fails. */
5897 if (lua_gettop(stream->hlua->T) <= 0)
5898 return 0;
5899
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005900 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005901 hlua_lua2smp(stream->hlua->T, -1, smp);
5902 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005903 return 1;
5904
5905 /* yield. */
5906 case HLUA_E_AGAIN:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005907 SEND_ERR(stream->be, "Lua converter '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005908 return 0;
5909
5910 /* finished with error. */
5911 case HLUA_E_ERRMSG:
5912 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005913 SEND_ERR(stream->be, "Lua converter '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005914 fcn->name, lua_tostring(stream->hlua->T, -1));
5915 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005916 return 0;
5917
Thierry Fournierd5b073c2018-05-21 19:42:47 +02005918 case HLUA_E_ETMOUT:
5919 SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name);
5920 return 0;
5921
5922 case HLUA_E_NOMEM:
5923 SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name);
5924 return 0;
5925
5926 case HLUA_E_YIELD:
5927 SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name);
5928 return 0;
5929
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005930 case HLUA_E_ERR:
5931 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005932 SEND_ERR(stream->be, "Lua converter '%s' returns an unknown error.\n", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005933
5934 default:
5935 return 0;
5936 }
5937}
5938
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005939/* Wrapper called by HAProxy to execute a sample-fetch. this wrapper
5940 * doesn't allow "yield" functions because the HAProxy engine cannot
Willy Tarreaube508f12016-03-10 11:47:01 +01005941 * resume sample-fetches. This function will be called by the sample
5942 * fetch engine to call lua-based fetch operations.
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005943 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +02005944static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp,
5945 const char *kw, void *private)
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005946{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02005947 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02005948 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01005949 const char *error;
Willy Tarreau83061a82018-07-13 11:56:34 +02005950 const struct buffer msg = { };
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005951
Willy Tarreaube508f12016-03-10 11:47:01 +01005952 if (!stream)
5953 return 0;
Christopher Fauletafd8f102018-11-08 11:34:21 +01005954
Willy Tarreau87b09662015-04-03 00:22:06 +02005955 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01005956 * Lua context can be not initialized. This behavior
5957 * permits to save performances because a systematic
5958 * Lua initialization cause 5% performances loss.
5959 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005960 if (!stream->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01005961 stream->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005962 if (!stream->hlua) {
5963 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
5964 return 0;
5965 }
5966 if (!hlua_ctx_init(stream->hlua, stream->task)) {
5967 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
5968 return 0;
5969 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01005970 }
5971
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005972 consistency_set(stream, smp->opt, &stream->hlua->cons);
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005973
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005974 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005975 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005976
5977 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005978 if (!SET_SAFE_LJMP(stream->hlua->T)) {
5979 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
5980 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01005981 else
5982 error = "critical error";
5983 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005984 return 0;
5985 }
5986
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005987 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005988 if (!lua_checkstack(stream->hlua->T, 2)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005989 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005990 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005991 return 0;
5992 }
5993
5994 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005995 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005996
5997 /* push arguments in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005998 if (!hlua_txn_new(stream->hlua->T, stream, smp->px, smp->opt & SMP_OPT_DIR,
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005999 HLUA_TXN_NOTERM)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006000 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006001 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006002 return 0;
6003 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006004 stream->hlua->nargs = 1;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006005
6006 /* push keywords in the stack. */
6007 for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) {
6008 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006009 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006010 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006011 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006012 return 0;
6013 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006014 hlua_arg2lua(stream->hlua->T, arg_p);
6015 stream->hlua->nargs++;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006016 }
6017
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006018 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006019 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006020
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006021 /* At this point the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006022 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006023 }
6024
6025 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006026 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006027 /* finished. */
6028 case HLUA_E_OK:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006029 if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) {
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006030 stream_int_retnclose(&stream->si[0], &msg);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02006031 return 0;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006032 }
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02006033 /* If the stack is empty, the function fails. */
6034 if (lua_gettop(stream->hlua->T) <= 0)
6035 return 0;
6036
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006037 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006038 hlua_lua2smp(stream->hlua->T, -1, smp);
6039 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006040
6041 /* Set the end of execution flag. */
6042 smp->flags &= ~SMP_F_MAY_CHANGE;
6043 return 1;
6044
6045 /* yield. */
6046 case HLUA_E_AGAIN:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006047 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006048 stream_int_retnclose(&stream->si[0], &msg);
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006049 SEND_ERR(smp->px, "Lua sample-fetch '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006050 return 0;
6051
6052 /* finished with error. */
6053 case HLUA_E_ERRMSG:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006054 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006055 stream_int_retnclose(&stream->si[0], &msg);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006056 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006057 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006058 fcn->name, lua_tostring(stream->hlua->T, -1));
6059 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006060 return 0;
6061
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006062 case HLUA_E_ETMOUT:
6063 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
6064 stream_int_retnclose(&stream->si[0], &msg);
6065 SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name);
6066 return 0;
6067
6068 case HLUA_E_NOMEM:
6069 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
6070 stream_int_retnclose(&stream->si[0], &msg);
6071 SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name);
6072 return 0;
6073
6074 case HLUA_E_YIELD:
6075 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
6076 stream_int_retnclose(&stream->si[0], &msg);
6077 SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name);
6078 return 0;
6079
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006080 case HLUA_E_ERR:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006081 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006082 stream_int_retnclose(&stream->si[0], &msg);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006083 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006084 SEND_ERR(smp->px, "Lua sample-fetch '%s' returns an unknown error.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006085
6086 default:
6087 return 0;
6088 }
6089}
6090
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006091/* This function is an LUA binding used for registering
6092 * "sample-conv" functions. It expects a converter name used
6093 * in the haproxy configuration file, and an LUA function.
6094 */
6095__LJMP static int hlua_register_converters(lua_State *L)
6096{
6097 struct sample_conv_kw_list *sck;
6098 const char *name;
6099 int ref;
6100 int len;
6101 struct hlua_function *fcn;
6102
6103 MAY_LJMP(check_args(L, 2, "register_converters"));
6104
6105 /* First argument : converter name. */
6106 name = MAY_LJMP(luaL_checkstring(L, 1));
6107
6108 /* Second argument : lua function. */
6109 ref = MAY_LJMP(hlua_checkfunction(L, 2));
6110
6111 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006112 sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006113 if (!sck)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006114 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006115 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006116 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006117 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006118
6119 /* Fill fcn. */
6120 fcn->name = strdup(name);
6121 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006122 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006123 fcn->function_ref = ref;
6124
6125 /* List head */
6126 sck->list.n = sck->list.p = NULL;
6127
6128 /* converter keyword. */
6129 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006130 sck->kw[0].kw = calloc(1, len);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006131 if (!sck->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006132 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006133
6134 snprintf((char *)sck->kw[0].kw, len, "lua.%s", name);
6135 sck->kw[0].process = hlua_sample_conv_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01006136 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 +01006137 sck->kw[0].val_args = NULL;
6138 sck->kw[0].in_type = SMP_T_STR;
6139 sck->kw[0].out_type = SMP_T_STR;
6140 sck->kw[0].private = fcn;
6141
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006142 /* Register this new converter */
6143 sample_register_convs(sck);
6144
6145 return 0;
6146}
6147
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006148/* This function is an LUA binding used for registering
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006149 * "sample-fetch" functions. It expects a converter name used
6150 * in the haproxy configuration file, and an LUA function.
6151 */
6152__LJMP static int hlua_register_fetches(lua_State *L)
6153{
6154 const char *name;
6155 int ref;
6156 int len;
6157 struct sample_fetch_kw_list *sfk;
6158 struct hlua_function *fcn;
6159
6160 MAY_LJMP(check_args(L, 2, "register_fetches"));
6161
6162 /* First argument : sample-fetch name. */
6163 name = MAY_LJMP(luaL_checkstring(L, 1));
6164
6165 /* Second argument : lua function. */
6166 ref = MAY_LJMP(hlua_checkfunction(L, 2));
6167
6168 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006169 sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006170 if (!sfk)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006171 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006172 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006173 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006174 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006175
6176 /* Fill fcn. */
6177 fcn->name = strdup(name);
6178 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006179 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006180 fcn->function_ref = ref;
6181
6182 /* List head */
6183 sfk->list.n = sfk->list.p = NULL;
6184
6185 /* sample-fetch keyword. */
6186 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006187 sfk->kw[0].kw = calloc(1, len);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006188 if (!sfk->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006189 return luaL_error(L, "Lua out of memory error.");
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006190
6191 snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name);
6192 sfk->kw[0].process = hlua_sample_fetch_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01006193 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 +01006194 sfk->kw[0].val_args = NULL;
6195 sfk->kw[0].out_type = SMP_T_STR;
6196 sfk->kw[0].use = SMP_USE_HTTP_ANY;
6197 sfk->kw[0].val = 0;
6198 sfk->kw[0].private = fcn;
6199
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006200 /* Register this new fetch. */
6201 sample_register_fetches(sfk);
6202
6203 return 0;
6204}
6205
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006206/* This function is a wrapper to execute each LUA function declared
6207 * as an action wrapper during the initialisation period. This function
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006208 * return ACT_RET_CONT if the processing is finished (with or without
6209 * error) and return ACT_RET_YIELD if the function must be called again
6210 * because the LUA returns a yield.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006211 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006212static enum act_return hlua_action(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +02006213 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006214{
6215 char **arg;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006216 unsigned int analyzer;
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02006217 int dir;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006218 const char *error;
Willy Tarreau83061a82018-07-13 11:56:34 +02006219 const struct buffer msg = { };
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006220
6221 switch (rule->from) {
Thierry FOURNIER6e01f382015-11-02 09:52:54 +01006222 case ACT_F_TCP_REQ_CNT: analyzer = AN_REQ_INSPECT_FE ; dir = SMP_OPT_DIR_REQ; break;
6223 case ACT_F_TCP_RES_CNT: analyzer = AN_RES_INSPECT ; dir = SMP_OPT_DIR_RES; break;
6224 case ACT_F_HTTP_REQ: analyzer = AN_REQ_HTTP_PROCESS_FE; dir = SMP_OPT_DIR_REQ; break;
6225 case ACT_F_HTTP_RES: analyzer = AN_RES_HTTP_PROCESS_BE; dir = SMP_OPT_DIR_RES; break;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006226 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006227 SEND_ERR(px, "Lua: internal error while execute action.\n");
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006228 return ACT_RET_CONT;
6229 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006230
Willy Tarreau87b09662015-04-03 00:22:06 +02006231 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006232 * Lua context can be not initialized. This behavior
6233 * permits to save performances because a systematic
6234 * Lua initialization cause 5% performances loss.
6235 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006236 if (!s->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006237 s->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006238 if (!s->hlua) {
6239 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
6240 rule->arg.hlua_rule->fcn.name);
6241 return ACT_RET_CONT;
6242 }
6243 if (!hlua_ctx_init(s->hlua, s->task)) {
6244 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
6245 rule->arg.hlua_rule->fcn.name);
6246 return ACT_RET_CONT;
6247 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006248 }
6249
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006250 consistency_set(s, dir, &s->hlua->cons);
6251
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006252 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006253 if (!HLUA_IS_RUNNING(s->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006254
6255 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006256 if (!SET_SAFE_LJMP(s->hlua->T)) {
6257 if (lua_type(s->hlua->T, -1) == LUA_TSTRING)
6258 error = lua_tostring(s->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006259 else
6260 error = "critical error";
6261 SEND_ERR(px, "Lua function '%s': %s.\n",
6262 rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006263 return ACT_RET_CONT;
6264 }
6265
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006266 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006267 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006268 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006269 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006270 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006271 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006272 }
6273
6274 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006275 lua_rawgeti(s->hlua->T, LUA_REGISTRYINDEX, rule->arg.hlua_rule->fcn.function_ref);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006276
Willy Tarreau87b09662015-04-03 00:22:06 +02006277 /* Create and and push object stream in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006278 if (!hlua_txn_new(s->hlua->T, s, px, dir, 0)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006279 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006280 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006281 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006282 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006283 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006284 s->hlua->nargs = 1;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006285
6286 /* push keywords in the stack. */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006287 for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006288 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006289 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006290 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006291 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006292 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006293 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006294 lua_pushstring(s->hlua->T, *arg);
6295 s->hlua->nargs++;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006296 }
6297
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006298 /* Now the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006299 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006300
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006301 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006302 s->hlua->max_time = hlua_timeout_session;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006303 }
6304
6305 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006306 switch (hlua_ctx_resume(s->hlua, !(flags & ACT_FLAG_FINAL))) {
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006307 /* finished. */
6308 case HLUA_E_OK:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006309 if (!consistency_check(s, dir, &s->hlua->cons)) {
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006310 stream_int_retnclose(&s->si[0], &msg);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02006311 return ACT_RET_ERR;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006312 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006313 if (s->hlua->flags & HLUA_STOP)
Thierry FOURNIER9bd52d42016-07-14 11:45:33 +02006314 return ACT_RET_STOP;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006315 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006316
6317 /* yield. */
6318 case HLUA_E_AGAIN:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006319 /* Set timeout in the required channel. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006320 if (s->hlua->wake_time != TICK_ETERNITY) {
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006321 if (analyzer & (AN_REQ_INSPECT_FE|AN_REQ_HTTP_PROCESS_FE))
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006322 s->req.analyse_exp = s->hlua->wake_time;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006323 else if (analyzer & (AN_RES_INSPECT|AN_RES_HTTP_PROCESS_BE))
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006324 s->res.analyse_exp = s->hlua->wake_time;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006325 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006326 /* Some actions can be wake up when a "write" event
6327 * is detected on a response channel. This is useful
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006328 * only for actions targeted on the requests.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006329 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006330 if (HLUA_IS_WAKERESWR(s->hlua)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006331 s->res.flags |= CF_WAKE_WRITE;
Willy Tarreau76bd97f2015-03-10 17:16:10 +01006332 if ((analyzer & (AN_REQ_INSPECT_FE|AN_REQ_HTTP_PROCESS_FE)))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006333 s->res.analysers |= analyzer;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006334 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006335 if (HLUA_IS_WAKEREQWR(s->hlua))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006336 s->req.flags |= CF_WAKE_WRITE;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006337 /* We can quit the function without consistency check
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006338 * because HAProxy is not able to manipulate data, it
6339 * is only allowed to call me again. */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006340 return ACT_RET_YIELD;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006341
6342 /* finished with error. */
6343 case HLUA_E_ERRMSG:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006344 if (!consistency_check(s, dir, &s->hlua->cons)) {
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006345 stream_int_retnclose(&s->si[0], &msg);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02006346 return ACT_RET_ERR;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006347 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006348 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006349 SEND_ERR(px, "Lua function '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006350 rule->arg.hlua_rule->fcn.name, lua_tostring(s->hlua->T, -1));
6351 lua_pop(s->hlua->T, 1);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006352 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006353
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006354 case HLUA_E_ETMOUT:
6355 if (!consistency_check(s, dir, &s->hlua->cons)) {
6356 stream_int_retnclose(&s->si[0], &msg);
6357 return ACT_RET_ERR;
6358 }
6359 SEND_ERR(px, "Lua function '%s': execution timeout.\n", rule->arg.hlua_rule->fcn.name);
6360 return 0;
6361
6362 case HLUA_E_NOMEM:
6363 if (!consistency_check(s, dir, &s->hlua->cons)) {
6364 stream_int_retnclose(&s->si[0], &msg);
6365 return ACT_RET_ERR;
6366 }
6367 SEND_ERR(px, "Lua function '%s': out of memory error.\n", rule->arg.hlua_rule->fcn.name);
6368 return 0;
6369
6370 case HLUA_E_YIELD:
6371 if (!consistency_check(s, dir, &s->hlua->cons)) {
6372 stream_int_retnclose(&s->si[0], &msg);
6373 return ACT_RET_ERR;
6374 }
6375 SEND_ERR(px, "Lua function '%s': aborting Lua processing on expired timeout.\n",
6376 rule->arg.hlua_rule->fcn.name);
6377 return 0;
6378
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006379 case HLUA_E_ERR:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006380 if (!consistency_check(s, dir, &s->hlua->cons)) {
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006381 stream_int_retnclose(&s->si[0], &msg);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02006382 return ACT_RET_ERR;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006383 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006384 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006385 SEND_ERR(px, "Lua function '%s' return an unknown error.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006386 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006387
6388 default:
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006389 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006390 }
6391}
6392
Olivier Houchard9f6af332018-05-25 14:04:04 +02006393struct task *hlua_applet_wakeup(struct task *t, void *context, unsigned short state)
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006394{
Olivier Houchard9f6af332018-05-25 14:04:04 +02006395 struct appctx *ctx = context;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006396
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006397 appctx_wakeup(ctx);
Willy Tarreaud9587412017-08-23 16:07:33 +02006398 t->expire = TICK_ETERNITY;
Willy Tarreaud1aa41f2017-07-21 16:41:56 +02006399 return t;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006400}
6401
6402static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
6403{
6404 struct stream_interface *si = ctx->owner;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006405 struct hlua *hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006406 struct task *task;
6407 char **arg;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006408 const char *error;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006409
Willy Tarreaubafbe012017-11-24 17:34:44 +01006410 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006411 if (!hlua) {
6412 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
6413 ctx->rule->arg.hlua_rule->fcn.name);
6414 return 0;
6415 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006416 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006417 ctx->ctx.hlua_apptcp.hlua = hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006418 ctx->ctx.hlua_apptcp.flags = 0;
6419
6420 /* Create task used by signal to wakeup applets. */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01006421 task = task_new(tid_bit);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006422 if (!task) {
6423 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
6424 ctx->rule->arg.hlua_rule->fcn.name);
6425 return 0;
6426 }
6427 task->nice = 0;
6428 task->context = ctx;
6429 task->process = hlua_applet_wakeup;
6430 ctx->ctx.hlua_apptcp.task = task;
6431
6432 /* In the execution wrappers linked with a stream, the
6433 * Lua context can be not initialized. This behavior
6434 * permits to save performances because a systematic
6435 * Lua initialization cause 5% performances loss.
6436 */
6437 if (!hlua_ctx_init(hlua, task)) {
6438 SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n",
6439 ctx->rule->arg.hlua_rule->fcn.name);
6440 return 0;
6441 }
6442
6443 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02006444 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006445
6446 /* The following Lua calls can fail. */
6447 if (!SET_SAFE_LJMP(hlua->T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01006448 if (lua_type(hlua->T, -1) == LUA_TSTRING)
6449 error = lua_tostring(hlua->T, -1);
6450 else
6451 error = "critical error";
6452 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
6453 ctx->rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006454 return 0;
6455 }
6456
6457 /* Check stack available size. */
6458 if (!lua_checkstack(hlua->T, 1)) {
6459 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
6460 ctx->rule->arg.hlua_rule->fcn.name);
6461 RESET_SAFE_LJMP(hlua->T);
6462 return 0;
6463 }
6464
6465 /* Restore the function in the stack. */
6466 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref);
6467
6468 /* Create and and push object stream in the stack. */
6469 if (!hlua_applet_tcp_new(hlua->T, ctx)) {
6470 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
6471 ctx->rule->arg.hlua_rule->fcn.name);
6472 RESET_SAFE_LJMP(hlua->T);
6473 return 0;
6474 }
6475 hlua->nargs = 1;
6476
6477 /* push keywords in the stack. */
6478 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
6479 if (!lua_checkstack(hlua->T, 1)) {
6480 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
6481 ctx->rule->arg.hlua_rule->fcn.name);
6482 RESET_SAFE_LJMP(hlua->T);
6483 return 0;
6484 }
6485 lua_pushstring(hlua->T, *arg);
6486 hlua->nargs++;
6487 }
6488
6489 RESET_SAFE_LJMP(hlua->T);
6490
6491 /* Wakeup the applet ASAP. */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01006492 si_cant_get(si);
Willy Tarreau3367d412018-11-15 10:57:41 +01006493 si_rx_endp_more(si);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006494
6495 return 1;
6496}
6497
6498static void hlua_applet_tcp_fct(struct appctx *ctx)
6499{
6500 struct stream_interface *si = ctx->owner;
6501 struct stream *strm = si_strm(si);
6502 struct channel *res = si_ic(si);
6503 struct act_rule *rule = ctx->rule;
6504 struct proxy *px = strm->be;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006505 struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006506
6507 /* The applet execution is already done. */
Olivier Houchard594c8c52018-08-28 14:41:31 +02006508 if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) {
6509 /* eat the whole request */
6510 co_skip(si_oc(si), co_data(si_oc(si)));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006511 return;
Olivier Houchard594c8c52018-08-28 14:41:31 +02006512 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006513
6514 /* If the stream is disconnect or closed, ldo nothing. */
6515 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
6516 return;
6517
6518 /* Execute the function. */
6519 switch (hlua_ctx_resume(hlua, 1)) {
6520 /* finished. */
6521 case HLUA_E_OK:
6522 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
6523
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006524 /* eat the whole request */
Willy Tarreaua79021a2018-06-15 18:07:57 +02006525 co_skip(si_oc(si), co_data(si_oc(si)));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006526 res->flags |= CF_READ_NULL;
6527 si_shutr(si);
6528 return;
6529
6530 /* yield. */
6531 case HLUA_E_AGAIN:
Thierry Fournier0164f202016-02-20 17:47:43 +01006532 if (hlua->wake_time != TICK_ETERNITY)
6533 task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006534 return;
6535
6536 /* finished with error. */
6537 case HLUA_E_ERRMSG:
6538 /* Display log. */
6539 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
6540 rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1));
6541 lua_pop(hlua->T, 1);
6542 goto error;
6543
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006544 case HLUA_E_ETMOUT:
6545 SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n",
6546 rule->arg.hlua_rule->fcn.name);
6547 goto error;
6548
6549 case HLUA_E_NOMEM:
6550 SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n",
6551 rule->arg.hlua_rule->fcn.name);
6552 goto error;
6553
6554 case HLUA_E_YIELD: /* unexpected */
6555 SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n",
6556 rule->arg.hlua_rule->fcn.name);
6557 goto error;
6558
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006559 case HLUA_E_ERR:
6560 /* Display log. */
6561 SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n",
6562 rule->arg.hlua_rule->fcn.name);
6563 goto error;
6564
6565 default:
6566 goto error;
6567 }
6568
6569error:
6570
6571 /* For all other cases, just close the stream. */
6572 si_shutw(si);
6573 si_shutr(si);
6574 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
6575}
6576
6577static void hlua_applet_tcp_release(struct appctx *ctx)
6578{
Willy Tarreaubd7fc952017-07-24 17:35:27 +02006579 task_delete(ctx->ctx.hlua_apptcp.task);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006580 task_free(ctx->ctx.hlua_apptcp.task);
6581 ctx->ctx.hlua_apptcp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006582 hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006583 ctx->ctx.hlua_apptcp.hlua = NULL;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006584}
6585
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006586/* The function returns 1 if the initialisation is complete, 0 if
6587 * an errors occurs and -1 if more data are required for initializing
6588 * the applet.
6589 */
6590static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
6591{
6592 struct stream_interface *si = ctx->owner;
6593 struct channel *req = si_oc(si);
6594 struct http_msg *msg;
6595 struct http_txn *txn;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006596 struct hlua *hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006597 char **arg;
6598 struct hdr_ctx hdr;
6599 struct task *task;
6600 struct sample smp; /* just used for a valid call to smp_prefetch_http. */
Thierry Fournierfd107a22016-02-19 19:57:23 +01006601 const char *error;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006602
6603 /* Wait for a full HTTP request. */
6604 if (!smp_prefetch_http(px, strm, 0, NULL, &smp, 0)) {
6605 if (smp.flags & SMP_F_MAY_CHANGE)
6606 return -1;
6607 return 0;
6608 }
6609 txn = strm->txn;
6610 msg = &txn->req;
6611
Willy Tarreau0078bfc2015-10-07 20:20:28 +02006612 /* We want two things in HTTP mode :
6613 * - enforce server-close mode if we were in keep-alive, so that the
6614 * applet is released after each response ;
6615 * - enable request body transfer to the applet in order to resync
6616 * with the response body.
6617 */
6618 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)
6619 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreau0078bfc2015-10-07 20:20:28 +02006620
Willy Tarreaubafbe012017-11-24 17:34:44 +01006621 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006622 if (!hlua) {
6623 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
6624 ctx->rule->arg.hlua_rule->fcn.name);
6625 return 0;
6626 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006627 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006628 ctx->ctx.hlua_apphttp.hlua = hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006629 ctx->ctx.hlua_apphttp.left_bytes = -1;
6630 ctx->ctx.hlua_apphttp.flags = 0;
6631
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +01006632 if (txn->req.flags & HTTP_MSGF_VER_11)
6633 ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11;
6634
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006635 /* Create task used by signal to wakeup applets. */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01006636 task = task_new(tid_bit);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006637 if (!task) {
6638 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
6639 ctx->rule->arg.hlua_rule->fcn.name);
6640 return 0;
6641 }
6642 task->nice = 0;
6643 task->context = ctx;
6644 task->process = hlua_applet_wakeup;
6645 ctx->ctx.hlua_apphttp.task = task;
6646
6647 /* In the execution wrappers linked with a stream, the
6648 * Lua context can be not initialized. This behavior
6649 * permits to save performances because a systematic
6650 * Lua initialization cause 5% performances loss.
6651 */
6652 if (!hlua_ctx_init(hlua, task)) {
6653 SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n",
6654 ctx->rule->arg.hlua_rule->fcn.name);
6655 return 0;
6656 }
6657
6658 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02006659 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006660
6661 /* The following Lua calls can fail. */
6662 if (!SET_SAFE_LJMP(hlua->T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01006663 if (lua_type(hlua->T, -1) == LUA_TSTRING)
6664 error = lua_tostring(hlua->T, -1);
6665 else
6666 error = "critical error";
6667 SEND_ERR(px, "Lua applet http '%s': %s.\n",
6668 ctx->rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006669 return 0;
6670 }
6671
6672 /* Check stack available size. */
6673 if (!lua_checkstack(hlua->T, 1)) {
6674 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
6675 ctx->rule->arg.hlua_rule->fcn.name);
6676 RESET_SAFE_LJMP(hlua->T);
6677 return 0;
6678 }
6679
6680 /* Restore the function in the stack. */
6681 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref);
6682
6683 /* Create and and push object stream in the stack. */
6684 if (!hlua_applet_http_new(hlua->T, ctx)) {
6685 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
6686 ctx->rule->arg.hlua_rule->fcn.name);
6687 RESET_SAFE_LJMP(hlua->T);
6688 return 0;
6689 }
6690 hlua->nargs = 1;
6691
6692 /* Look for a 100-continue expected. */
6693 if (msg->flags & HTTP_MSGF_VER_11) {
6694 hdr.idx = 0;
Willy Tarreaua79021a2018-06-15 18:07:57 +02006695 if (http_find_header2("Expect", 6, ci_head(req), &txn->hdr_idx, &hdr) &&
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006696 unlikely(hdr.vlen == 12 && strncasecmp(hdr.line+hdr.val, "100-continue", 12) == 0))
6697 ctx->ctx.hlua_apphttp.flags |= APPLET_100C;
6698 }
6699
6700 /* push keywords in the stack. */
6701 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
6702 if (!lua_checkstack(hlua->T, 1)) {
6703 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
6704 ctx->rule->arg.hlua_rule->fcn.name);
6705 RESET_SAFE_LJMP(hlua->T);
6706 return 0;
6707 }
6708 lua_pushstring(hlua->T, *arg);
6709 hlua->nargs++;
6710 }
6711
6712 RESET_SAFE_LJMP(hlua->T);
6713
6714 /* Wakeup the applet when data is ready for read. */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01006715 si_cant_get(si);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006716
6717 return 1;
6718}
6719
6720static void hlua_applet_http_fct(struct appctx *ctx)
6721{
6722 struct stream_interface *si = ctx->owner;
6723 struct stream *strm = si_strm(si);
6724 struct channel *res = si_ic(si);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006725 struct act_rule *rule = ctx->rule;
6726 struct proxy *px = strm->be;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006727 struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua;
Willy Tarreau206ba832018-06-14 15:27:31 +02006728 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02006729 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02006730 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02006731 size_t len2;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006732 int ret;
6733
6734 /* If the stream is disconnect or closed, ldo nothing. */
6735 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
6736 return;
6737
6738 /* Set the currently running flag. */
6739 if (!HLUA_IS_RUNNING(hlua) &&
6740 !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
6741
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006742 /* Wait for full HTTP analysys. */
6743 if (unlikely(strm->txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01006744 si_cant_get(si);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006745 return;
6746 }
6747
6748 /* Store the max amount of bytes that we can read. */
6749 ctx->ctx.hlua_apphttp.left_bytes = strm->txn->req.body_len;
6750
6751 /* We need to flush the request header. This left the body
6752 * for the Lua.
6753 */
6754
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006755 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02006756 ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006757 if (ret == -1)
6758 return;
6759
6760 /* No data available, ask for more data. */
6761 if (ret == 1)
6762 len2 = 0;
6763 if (ret == 0)
6764 len1 = 0;
Thierry FOURNIER70d318c2018-06-30 10:37:33 +02006765 if (len1 + len2 < strm->txn->req.eoh + strm->txn->req.eol) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01006766 si_cant_get(si);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006767 return;
6768 }
6769
6770 /* skip the requests bytes. */
Thierry FOURNIER70d318c2018-06-30 10:37:33 +02006771 co_skip(si_oc(si), strm->txn->req.eoh + strm->txn->req.eol);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006772 }
6773
6774 /* Executes The applet if it is not done. */
6775 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
6776
6777 /* Execute the function. */
6778 switch (hlua_ctx_resume(hlua, 1)) {
6779 /* finished. */
6780 case HLUA_E_OK:
6781 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
6782 break;
6783
6784 /* yield. */
6785 case HLUA_E_AGAIN:
Thierry Fournier0164f202016-02-20 17:47:43 +01006786 if (hlua->wake_time != TICK_ETERNITY)
6787 task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006788 return;
6789
6790 /* finished with error. */
6791 case HLUA_E_ERRMSG:
6792 /* Display log. */
6793 SEND_ERR(px, "Lua applet http '%s': %s.\n",
6794 rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1));
6795 lua_pop(hlua->T, 1);
6796 goto error;
6797
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006798 case HLUA_E_ETMOUT:
6799 SEND_ERR(px, "Lua applet http '%s': execution timeout.\n",
6800 rule->arg.hlua_rule->fcn.name);
6801 goto error;
6802
6803 case HLUA_E_NOMEM:
6804 SEND_ERR(px, "Lua applet http '%s': out of memory error.\n",
6805 rule->arg.hlua_rule->fcn.name);
6806 goto error;
6807
6808 case HLUA_E_YIELD: /* unexpected */
6809 SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n",
6810 rule->arg.hlua_rule->fcn.name);
6811 goto error;
6812
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006813 case HLUA_E_ERR:
6814 /* Display log. */
6815 SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n",
6816 rule->arg.hlua_rule->fcn.name);
6817 goto error;
6818
6819 default:
6820 goto error;
6821 }
6822 }
6823
6824 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
6825
6826 /* We must send the final chunk. */
6827 if (ctx->ctx.hlua_apphttp.flags & APPLET_CHUNKED &&
6828 !(ctx->ctx.hlua_apphttp.flags & APPLET_LAST_CHK)) {
6829
6830 /* sent last chunk at once. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02006831 ret = ci_putblk(res, "0\r\n\r\n", 5);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006832
6833 /* critical error. */
6834 if (ret == -2 || ret == -3) {
6835 SEND_ERR(px, "Lua applet http '%s'cannont send last chunk.\n",
6836 rule->arg.hlua_rule->fcn.name);
6837 goto error;
6838 }
6839
6840 /* no enough space error. */
6841 if (ret == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01006842 si_rx_room_blk(si);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006843 return;
6844 }
6845
6846 /* set the last chunk sent. */
6847 ctx->ctx.hlua_apphttp.flags |= APPLET_LAST_CHK;
6848 }
6849
6850 /* close the connection. */
6851
Frédéric Lécaille83ed5d52018-07-18 14:25:26 +02006852 /* status */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006853 strm->txn->status = ctx->ctx.hlua_apphttp.status;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006854
6855 /* eat the whole request */
Willy Tarreaua79021a2018-06-15 18:07:57 +02006856 co_skip(si_oc(si), co_data(si_oc(si)));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006857 res->flags |= CF_READ_NULL;
6858 si_shutr(si);
6859
6860 return;
6861 }
6862
6863error:
6864
6865 /* If we are in HTTP mode, and we are not send any
6866 * data, return a 500 server error in best effort:
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006867 * if there is no room available in the buffer,
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006868 * just close the connection.
6869 */
Willy Tarreau06d80a92017-10-19 14:32:15 +02006870 ci_putblk(res, error_500, strlen(error_500));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006871 if (!(strm->flags & SF_ERR_MASK))
6872 strm->flags |= SF_ERR_RESOURCE;
6873 si_shutw(si);
6874 si_shutr(si);
6875 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
6876}
6877
6878static void hlua_applet_http_release(struct appctx *ctx)
6879{
Willy Tarreaubd7fc952017-07-24 17:35:27 +02006880 task_delete(ctx->ctx.hlua_apphttp.task);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006881 task_free(ctx->ctx.hlua_apphttp.task);
6882 ctx->ctx.hlua_apphttp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006883 hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006884 ctx->ctx.hlua_apphttp.hlua = NULL;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006885}
6886
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006887/* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in
6888 * succes case, else return ACT_RET_PRS_ERR.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006889 *
6890 * This function can fail with an abort() due to an Lua critical error.
6891 * We are in the configuration parsing process of HAProxy, this abort() is
6892 * tolerated.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006893 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006894static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px,
6895 struct act_rule *rule, char **err)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006896{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006897 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006898 int i;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006899
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006900 /* Memory for the rule. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006901 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006902 if (!rule->arg.hlua_rule) {
6903 memprintf(err, "out of memory error");
Thierry FOURNIERafa80492015-08-19 09:04:15 +02006904 return ACT_RET_PRS_ERR;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006905 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006906
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006907 /* Memory for arguments. */
6908 rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, sizeof(char *));
6909 if (!rule->arg.hlua_rule->args) {
6910 memprintf(err, "out of memory error");
6911 return ACT_RET_PRS_ERR;
6912 }
6913
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006914 /* Reference the Lua function and store the reference. */
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006915 rule->arg.hlua_rule->fcn = *fcn;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006916
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006917 /* Expect some arguments */
6918 for (i = 0; i < fcn->nargs; i++) {
6919 if (*args[i+1] == '\0') {
6920 memprintf(err, "expect %d arguments", fcn->nargs);
6921 return ACT_RET_PRS_ERR;
6922 }
6923 rule->arg.hlua_rule->args[i] = strdup(args[i + 1]);
6924 if (!rule->arg.hlua_rule->args[i]) {
6925 memprintf(err, "out of memory error");
6926 return ACT_RET_PRS_ERR;
6927 }
6928 (*cur_arg)++;
6929 }
6930 rule->arg.hlua_rule->args[i] = NULL;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006931
Thierry FOURNIER42148732015-09-02 17:17:33 +02006932 rule->action = ACT_CUSTOM;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006933 rule->action_ptr = hlua_action;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02006934 return ACT_RET_PRS_OK;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006935}
6936
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006937static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px,
6938 struct act_rule *rule, char **err)
6939{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006940 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006941
Christopher Fauletafd8f102018-11-08 11:34:21 +01006942 if (px->options2 & PR_O2_USE_HTX) {
6943 memprintf(err, "Lua services cannot be used when the HTX internal representation is enabled");
6944 return ACT_RET_PRS_ERR;
6945 }
6946
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01006947 /* HTTP applets are forbidden in tcp-request rules.
6948 * HTTP applet request requires everything initilized by
6949 * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER).
6950 * The applet will be immediately initilized, but its before
6951 * the call of this analyzer.
6952 */
6953 if (rule->from != ACT_F_HTTP_REQ) {
6954 memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets");
6955 return ACT_RET_PRS_ERR;
6956 }
6957
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006958 /* Memory for the rule. */
6959 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
6960 if (!rule->arg.hlua_rule) {
6961 memprintf(err, "out of memory error");
6962 return ACT_RET_PRS_ERR;
6963 }
6964
6965 /* Reference the Lua function and store the reference. */
6966 rule->arg.hlua_rule->fcn = *fcn;
6967
6968 /* TODO: later accept arguments. */
6969 rule->arg.hlua_rule->args = NULL;
6970
6971 /* Add applet pointer in the rule. */
6972 rule->applet.obj_type = OBJ_TYPE_APPLET;
6973 rule->applet.name = fcn->name;
6974 rule->applet.init = hlua_applet_http_init;
6975 rule->applet.fct = hlua_applet_http_fct;
6976 rule->applet.release = hlua_applet_http_release;
6977 rule->applet.timeout = hlua_timeout_applet;
6978
6979 return ACT_RET_PRS_OK;
6980}
6981
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006982/* This function is an LUA binding used for registering
6983 * "sample-conv" functions. It expects a converter name used
6984 * in the haproxy configuration file, and an LUA function.
6985 */
6986__LJMP static int hlua_register_action(lua_State *L)
6987{
6988 struct action_kw_list *akl;
6989 const char *name;
6990 int ref;
6991 int len;
6992 struct hlua_function *fcn;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006993 int nargs;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006994
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006995 /* Initialise the number of expected arguments at 0. */
6996 nargs = 0;
6997
6998 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
6999 WILL_LJMP(luaL_error(L, "'register_action' needs between 3 and 4 arguments"));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007000
7001 /* First argument : converter name. */
7002 name = MAY_LJMP(luaL_checkstring(L, 1));
7003
7004 /* Second argument : environment. */
7005 if (lua_type(L, 2) != LUA_TTABLE)
7006 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
7007
7008 /* Third argument : lua function. */
7009 ref = MAY_LJMP(hlua_checkfunction(L, 3));
7010
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007011 /* Fourth argument : number of mandatory arguments expected on the configuration line. */
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007012 if (lua_gettop(L) >= 4)
7013 nargs = MAY_LJMP(luaL_checkinteger(L, 4));
7014
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007015 /* browse the second argument as an array. */
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007016 lua_pushnil(L);
7017 while (lua_next(L, 2) != 0) {
7018 if (lua_type(L, -1) != LUA_TSTRING)
7019 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
7020
7021 /* Check required environment. Only accepted "http" or "tcp". */
7022 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007023 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007024 if (!akl)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007025 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007026 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007027 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007028 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007029
7030 /* Fill fcn. */
7031 fcn->name = strdup(name);
7032 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007033 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007034 fcn->function_ref = ref;
7035
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007036 /* Set the expected number od arguments. */
7037 fcn->nargs = nargs;
7038
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007039 /* List head */
7040 akl->list.n = akl->list.p = NULL;
7041
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007042 /* action keyword. */
7043 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007044 akl->kw[0].kw = calloc(1, len);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007045 if (!akl->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007046 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007047
7048 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
7049
7050 akl->kw[0].match_pfx = 0;
7051 akl->kw[0].private = fcn;
7052 akl->kw[0].parse = action_register_lua;
7053
7054 /* select the action registering point. */
7055 if (strcmp(lua_tostring(L, -1), "tcp-req") == 0)
7056 tcp_req_cont_keywords_register(akl);
7057 else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0)
7058 tcp_res_cont_keywords_register(akl);
7059 else if (strcmp(lua_tostring(L, -1), "http-req") == 0)
7060 http_req_keywords_register(akl);
7061 else if (strcmp(lua_tostring(L, -1), "http-res") == 0)
7062 http_res_keywords_register(akl);
7063 else
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007064 WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. "
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007065 "'tcp-req', 'tcp-res', 'http-req' or 'http-res' "
7066 "are expected.", lua_tostring(L, -1)));
7067
7068 /* pop the environment string. */
7069 lua_pop(L, 1);
7070 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007071 return ACT_RET_PRS_OK;
7072}
7073
7074static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px,
7075 struct act_rule *rule, char **err)
7076{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007077 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007078
Christopher Fauletafd8f102018-11-08 11:34:21 +01007079 if (px->options2 & PR_O2_USE_HTX) {
7080 memprintf(err, "Lua services cannot be used when the HTX internal representation is enabled");
7081 return ACT_RET_PRS_ERR;
7082 }
7083
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007084 /* Memory for the rule. */
7085 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
7086 if (!rule->arg.hlua_rule) {
7087 memprintf(err, "out of memory error");
7088 return ACT_RET_PRS_ERR;
7089 }
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007090
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007091 /* Reference the Lua function and store the reference. */
7092 rule->arg.hlua_rule->fcn = *fcn;
7093
7094 /* TODO: later accept arguments. */
7095 rule->arg.hlua_rule->args = NULL;
7096
7097 /* Add applet pointer in the rule. */
7098 rule->applet.obj_type = OBJ_TYPE_APPLET;
7099 rule->applet.name = fcn->name;
7100 rule->applet.init = hlua_applet_tcp_init;
7101 rule->applet.fct = hlua_applet_tcp_fct;
7102 rule->applet.release = hlua_applet_tcp_release;
7103 rule->applet.timeout = hlua_timeout_applet;
7104
7105 return 0;
7106}
7107
7108/* This function is an LUA binding used for registering
7109 * "sample-conv" functions. It expects a converter name used
7110 * in the haproxy configuration file, and an LUA function.
7111 */
7112__LJMP static int hlua_register_service(lua_State *L)
7113{
7114 struct action_kw_list *akl;
7115 const char *name;
7116 const char *env;
7117 int ref;
7118 int len;
7119 struct hlua_function *fcn;
7120
7121 MAY_LJMP(check_args(L, 3, "register_service"));
7122
7123 /* First argument : converter name. */
7124 name = MAY_LJMP(luaL_checkstring(L, 1));
7125
7126 /* Second argument : environment. */
7127 env = MAY_LJMP(luaL_checkstring(L, 2));
7128
7129 /* Third argument : lua function. */
7130 ref = MAY_LJMP(hlua_checkfunction(L, 3));
7131
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007132 /* Allocate and fill the sample fetch keyword struct. */
7133 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
7134 if (!akl)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007135 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007136 fcn = calloc(1, sizeof(*fcn));
7137 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007138 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007139
7140 /* Fill fcn. */
7141 len = strlen("<lua.>") + strlen(name) + 1;
7142 fcn->name = calloc(1, len);
7143 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007144 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007145 snprintf((char *)fcn->name, len, "<lua.%s>", name);
7146 fcn->function_ref = ref;
7147
7148 /* List head */
7149 akl->list.n = akl->list.p = NULL;
7150
7151 /* converter keyword. */
7152 len = strlen("lua.") + strlen(name) + 1;
7153 akl->kw[0].kw = calloc(1, len);
7154 if (!akl->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007155 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007156
7157 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
7158
Thierry FOURNIER / OZON.IO02564fd2016-11-12 11:07:05 +01007159 /* Check required environment. Only accepted "http" or "tcp". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007160 if (strcmp(env, "tcp") == 0)
7161 akl->kw[0].parse = action_register_service_tcp;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007162 else if (strcmp(env, "http") == 0)
7163 akl->kw[0].parse = action_register_service_http;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007164 else
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007165 WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. "
Eric Salamafe7456f2017-12-21 14:30:07 +01007166 "'tcp' or 'http' are expected.", env));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007167
7168 akl->kw[0].match_pfx = 0;
7169 akl->kw[0].private = fcn;
7170
7171 /* End of array. */
7172 memset(&akl->kw[1], 0, sizeof(*akl->kw));
7173
7174 /* Register this new converter */
7175 service_keywords_register(akl);
7176
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007177 return 0;
7178}
7179
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007180/* This function initialises Lua cli handler. It copies the
7181 * arguments in the Lua stack and create channel IO objects.
7182 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02007183static int hlua_cli_parse_fct(char **args, char *payload, struct appctx *appctx, void *private)
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007184{
7185 struct hlua *hlua;
7186 struct hlua_function *fcn;
7187 int i;
7188 const char *error;
7189
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007190 fcn = private;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007191 appctx->ctx.hlua_cli.fcn = private;
7192
Willy Tarreaubafbe012017-11-24 17:34:44 +01007193 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007194 if (!hlua) {
7195 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007196 return 1;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007197 }
7198 HLUA_INIT(hlua);
7199 appctx->ctx.hlua_cli.hlua = hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007200
7201 /* Create task used by signal to wakeup applets.
7202 * We use the same wakeup fonction than the Lua applet_tcp and
7203 * applet_http. It is absolutely compatible.
7204 */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01007205 appctx->ctx.hlua_cli.task = task_new(tid_bit);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007206 if (!appctx->ctx.hlua_cli.task) {
Thierry FOURNIERffbf5692016-12-16 11:14:06 +01007207 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007208 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007209 }
7210 appctx->ctx.hlua_cli.task->nice = 0;
7211 appctx->ctx.hlua_cli.task->context = appctx;
7212 appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup;
7213
7214 /* Initialises the Lua context */
7215 if (!hlua_ctx_init(hlua, appctx->ctx.hlua_cli.task)) {
7216 SEND_ERR(NULL, "Lua cli '%s': can't initialize Lua context.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007217 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007218 }
7219
7220 /* The following Lua calls can fail. */
7221 if (!SET_SAFE_LJMP(hlua->T)) {
7222 if (lua_type(hlua->T, -1) == LUA_TSTRING)
7223 error = lua_tostring(hlua->T, -1);
7224 else
7225 error = "critical error";
7226 SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error);
7227 goto error;
7228 }
7229
7230 /* Check stack available size. */
7231 if (!lua_checkstack(hlua->T, 2)) {
7232 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7233 goto error;
7234 }
7235
7236 /* Restore the function in the stack. */
7237 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
7238
7239 /* Once the arguments parsed, the CLI is like an AppletTCP,
7240 * so push AppletTCP in the stack.
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007241 */
7242 if (!hlua_applet_tcp_new(hlua->T, appctx)) {
7243 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7244 goto error;
7245 }
7246 hlua->nargs = 1;
7247
7248 /* push keywords in the stack. */
7249 for (i = 0; *args[i]; i++) {
7250 /* Check stack available size. */
7251 if (!lua_checkstack(hlua->T, 1)) {
7252 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7253 goto error;
7254 }
7255 lua_pushstring(hlua->T, args[i]);
7256 hlua->nargs++;
7257 }
7258
7259 /* We must initialize the execution timeouts. */
7260 hlua->max_time = hlua_timeout_session;
7261
7262 /* At this point the execution is safe. */
7263 RESET_SAFE_LJMP(hlua->T);
7264
7265 /* It's ok */
7266 return 0;
7267
7268 /* It's not ok. */
7269error:
7270 RESET_SAFE_LJMP(hlua->T);
7271 hlua_ctx_destroy(hlua);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007272 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007273 return 1;
7274}
7275
7276static int hlua_cli_io_handler_fct(struct appctx *appctx)
7277{
7278 struct hlua *hlua;
7279 struct stream_interface *si;
7280 struct hlua_function *fcn;
7281
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007282 hlua = appctx->ctx.hlua_cli.hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007283 si = appctx->owner;
Willy Tarreau8ae4f752016-12-14 15:41:45 +01007284 fcn = appctx->ctx.hlua_cli.fcn;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007285
7286 /* If the stream is disconnect or closed, ldo nothing. */
7287 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
7288 return 1;
7289
7290 /* Execute the function. */
7291 switch (hlua_ctx_resume(hlua, 1)) {
7292
7293 /* finished. */
7294 case HLUA_E_OK:
7295 return 1;
7296
7297 /* yield. */
7298 case HLUA_E_AGAIN:
7299 /* We want write. */
7300 if (HLUA_IS_WAKERESWR(hlua))
Willy Tarreaudb398432018-11-15 11:08:52 +01007301 si_rx_room_blk(si);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007302 /* Set the timeout. */
7303 if (hlua->wake_time != TICK_ETERNITY)
7304 task_schedule(hlua->task, hlua->wake_time);
7305 return 0;
7306
7307 /* finished with error. */
7308 case HLUA_E_ERRMSG:
7309 /* Display log. */
7310 SEND_ERR(NULL, "Lua cli '%s': %s.\n",
7311 fcn->name, lua_tostring(hlua->T, -1));
7312 lua_pop(hlua->T, 1);
7313 return 1;
7314
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007315 case HLUA_E_ETMOUT:
7316 SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n",
7317 fcn->name);
7318 return 1;
7319
7320 case HLUA_E_NOMEM:
7321 SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n",
7322 fcn->name);
7323 return 1;
7324
7325 case HLUA_E_YIELD: /* unexpected */
7326 SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n",
7327 fcn->name);
7328 return 1;
7329
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007330 case HLUA_E_ERR:
7331 /* Display log. */
7332 SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n",
7333 fcn->name);
7334 return 1;
7335
7336 default:
7337 return 1;
7338 }
7339
7340 return 1;
7341}
7342
7343static void hlua_cli_io_release_fct(struct appctx *appctx)
7344{
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007345 hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007346 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007347}
7348
7349/* This function is an LUA binding used for registering
7350 * new keywords in the cli. It expects a list of keywords
7351 * which are the "path". It is limited to 5 keywords. A
7352 * description of the command, a function to be executed
7353 * for the parsing and a function for io handlers.
7354 */
7355__LJMP static int hlua_register_cli(lua_State *L)
7356{
7357 struct cli_kw_list *cli_kws;
7358 const char *message;
7359 int ref_io;
7360 int len;
7361 struct hlua_function *fcn;
7362 int index;
7363 int i;
7364
7365 MAY_LJMP(check_args(L, 3, "register_cli"));
7366
7367 /* First argument : an array of maximum 5 keywords. */
7368 if (!lua_istable(L, 1))
7369 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table"));
7370
7371 /* Second argument : string with contextual message. */
7372 message = MAY_LJMP(luaL_checkstring(L, 2));
7373
7374 /* Third and fourth argument : lua function. */
7375 ref_io = MAY_LJMP(hlua_checkfunction(L, 3));
7376
7377 /* Allocate and fill the sample fetch keyword struct. */
7378 cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2);
7379 if (!cli_kws)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007380 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007381 fcn = calloc(1, sizeof(*fcn));
7382 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007383 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007384
7385 /* Fill path. */
7386 index = 0;
7387 lua_pushnil(L);
7388 while(lua_next(L, 1) != 0) {
7389 if (index >= 5)
7390 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries"));
7391 if (lua_type(L, -1) != LUA_TSTRING)
7392 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings"));
7393 cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1));
7394 if (!cli_kws->kw[0].str_kw[index])
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007395 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007396 index++;
7397 lua_pop(L, 1);
7398 }
7399
7400 /* Copy help message. */
7401 cli_kws->kw[0].usage = strdup(message);
7402 if (!cli_kws->kw[0].usage)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007403 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007404
7405 /* Fill fcn io handler. */
7406 len = strlen("<lua.cli>") + 1;
7407 for (i = 0; i < index; i++)
7408 len += strlen(cli_kws->kw[0].str_kw[i]) + 1;
7409 fcn->name = calloc(1, len);
7410 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007411 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007412 strncat((char *)fcn->name, "<lua.cli", len);
7413 for (i = 0; i < index; i++) {
7414 strncat((char *)fcn->name, ".", len);
7415 strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len);
7416 }
7417 strncat((char *)fcn->name, ">", len);
7418 fcn->function_ref = ref_io;
7419
7420 /* Fill last entries. */
7421 cli_kws->kw[0].private = fcn;
7422 cli_kws->kw[0].parse = hlua_cli_parse_fct;
7423 cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct;
7424 cli_kws->kw[0].io_release = hlua_cli_io_release_fct;
7425
7426 /* Register this new converter */
7427 cli_register_kw(cli_kws);
7428
7429 return 0;
7430}
7431
Thierry FOURNIERbd413492015-03-03 16:52:26 +01007432static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx,
7433 struct proxy *defpx, const char *file, int line,
7434 char **err, unsigned int *timeout)
7435{
7436 const char *error;
7437
7438 error = parse_time_err(args[1], timeout, TIME_UNIT_MS);
7439 if (error && *error != '\0') {
7440 memprintf(err, "%s: invalid timeout", args[0]);
7441 return -1;
7442 }
7443 return 0;
7444}
7445
7446static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx,
7447 struct proxy *defpx, const char *file, int line,
7448 char **err)
7449{
7450 return hlua_read_timeout(args, section_type, curpx, defpx,
7451 file, line, err, &hlua_timeout_session);
7452}
7453
7454static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx,
7455 struct proxy *defpx, const char *file, int line,
7456 char **err)
7457{
7458 return hlua_read_timeout(args, section_type, curpx, defpx,
7459 file, line, err, &hlua_timeout_task);
7460}
7461
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007462static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx,
7463 struct proxy *defpx, const char *file, int line,
7464 char **err)
7465{
7466 return hlua_read_timeout(args, section_type, curpx, defpx,
7467 file, line, err, &hlua_timeout_applet);
7468}
7469
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01007470static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx,
7471 struct proxy *defpx, const char *file, int line,
7472 char **err)
7473{
7474 char *error;
7475
7476 hlua_nb_instruction = strtoll(args[1], &error, 10);
7477 if (*error != '\0') {
7478 memprintf(err, "%s: invalid number", args[0]);
7479 return -1;
7480 }
7481 return 0;
7482}
7483
Willy Tarreau32f61e22015-03-18 17:54:59 +01007484static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx,
7485 struct proxy *defpx, const char *file, int line,
7486 char **err)
7487{
7488 char *error;
7489
7490 if (*(args[1]) == 0) {
7491 memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]);
7492 return -1;
7493 }
7494 hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L;
7495 if (*error != '\0') {
7496 memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error);
7497 return -1;
7498 }
7499 return 0;
7500}
7501
7502
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007503/* This function is called by the main configuration key "lua-load". It loads and
7504 * execute an lua file during the parsing of the HAProxy configuration file. It is
7505 * the main lua entry point.
7506 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007507 * This function runs with the HAProxy keywords API. It returns -1 if an error
7508 * occurs, otherwise it returns 0.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007509 *
7510 * In some error case, LUA set an error message in top of the stack. This function
7511 * returns this error message in the HAProxy logs and pop it from the stack.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007512 *
7513 * This function can fail with an abort() due to an Lua critical error.
7514 * We are in the configuration parsing process of HAProxy, this abort() is
7515 * tolerated.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007516 */
7517static int hlua_load(char **args, int section_type, struct proxy *curpx,
7518 struct proxy *defpx, const char *file, int line,
7519 char **err)
7520{
7521 int error;
7522
7523 /* Just load and compile the file. */
7524 error = luaL_loadfile(gL.T, args[1]);
7525 if (error) {
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007526 memprintf(err, "error in Lua file '%s': %s", args[1], lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007527 lua_pop(gL.T, 1);
7528 return -1;
7529 }
7530
7531 /* If no syntax error where detected, execute the code. */
7532 error = lua_pcall(gL.T, 0, LUA_MULTRET, 0);
7533 switch (error) {
7534 case LUA_OK:
7535 break;
7536 case LUA_ERRRUN:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007537 memprintf(err, "Lua runtime error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007538 lua_pop(gL.T, 1);
7539 return -1;
7540 case LUA_ERRMEM:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007541 memprintf(err, "Lua out of memory error.n");
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007542 return -1;
7543 case LUA_ERRERR:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007544 memprintf(err, "Lua message handler error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007545 lua_pop(gL.T, 1);
7546 return -1;
7547 case LUA_ERRGCMM:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007548 memprintf(err, "Lua garbage collector error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007549 lua_pop(gL.T, 1);
7550 return -1;
7551 default:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007552 memprintf(err, "Lua unknonwn error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007553 lua_pop(gL.T, 1);
7554 return -1;
7555 }
7556
7557 return 0;
7558}
7559
7560/* configuration keywords declaration */
7561static struct cfg_kw_list cfg_kws = {{ },{
Thierry FOURNIERbd413492015-03-03 16:52:26 +01007562 { CFG_GLOBAL, "lua-load", hlua_load },
7563 { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout },
7564 { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout },
Thierry FOURNIER56da1012015-10-01 08:42:31 +02007565 { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout },
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01007566 { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield },
Willy Tarreau32f61e22015-03-18 17:54:59 +01007567 { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem },
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007568 { 0, NULL, NULL },
7569}};
7570
Willy Tarreau0108d902018-11-25 19:14:37 +01007571INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
7572
Christopher Fauletafd8f102018-11-08 11:34:21 +01007573
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007574/* This function can fail with an abort() due to an Lua critical error.
7575 * We are in the initialisation process of HAProxy, this abort() is
7576 * tolerated.
7577 */
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007578int hlua_post_init()
7579{
7580 struct hlua_init_function *init;
7581 const char *msg;
7582 enum hlua_exec ret;
Thierry Fournierfd107a22016-02-19 19:57:23 +01007583 const char *error;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007584
Thierry Fournier3d4a6752016-02-19 20:53:30 +01007585 /* Call post initialisation function in safe environement. */
7586 if (!SET_SAFE_LJMP(gL.T)) {
7587 if (lua_type(gL.T, -1) == LUA_TSTRING)
7588 error = lua_tostring(gL.T, -1);
7589 else
7590 error = "critical error";
7591 fprintf(stderr, "Lua post-init: %s.\n", error);
7592 exit(1);
7593 }
Frédéric Lécaille54f2bcf2018-08-29 13:46:24 +02007594
7595#if USE_OPENSSL
7596 /* Initialize SSL server. */
7597 if (socket_ssl.xprt->prepare_srv) {
7598 int saved_used_backed = global.ssl_used_backend;
7599 // don't affect maxconn automatic computation
7600 socket_ssl.xprt->prepare_srv(&socket_ssl);
7601 global.ssl_used_backend = saved_used_backed;
7602 }
7603#endif
7604
Thierry Fournier3d4a6752016-02-19 20:53:30 +01007605 hlua_fcn_post_init(gL.T);
7606 RESET_SAFE_LJMP(gL.T);
7607
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007608 list_for_each_entry(init, &hlua_init_functions, l) {
7609 lua_rawgeti(gL.T, LUA_REGISTRYINDEX, init->function_ref);
7610 ret = hlua_ctx_resume(&gL, 0);
7611 switch (ret) {
7612 case HLUA_E_OK:
7613 lua_pop(gL.T, -1);
7614 return 1;
7615 case HLUA_E_AGAIN:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007616 ha_alert("Lua init: yield not allowed.\n");
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007617 return 0;
7618 case HLUA_E_ERRMSG:
7619 msg = lua_tostring(gL.T, -1);
Christopher Faulet767a84b2017-11-24 16:50:31 +01007620 ha_alert("lua init: %s.\n", msg);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007621 return 0;
7622 case HLUA_E_ERR:
7623 default:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007624 ha_alert("Lua init: unknown runtime error.\n");
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007625 return 0;
7626 }
7627 }
7628 return 1;
7629}
7630
Willy Tarreau32f61e22015-03-18 17:54:59 +01007631/* The memory allocator used by the Lua stack. <ud> is a pointer to the
7632 * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize>
7633 * is the previously allocated size or the kind of object in case of a new
7634 * allocation. <nsize> is the requested new size.
7635 */
7636static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
7637{
7638 struct hlua_mem_allocator *zone = ud;
7639
7640 if (nsize == 0) {
7641 /* it's a free */
7642 if (ptr)
7643 zone->allocated -= osize;
7644 free(ptr);
7645 return NULL;
7646 }
7647
7648 if (!ptr) {
7649 /* it's a new allocation */
7650 if (zone->limit && zone->allocated + nsize > zone->limit)
7651 return NULL;
7652
7653 ptr = malloc(nsize);
7654 if (ptr)
7655 zone->allocated += nsize;
7656 return ptr;
7657 }
7658
7659 /* it's a realloc */
7660 if (zone->limit && zone->allocated + nsize - osize > zone->limit)
7661 return NULL;
7662
7663 ptr = realloc(ptr, nsize);
7664 if (ptr)
7665 zone->allocated += nsize - osize;
7666 return ptr;
7667}
7668
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007669/* Ithis function can fail with an abort() due to an Lua critical error.
7670 * We are in the initialisation process of HAProxy, this abort() is
7671 * tolerated.
7672 */
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01007673void hlua_init(void)
7674{
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007675 int i;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01007676 int idx;
7677 struct sample_fetch *sf;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007678 struct sample_conv *sc;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01007679 char *p;
Thierry Fournierfd107a22016-02-19 19:57:23 +01007680 const char *error_msg;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01007681#ifdef USE_OPENSSL
Willy Tarreau80f5fae2015-02-27 16:38:20 +01007682 struct srv_kw *kw;
7683 int tmp_error;
7684 char *error;
Thierry FOURNIER36d13742015-03-17 16:48:53 +01007685 char *args[] = { /* SSL client configuration. */
7686 "ssl",
7687 "verify",
7688 "none",
Thierry FOURNIER36d13742015-03-17 16:48:53 +01007689 NULL
7690 };
Willy Tarreau80f5fae2015-02-27 16:38:20 +01007691#endif
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007692
Thierry FOURNIER380d0932015-01-23 14:27:52 +01007693 /* Init main lua stack. */
7694 gL.Mref = LUA_REFNIL;
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01007695 gL.flags = 0;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01007696 LIST_INIT(&gL.com);
Willy Tarreau42ef75f2017-04-12 21:40:29 +02007697 gL.T = lua_newstate(hlua_alloc, &hlua_global_allocator);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01007698 hlua_sethlua(&gL);
7699 gL.Tref = LUA_REFNIL;
7700 gL.task = NULL;
7701
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007702 /* From this point, until the end of the initialisation function,
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007703 * the Lua function can fail with an abort. We are in the initialisation
7704 * process of HAProxy, this abort() is tolerated.
7705 */
7706
Thierry FOURNIER380d0932015-01-23 14:27:52 +01007707 /* Initialise lua. */
7708 luaL_openlibs(gL.T);
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007709
Thierry Fournier75933d42016-01-21 09:30:18 +01007710 /* Set safe environment for the initialisation. */
7711 if (!SET_SAFE_LJMP(gL.T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01007712 if (lua_type(gL.T, -1) == LUA_TSTRING)
7713 error_msg = lua_tostring(gL.T, -1);
7714 else
7715 error_msg = "critical error";
7716 fprintf(stderr, "Lua init: %s.\n", error_msg);
Thierry Fournier75933d42016-01-21 09:30:18 +01007717 exit(1);
7718 }
7719
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007720 /*
7721 *
7722 * Create "core" object.
7723 *
7724 */
7725
Thierry FOURNIERa2d8c652015-03-11 17:29:39 +01007726 /* This table entry is the object "core" base. */
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007727 lua_newtable(gL.T);
7728
7729 /* Push the loglevel constants. */
Willy Tarreau80f5fae2015-02-27 16:38:20 +01007730 for (i = 0; i < NB_LOG_LEVELS; i++)
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007731 hlua_class_const_int(gL.T, log_levels[i], i);
7732
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007733 /* Register special functions. */
7734 hlua_class_function(gL.T, "register_init", hlua_register_init);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01007735 hlua_class_function(gL.T, "register_task", hlua_register_task);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01007736 hlua_class_function(gL.T, "register_fetches", hlua_register_fetches);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01007737 hlua_class_function(gL.T, "register_converters", hlua_register_converters);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007738 hlua_class_function(gL.T, "register_action", hlua_register_action);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007739 hlua_class_function(gL.T, "register_service", hlua_register_service);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007740 hlua_class_function(gL.T, "register_cli", hlua_register_cli);
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01007741 hlua_class_function(gL.T, "yield", hlua_yield);
Willy Tarreau59551662015-03-10 14:23:13 +01007742 hlua_class_function(gL.T, "set_nice", hlua_set_nice);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01007743 hlua_class_function(gL.T, "sleep", hlua_sleep);
7744 hlua_class_function(gL.T, "msleep", hlua_msleep);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01007745 hlua_class_function(gL.T, "add_acl", hlua_add_acl);
7746 hlua_class_function(gL.T, "del_acl", hlua_del_acl);
7747 hlua_class_function(gL.T, "set_map", hlua_set_map);
7748 hlua_class_function(gL.T, "del_map", hlua_del_map);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007749 hlua_class_function(gL.T, "tcp", hlua_socket_new);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01007750 hlua_class_function(gL.T, "log", hlua_log);
7751 hlua_class_function(gL.T, "Debug", hlua_log_debug);
7752 hlua_class_function(gL.T, "Info", hlua_log_info);
7753 hlua_class_function(gL.T, "Warning", hlua_log_warning);
7754 hlua_class_function(gL.T, "Alert", hlua_log_alert);
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02007755 hlua_class_function(gL.T, "done", hlua_done);
Thierry Fournierfb0b5462016-01-21 09:28:58 +01007756 hlua_fcn_reg_core_fcn(gL.T);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007757
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007758 lua_setglobal(gL.T, "core");
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01007759
7760 /*
7761 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02007762 * Register class Map
7763 *
7764 */
7765
7766 /* This table entry is the object "Map" base. */
7767 lua_newtable(gL.T);
7768
7769 /* register pattern types. */
7770 for (i=0; i<PAT_MATCH_NUM; i++)
7771 hlua_class_const_int(gL.T, pat_match_names[i], i);
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01007772 for (i=0; i<PAT_MATCH_NUM; i++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007773 snprintf(trash.area, trash.size, "_%s", pat_match_names[i]);
7774 hlua_class_const_int(gL.T, trash.area, i);
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01007775 }
Thierry FOURNIER3def3932015-04-07 11:27:54 +02007776
7777 /* register constructor. */
7778 hlua_class_function(gL.T, "new", hlua_map_new);
7779
7780 /* Create and fill the metatable. */
7781 lua_newtable(gL.T);
7782
7783 /* Create and fille the __index entry. */
7784 lua_pushstring(gL.T, "__index");
7785 lua_newtable(gL.T);
7786
7787 /* Register . */
7788 hlua_class_function(gL.T, "lookup", hlua_map_lookup);
7789 hlua_class_function(gL.T, "slookup", hlua_map_slookup);
7790
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007791 lua_rawset(gL.T, -3);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02007792
Thierry Fournier45e78d72016-02-19 18:34:46 +01007793 /* Register previous table in the registry with reference and named entry.
7794 * The function hlua_register_metatable() pops the stack, so we
7795 * previously create a copy of the table.
7796 */
Thierry FOURNIER3def3932015-04-07 11:27:54 +02007797 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007798 class_map_ref = hlua_register_metatable(gL.T, CLASS_MAP);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02007799
7800 /* Assign the metatable to the mai Map object. */
7801 lua_setmetatable(gL.T, -2);
7802
7803 /* Set a name to the table. */
7804 lua_setglobal(gL.T, "Map");
7805
7806 /*
7807 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01007808 * Register class Channel
7809 *
7810 */
7811
7812 /* Create and fill the metatable. */
7813 lua_newtable(gL.T);
7814
7815 /* Create and fille the __index entry. */
7816 lua_pushstring(gL.T, "__index");
7817 lua_newtable(gL.T);
7818
7819 /* Register . */
7820 hlua_class_function(gL.T, "get", hlua_channel_get);
7821 hlua_class_function(gL.T, "dup", hlua_channel_dup);
7822 hlua_class_function(gL.T, "getline", hlua_channel_getline);
7823 hlua_class_function(gL.T, "set", hlua_channel_set);
7824 hlua_class_function(gL.T, "append", hlua_channel_append);
7825 hlua_class_function(gL.T, "send", hlua_channel_send);
7826 hlua_class_function(gL.T, "forward", hlua_channel_forward);
7827 hlua_class_function(gL.T, "get_in_len", hlua_channel_get_in_len);
7828 hlua_class_function(gL.T, "get_out_len", hlua_channel_get_out_len);
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01007829 hlua_class_function(gL.T, "is_full", hlua_channel_is_full);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01007830
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007831 lua_rawset(gL.T, -3);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01007832
7833 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007834 class_channel_ref = hlua_register_metatable(gL.T, CLASS_CHANNEL);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01007835
7836 /*
7837 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01007838 * Register class Fetches
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01007839 *
7840 */
7841
7842 /* Create and fill the metatable. */
7843 lua_newtable(gL.T);
7844
7845 /* Create and fille the __index entry. */
7846 lua_pushstring(gL.T, "__index");
7847 lua_newtable(gL.T);
7848
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01007849 /* Browse existing fetches and create the associated
7850 * object method.
7851 */
7852 sf = NULL;
7853 while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) {
7854
7855 /* Dont register the keywork if the arguments check function are
7856 * not safe during the runtime.
7857 */
7858 if ((sf->val_args != NULL) &&
7859 (sf->val_args != val_payload_lv) &&
7860 (sf->val_args != val_hdr))
7861 continue;
7862
7863 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
7864 * by an underscore.
7865 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007866 strncpy(trash.area, sf->kw, trash.size);
7867 trash.area[trash.size - 1] = '\0';
7868 for (p = trash.area; *p; p++)
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01007869 if (*p == '.' || *p == '-' || *p == '+')
7870 *p = '_';
7871
7872 /* Register the function. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007873 lua_pushstring(gL.T, trash.area);
Willy Tarreau2ec22742015-03-10 14:27:20 +01007874 lua_pushlightuserdata(gL.T, sf);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01007875 lua_pushcclosure(gL.T, hlua_run_sample_fetch, 1);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007876 lua_rawset(gL.T, -3);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01007877 }
7878
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007879 lua_rawset(gL.T, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01007880
7881 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007882 class_fetches_ref = hlua_register_metatable(gL.T, CLASS_FETCHES);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01007883
7884 /*
7885 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007886 * Register class Converters
7887 *
7888 */
7889
7890 /* Create and fill the metatable. */
7891 lua_newtable(gL.T);
7892
7893 /* Create and fill the __index entry. */
7894 lua_pushstring(gL.T, "__index");
7895 lua_newtable(gL.T);
7896
7897 /* Browse existing converters and create the associated
7898 * object method.
7899 */
7900 sc = NULL;
7901 while ((sc = sample_conv_getnext(sc, &idx)) != NULL) {
7902 /* Dont register the keywork if the arguments check function are
7903 * not safe during the runtime.
7904 */
7905 if (sc->val_args != NULL)
7906 continue;
7907
7908 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
7909 * by an underscore.
7910 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007911 strncpy(trash.area, sc->kw, trash.size);
7912 trash.area[trash.size - 1] = '\0';
7913 for (p = trash.area; *p; p++)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007914 if (*p == '.' || *p == '-' || *p == '+')
7915 *p = '_';
7916
7917 /* Register the function. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007918 lua_pushstring(gL.T, trash.area);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007919 lua_pushlightuserdata(gL.T, sc);
7920 lua_pushcclosure(gL.T, hlua_run_sample_conv, 1);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007921 lua_rawset(gL.T, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007922 }
7923
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007924 lua_rawset(gL.T, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007925
7926 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007927 class_converters_ref = hlua_register_metatable(gL.T, CLASS_CONVERTERS);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007928
7929 /*
7930 *
Thierry FOURNIER08504f42015-03-16 14:17:08 +01007931 * Register class HTTP
7932 *
7933 */
7934
7935 /* Create and fill the metatable. */
7936 lua_newtable(gL.T);
7937
7938 /* Create and fille the __index entry. */
7939 lua_pushstring(gL.T, "__index");
7940 lua_newtable(gL.T);
7941
7942 /* Register Lua functions. */
7943 hlua_class_function(gL.T, "req_get_headers",hlua_http_req_get_headers);
7944 hlua_class_function(gL.T, "req_del_header", hlua_http_req_del_hdr);
7945 hlua_class_function(gL.T, "req_rep_header", hlua_http_req_rep_hdr);
7946 hlua_class_function(gL.T, "req_rep_value", hlua_http_req_rep_val);
7947 hlua_class_function(gL.T, "req_add_header", hlua_http_req_add_hdr);
7948 hlua_class_function(gL.T, "req_set_header", hlua_http_req_set_hdr);
7949 hlua_class_function(gL.T, "req_set_method", hlua_http_req_set_meth);
7950 hlua_class_function(gL.T, "req_set_path", hlua_http_req_set_path);
7951 hlua_class_function(gL.T, "req_set_query", hlua_http_req_set_query);
7952 hlua_class_function(gL.T, "req_set_uri", hlua_http_req_set_uri);
7953
7954 hlua_class_function(gL.T, "res_get_headers",hlua_http_res_get_headers);
7955 hlua_class_function(gL.T, "res_del_header", hlua_http_res_del_hdr);
7956 hlua_class_function(gL.T, "res_rep_header", hlua_http_res_rep_hdr);
7957 hlua_class_function(gL.T, "res_rep_value", hlua_http_res_rep_val);
7958 hlua_class_function(gL.T, "res_add_header", hlua_http_res_add_hdr);
7959 hlua_class_function(gL.T, "res_set_header", hlua_http_res_set_hdr);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02007960 hlua_class_function(gL.T, "res_set_status", hlua_http_res_set_status);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01007961
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007962 lua_rawset(gL.T, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01007963
7964 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007965 class_http_ref = hlua_register_metatable(gL.T, CLASS_HTTP);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01007966
7967 /*
7968 *
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007969 * Register class AppletTCP
7970 *
7971 */
7972
7973 /* Create and fill the metatable. */
7974 lua_newtable(gL.T);
7975
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007976 /* Create and fille the __index entry. */
7977 lua_pushstring(gL.T, "__index");
7978 lua_newtable(gL.T);
7979
7980 /* Register Lua functions. */
Thierry FOURNIER / OZON.IO3e1d7912016-12-12 12:29:34 +01007981 hlua_class_function(gL.T, "getline", hlua_applet_tcp_getline);
7982 hlua_class_function(gL.T, "receive", hlua_applet_tcp_recv);
7983 hlua_class_function(gL.T, "send", hlua_applet_tcp_send);
7984 hlua_class_function(gL.T, "set_priv", hlua_applet_tcp_set_priv);
7985 hlua_class_function(gL.T, "get_priv", hlua_applet_tcp_get_priv);
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01007986 hlua_class_function(gL.T, "set_var", hlua_applet_tcp_set_var);
7987 hlua_class_function(gL.T, "unset_var", hlua_applet_tcp_unset_var);
7988 hlua_class_function(gL.T, "get_var", hlua_applet_tcp_get_var);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007989
7990 lua_settable(gL.T, -3);
7991
7992 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007993 class_applet_tcp_ref = hlua_register_metatable(gL.T, CLASS_APPLET_TCP);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007994
7995 /*
7996 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007997 * Register class AppletHTTP
7998 *
7999 */
8000
8001 /* Create and fill the metatable. */
8002 lua_newtable(gL.T);
8003
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02008004 /* Create and fille the __index entry. */
8005 lua_pushstring(gL.T, "__index");
8006 lua_newtable(gL.T);
8007
8008 /* Register Lua functions. */
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01008009 hlua_class_function(gL.T, "set_priv", hlua_applet_http_set_priv);
8010 hlua_class_function(gL.T, "get_priv", hlua_applet_http_get_priv);
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01008011 hlua_class_function(gL.T, "set_var", hlua_applet_http_set_var);
8012 hlua_class_function(gL.T, "unset_var", hlua_applet_http_unset_var);
8013 hlua_class_function(gL.T, "get_var", hlua_applet_http_get_var);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02008014 hlua_class_function(gL.T, "getline", hlua_applet_http_getline);
8015 hlua_class_function(gL.T, "receive", hlua_applet_http_recv);
8016 hlua_class_function(gL.T, "send", hlua_applet_http_send);
8017 hlua_class_function(gL.T, "add_header", hlua_applet_http_addheader);
8018 hlua_class_function(gL.T, "set_status", hlua_applet_http_status);
8019 hlua_class_function(gL.T, "start_response", hlua_applet_http_start_response);
8020
8021 lua_settable(gL.T, -3);
8022
8023 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008024 class_applet_http_ref = hlua_register_metatable(gL.T, CLASS_APPLET_HTTP);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02008025
8026 /*
8027 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008028 * Register class TXN
8029 *
8030 */
8031
8032 /* Create and fill the metatable. */
8033 lua_newtable(gL.T);
8034
8035 /* Create and fille the __index entry. */
8036 lua_pushstring(gL.T, "__index");
8037 lua_newtable(gL.T);
8038
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01008039 /* Register Lua functions. */
Patrick Hemmer268a7072018-05-11 12:52:31 -04008040 hlua_class_function(gL.T, "set_priv", hlua_set_priv);
8041 hlua_class_function(gL.T, "get_priv", hlua_get_priv);
8042 hlua_class_function(gL.T, "set_var", hlua_set_var);
8043 hlua_class_function(gL.T, "unset_var", hlua_unset_var);
8044 hlua_class_function(gL.T, "get_var", hlua_get_var);
8045 hlua_class_function(gL.T, "done", hlua_txn_done);
8046 hlua_class_function(gL.T, "set_loglevel", hlua_txn_set_loglevel);
8047 hlua_class_function(gL.T, "set_tos", hlua_txn_set_tos);
8048 hlua_class_function(gL.T, "set_mark", hlua_txn_set_mark);
8049 hlua_class_function(gL.T, "set_priority_class", hlua_txn_set_priority_class);
8050 hlua_class_function(gL.T, "set_priority_offset", hlua_txn_set_priority_offset);
8051 hlua_class_function(gL.T, "deflog", hlua_txn_deflog);
8052 hlua_class_function(gL.T, "log", hlua_txn_log);
8053 hlua_class_function(gL.T, "Debug", hlua_txn_log_debug);
8054 hlua_class_function(gL.T, "Info", hlua_txn_log_info);
8055 hlua_class_function(gL.T, "Warning", hlua_txn_log_warning);
8056 hlua_class_function(gL.T, "Alert", hlua_txn_log_alert);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01008057
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008058 lua_rawset(gL.T, -3);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008059
8060 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008061 class_txn_ref = hlua_register_metatable(gL.T, CLASS_TXN);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008062
8063 /*
8064 *
8065 * Register class Socket
8066 *
8067 */
8068
8069 /* Create and fill the metatable. */
8070 lua_newtable(gL.T);
8071
8072 /* Create and fille the __index entry. */
8073 lua_pushstring(gL.T, "__index");
8074 lua_newtable(gL.T);
8075
Baptiste Assmann84bb4932015-03-02 21:40:06 +01008076#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008077 hlua_class_function(gL.T, "connect_ssl", hlua_socket_connect_ssl);
Baptiste Assmann84bb4932015-03-02 21:40:06 +01008078#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008079 hlua_class_function(gL.T, "connect", hlua_socket_connect);
8080 hlua_class_function(gL.T, "send", hlua_socket_send);
8081 hlua_class_function(gL.T, "receive", hlua_socket_receive);
8082 hlua_class_function(gL.T, "close", hlua_socket_close);
8083 hlua_class_function(gL.T, "getpeername", hlua_socket_getpeername);
8084 hlua_class_function(gL.T, "getsockname", hlua_socket_getsockname);
8085 hlua_class_function(gL.T, "setoption", hlua_socket_setoption);
8086 hlua_class_function(gL.T, "settimeout", hlua_socket_settimeout);
8087
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008088 lua_rawset(gL.T, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008089
8090 /* Register the garbage collector entry. */
8091 lua_pushstring(gL.T, "__gc");
8092 lua_pushcclosure(gL.T, hlua_socket_gc, 0);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008093 lua_rawset(gL.T, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008094
8095 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008096 class_socket_ref = hlua_register_metatable(gL.T, CLASS_SOCKET);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008097
8098 /* Proxy and server configuration initialisation. */
8099 memset(&socket_proxy, 0, sizeof(socket_proxy));
8100 init_new_proxy(&socket_proxy);
8101 socket_proxy.parent = NULL;
8102 socket_proxy.last_change = now.tv_sec;
8103 socket_proxy.id = "LUA-SOCKET";
8104 socket_proxy.cap = PR_CAP_FE | PR_CAP_BE;
8105 socket_proxy.maxconn = 0;
8106 socket_proxy.accept = NULL;
8107 socket_proxy.options2 |= PR_O2_INDEPSTR;
8108 socket_proxy.srv = NULL;
8109 socket_proxy.conn_retries = 0;
8110 socket_proxy.timeout.connect = 5000; /* By default the timeout connection is 5s. */
8111
8112 /* Init TCP server: unchanged parameters */
8113 memset(&socket_tcp, 0, sizeof(socket_tcp));
8114 socket_tcp.next = NULL;
8115 socket_tcp.proxy = &socket_proxy;
8116 socket_tcp.obj_type = OBJ_TYPE_SERVER;
8117 LIST_INIT(&socket_tcp.actconns);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04008118 socket_tcp.pendconns = EB_ROOT;
Christopher Faulet40a007c2017-07-03 15:41:01 +02008119 socket_tcp.priv_conns = NULL;
8120 socket_tcp.idle_conns = NULL;
8121 socket_tcp.safe_conns = NULL;
Emeric Brun52a91d32017-08-31 14:41:55 +02008122 socket_tcp.next_state = SRV_ST_RUNNING; /* early server setup */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008123 socket_tcp.last_change = 0;
8124 socket_tcp.id = "LUA-TCP-CONN";
8125 socket_tcp.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8126 socket_tcp.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8127 socket_tcp.pp_opts = 0; /* Remove proxy protocol. */
8128
8129 /* XXX: Copy default parameter from default server,
8130 * but the default server is not initialized.
8131 */
8132 socket_tcp.maxqueue = socket_proxy.defsrv.maxqueue;
8133 socket_tcp.minconn = socket_proxy.defsrv.minconn;
8134 socket_tcp.maxconn = socket_proxy.defsrv.maxconn;
8135 socket_tcp.slowstart = socket_proxy.defsrv.slowstart;
8136 socket_tcp.onerror = socket_proxy.defsrv.onerror;
8137 socket_tcp.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
8138 socket_tcp.onmarkedup = socket_proxy.defsrv.onmarkedup;
8139 socket_tcp.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
8140 socket_tcp.uweight = socket_proxy.defsrv.iweight;
8141 socket_tcp.iweight = socket_proxy.defsrv.iweight;
8142
8143 socket_tcp.check.status = HCHK_STATUS_INI;
8144 socket_tcp.check.rise = socket_proxy.defsrv.check.rise;
8145 socket_tcp.check.fall = socket_proxy.defsrv.check.fall;
8146 socket_tcp.check.health = socket_tcp.check.rise; /* socket, but will fall down at first failure */
8147 socket_tcp.check.server = &socket_tcp;
8148
8149 socket_tcp.agent.status = HCHK_STATUS_INI;
8150 socket_tcp.agent.rise = socket_proxy.defsrv.agent.rise;
8151 socket_tcp.agent.fall = socket_proxy.defsrv.agent.fall;
8152 socket_tcp.agent.health = socket_tcp.agent.rise; /* socket, but will fall down at first failure */
8153 socket_tcp.agent.server = &socket_tcp;
8154
Willy Tarreaua261e9b2016-12-22 20:44:00 +01008155 socket_tcp.xprt = xprt_get(XPRT_RAW);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008156
8157#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008158 /* Init TCP server: unchanged parameters */
8159 memset(&socket_ssl, 0, sizeof(socket_ssl));
8160 socket_ssl.next = NULL;
8161 socket_ssl.proxy = &socket_proxy;
8162 socket_ssl.obj_type = OBJ_TYPE_SERVER;
8163 LIST_INIT(&socket_ssl.actconns);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04008164 socket_ssl.pendconns = EB_ROOT;
Christopher Faulet40a007c2017-07-03 15:41:01 +02008165 socket_tcp.priv_conns = NULL;
8166 socket_tcp.idle_conns = NULL;
8167 socket_tcp.safe_conns = NULL;
Emeric Brun52a91d32017-08-31 14:41:55 +02008168 socket_ssl.next_state = SRV_ST_RUNNING; /* early server setup */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008169 socket_ssl.last_change = 0;
8170 socket_ssl.id = "LUA-SSL-CONN";
8171 socket_ssl.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8172 socket_ssl.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8173 socket_ssl.pp_opts = 0; /* Remove proxy protocol. */
8174
8175 /* XXX: Copy default parameter from default server,
8176 * but the default server is not initialized.
8177 */
8178 socket_ssl.maxqueue = socket_proxy.defsrv.maxqueue;
8179 socket_ssl.minconn = socket_proxy.defsrv.minconn;
8180 socket_ssl.maxconn = socket_proxy.defsrv.maxconn;
8181 socket_ssl.slowstart = socket_proxy.defsrv.slowstart;
8182 socket_ssl.onerror = socket_proxy.defsrv.onerror;
8183 socket_ssl.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
8184 socket_ssl.onmarkedup = socket_proxy.defsrv.onmarkedup;
8185 socket_ssl.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
8186 socket_ssl.uweight = socket_proxy.defsrv.iweight;
8187 socket_ssl.iweight = socket_proxy.defsrv.iweight;
8188
8189 socket_ssl.check.status = HCHK_STATUS_INI;
8190 socket_ssl.check.rise = socket_proxy.defsrv.check.rise;
8191 socket_ssl.check.fall = socket_proxy.defsrv.check.fall;
8192 socket_ssl.check.health = socket_ssl.check.rise; /* socket, but will fall down at first failure */
8193 socket_ssl.check.server = &socket_ssl;
8194
8195 socket_ssl.agent.status = HCHK_STATUS_INI;
8196 socket_ssl.agent.rise = socket_proxy.defsrv.agent.rise;
8197 socket_ssl.agent.fall = socket_proxy.defsrv.agent.fall;
8198 socket_ssl.agent.health = socket_ssl.agent.rise; /* socket, but will fall down at first failure */
8199 socket_ssl.agent.server = &socket_ssl;
8200
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008201 socket_ssl.use_ssl = 1;
Willy Tarreaua261e9b2016-12-22 20:44:00 +01008202 socket_ssl.xprt = xprt_get(XPRT_SSL);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008203
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008204 for (idx = 0; args[idx] != NULL; idx++) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008205 if ((kw = srv_find_kw(args[idx])) != NULL) { /* Maybe it's registered server keyword */
8206 /*
8207 *
8208 * If the keyword is not known, we can search in the registered
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08008209 * server keywords. This is useful to configure special SSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008210 * features like client certificates and ssl_verify.
8211 *
8212 */
8213 tmp_error = kw->parse(args, &idx, &socket_proxy, &socket_ssl, &error);
8214 if (tmp_error != 0) {
8215 fprintf(stderr, "INTERNAL ERROR: %s\n", error);
8216 abort(); /* This must be never arrives because the command line
8217 not editable by the user. */
8218 }
8219 idx += kw->skip;
8220 }
8221 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008222#endif
Thierry Fournier75933d42016-01-21 09:30:18 +01008223
8224 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01008225}
Willy Tarreaubb57d942016-12-21 19:04:56 +01008226
Willy Tarreau80713382018-11-26 10:19:54 +01008227static void hlua_register_build_options(void)
8228{
Willy Tarreaubb57d942016-12-21 19:04:56 +01008229 char *ptr = NULL;
Willy Tarreau80713382018-11-26 10:19:54 +01008230
Willy Tarreaubb57d942016-12-21 19:04:56 +01008231 memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE);
8232 hap_register_build_opts(ptr, 1);
8233}
Willy Tarreau80713382018-11-26 10:19:54 +01008234
8235INITCALL0(STG_REGISTER, hlua_register_build_options);