blob: fe67f045f4a89e3197d33ce5be2d93004ae5e3b3 [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 FOURNIERa097fdf2015-03-03 15:17:35 +010045#include <proto/hlua.h>
Thierry Fournierfb0b5462016-01-21 09:28:58 +010046#include <proto/hlua_fcn.h>
Willy Tarreau79e57332018-10-02 16:01:16 +020047#include <proto/http_fetch.h>
Christopher Faulet724a12c2018-12-13 22:12:15 +010048#include <proto/http_htx.h>
Willy Tarreau61c112a2018-10-02 16:43:32 +020049#include <proto/http_rules.h>
Thierry FOURNIER3def3932015-04-07 11:27:54 +020050#include <proto/map.h>
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010051#include <proto/obj_type.h>
Patrick Hemmer268a7072018-05-11 12:52:31 -040052#include <proto/queue.h>
Thierry FOURNIER83758bb2015-02-04 13:21:04 +010053#include <proto/pattern.h>
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +010054#include <proto/payload.h>
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020055#include <proto/http_ana.h>
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +010056#include <proto/sample.h>
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010057#include <proto/server.h>
Willy Tarreaufeb76402015-04-03 14:10:06 +020058#include <proto/session.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020059#include <proto/stream.h>
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010060#include <proto/stream_interface.h>
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +010061#include <proto/task.h>
Willy Tarreau39713102016-11-25 15:49:32 +010062#include <proto/tcp_rules.h>
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +020063#include <proto/vars.h>
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +010064
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +010065/* Lua uses longjmp to perform yield or throwing errors. This
66 * macro is used only for identifying the function that can
67 * not return because a longjmp is executed.
68 * __LJMP marks a prototype of hlua file that can use longjmp.
69 * WILL_LJMP() marks an lua function that will use longjmp.
70 * MAY_LJMP() marks an lua function that may use longjmp.
71 */
72#define __LJMP
Willy Tarreau4e7cc332018-10-20 17:45:48 +020073#define WILL_LJMP(func) do { func; my_unreachable(); } while(0)
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +010074#define MAY_LJMP(func) func
75
Thierry FOURNIERbabae282015-09-17 11:36:37 +020076/* This couple of function executes securely some Lua calls outside of
77 * the lua runtime environment. Each Lua call can return a longjmp
78 * if it encounter a memory error.
79 *
80 * Lua documentation extract:
81 *
82 * If an error happens outside any protected environment, Lua calls
83 * a panic function (see lua_atpanic) and then calls abort, thus
84 * exiting the host application. Your panic function can avoid this
85 * exit by never returning (e.g., doing a long jump to your own
86 * recovery point outside Lua).
87 *
88 * The panic function runs as if it were a message handler (see
89 * §2.3); in particular, the error message is at the top of the
90 * stack. However, there is no guarantee about stack space. To push
91 * anything on the stack, the panic function must first check the
92 * available space (see §4.2).
93 *
94 * We must check all the Lua entry point. This includes:
95 * - The include/proto/hlua.h exported functions
96 * - the task wrapper function
97 * - The action wrapper function
98 * - The converters wrapper function
99 * - The sample-fetch wrapper functions
100 *
101 * It is tolerated that the initilisation function returns an abort.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800102 * Before each Lua abort, an error message is written on stderr.
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200103 *
104 * The macro SET_SAFE_LJMP initialise the longjmp. The Macro
105 * RESET_SAFE_LJMP reset the longjmp. These function must be macro
106 * because they must be exists in the program stack when the longjmp
107 * is called.
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +0200108 *
109 * Note that the Lua processing is not really thread safe. It provides
110 * heavy system which consists to add our own lock function in the Lua
111 * code and recompile the library. This system will probably not accepted
112 * by maintainers of various distribs.
113 *
114 * Our main excution point of the Lua is the function lua_resume(). A
115 * quick looking on the Lua sources displays a lua_lock() a the start
116 * of function and a lua_unlock() at the end of the function. So I
117 * conclude that the Lua thread safe mode just perform a mutex around
118 * all execution. So I prefer to do this in the HAProxy code, it will be
119 * easier for distro maintainers.
120 *
121 * Note that the HAProxy lua functions rounded by the macro SET_SAFE_LJMP
122 * and RESET_SAFE_LJMP manipulates the Lua stack, so it will be careful
123 * to set mutex around these functions.
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200124 */
Willy Tarreau86abe442018-11-25 20:12:18 +0100125__decl_spinlock(hlua_global_lock);
Thierry FOURNIERffbad792017-07-12 11:39:04 +0200126THREAD_LOCAL jmp_buf safe_ljmp_env;
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200127static int hlua_panic_safe(lua_State *L) { return 0; }
128static int hlua_panic_ljmp(lua_State *L) { longjmp(safe_ljmp_env, 1); }
129
130#define SET_SAFE_LJMP(__L) \
131 ({ \
132 int ret; \
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100133 HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200134 if (setjmp(safe_ljmp_env) != 0) { \
135 lua_atpanic(__L, hlua_panic_safe); \
136 ret = 0; \
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100137 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200138 } else { \
139 lua_atpanic(__L, hlua_panic_ljmp); \
140 ret = 1; \
141 } \
142 ret; \
143 })
144
145/* If we are the last function catching Lua errors, we
146 * must reset the panic function.
147 */
148#define RESET_SAFE_LJMP(__L) \
149 do { \
150 lua_atpanic(__L, hlua_panic_safe); \
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100151 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200152 } while(0)
153
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200154/* Applet status flags */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200155#define APPLET_DONE 0x01 /* applet processing is done. */
Christopher Faulet18c2e8d2019-03-01 12:02:08 +0100156/* unused: 0x02 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200157#define APPLET_HDR_SENT 0x04 /* Response header sent. */
Christopher Fauleta2097962019-07-15 16:25:33 +0200158/* unused: 0x08, 0x10 */
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +0100159#define APPLET_HTTP11 0x20 /* Last chunk sent. */
Christopher Faulet56a3d6e2019-02-27 22:06:23 +0100160#define APPLET_RSP_SENT 0x40 /* The response was fully sent */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200161
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100162/* The main Lua execution context. */
163struct hlua gL;
164
Thierry FOURNIERebed6e92016-12-16 11:54:07 +0100165/* This is the memory pool containing struct lua for applets
166 * (including cli).
167 */
Willy Tarreau8ceae722018-11-26 11:58:30 +0100168DECLARE_STATIC_POOL(pool_head_hlua, "hlua", sizeof(struct hlua));
Thierry FOURNIERebed6e92016-12-16 11:54:07 +0100169
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100170/* Used for Socket connection. */
171static struct proxy socket_proxy;
172static struct server socket_tcp;
173#ifdef USE_OPENSSL
174static struct server socket_ssl;
175#endif
176
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +0100177/* List head of the function called at the initialisation time. */
178struct list hlua_init_functions = LIST_HEAD_INIT(hlua_init_functions);
179
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +0100180/* The following variables contains the reference of the different
181 * Lua classes. These references are useful for identify metadata
182 * associated with an object.
183 */
Thierry FOURNIER65f34c62015-02-16 20:11:43 +0100184static int class_txn_ref;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100185static int class_socket_ref;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +0100186static int class_channel_ref;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +0100187static int class_fetches_ref;
Thierry FOURNIER594afe72015-03-10 23:58:30 +0100188static int class_converters_ref;
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100189static int class_http_ref;
Thierry FOURNIER3def3932015-04-07 11:27:54 +0200190static int class_map_ref;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200191static int class_applet_tcp_ref;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200192static int class_applet_http_ref;
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +0100193
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100194/* Global Lua execution timeout. By default Lua, execution linked
Willy Tarreau87b09662015-04-03 00:22:06 +0200195 * with stream (actions, sample-fetches and converters) have a
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100196 * short timeout. Lua linked with tasks doesn't have a timeout
197 * because a task may remain alive during all the haproxy execution.
198 */
199static unsigned int hlua_timeout_session = 4000; /* session timeout. */
200static unsigned int hlua_timeout_task = TICK_ETERNITY; /* task timeout. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200201static unsigned int hlua_timeout_applet = 4000; /* applet timeout. */
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100202
Thierry FOURNIERee9f8022015-03-03 17:37:37 +0100203/* Interrupts the Lua processing each "hlua_nb_instruction" instructions.
204 * it is used for preventing infinite loops.
205 *
206 * I test the scheer with an infinite loop containing one incrementation
207 * and one test. I run this loop between 10 seconds, I raise a ceil of
208 * 710M loops from one interrupt each 9000 instructions, so I fix the value
209 * to one interrupt each 10 000 instructions.
210 *
211 * configured | Number of
212 * instructions | loops executed
213 * between two | in milions
214 * forced yields |
215 * ---------------+---------------
216 * 10 | 160
217 * 500 | 670
218 * 1000 | 680
219 * 5000 | 700
220 * 7000 | 700
221 * 8000 | 700
222 * 9000 | 710 <- ceil
223 * 10000 | 710
224 * 100000 | 710
225 * 1000000 | 710
226 *
227 */
228static unsigned int hlua_nb_instruction = 10000;
229
Willy Tarreau32f61e22015-03-18 17:54:59 +0100230/* Descriptor for the memory allocation state. If limit is not null, it will
231 * be enforced on any memory allocation.
232 */
233struct hlua_mem_allocator {
234 size_t allocated;
235 size_t limit;
236};
237
238static struct hlua_mem_allocator hlua_global_allocator;
239
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100240/* These functions converts types between HAProxy internal args or
241 * sample and LUA types. Another function permits to check if the
242 * LUA stack contains arguments according with an required ARG_T
243 * format.
244 */
245static int hlua_arg2lua(lua_State *L, const struct arg *arg);
246static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100247__LJMP static int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp,
David Carlier0c437f42016-04-27 16:21:56 +0100248 uint64_t mask, struct proxy *p);
Willy Tarreau5eadada2015-03-10 17:28:54 +0100249static int hlua_smp2lua(lua_State *L, struct sample *smp);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100250static int hlua_smp2lua_str(lua_State *L, struct sample *smp);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100251static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp);
252
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200253__LJMP static int hlua_http_get_headers(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg);
254
Thierry FOURNIER23bc3752015-09-11 19:15:43 +0200255#define SEND_ERR(__be, __fmt, __args...) \
256 do { \
257 send_log(__be, LOG_ERR, __fmt, ## __args); \
258 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) \
Christopher Faulet767a84b2017-11-24 16:50:31 +0100259 ha_alert(__fmt, ## __args); \
Thierry FOURNIER23bc3752015-09-11 19:15:43 +0200260 } while (0)
261
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100262/* Used to check an Lua function type in the stack. It creates and
263 * returns a reference of the function. This function throws an
264 * error if the rgument is not a "function".
265 */
266__LJMP unsigned int hlua_checkfunction(lua_State *L, int argno)
267{
268 if (!lua_isfunction(L, argno)) {
Thierry FOURNIERfd1e9552018-02-23 18:41:18 +0100269 const char *msg = lua_pushfstring(L, "function expected, got %s", luaL_typename(L, argno));
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100270 WILL_LJMP(luaL_argerror(L, argno, msg));
271 }
272 lua_pushvalue(L, argno);
273 return luaL_ref(L, LUA_REGISTRYINDEX);
274}
275
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200276/* Return the string that is of the top of the stack. */
277const char *hlua_get_top_error_string(lua_State *L)
278{
279 if (lua_gettop(L) < 1)
280 return "unknown error";
281 if (lua_type(L, -1) != LUA_TSTRING)
282 return "unknown error";
283 return lua_tostring(L, -1);
284}
285
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200286__LJMP static const char *hlua_traceback(lua_State *L)
287{
288 lua_Debug ar;
289 int level = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +0200290 struct buffer *msg = get_trash_chunk();
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200291 int filled = 0;
292
293 while (lua_getstack(L, level++, &ar)) {
294
295 /* Add separator */
296 if (filled)
297 chunk_appendf(msg, ", ");
298 filled = 1;
299
300 /* Fill fields:
301 * 'S': fills in the fields source, short_src, linedefined, lastlinedefined, and what;
302 * 'l': fills in the field currentline;
303 * 'n': fills in the field name and namewhat;
304 * 't': fills in the field istailcall;
305 */
306 lua_getinfo(L, "Slnt", &ar);
307
308 /* Append code localisation */
309 if (ar.currentline > 0)
310 chunk_appendf(msg, "%s:%d ", ar.short_src, ar.currentline);
311 else
312 chunk_appendf(msg, "%s ", ar.short_src);
313
314 /*
315 * Get function name
316 *
317 * if namewhat is no empty, name is defined.
318 * what contains "Lua" for Lua function, "C" for C function,
319 * or "main" for main code.
320 */
321 if (*ar.namewhat != '\0' && ar.name != NULL) /* is there a name from code? */
322 chunk_appendf(msg, "%s '%s'", ar.namewhat, ar.name); /* use it */
323
324 else if (*ar.what == 'm') /* "main", the code is not executed in a function */
325 chunk_appendf(msg, "main chunk");
326
327 else if (*ar.what != 'C') /* for Lua functions, use <file:line> */
328 chunk_appendf(msg, "C function line %d", ar.linedefined);
329
330 else /* nothing left... */
331 chunk_appendf(msg, "?");
332
333
334 /* Display tailed call */
335 if (ar.istailcall)
336 chunk_appendf(msg, " ...");
337 }
338
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200339 return msg->area;
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200340}
341
342
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100343/* This function check the number of arguments available in the
344 * stack. If the number of arguments available is not the same
345 * then <nb> an error is throwed.
346 */
347__LJMP static inline void check_args(lua_State *L, int nb, char *fcn)
348{
349 if (lua_gettop(L) == nb)
350 return;
351 WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb));
352}
353
Mark Lakes22154b42018-01-29 14:38:40 -0800354/* This function pushes an error string prefixed by the file name
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100355 * and the line number where the error is encountered.
356 */
357static int hlua_pusherror(lua_State *L, const char *fmt, ...)
358{
359 va_list argp;
360 va_start(argp, fmt);
361 luaL_where(L, 1);
362 lua_pushvfstring(L, fmt, argp);
363 va_end(argp);
364 lua_concat(L, 2);
365 return 1;
366}
367
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100368/* This functions is used with sample fetch and converters. It
369 * converts the HAProxy configuration argument in a lua stack
370 * values.
371 *
372 * It takes an array of "arg", and each entry of the array is
373 * converted and pushed in the LUA stack.
374 */
375static int hlua_arg2lua(lua_State *L, const struct arg *arg)
376{
377 switch (arg->type) {
378 case ARGT_SINT:
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100379 case ARGT_TIME:
380 case ARGT_SIZE:
Thierry FOURNIERf90838b2015-03-06 13:48:32 +0100381 lua_pushinteger(L, arg->data.sint);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100382 break;
383
384 case ARGT_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200385 lua_pushlstring(L, arg->data.str.area, arg->data.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100386 break;
387
388 case ARGT_IPV4:
389 case ARGT_IPV6:
390 case ARGT_MSK4:
391 case ARGT_MSK6:
392 case ARGT_FE:
393 case ARGT_BE:
394 case ARGT_TAB:
395 case ARGT_SRV:
396 case ARGT_USR:
397 case ARGT_MAP:
398 default:
399 lua_pushnil(L);
400 break;
401 }
402 return 1;
403}
404
405/* This function take one entrie in an LUA stack at the index "ud",
406 * and try to convert it in an HAProxy argument entry. This is useful
407 * with sample fetch wrappers. The input arguments are gived to the
408 * lua wrapper and converted as arg list by thi function.
409 */
410static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg)
411{
412 switch (lua_type(L, ud)) {
413
414 case LUA_TNUMBER:
415 case LUA_TBOOLEAN:
416 arg->type = ARGT_SINT;
417 arg->data.sint = lua_tointeger(L, ud);
418 break;
419
420 case LUA_TSTRING:
421 arg->type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200422 arg->data.str.area = (char *)lua_tolstring(L, ud, (size_t *)&arg->data.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100423 break;
424
425 case LUA_TUSERDATA:
426 case LUA_TNIL:
427 case LUA_TTABLE:
428 case LUA_TFUNCTION:
429 case LUA_TTHREAD:
430 case LUA_TLIGHTUSERDATA:
431 arg->type = ARGT_SINT;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +0200432 arg->data.sint = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100433 break;
434 }
435 return 1;
436}
437
438/* the following functions are used to convert a struct sample
439 * in Lua type. This useful to convert the return of the
440 * fetchs or converters.
441 */
Willy Tarreau5eadada2015-03-10 17:28:54 +0100442static int hlua_smp2lua(lua_State *L, struct sample *smp)
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100443{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200444 switch (smp->data.type) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100445 case SMP_T_SINT:
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100446 case SMP_T_BOOL:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200447 lua_pushinteger(L, smp->data.u.sint);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100448 break;
449
450 case SMP_T_BIN:
451 case SMP_T_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200452 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100453 break;
454
455 case SMP_T_METH:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200456 switch (smp->data.u.meth.meth) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100457 case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break;
458 case HTTP_METH_GET: lua_pushstring(L, "GET"); break;
459 case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break;
460 case HTTP_METH_POST: lua_pushstring(L, "POST"); break;
461 case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break;
462 case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break;
463 case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break;
464 case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break;
465 case HTTP_METH_OTHER:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200466 lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100467 break;
468 default:
469 lua_pushnil(L);
470 break;
471 }
472 break;
473
474 case SMP_T_IPV4:
475 case SMP_T_IPV6:
476 case SMP_T_ADDR: /* This type is never used to qualify a sample. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200477 if (sample_casts[smp->data.type][SMP_T_STR] &&
478 sample_casts[smp->data.type][SMP_T_STR](smp))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200479 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Willy Tarreau5eadada2015-03-10 17:28:54 +0100480 else
481 lua_pushnil(L);
482 break;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100483 default:
484 lua_pushnil(L);
485 break;
486 }
487 return 1;
488}
489
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100490/* the following functions are used to convert a struct sample
491 * in Lua strings. This is useful to convert the return of the
492 * fetchs or converters.
493 */
494static int hlua_smp2lua_str(lua_State *L, struct sample *smp)
495{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200496 switch (smp->data.type) {
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100497
498 case SMP_T_BIN:
499 case SMP_T_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200500 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100501 break;
502
503 case SMP_T_METH:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200504 switch (smp->data.u.meth.meth) {
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100505 case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break;
506 case HTTP_METH_GET: lua_pushstring(L, "GET"); break;
507 case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break;
508 case HTTP_METH_POST: lua_pushstring(L, "POST"); break;
509 case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break;
510 case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break;
511 case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break;
512 case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break;
513 case HTTP_METH_OTHER:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200514 lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100515 break;
516 default:
517 lua_pushstring(L, "");
518 break;
519 }
520 break;
521
522 case SMP_T_SINT:
523 case SMP_T_BOOL:
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100524 case SMP_T_IPV4:
525 case SMP_T_IPV6:
526 case SMP_T_ADDR: /* This type is never used to qualify a sample. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200527 if (sample_casts[smp->data.type][SMP_T_STR] &&
528 sample_casts[smp->data.type][SMP_T_STR](smp))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200529 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100530 else
531 lua_pushstring(L, "");
532 break;
533 default:
534 lua_pushstring(L, "");
535 break;
536 }
537 return 1;
538}
539
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100540/* the following functions are used to convert an Lua type in a
541 * struct sample. This is useful to provide data from a converter
542 * to the LUA code.
543 */
544static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp)
545{
546 switch (lua_type(L, ud)) {
547
548 case LUA_TNUMBER:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200549 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200550 smp->data.u.sint = lua_tointeger(L, ud);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100551 break;
552
553
554 case LUA_TBOOLEAN:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200555 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200556 smp->data.u.sint = lua_toboolean(L, ud);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100557 break;
558
559 case LUA_TSTRING:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200560 smp->data.type = SMP_T_STR;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100561 smp->flags |= SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200562 smp->data.u.str.area = (char *)lua_tolstring(L, ud, (size_t *)&smp->data.u.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100563 break;
564
565 case LUA_TUSERDATA:
566 case LUA_TNIL:
567 case LUA_TTABLE:
568 case LUA_TFUNCTION:
569 case LUA_TTHREAD:
570 case LUA_TLIGHTUSERDATA:
Thierry FOURNIER93405e12015-08-26 14:19:03 +0200571 case LUA_TNONE:
572 default:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200573 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200574 smp->data.u.sint = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100575 break;
576 }
577 return 1;
578}
579
580/* This function check the "argp" builded by another conversion function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800581 * is in accord with the expected argp defined by the "mask". The function
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100582 * returns true or false. It can be adjust the types if there compatibles.
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100583 *
584 * This function assumes thant the argp argument contains ARGM_NBARGS + 1
585 * entries.
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100586 */
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100587__LJMP int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp,
David Carlier0c437f42016-04-27 16:21:56 +0100588 uint64_t mask, struct proxy *p)
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100589{
590 int min_arg;
591 int idx;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100592 struct proxy *px;
593 char *sname, *pname;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100594
595 idx = 0;
596 min_arg = ARGM(mask);
597 mask >>= ARGM_BITS;
598
599 while (1) {
600
601 /* Check oversize. */
602 if (idx >= ARGM_NBARGS && argp[idx].type != ARGT_STOP) {
Cyril Bonté577a36a2015-03-02 00:08:38 +0100603 WILL_LJMP(luaL_argerror(L, first + idx, "Malformed argument mask"));
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100604 }
605
606 /* Check for mandatory arguments. */
607 if (argp[idx].type == ARGT_STOP) {
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100608 if (idx < min_arg) {
609
610 /* If miss other argument than the first one, we return an error. */
611 if (idx > 0)
612 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
613
614 /* If first argument have a certain type, some default values
615 * may be used. See the function smp_resolve_args().
616 */
617 switch (mask & ARGT_MASK) {
618
619 case ARGT_FE:
620 if (!(p->cap & PR_CAP_FE))
621 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
622 argp[idx].data.prx = p;
623 argp[idx].type = ARGT_FE;
624 argp[idx+1].type = ARGT_STOP;
625 break;
626
627 case ARGT_BE:
628 if (!(p->cap & PR_CAP_BE))
629 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
630 argp[idx].data.prx = p;
631 argp[idx].type = ARGT_BE;
632 argp[idx+1].type = ARGT_STOP;
633 break;
634
635 case ARGT_TAB:
636 argp[idx].data.prx = p;
637 argp[idx].type = ARGT_TAB;
638 argp[idx+1].type = ARGT_STOP;
639 break;
640
641 default:
642 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
643 break;
644 }
645 }
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100646 return 0;
647 }
648
649 /* Check for exceed the number of requiered argument. */
650 if ((mask & ARGT_MASK) == ARGT_STOP &&
651 argp[idx].type != ARGT_STOP) {
652 WILL_LJMP(luaL_argerror(L, first + idx, "Last argument expected"));
653 }
654
655 if ((mask & ARGT_MASK) == ARGT_STOP &&
656 argp[idx].type == ARGT_STOP) {
657 return 0;
658 }
659
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100660 /* Convert some argument types. */
661 switch (mask & ARGT_MASK) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100662 case ARGT_SINT:
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100663 if (argp[idx].type != ARGT_SINT)
664 WILL_LJMP(luaL_argerror(L, first + idx, "integer expected"));
665 argp[idx].type = ARGT_SINT;
666 break;
667
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100668 case ARGT_TIME:
669 if (argp[idx].type != ARGT_SINT)
670 WILL_LJMP(luaL_argerror(L, first + idx, "integer expected"));
Thierry FOURNIER29176f32015-07-07 00:41:29 +0200671 argp[idx].type = ARGT_TIME;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100672 break;
673
674 case ARGT_SIZE:
675 if (argp[idx].type != ARGT_SINT)
676 WILL_LJMP(luaL_argerror(L, first + idx, "integer expected"));
Thierry FOURNIER29176f32015-07-07 00:41:29 +0200677 argp[idx].type = ARGT_SIZE;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100678 break;
679
680 case ARGT_FE:
681 if (argp[idx].type != ARGT_STR)
682 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200683 memcpy(trash.area, argp[idx].data.str.area,
684 argp[idx].data.str.data);
685 trash.area[argp[idx].data.str.data] = 0;
686 argp[idx].data.prx = proxy_fe_by_name(trash.area);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100687 if (!argp[idx].data.prx)
688 WILL_LJMP(luaL_argerror(L, first + idx, "frontend doesn't exist"));
689 argp[idx].type = ARGT_FE;
690 break;
691
692 case ARGT_BE:
693 if (argp[idx].type != ARGT_STR)
694 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200695 memcpy(trash.area, argp[idx].data.str.area,
696 argp[idx].data.str.data);
697 trash.area[argp[idx].data.str.data] = 0;
698 argp[idx].data.prx = proxy_be_by_name(trash.area);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100699 if (!argp[idx].data.prx)
700 WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist"));
701 argp[idx].type = ARGT_BE;
702 break;
703
704 case ARGT_TAB:
705 if (argp[idx].type != ARGT_STR)
706 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200707 memcpy(trash.area, argp[idx].data.str.area,
708 argp[idx].data.str.data);
709 trash.area[argp[idx].data.str.data] = 0;
710 argp[idx].data.prx = proxy_tbl_by_name(trash.area);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100711 if (!argp[idx].data.prx)
712 WILL_LJMP(luaL_argerror(L, first + idx, "table doesn't exist"));
713 argp[idx].type = ARGT_TAB;
714 break;
715
716 case ARGT_SRV:
717 if (argp[idx].type != ARGT_STR)
718 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200719 memcpy(trash.area, argp[idx].data.str.area,
720 argp[idx].data.str.data);
721 trash.area[argp[idx].data.str.data] = 0;
722 sname = strrchr(trash.area, '/');
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100723 if (sname) {
724 *sname++ = '\0';
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200725 pname = trash.area;
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200726 px = proxy_be_by_name(pname);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100727 if (!px)
728 WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist"));
729 }
730 else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200731 sname = trash.area;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100732 px = p;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100733 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100734 argp[idx].data.srv = findserver(px, sname);
735 if (!argp[idx].data.srv)
736 WILL_LJMP(luaL_argerror(L, first + idx, "server doesn't exist"));
737 argp[idx].type = ARGT_SRV;
738 break;
739
740 case ARGT_IPV4:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200741 memcpy(trash.area, argp[idx].data.str.area,
742 argp[idx].data.str.data);
743 trash.area[argp[idx].data.str.data] = 0;
744 if (inet_pton(AF_INET, trash.area, &argp[idx].data.ipv4))
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100745 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 address"));
746 argp[idx].type = ARGT_IPV4;
747 break;
748
749 case ARGT_MSK4:
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 (!str2mask(trash.area, &argp[idx].data.ipv4))
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100754 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 mask"));
755 argp[idx].type = ARGT_MSK4;
756 break;
757
758 case ARGT_IPV6:
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 (inet_pton(AF_INET6, trash.area, &argp[idx].data.ipv6))
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100763 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 address"));
764 argp[idx].type = ARGT_IPV6;
765 break;
766
767 case ARGT_MSK6:
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 (!str2mask6(trash.area, &argp[idx].data.ipv6))
Tim Duesterhusb814da62018-01-25 16:24:50 +0100772 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 mask"));
773 argp[idx].type = ARGT_MSK6;
774 break;
775
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100776 case ARGT_MAP:
777 case ARGT_REG:
778 case ARGT_USR:
779 WILL_LJMP(luaL_argerror(L, first + idx, "type not yet supported"));
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100780 break;
781 }
782
783 /* Check for type of argument. */
784 if ((mask & ARGT_MASK) != argp[idx].type) {
785 const char *msg = lua_pushfstring(L, "'%s' expected, got '%s'",
786 arg_type_names[(mask & ARGT_MASK)],
787 arg_type_names[argp[idx].type & ARGT_MASK]);
788 WILL_LJMP(luaL_argerror(L, first + idx, msg));
789 }
790
791 /* Next argument. */
792 mask >>= ARGT_BITS;
793 idx++;
794 }
795}
796
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100797/*
798 * The following functions are used to make correspondance between the the
799 * executed lua pointer and the "struct hlua *" that contain the context.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100800 *
801 * - hlua_gethlua : return the hlua context associated with an lua_State.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100802 * - hlua_sethlua : create the association between hlua context and lua_state.
803 */
804static inline struct hlua *hlua_gethlua(lua_State *L)
805{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +0100806 struct hlua **hlua = lua_getextraspace(L);
807 return *hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100808}
809static inline void hlua_sethlua(struct hlua *hlua)
810{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +0100811 struct hlua **hlua_store = lua_getextraspace(hlua->T);
812 *hlua_store = hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100813}
814
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100815/* This function is used to send logs. It try to send on screen (stderr)
816 * and on the default syslog server.
817 */
818static inline void hlua_sendlog(struct proxy *px, int level, const char *msg)
819{
820 struct tm tm;
821 char *p;
822
823 /* Cleanup the log message. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200824 p = trash.area;
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100825 for (; *msg != '\0'; msg++, p++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200826 if (p >= trash.area + trash.size - 1) {
Thierry FOURNIERccf00632015-09-16 12:47:03 +0200827 /* Break the message if exceed the buffer size. */
828 *(p-4) = ' ';
829 *(p-3) = '.';
830 *(p-2) = '.';
831 *(p-1) = '.';
832 break;
833 }
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100834 if (isprint(*msg))
835 *p = *msg;
836 else
837 *p = '.';
838 }
839 *p = '\0';
840
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200841 send_log(px, level, "%s\n", trash.area);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100842 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
Willy Tarreaua678b432015-08-28 10:14:59 +0200843 get_localtime(date.tv_sec, &tm);
844 fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n",
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100845 log_levels[level], tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec,
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200846 (int)getpid(), trash.area);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100847 fflush(stderr);
848 }
849}
850
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100851/* This function just ensure that the yield will be always
852 * returned with a timeout and permit to set some flags
853 */
854__LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx,
Thierry FOURNIERf90838b2015-03-06 13:48:32 +0100855 lua_KFunction k, int timeout, unsigned int flags)
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100856{
857 struct hlua *hlua = hlua_gethlua(L);
858
859 /* Set the wake timeout. If timeout is required, we set
860 * the expiration time.
861 */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +0200862 hlua->wake_time = timeout;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100863
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +0100864 hlua->flags |= flags;
865
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100866 /* Process the yield. */
Willy Tarreau9635e032018-10-16 17:52:55 +0200867 MAY_LJMP(lua_yieldk(L, nresults, ctx, k));
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100868}
869
Willy Tarreau87b09662015-04-03 00:22:06 +0200870/* This function initialises the Lua environment stored in the stream.
871 * It must be called at the start of the stream. This function creates
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100872 * an LUA coroutine. It can not be use to crete the main LUA context.
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200873 *
874 * This function is particular. it initialises a new Lua thread. If the
875 * initialisation fails (example: out of memory error), the lua function
876 * throws an error (longjmp).
877 *
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100878 * In some case (at least one), this function can be called from safe
879 * environement, so we must not initialise it. While the support of
880 * threads appear, the safe environment set a lock to ensure only one
881 * Lua execution at a time. If we initialize safe environment in another
882 * safe environmenet, we have a dead lock.
883 *
884 * set "already_safe" true if the context is initialized form safe
885 * Lua fonction.
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 */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100892int hlua_ctx_init(struct hlua *lua, struct task *task, int already_safe)
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100893{
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100894 if (!already_safe) {
895 if (!SET_SAFE_LJMP(gL.T)) {
896 lua->Tref = LUA_REFNIL;
897 return 0;
898 }
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200899 }
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100900 lua->Mref = LUA_REFNIL;
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +0100901 lua->flags = 0;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100902 LIST_INIT(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100903 lua->T = lua_newthread(gL.T);
904 if (!lua->T) {
905 lua->Tref = LUA_REFNIL;
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100906 if (!already_safe)
907 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100908 return 0;
909 }
910 hlua_sethlua(lua);
911 lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
912 lua->task = task;
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100913 if (!already_safe)
914 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100915 return 1;
916}
917
Willy Tarreau87b09662015-04-03 00:22:06 +0200918/* Used to destroy the Lua coroutine when the attached stream or task
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100919 * is destroyed. The destroy also the memory context. The struct "lua"
920 * is not freed.
921 */
922void hlua_ctx_destroy(struct hlua *lua)
923{
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100924 if (!lua)
Thierry FOURNIERa718b292015-03-04 16:48:34 +0100925 return;
926
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100927 if (!lua->T)
928 goto end;
929
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100930 /* Purge all the pending signals. */
Thierry FOURNIERd6975962017-07-12 14:31:10 +0200931 notification_purge(&lua->com);
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100932
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200933 if (!SET_SAFE_LJMP(lua->T))
934 return;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100935 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200936 RESET_SAFE_LJMP(lua->T);
Thierry FOURNIER5a50a852015-09-23 16:59:28 +0200937
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200938 if (!SET_SAFE_LJMP(gL.T))
939 return;
940 luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref);
941 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER5a50a852015-09-23 16:59:28 +0200942 /* Forces a garbage collecting process. If the Lua program is finished
943 * without error, we run the GC on the thread pointer. Its freed all
944 * the unused memory.
945 * If the thread is finnish with an error or is currently yielded,
946 * it seems that the GC applied on the thread doesn't clean anything,
947 * so e run the GC on the main thread.
948 * NOTE: maybe this action locks all the Lua threads untiml the en of
949 * the garbage collection.
950 */
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +0200951 if (lua->flags & HLUA_MUST_GC) {
Thierry FOURNIER7bd10d52017-07-17 00:44:40 +0200952 if (!SET_SAFE_LJMP(gL.T))
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200953 return;
Thierry FOURNIER7bd10d52017-07-17 00:44:40 +0200954 lua_gc(gL.T, LUA_GCCOLLECT, 0);
955 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +0200956 }
Thierry FOURNIER5a50a852015-09-23 16:59:28 +0200957
Thierry FOURNIERa7b536b2015-09-21 22:50:24 +0200958 lua->T = NULL;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100959
960end:
Willy Tarreaubafbe012017-11-24 17:34:44 +0100961 pool_free(pool_head_hlua, lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100962}
963
964/* This function is used to restore the Lua context when a coroutine
965 * fails. This function copy the common memory between old coroutine
966 * and the new coroutine. The old coroutine is destroyed, and its
967 * replaced by the new coroutine.
968 * If the flag "keep_msg" is set, the last entry of the old is assumed
969 * as string error message and it is copied in the new stack.
970 */
971static int hlua_ctx_renew(struct hlua *lua, int keep_msg)
972{
973 lua_State *T;
974 int new_ref;
975
976 /* Renew the main LUA stack doesn't have sense. */
977 if (lua == &gL)
978 return 0;
979
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100980 /* New Lua coroutine. */
981 T = lua_newthread(gL.T);
982 if (!T)
983 return 0;
984
985 /* Copy last error message. */
986 if (keep_msg)
987 lua_xmove(lua->T, T, 1);
988
989 /* Copy data between the coroutines. */
990 lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref);
991 lua_xmove(lua->T, T, 1);
992 new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Valur poped. */
993
994 /* Destroy old data. */
995 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
996
997 /* The thread is garbage collected by Lua. */
998 luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref);
999
1000 /* Fill the struct with the new coroutine values. */
1001 lua->Mref = new_ref;
1002 lua->T = T;
1003 lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
1004
1005 /* Set context. */
1006 hlua_sethlua(lua);
1007
1008 return 1;
1009}
1010
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001011void hlua_hook(lua_State *L, lua_Debug *ar)
1012{
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001013 struct hlua *hlua = hlua_gethlua(L);
1014
1015 /* Lua cannot yield when its returning from a function,
1016 * so, we can fix the interrupt hook to 1 instruction,
1017 * expecting that the function is finnished.
1018 */
1019 if (lua_gethookmask(L) & LUA_MASKRET) {
1020 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1);
1021 return;
1022 }
1023
1024 /* restore the interrupt condition. */
1025 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
1026
1027 /* If we interrupt the Lua processing in yieldable state, we yield.
1028 * If the state is not yieldable, trying yield causes an error.
1029 */
1030 if (lua_isyieldable(L))
Willy Tarreau9635e032018-10-16 17:52:55 +02001031 MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD));
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001032
Thierry FOURNIERa85cfb12015-03-13 14:50:06 +01001033 /* If we cannot yield, update the clock and check the timeout. */
1034 tv_update_date(0, 1);
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001035 hlua->run_time += now_ms - hlua->start_time;
1036 if (hlua->max_time && hlua->run_time >= hlua->max_time) {
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001037 lua_pushfstring(L, "execution timeout");
1038 WILL_LJMP(lua_error(L));
1039 }
1040
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001041 /* Update the start time. */
1042 hlua->start_time = now_ms;
1043
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001044 /* Try to interrupt the process at the end of the current
1045 * unyieldable function.
1046 */
1047 lua_sethook(hlua->T, hlua_hook, LUA_MASKRET|LUA_MASKCOUNT, hlua_nb_instruction);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001048}
1049
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001050/* This function start or resumes the Lua stack execution. If the flag
1051 * "yield_allowed" if no set and the LUA stack execution returns a yield
1052 * The function return an error.
1053 *
1054 * The function can returns 4 values:
1055 * - HLUA_E_OK : The execution is terminated without any errors.
1056 * - HLUA_E_AGAIN : The execution must continue at the next associated
1057 * task wakeup.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001058 * - HLUA_E_ERRMSG : An error has occurred, an error message is set in
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001059 * the top of the stack.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001060 * - HLUA_E_ERR : An error has occurred without error message.
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001061 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001062 * If an error occurred, the stack is renewed and it is ready to run new
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001063 * LUA code.
1064 */
1065static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed)
1066{
1067 int ret;
1068 const char *msg;
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001069 const char *trace;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001070
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001071 /* Initialise run time counter. */
1072 if (!HLUA_IS_RUNNING(lua))
1073 lua->run_time = 0;
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001074
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001075 /* Lock the whole Lua execution. This lock must be before the
1076 * label "resume_execution".
1077 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001078 HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock);
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001079
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001080resume_execution:
1081
1082 /* This hook interrupts the Lua processing each 'hlua_nb_instruction'
1083 * instructions. it is used for preventing infinite loops.
1084 */
1085 lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
1086
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +01001087 /* Remove all flags except the running flags. */
Thierry FOURNIER2f3867f2015-09-28 01:02:01 +02001088 HLUA_SET_RUN(lua);
1089 HLUA_CLR_CTRLYIELD(lua);
1090 HLUA_CLR_WAKERESWR(lua);
1091 HLUA_CLR_WAKEREQWR(lua);
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +01001092
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001093 /* Update the start time. */
1094 lua->start_time = now_ms;
1095
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001096 /* Call the function. */
1097 ret = lua_resume(lua->T, gL.T, lua->nargs);
1098 switch (ret) {
1099
1100 case LUA_OK:
1101 ret = HLUA_E_OK;
1102 break;
1103
1104 case LUA_YIELD:
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001105 /* Check if the execution timeout is expired. It it is the case, we
1106 * break the Lua execution.
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001107 */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001108 tv_update_date(0, 1);
1109 lua->run_time += now_ms - lua->start_time;
1110 if (lua->max_time && lua->run_time > lua->max_time) {
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001111 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001112 ret = HLUA_E_ETMOUT;
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001113 break;
1114 }
1115 /* Process the forced yield. if the general yield is not allowed or
1116 * if no task were associated this the current Lua execution
1117 * coroutine, we resume the execution. Else we want to return in the
1118 * scheduler and we want to be waked up again, to continue the
1119 * current Lua execution. So we schedule our own task.
1120 */
1121 if (HLUA_IS_CTRLYIELDING(lua)) {
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001122 if (!yield_allowed || !lua->task)
1123 goto resume_execution;
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001124 task_wakeup(lua->task, TASK_WOKEN_MSG);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001125 }
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001126 if (!yield_allowed) {
1127 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001128 ret = HLUA_E_YIELD;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001129 break;
1130 }
1131 ret = HLUA_E_AGAIN;
1132 break;
1133
1134 case LUA_ERRRUN:
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001135
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001136 /* Special exit case. The traditional exit is returned as an error
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001137 * because the errors ares the only one mean to return immediately
1138 * from and lua execution.
1139 */
1140 if (lua->flags & HLUA_EXIT) {
1141 ret = HLUA_E_OK;
Thierry FOURNIERe1587b32015-08-28 09:54:13 +02001142 hlua_ctx_renew(lua, 0);
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001143 break;
1144 }
1145
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001146 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001147 if (!lua_checkstack(lua->T, 1)) {
1148 ret = HLUA_E_ERR;
1149 break;
1150 }
1151 msg = lua_tostring(lua->T, -1);
1152 lua_settop(lua->T, 0); /* Empty the stack. */
1153 lua_pop(lua->T, 1);
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001154 trace = hlua_traceback(lua->T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001155 if (msg)
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001156 lua_pushfstring(lua->T, "runtime error: %s from %s", msg, trace);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001157 else
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001158 lua_pushfstring(lua->T, "unknown runtime error from %s", trace);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001159 ret = HLUA_E_ERRMSG;
1160 break;
1161
1162 case LUA_ERRMEM:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001163 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001164 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001165 ret = HLUA_E_NOMEM;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001166 break;
1167
1168 case LUA_ERRERR:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001169 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001170 if (!lua_checkstack(lua->T, 1)) {
1171 ret = HLUA_E_ERR;
1172 break;
1173 }
1174 msg = lua_tostring(lua->T, -1);
1175 lua_settop(lua->T, 0); /* Empty the stack. */
1176 lua_pop(lua->T, 1);
1177 if (msg)
1178 lua_pushfstring(lua->T, "message handler error: %s", msg);
1179 else
1180 lua_pushfstring(lua->T, "message handler error");
1181 ret = HLUA_E_ERRMSG;
1182 break;
1183
1184 default:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001185 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001186 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001187 ret = HLUA_E_ERR;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001188 break;
1189 }
1190
Thierry FOURNIER6ab4d8e2015-09-27 22:17:19 +02001191 /* This GC permits to destroy some object when a Lua timeout strikes. */
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +02001192 if (lua->flags & HLUA_MUST_GC &&
1193 ret != HLUA_E_AGAIN)
Thierry FOURNIER6ab4d8e2015-09-27 22:17:19 +02001194 lua_gc(lua->T, LUA_GCCOLLECT, 0);
1195
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001196 switch (ret) {
1197 case HLUA_E_AGAIN:
1198 break;
1199
1200 case HLUA_E_ERRMSG:
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001201 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001202 hlua_ctx_renew(lua, 1);
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001203 HLUA_CLR_RUN(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001204 break;
1205
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001206 case HLUA_E_ETMOUT:
1207 case HLUA_E_NOMEM:
1208 case HLUA_E_YIELD:
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001209 case HLUA_E_ERR:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001210 HLUA_CLR_RUN(lua);
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001211 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001212 hlua_ctx_renew(lua, 0);
1213 break;
1214
1215 case HLUA_E_OK:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001216 HLUA_CLR_RUN(lua);
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001217 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001218 break;
1219 }
1220
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001221 /* This is the main exit point, remove the Lua lock. */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001222 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock);
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001223
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001224 return ret;
1225}
1226
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001227/* This function exit the current code. */
1228__LJMP static int hlua_done(lua_State *L)
1229{
1230 struct hlua *hlua = hlua_gethlua(L);
1231
1232 hlua->flags |= HLUA_EXIT;
1233 WILL_LJMP(lua_error(L));
1234
1235 return 0;
1236}
1237
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001238/* This function is an LUA binding. It provides a function
1239 * for deleting ACL from a referenced ACL file.
1240 */
1241__LJMP static int hlua_del_acl(lua_State *L)
1242{
1243 const char *name;
1244 const char *key;
1245 struct pat_ref *ref;
1246
1247 MAY_LJMP(check_args(L, 2, "del_acl"));
1248
1249 name = MAY_LJMP(luaL_checkstring(L, 1));
1250 key = MAY_LJMP(luaL_checkstring(L, 2));
1251
1252 ref = pat_ref_lookup(name);
1253 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001254 WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001255
1256 pat_ref_delete(ref, key);
1257 return 0;
1258}
1259
1260/* This function is an LUA binding. It provides a function
1261 * for deleting map entry from a referenced map file.
1262 */
1263static int hlua_del_map(lua_State *L)
1264{
1265 const char *name;
1266 const char *key;
1267 struct pat_ref *ref;
1268
1269 MAY_LJMP(check_args(L, 2, "del_map"));
1270
1271 name = MAY_LJMP(luaL_checkstring(L, 1));
1272 key = MAY_LJMP(luaL_checkstring(L, 2));
1273
1274 ref = pat_ref_lookup(name);
1275 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001276 WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001277
1278 pat_ref_delete(ref, key);
1279 return 0;
1280}
1281
1282/* This function is an LUA binding. It provides a function
1283 * for adding ACL pattern from a referenced ACL file.
1284 */
1285static int hlua_add_acl(lua_State *L)
1286{
1287 const char *name;
1288 const char *key;
1289 struct pat_ref *ref;
1290
1291 MAY_LJMP(check_args(L, 2, "add_acl"));
1292
1293 name = MAY_LJMP(luaL_checkstring(L, 1));
1294 key = MAY_LJMP(luaL_checkstring(L, 2));
1295
1296 ref = pat_ref_lookup(name);
1297 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001298 WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001299
1300 if (pat_ref_find_elt(ref, key) == NULL)
1301 pat_ref_add(ref, key, NULL, NULL);
1302 return 0;
1303}
1304
1305/* This function is an LUA binding. It provides a function
1306 * for setting map pattern and sample from a referenced map
1307 * file.
1308 */
1309static int hlua_set_map(lua_State *L)
1310{
1311 const char *name;
1312 const char *key;
1313 const char *value;
1314 struct pat_ref *ref;
1315
1316 MAY_LJMP(check_args(L, 3, "set_map"));
1317
1318 name = MAY_LJMP(luaL_checkstring(L, 1));
1319 key = MAY_LJMP(luaL_checkstring(L, 2));
1320 value = MAY_LJMP(luaL_checkstring(L, 3));
1321
1322 ref = pat_ref_lookup(name);
1323 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001324 WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001325
1326 if (pat_ref_find_elt(ref, key) != NULL)
1327 pat_ref_set(ref, key, value, NULL);
1328 else
1329 pat_ref_add(ref, key, value, NULL);
1330 return 0;
1331}
1332
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001333/* A class is a lot of memory that contain data. This data can be a table,
1334 * an integer or user data. This data is associated with a metatable. This
1335 * metatable have an original version registred in the global context with
1336 * the name of the object (_G[<name>] = <metable> ).
1337 *
1338 * A metable is a table that modify the standard behavior of a standard
1339 * access to the associated data. The entries of this new metatable are
1340 * defined as is:
1341 *
1342 * http://lua-users.org/wiki/MetatableEvents
1343 *
1344 * __index
1345 *
1346 * we access an absent field in a table, the result is nil. This is
1347 * true, but it is not the whole truth. Actually, such access triggers
1348 * the interpreter to look for an __index metamethod: If there is no
1349 * such method, as usually happens, then the access results in nil;
1350 * otherwise, the metamethod will provide the result.
1351 *
1352 * Control 'prototype' inheritance. When accessing "myTable[key]" and
1353 * the key does not appear in the table, but the metatable has an __index
1354 * property:
1355 *
1356 * - if the value is a function, the function is called, passing in the
1357 * table and the key; the return value of that function is returned as
1358 * the result.
1359 *
1360 * - if the value is another table, the value of the key in that table is
1361 * asked for and returned (and if it doesn't exist in that table, but that
1362 * table's metatable has an __index property, then it continues on up)
1363 *
1364 * - Use "rawget(myTable,key)" to skip this metamethod.
1365 *
1366 * http://www.lua.org/pil/13.4.1.html
1367 *
1368 * __newindex
1369 *
1370 * Like __index, but control property assignment.
1371 *
1372 * __mode - Control weak references. A string value with one or both
1373 * of the characters 'k' and 'v' which specifies that the the
1374 * keys and/or values in the table are weak references.
1375 *
1376 * __call - Treat a table like a function. When a table is followed by
1377 * parenthesis such as "myTable( 'foo' )" and the metatable has
1378 * a __call key pointing to a function, that function is invoked
1379 * (passing any specified arguments) and the return value is
1380 * returned.
1381 *
1382 * __metatable - Hide the metatable. When "getmetatable( myTable )" is
1383 * called, if the metatable for myTable has a __metatable
1384 * key, the value of that key is returned instead of the
1385 * actual metatable.
1386 *
1387 * __tostring - Control string representation. When the builtin
1388 * "tostring( myTable )" function is called, if the metatable
1389 * for myTable has a __tostring property set to a function,
1390 * that function is invoked (passing myTable to it) and the
1391 * return value is used as the string representation.
1392 *
1393 * __len - Control table length. When the table length is requested using
1394 * the length operator ( '#' ), if the metatable for myTable has
1395 * a __len key pointing to a function, that function is invoked
1396 * (passing myTable to it) and the return value used as the value
1397 * of "#myTable".
1398 *
1399 * __gc - Userdata finalizer code. When userdata is set to be garbage
1400 * collected, if the metatable has a __gc field pointing to a
1401 * function, that function is first invoked, passing the userdata
1402 * to it. The __gc metamethod is not called for tables.
1403 * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html)
1404 *
1405 * Special metamethods for redefining standard operators:
1406 * http://www.lua.org/pil/13.1.html
1407 *
1408 * __add "+"
1409 * __sub "-"
1410 * __mul "*"
1411 * __div "/"
1412 * __unm "!"
1413 * __pow "^"
1414 * __concat ".."
1415 *
1416 * Special methods for redfining standar relations
1417 * http://www.lua.org/pil/13.2.html
1418 *
1419 * __eq "=="
1420 * __lt "<"
1421 * __le "<="
1422 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001423
1424/*
1425 *
1426 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001427 * Class Map
1428 *
1429 *
1430 */
1431
1432/* Returns a struct hlua_map if the stack entry "ud" is
1433 * a class session, otherwise it throws an error.
1434 */
1435__LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud)
1436{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001437 return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001438}
1439
1440/* This function is the map constructor. It don't need
1441 * the class Map object. It creates and return a new Map
1442 * object. It must be called only during "body" or "init"
1443 * context because it process some filesystem accesses.
1444 */
1445__LJMP static int hlua_map_new(struct lua_State *L)
1446{
1447 const char *fn;
1448 int match = PAT_MATCH_STR;
1449 struct sample_conv conv;
1450 const char *file = "";
1451 int line = 0;
1452 lua_Debug ar;
1453 char *err = NULL;
1454 struct arg args[2];
1455
1456 if (lua_gettop(L) < 1 || lua_gettop(L) > 2)
1457 WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument."));
1458
1459 fn = MAY_LJMP(luaL_checkstring(L, 1));
1460
1461 if (lua_gettop(L) >= 2) {
1462 match = MAY_LJMP(luaL_checkinteger(L, 2));
1463 if (match < 0 || match >= PAT_MATCH_NUM)
1464 WILL_LJMP(luaL_error(L, "'new' needs a valid match method."));
1465 }
1466
1467 /* Get Lua filename and line number. */
1468 if (lua_getstack(L, 1, &ar)) { /* check function at level */
1469 lua_getinfo(L, "Sl", &ar); /* get info about it */
1470 if (ar.currentline > 0) { /* is there info? */
1471 file = ar.short_src;
1472 line = ar.currentline;
1473 }
1474 }
1475
1476 /* fill fake sample_conv struct. */
1477 conv.kw = ""; /* unused. */
1478 conv.process = NULL; /* unused. */
1479 conv.arg_mask = 0; /* unused. */
1480 conv.val_args = NULL; /* unused. */
1481 conv.out_type = SMP_T_STR;
1482 conv.private = (void *)(long)match;
1483 switch (match) {
1484 case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break;
1485 case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break;
1486 case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break;
1487 case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break;
1488 case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break;
1489 case PAT_MATCH_END: conv.in_type = SMP_T_STR; break;
1490 case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001491 case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001492 case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break;
1493 default:
1494 WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode."));
1495 }
1496
1497 /* fill fake args. */
1498 args[0].type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001499 args[0].data.str.area = (char *)fn;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001500 args[1].type = ARGT_STOP;
1501
1502 /* load the map. */
1503 if (!sample_load_map(args, &conv, file, line, &err)) {
1504 /* error case: we cant use luaL_error because we must
1505 * free the err variable.
1506 */
1507 luaL_where(L, 1);
1508 lua_pushfstring(L, "'new': %s.", err);
1509 lua_concat(L, 2);
1510 free(err);
1511 WILL_LJMP(lua_error(L));
1512 }
1513
1514 /* create the lua object. */
1515 lua_newtable(L);
1516 lua_pushlightuserdata(L, args[0].data.map);
1517 lua_rawseti(L, -2, 0);
1518
1519 /* Pop a class Map metatable and affect it to the userdata. */
1520 lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref);
1521 lua_setmetatable(L, -2);
1522
1523
1524 return 1;
1525}
1526
1527__LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str)
1528{
1529 struct map_descriptor *desc;
1530 struct pattern *pat;
1531 struct sample smp;
1532
1533 MAY_LJMP(check_args(L, 2, "lookup"));
1534 desc = MAY_LJMP(hlua_checkmap(L, 1));
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001535 if (desc->pat.expect_type == SMP_T_SINT) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001536 smp.data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001537 smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001538 }
1539 else {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001540 smp.data.type = SMP_T_STR;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001541 smp.flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001542 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 +02001543 }
1544
1545 pat = pattern_exec_match(&desc->pat, &smp, 1);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001546 if (!pat || !pat->data) {
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001547 if (str)
1548 lua_pushstring(L, "");
1549 else
1550 lua_pushnil(L);
1551 return 1;
1552 }
1553
1554 /* The Lua pattern must return a string, so we can't check the returned type */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001555 lua_pushlstring(L, pat->data->u.str.area, pat->data->u.str.data);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001556 return 1;
1557}
1558
1559__LJMP static int hlua_map_lookup(struct lua_State *L)
1560{
1561 return _hlua_map_lookup(L, 0);
1562}
1563
1564__LJMP static int hlua_map_slookup(struct lua_State *L)
1565{
1566 return _hlua_map_lookup(L, 1);
1567}
1568
1569/*
1570 *
1571 *
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001572 * Class Socket
1573 *
1574 *
1575 */
1576
1577__LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud)
1578{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001579 return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001580}
1581
1582/* This function is the handler called for each I/O on the established
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001583 * connection. It is used for notify space available to send or data
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001584 * received.
1585 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001586static void hlua_socket_handler(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001587{
Willy Tarreau00a37f02015-04-13 12:05:19 +02001588 struct stream_interface *si = appctx->owner;
Olivier Houchard9aaf7782017-09-13 18:30:23 +02001589 struct connection *c = cs_conn(objt_cs(si_opposite(si)->end));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001590
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001591 if (appctx->ctx.hlua_cosocket.die) {
1592 si_shutw(si);
1593 si_shutr(si);
1594 si_ic(si)->flags |= CF_READ_NULL;
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001595 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1596 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001597 stream_shutdown(si_strm(si), SF_ERR_KILLED);
1598 }
1599
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001600 /* If the connection object is not available, close all the
1601 * streams and wakeup everything waiting for.
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001602 */
1603 if (!c) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001604 si_shutw(si);
1605 si_shutr(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01001606 si_ic(si)->flags |= CF_READ_NULL;
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001607 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1608 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Willy Tarreaud4da1962015-04-20 01:31:23 +02001609 return;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001610 }
1611
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001612 /* If we cant write, wakeup the pending write signals. */
1613 if (channel_output_closed(si_ic(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001614 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001615
1616 /* If we cant read, wakeup the pending read signals. */
1617 if (channel_input_closed(si_oc(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001618 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001619
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001620 /* if the connection is not estabkished, inform the stream that we want
1621 * to be notified whenever the connection completes.
1622 */
1623 if (!(c->flags & CO_FL_CONNECTED)) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01001624 si_cant_get(si);
Willy Tarreau12c24232018-12-06 15:29:50 +01001625 si_rx_conn_blk(si);
Willy Tarreau3367d412018-11-15 10:57:41 +01001626 si_rx_endp_more(si);
Willy Tarreaud4da1962015-04-20 01:31:23 +02001627 return;
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001628 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001629
1630 /* This function is called after the connect. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01001631 appctx->ctx.hlua_cosocket.connected = 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001632
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001633 /* Wake the tasks which wants to write if the buffer have available space. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001634 if (channel_may_recv(si_ic(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001635 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001636
1637 /* Wake the tasks which wants to read if the buffer contains data. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001638 if (!channel_is_empty(si_oc(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001639 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001640
1641 /* Some data were injected in the buffer, notify the stream
1642 * interface.
1643 */
1644 if (!channel_is_empty(si_ic(si)))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01001645 si_update(si);
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001646
1647 /* If write notifications are registered, we considers we want
Willy Tarreau3367d412018-11-15 10:57:41 +01001648 * to write, so we clear the blocking flag.
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001649 */
1650 if (notification_registered(&appctx->ctx.hlua_cosocket.wake_on_write))
Willy Tarreau3367d412018-11-15 10:57:41 +01001651 si_rx_endp_more(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001652}
1653
Willy Tarreau87b09662015-04-03 00:22:06 +02001654/* This function is called when the "struct stream" is destroyed.
1655 * Remove the link from the object to this stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001656 * Wake all the pending signals.
1657 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001658static void hlua_socket_release(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001659{
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001660 struct xref *peer;
1661
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001662 /* Remove my link in the original object. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001663 peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref);
1664 if (peer)
1665 xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001666
1667 /* Wake all the task waiting for me. */
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001668 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1669 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001670}
1671
1672/* If the garbage collectio of the object is launch, nobody
Willy Tarreau87b09662015-04-03 00:22:06 +02001673 * uses this object. If the stream does not exists, just quit.
1674 * Send the shutdown signal to the stream. In some cases,
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001675 * pending signal can rest in the read and write lists. destroy
1676 * it.
1677 */
1678__LJMP static int hlua_socket_gc(lua_State *L)
1679{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001680 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001681 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001682 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001683
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001684 MAY_LJMP(check_args(L, 1, "__gc"));
1685
1686 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001687 peer = xref_get_peer_and_lock(&socket->xref);
1688 if (!peer)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001689 return 0;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001690 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001691
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001692 /* Set the flag which destroy the session. */
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001693 appctx->ctx.hlua_cosocket.die = 1;
1694 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001695
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001696 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001697 xref_disconnect(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001698 return 0;
1699}
1700
1701/* The close function send shutdown signal and break the
Willy Tarreau87b09662015-04-03 00:22:06 +02001702 * links between the stream and the object.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001703 */
sada05ed3302018-05-11 11:48:18 -07001704__LJMP static int hlua_socket_close_helper(lua_State *L)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001705{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001706 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001707 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001708 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001709
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001710 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001711
1712 /* Check if we run on the same thread than the xreator thread.
1713 * We cannot access to the socket if the thread is different.
1714 */
1715 if (socket->tid != tid)
1716 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1717
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001718 peer = xref_get_peer_and_lock(&socket->xref);
1719 if (!peer)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001720 return 0;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001721 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001722
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001723 /* Set the flag which destroy the session. */
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001724 appctx->ctx.hlua_cosocket.die = 1;
1725 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001726
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001727 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001728 xref_disconnect(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001729 return 0;
1730}
1731
sada05ed3302018-05-11 11:48:18 -07001732/* The close function calls close_helper.
1733 */
1734__LJMP static int hlua_socket_close(lua_State *L)
1735{
1736 MAY_LJMP(check_args(L, 1, "close"));
1737 return hlua_socket_close_helper(L);
1738}
1739
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001740/* This Lua function assumes that the stack contain three parameters.
1741 * 1 - USERDATA containing a struct socket
1742 * 2 - INTEGER with values of the macro defined below
1743 * If the integer is -1, we must read at most one line.
1744 * If the integer is -2, we ust read all the data until the
1745 * end of the stream.
1746 * If the integer is positive value, we must read a number of
1747 * bytes corresponding to this value.
1748 */
1749#define HLSR_READ_LINE (-1)
1750#define HLSR_READ_ALL (-2)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001751__LJMP static int hlua_socket_receive_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001752{
1753 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
1754 int wanted = lua_tointeger(L, 2);
1755 struct hlua *hlua = hlua_gethlua(L);
1756 struct appctx *appctx;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001757 size_t len;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001758 int nblk;
Willy Tarreau206ba832018-06-14 15:27:31 +02001759 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001760 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02001761 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001762 size_t len2;
Thierry FOURNIER00543922015-03-09 18:35:06 +01001763 int skip_at_end = 0;
Willy Tarreau81389672015-03-10 12:03:52 +01001764 struct channel *oc;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001765 struct stream_interface *si;
1766 struct stream *s;
1767 struct xref *peer;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001768 int missing_bytes;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001769
1770 /* Check if this lua stack is schedulable. */
1771 if (!hlua || !hlua->task)
1772 WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in "
1773 "'frontend', 'backend' or 'task'"));
1774
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001775 /* Check if we run on the same thread than the xreator thread.
1776 * We cannot access to the socket if the thread is different.
1777 */
1778 if (socket->tid != tid)
1779 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1780
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001781 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001782 peer = xref_get_peer_and_lock(&socket->xref);
1783 if (!peer)
1784 goto no_peer;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001785 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
1786 si = appctx->owner;
1787 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001788
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001789 oc = &s->res;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001790 if (wanted == HLSR_READ_LINE) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001791 /* Read line. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001792 nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001793 if (nblk < 0) /* Connection close. */
1794 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001795 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001796 goto connection_empty;
Thierry FOURNIER00543922015-03-09 18:35:06 +01001797
1798 /* remove final \r\n. */
1799 if (nblk == 1) {
1800 if (blk1[len1-1] == '\n') {
1801 len1--;
1802 skip_at_end++;
1803 if (blk1[len1-1] == '\r') {
1804 len1--;
1805 skip_at_end++;
1806 }
1807 }
1808 }
1809 else {
1810 if (blk2[len2-1] == '\n') {
1811 len2--;
1812 skip_at_end++;
1813 if (blk2[len2-1] == '\r') {
1814 len2--;
1815 skip_at_end++;
1816 }
1817 }
1818 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001819 }
1820
1821 else if (wanted == HLSR_READ_ALL) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001822 /* Read all the available data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001823 nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001824 if (nblk < 0) /* Connection close. */
1825 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001826 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001827 goto connection_empty;
1828 }
1829
1830 else {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001831 /* Read a block of data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001832 nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001833 if (nblk < 0) /* Connection close. */
1834 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001835 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001836 goto connection_empty;
1837
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001838 missing_bytes = wanted - socket->b.n;
1839 if (len1 > missing_bytes) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001840 nblk = 1;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001841 len1 = missing_bytes;
1842 } if (nblk == 2 && len1 + len2 > missing_bytes)
1843 len2 = missing_bytes - len1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001844 }
1845
1846 len = len1;
1847
1848 luaL_addlstring(&socket->b, blk1, len1);
1849 if (nblk == 2) {
1850 len += len2;
1851 luaL_addlstring(&socket->b, blk2, len2);
1852 }
1853
1854 /* Consume data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001855 co_skip(oc, len + skip_at_end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001856
1857 /* Don't wait anything. */
Thierry FOURNIER7e4ee472018-05-25 15:03:50 +02001858 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001859
1860 /* If the pattern reclaim to read all the data
1861 * in the connection, got out.
1862 */
1863 if (wanted == HLSR_READ_ALL)
1864 goto connection_empty;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001865 else if (wanted >= 0 && socket->b.n < wanted)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001866 goto connection_empty;
1867
1868 /* Return result. */
1869 luaL_pushresult(&socket->b);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001870 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001871 return 1;
1872
1873connection_closed:
1874
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001875 xref_unlock(&socket->xref, peer);
1876
1877no_peer:
1878
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001879 /* If the buffer containds data. */
1880 if (socket->b.n > 0) {
1881 luaL_pushresult(&socket->b);
1882 return 1;
1883 }
1884 lua_pushnil(L);
1885 lua_pushstring(L, "connection closed.");
1886 return 2;
1887
1888connection_empty:
1889
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001890 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) {
1891 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001892 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001893 }
1894 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02001895 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_receive_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001896 return 0;
1897}
1898
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001899/* This Lua function gets two parameters. The first one can be string
1900 * or a number. If the string is "*l", the user requires one line. If
1901 * the string is "*a", the user requires all the contents of the stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001902 * If the value is a number, the user require a number of bytes equal
1903 * to the value. The default value is "*l" (a line).
1904 *
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001905 * This parameter with a variable type is converted in integer. This
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001906 * integer takes this values:
1907 * -1 : read a line
1908 * -2 : read all the stream
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001909 * >0 : amount of bytes.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001910 *
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001911 * The second parameter is optional. It contains a string that must be
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001912 * concatenated with the read data.
1913 */
1914__LJMP static int hlua_socket_receive(struct lua_State *L)
1915{
1916 int wanted = HLSR_READ_LINE;
1917 const char *pattern;
1918 int type;
1919 char *error;
1920 size_t len;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001921 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001922
1923 if (lua_gettop(L) < 1 || lua_gettop(L) > 3)
1924 WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments."));
1925
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001926 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001927
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001928 /* Check if we run on the same thread than the xreator thread.
1929 * We cannot access to the socket if the thread is different.
1930 */
1931 if (socket->tid != tid)
1932 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1933
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001934 /* check for pattern. */
1935 if (lua_gettop(L) >= 2) {
1936 type = lua_type(L, 2);
1937 if (type == LUA_TSTRING) {
1938 pattern = lua_tostring(L, 2);
1939 if (strcmp(pattern, "*a") == 0)
1940 wanted = HLSR_READ_ALL;
1941 else if (strcmp(pattern, "*l") == 0)
1942 wanted = HLSR_READ_LINE;
1943 else {
1944 wanted = strtoll(pattern, &error, 10);
1945 if (*error != '\0')
1946 WILL_LJMP(luaL_error(L, "Unsupported pattern."));
1947 }
1948 }
1949 else if (type == LUA_TNUMBER) {
1950 wanted = lua_tointeger(L, 2);
1951 if (wanted < 0)
1952 WILL_LJMP(luaL_error(L, "Unsupported size."));
1953 }
1954 }
1955
1956 /* Set pattern. */
1957 lua_pushinteger(L, wanted);
Tim Duesterhusc6e377e2018-01-04 19:32:13 +01001958
1959 /* Check if we would replace the top by itself. */
1960 if (lua_gettop(L) != 2)
1961 lua_replace(L, 2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001962
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001963 /* init buffer, and fill it with prefix. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001964 luaL_buffinit(L, &socket->b);
1965
1966 /* Check prefix. */
1967 if (lua_gettop(L) >= 3) {
1968 if (lua_type(L, 3) != LUA_TSTRING)
1969 WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix"));
1970 pattern = lua_tolstring(L, 3, &len);
1971 luaL_addlstring(&socket->b, pattern, len);
1972 }
1973
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001974 return __LJMP(hlua_socket_receive_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001975}
1976
1977/* Write the Lua input string in the output buffer.
Mark Lakes22154b42018-01-29 14:38:40 -08001978 * This function returns a yield if no space is available.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001979 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001980static int hlua_socket_write_yield(struct lua_State *L,int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001981{
1982 struct hlua_socket *socket;
1983 struct hlua *hlua = hlua_gethlua(L);
1984 struct appctx *appctx;
1985 size_t buf_len;
1986 const char *buf;
1987 int len;
1988 int send_len;
1989 int sent;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001990 struct xref *peer;
1991 struct stream_interface *si;
1992 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001993
1994 /* Check if this lua stack is schedulable. */
1995 if (!hlua || !hlua->task)
1996 WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in "
1997 "'frontend', 'backend' or 'task'"));
1998
1999 /* Get object */
2000 socket = MAY_LJMP(hlua_checksocket(L, 1));
2001 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002002 sent = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002003
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002004 /* Check if we run on the same thread than the xreator thread.
2005 * We cannot access to the socket if the thread is different.
2006 */
2007 if (socket->tid != tid)
2008 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2009
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002010 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002011 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002012 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002013 lua_pushinteger(L, -1);
2014 return 1;
2015 }
2016 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2017 si = appctx->owner;
2018 s = si_strm(si);
2019
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002020 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002021 if (channel_output_closed(&s->req)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002022 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002023 lua_pushinteger(L, -1);
2024 return 1;
2025 }
2026
2027 /* Update the input buffer data. */
2028 buf += sent;
2029 send_len = buf_len - sent;
2030
2031 /* All the data are sent. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002032 if (sent >= buf_len) {
2033 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002034 return 1; /* Implicitly return the length sent. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002035 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002036
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002037 /* Check if the buffer is available because HAProxy doesn't allocate
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01002038 * the request buffer if its not required.
2039 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002040 if (s->req.buf.size == 0) {
Willy Tarreau581abd32018-10-25 10:21:41 +02002041 if (!si_alloc_ibuf(si, &appctx->buffer_wait))
Christopher Faulet33834b12016-12-19 09:29:06 +01002042 goto hlua_socket_write_yield_return;
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01002043 }
2044
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002045 /* Check for available space. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002046 len = b_room(&s->req.buf);
Christopher Faulet33834b12016-12-19 09:29:06 +01002047 if (len <= 0) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002048 goto hlua_socket_write_yield_return;
Christopher Faulet33834b12016-12-19 09:29:06 +01002049 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002050
2051 /* send data */
2052 if (len < send_len)
2053 send_len = len;
Thierry FOURNIER66b89192018-05-27 01:14:47 +02002054 len = ci_putblk(&s->req, buf, send_len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002055
2056 /* "Not enough space" (-1), "Buffer too little to contain
2057 * the data" (-2) are not expected because the available length
2058 * is tested.
2059 * Other unknown error are also not expected.
2060 */
2061 if (len <= 0) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01002062 if (len == -1)
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002063 s->req.flags |= CF_WAKE_WRITE;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002064
sada05ed3302018-05-11 11:48:18 -07002065 MAY_LJMP(hlua_socket_close_helper(L));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002066 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002067 lua_pushinteger(L, -1);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002068 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002069 return 1;
2070 }
2071
2072 /* update buffers. */
Thierry FOURNIER101b9762018-05-27 01:27:40 +02002073 appctx_wakeup(appctx);
Willy Tarreaude70fa12015-09-26 11:25:05 +02002074
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002075 s->req.rex = TICK_ETERNITY;
2076 s->res.wex = TICK_ETERNITY;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002077
2078 /* Update length sent. */
2079 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002080 lua_pushinteger(L, sent + len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002081
2082 /* All the data buffer is sent ? */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002083 if (sent + len >= buf_len) {
2084 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002085 return 1;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002086 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002087
2088hlua_socket_write_yield_return:
Thierry FOURNIERba42fcd2018-05-27 00:59:48 +02002089 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2090 xref_unlock(&socket->xref, peer);
2091 WILL_LJMP(luaL_error(L, "out of memory"));
2092 }
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002093 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002094 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_write_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002095 return 0;
2096}
2097
2098/* This function initiate the send of data. It just check the input
2099 * parameters and push an integer in the Lua stack that contain the
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002100 * amount of data written to the buffer. This is used by the function
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002101 * "hlua_socket_write_yield" that can yield.
2102 *
2103 * The Lua function gets between 3 and 4 parameters. The first one is
2104 * the associated object. The second is a string buffer. The third is
2105 * a facultative integer that represents where is the buffer position
2106 * of the start of the data that can send. The first byte is the
2107 * position "1". The default value is "1". The fourth argument is a
2108 * facultative integer that represents where is the buffer position
2109 * of the end of the data that can send. The default is the last byte.
2110 */
2111static int hlua_socket_send(struct lua_State *L)
2112{
2113 int i;
2114 int j;
2115 const char *buf;
2116 size_t buf_len;
2117
2118 /* Check number of arguments. */
2119 if (lua_gettop(L) < 2 || lua_gettop(L) > 4)
2120 WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments"));
2121
2122 /* Get the string. */
2123 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
2124
2125 /* Get and check j. */
2126 if (lua_gettop(L) == 4) {
2127 j = MAY_LJMP(luaL_checkinteger(L, 4));
2128 if (j < 0)
2129 j = buf_len + j + 1;
2130 if (j > buf_len)
2131 j = buf_len + 1;
2132 lua_pop(L, 1);
2133 }
2134 else
2135 j = buf_len;
2136
2137 /* Get and check i. */
2138 if (lua_gettop(L) == 3) {
2139 i = MAY_LJMP(luaL_checkinteger(L, 3));
2140 if (i < 0)
2141 i = buf_len + i + 1;
2142 if (i > buf_len)
2143 i = buf_len + 1;
2144 lua_pop(L, 1);
2145 } else
2146 i = 1;
2147
2148 /* Check bth i and j. */
2149 if (i > j) {
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 && j == 0) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002154 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002155 return 1;
2156 }
2157 if (i == 0)
2158 i = 1;
2159 if (j == 0)
2160 j = 1;
2161
2162 /* Pop the string. */
2163 lua_pop(L, 1);
2164
2165 /* Update the buffer length. */
2166 buf += i - 1;
2167 buf_len = j - i + 1;
2168 lua_pushlstring(L, buf, buf_len);
2169
2170 /* This unsigned is used to remember the amount of sent data. */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002171 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002172
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002173 return MAY_LJMP(hlua_socket_write_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002174}
2175
Willy Tarreau22b0a682015-06-17 19:43:49 +02002176#define SOCKET_INFO_MAX_LEN sizeof("[0000:0000:0000:0000:0000:0000:0000:0000]:12345")
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002177__LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr)
2178{
2179 static char buffer[SOCKET_INFO_MAX_LEN];
2180 int ret;
2181 int len;
2182 char *p;
2183
2184 ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1);
2185 if (ret <= 0) {
2186 lua_pushnil(L);
2187 return 1;
2188 }
2189
2190 if (ret == AF_UNIX) {
2191 lua_pushstring(L, buffer+1);
2192 return 1;
2193 }
2194 else if (ret == AF_INET6) {
2195 buffer[0] = '[';
2196 len = strlen(buffer);
2197 buffer[len] = ']';
2198 len++;
2199 buffer[len] = ':';
2200 len++;
2201 p = buffer;
2202 }
2203 else if (ret == AF_INET) {
2204 p = buffer + 1;
2205 len = strlen(p);
2206 p[len] = ':';
2207 len++;
2208 }
2209 else {
2210 lua_pushnil(L);
2211 return 1;
2212 }
2213
2214 if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) {
2215 lua_pushnil(L);
2216 return 1;
2217 }
2218
2219 lua_pushstring(L, p);
2220 return 1;
2221}
2222
2223/* Returns information about the peer of the connection. */
2224__LJMP static int hlua_socket_getpeername(struct lua_State *L)
2225{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002226 struct hlua_socket *socket;
2227 struct connection *conn;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002228 struct xref *peer;
2229 struct appctx *appctx;
2230 struct stream_interface *si;
2231 struct stream *s;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002232 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002233
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002234 MAY_LJMP(check_args(L, 1, "getpeername"));
2235
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002236 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002237
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002238 /* Check if we run on the same thread than the xreator thread.
2239 * We cannot access to the socket if the thread is different.
2240 */
2241 if (socket->tid != tid)
2242 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2243
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002244 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002245 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002246 if (!peer) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002247 lua_pushnil(L);
2248 return 1;
2249 }
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002250 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2251 si = appctx->owner;
2252 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002253
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002254 conn = cs_conn(objt_cs(s->si[1].end));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002255 if (!conn) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002256 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002257 lua_pushnil(L);
2258 return 1;
2259 }
2260
Willy Tarreaua71f6422016-11-16 17:00:14 +01002261 conn_get_to_addr(conn);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002262 if (!(conn->flags & CO_FL_ADDR_TO_SET)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002263 xref_unlock(&socket->xref, peer);
Willy Tarreaua71f6422016-11-16 17:00:14 +01002264 lua_pushnil(L);
2265 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002266 }
2267
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002268 ret = MAY_LJMP(hlua_socket_info(L, &conn->addr.to));
2269 xref_unlock(&socket->xref, peer);
2270 return ret;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002271}
2272
2273/* Returns information about my connection side. */
2274static int hlua_socket_getsockname(struct lua_State *L)
2275{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002276 struct hlua_socket *socket;
2277 struct connection *conn;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002278 struct appctx *appctx;
2279 struct xref *peer;
2280 struct stream_interface *si;
2281 struct stream *s;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002282 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002283
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002284 MAY_LJMP(check_args(L, 1, "getsockname"));
2285
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002286 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002287
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002288 /* Check if we run on the same thread than the xreator thread.
2289 * We cannot access to the socket if the thread is different.
2290 */
2291 if (socket->tid != tid)
2292 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2293
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002294 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002295 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002296 if (!peer) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002297 lua_pushnil(L);
2298 return 1;
2299 }
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002300 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2301 si = appctx->owner;
2302 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002303
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002304 conn = cs_conn(objt_cs(s->si[1].end));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002305 if (!conn) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002306 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002307 lua_pushnil(L);
2308 return 1;
2309 }
2310
Willy Tarreaua71f6422016-11-16 17:00:14 +01002311 conn_get_from_addr(conn);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002312 if (!(conn->flags & CO_FL_ADDR_FROM_SET)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002313 xref_unlock(&socket->xref, peer);
Willy Tarreaua71f6422016-11-16 17:00:14 +01002314 lua_pushnil(L);
2315 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002316 }
2317
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002318 ret = hlua_socket_info(L, &conn->addr.from);
2319 xref_unlock(&socket->xref, peer);
2320 return ret;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002321}
2322
2323/* This struct define the applet. */
Willy Tarreau30576452015-04-13 13:50:30 +02002324static struct applet update_applet = {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002325 .obj_type = OBJ_TYPE_APPLET,
2326 .name = "<LUA_TCP>",
2327 .fct = hlua_socket_handler,
2328 .release = hlua_socket_release,
2329};
2330
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002331__LJMP static int hlua_socket_connect_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002332{
2333 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
2334 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002335 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002336 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002337 struct stream_interface *si;
2338 struct stream *s;
2339
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002340 /* Check if we run on the same thread than the xreator thread.
2341 * We cannot access to the socket if the thread is different.
2342 */
2343 if (socket->tid != tid)
2344 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2345
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002346 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002347 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002348 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002349 lua_pushnil(L);
2350 lua_pushstring(L, "Can't connect");
2351 return 2;
2352 }
2353 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2354 si = appctx->owner;
2355 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002356
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002357 /* Check if we run on the same thread than the xreator thread.
2358 * We cannot access to the socket if the thread is different.
2359 */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002360 if (socket->tid != tid) {
2361 xref_unlock(&socket->xref, peer);
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002362 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002363 }
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002364
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002365 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002366 if (!hlua || channel_output_closed(&s->req)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002367 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002368 lua_pushnil(L);
2369 lua_pushstring(L, "Can't connect");
2370 return 2;
2371 }
2372
Willy Tarreaue09101e2018-10-16 17:37:12 +02002373 appctx = __objt_appctx(s->si[0].end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002374
2375 /* Check for connection established. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002376 if (appctx->ctx.hlua_cosocket.connected) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002377 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002378 lua_pushinteger(L, 1);
2379 return 1;
2380 }
2381
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002382 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2383 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002384 WILL_LJMP(luaL_error(L, "out of memory error"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002385 }
2386 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002387 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002388 return 0;
2389}
2390
2391/* This function fail or initite the connection. */
2392__LJMP static int hlua_socket_connect(struct lua_State *L)
2393{
2394 struct hlua_socket *socket;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002395 int port = -1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002396 const char *ip;
2397 struct connection *conn;
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002398 struct hlua *hlua;
2399 struct appctx *appctx;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002400 int low, high;
2401 struct sockaddr_storage *addr;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002402 struct xref *peer;
2403 struct stream_interface *si;
2404 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002405
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002406 if (lua_gettop(L) < 2)
2407 WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments"));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002408
2409 /* Get args. */
2410 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002411
2412 /* Check if we run on the same thread than the xreator thread.
2413 * We cannot access to the socket if the thread is different.
2414 */
2415 if (socket->tid != tid)
2416 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2417
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002418 ip = MAY_LJMP(luaL_checkstring(L, 2));
Tim Duesterhus6edab862018-01-06 19:04:45 +01002419 if (lua_gettop(L) >= 3) {
2420 luaL_Buffer b;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002421 port = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002422
Tim Duesterhus6edab862018-01-06 19:04:45 +01002423 /* Force the ip to end with a colon, to support IPv6 addresses
2424 * that are not enclosed within square brackets.
2425 */
2426 if (port > 0) {
2427 luaL_buffinit(L, &b);
2428 luaL_addstring(&b, ip);
2429 luaL_addchar(&b, ':');
2430 luaL_pushresult(&b);
2431 ip = lua_tolstring(L, lua_gettop(L), NULL);
2432 }
2433 }
2434
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002435 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002436 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002437 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002438 lua_pushnil(L);
2439 return 1;
2440 }
2441 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2442 si = appctx->owner;
2443 s = si_strm(si);
2444
2445 /* Initialise connection. */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002446 conn = cs_conn(si_alloc_cs(&s->si[1], NULL));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002447 if (!conn) {
2448 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002449 WILL_LJMP(luaL_error(L, "connect: internal error"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002450 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002451
Willy Tarreau3adac082015-09-26 17:51:09 +02002452 /* needed for the connection not to be closed */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002453 conn->target = s->target;
Willy Tarreau3adac082015-09-26 17:51:09 +02002454
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002455 /* Parse ip address. */
Willy Tarreau48ef4c92017-01-06 18:32:38 +01002456 addr = str2sa_range(ip, NULL, &low, &high, NULL, NULL, NULL, 0);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002457 if (!addr) {
2458 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002459 WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002460 }
2461 if (low != high) {
2462 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002463 WILL_LJMP(luaL_error(L, "connect: port ranges not supported : address '%s'", ip));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002464 }
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002465 memcpy(&conn->addr.to, addr, sizeof(struct sockaddr_storage));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002466
2467 /* Set port. */
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002468 if (low == 0) {
2469 if (conn->addr.to.ss_family == AF_INET) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002470 if (port == -1) {
2471 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002472 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002473 }
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002474 ((struct sockaddr_in *)&conn->addr.to)->sin_port = htons(port);
2475 } else if (conn->addr.to.ss_family == AF_INET6) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002476 if (port == -1) {
2477 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002478 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002479 }
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002480 ((struct sockaddr_in6 *)&conn->addr.to)->sin6_port = htons(port);
2481 }
2482 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002483
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002484 hlua = hlua_gethlua(L);
Willy Tarreaue09101e2018-10-16 17:37:12 +02002485 appctx = __objt_appctx(s->si[0].end);
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002486
2487 /* inform the stream that we want to be notified whenever the
2488 * connection completes.
2489 */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01002490 si_cant_get(&s->si[0]);
Willy Tarreau3367d412018-11-15 10:57:41 +01002491 si_rx_endp_more(&s->si[0]);
Thierry FOURNIER8c8fbbe2015-09-26 17:02:35 +02002492 appctx_wakeup(appctx);
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002493
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +02002494 hlua->flags |= HLUA_MUST_GC;
2495
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002496 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2497 xref_unlock(&socket->xref, peer);
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002498 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002499 }
2500 xref_unlock(&socket->xref, peer);
PiBa-NL706d5ee2018-05-05 23:51:42 +02002501
2502 task_wakeup(s->task, TASK_WOKEN_INIT);
2503 /* Return yield waiting for connection. */
2504
Willy Tarreau9635e032018-10-16 17:52:55 +02002505 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002506
2507 return 0;
2508}
2509
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002510#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002511__LJMP static int hlua_socket_connect_ssl(struct lua_State *L)
2512{
2513 struct hlua_socket *socket;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002514 struct xref *peer;
2515 struct appctx *appctx;
2516 struct stream_interface *si;
2517 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002518
2519 MAY_LJMP(check_args(L, 3, "connect_ssl"));
2520 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002521
2522 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002523 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002524 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002525 lua_pushnil(L);
2526 return 1;
2527 }
2528 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2529 si = appctx->owner;
2530 s = si_strm(si);
2531
2532 s->target = &socket_ssl.obj_type;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002533 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002534 return MAY_LJMP(hlua_socket_connect(L));
2535}
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002536#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002537
2538__LJMP static int hlua_socket_setoption(struct lua_State *L)
2539{
2540 return 0;
2541}
2542
2543__LJMP static int hlua_socket_settimeout(struct lua_State *L)
2544{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002545 struct hlua_socket *socket;
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002546 int tmout;
Mark Lakes56cc1252018-03-27 09:48:06 +02002547 double dtmout;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002548 struct xref *peer;
2549 struct appctx *appctx;
2550 struct stream_interface *si;
2551 struct stream *s;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002552
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002553 MAY_LJMP(check_args(L, 2, "settimeout"));
2554
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002555 socket = MAY_LJMP(hlua_checksocket(L, 1));
Mark Lakes56cc1252018-03-27 09:48:06 +02002556
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002557 /* convert the timeout to millis */
2558 dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002559
Thierry Fournier17a921b2018-03-08 09:59:02 +01002560 /* Check for negative values */
Mark Lakes56cc1252018-03-27 09:48:06 +02002561 if (dtmout < 0)
Thierry Fournier17a921b2018-03-08 09:59:02 +01002562 WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values"));
2563
Mark Lakes56cc1252018-03-27 09:48:06 +02002564 if (dtmout > INT_MAX) /* overflow check */
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002565 WILL_LJMP(luaL_error(L, "settimeout: cannot set values larger than %d ms", INT_MAX));
Mark Lakes56cc1252018-03-27 09:48:06 +02002566
2567 tmout = MS_TO_TICKS((int)dtmout);
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002568 if (tmout == 0)
2569 tmout++; /* very small timeouts are adjusted to a minium of 1ms */
Mark Lakes56cc1252018-03-27 09:48:06 +02002570
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002571 /* Check if we run on the same thread than the xreator thread.
2572 * We cannot access to the socket if the thread is different.
2573 */
2574 if (socket->tid != tid)
2575 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2576
Mark Lakes56cc1252018-03-27 09:48:06 +02002577 /* check for connection break. If some data were read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002578 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002579 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002580 hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts.");
2581 WILL_LJMP(lua_error(L));
2582 return 0;
2583 }
2584 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2585 si = appctx->owner;
2586 s = si_strm(si);
2587
Cyril Bonté7bb63452018-08-17 23:51:02 +02002588 s->sess->fe->timeout.connect = tmout;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002589 s->req.rto = tmout;
2590 s->req.wto = tmout;
2591 s->res.rto = tmout;
2592 s->res.wto = tmout;
Cyril Bonté7bb63452018-08-17 23:51:02 +02002593 s->req.rex = tick_add_ifset(now_ms, tmout);
2594 s->req.wex = tick_add_ifset(now_ms, tmout);
2595 s->res.rex = tick_add_ifset(now_ms, tmout);
2596 s->res.wex = tick_add_ifset(now_ms, tmout);
2597
2598 s->task->expire = tick_add_ifset(now_ms, tmout);
2599 task_queue(s->task);
2600
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002601 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002602
Thierry Fourniere9636f12018-03-08 09:54:32 +01002603 lua_pushinteger(L, 1);
Tim Duesterhus119a5f12018-01-06 19:16:25 +01002604 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002605}
2606
2607__LJMP static int hlua_socket_new(lua_State *L)
2608{
2609 struct hlua_socket *socket;
2610 struct appctx *appctx;
Willy Tarreau15b5e142015-04-04 14:38:25 +02002611 struct session *sess;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002612 struct stream *strm;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002613
2614 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002615 if (!lua_checkstack(L, 3)) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002616 hlua_pusherror(L, "socket: full stack");
2617 goto out_fail_conf;
2618 }
2619
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002620 /* Create the object: obj[0] = userdata. */
2621 lua_newtable(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002622 socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket)));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002623 lua_rawseti(L, -2, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002624 memset(socket, 0, sizeof(*socket));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002625 socket->tid = tid;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002626
Thierry FOURNIER4a6170c2015-03-09 17:07:10 +01002627 /* Check if the various memory pools are intialized. */
Willy Tarreaubafbe012017-11-24 17:34:44 +01002628 if (!pool_head_stream || !pool_head_buffer) {
Thierry FOURNIER4a6170c2015-03-09 17:07:10 +01002629 hlua_pusherror(L, "socket: uninitialized pools.");
2630 goto out_fail_conf;
2631 }
2632
Willy Tarreau87b09662015-04-03 00:22:06 +02002633 /* Pop a class stream metatable and affect it to the userdata. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002634 lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref);
2635 lua_setmetatable(L, -2);
2636
Willy Tarreaud420a972015-04-06 00:39:18 +02002637 /* Create the applet context */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01002638 appctx = appctx_new(&update_applet, tid_bit);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002639 if (!appctx) {
2640 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaufeb76402015-04-03 14:10:06 +02002641 goto out_fail_conf;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002642 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002643
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002644 appctx->ctx.hlua_cosocket.connected = 0;
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02002645 appctx->ctx.hlua_cosocket.die = 0;
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002646 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write);
2647 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read);
Willy Tarreaub2bf8332015-04-04 15:58:58 +02002648
Willy Tarreaud420a972015-04-06 00:39:18 +02002649 /* Now create a session, task and stream for this applet */
2650 sess = session_new(&socket_proxy, NULL, &appctx->obj_type);
2651 if (!sess) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002652 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002653 goto out_fail_sess;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002654 }
2655
Willy Tarreau87787ac2017-08-28 16:22:54 +02002656 strm = stream_new(sess, &appctx->obj_type);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002657 if (!strm) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002658 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002659 goto out_fail_stream;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002660 }
2661
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002662 /* Initialise cross reference between stream and Lua socket object. */
2663 xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002664
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002665 /* Configure "right" stream interface. this "si" is used to connect
2666 * and retrieve data from the server. The connection is initialized
2667 * with the "struct server".
2668 */
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002669 si_set_state(&strm->si[1], SI_ST_ASS);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002670
2671 /* Force destination server. */
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002672 strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET | SF_BE_ASSIGNED;
2673 strm->target = &socket_tcp.obj_type;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002674
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002675 return 1;
2676
Willy Tarreaud420a972015-04-06 00:39:18 +02002677 out_fail_stream:
Willy Tarreau11c36242015-04-04 15:54:03 +02002678 session_free(sess);
Willy Tarreaud420a972015-04-06 00:39:18 +02002679 out_fail_sess:
2680 appctx_free(appctx);
2681 out_fail_conf:
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002682 WILL_LJMP(lua_error(L));
2683 return 0;
2684}
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01002685
2686/*
2687 *
2688 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002689 * Class Channel
2690 *
2691 *
2692 */
2693
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002694/* This function is called before the Lua execution. It stores
2695 * the differents parsers state before executing some Lua code.
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002696 */
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002697static inline void consistency_set(struct stream *stream, int opt, struct hlua_consistency *c)
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002698{
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002699 c->mode = stream->be->mode;
2700 switch (c->mode) {
2701 case PR_MODE_HTTP:
2702 c->data.http.dir = opt & SMP_OPT_DIR;
2703 if (c->data.http.dir == SMP_OPT_DIR_REQ)
2704 c->data.http.state = stream->txn->req.msg_state;
2705 else
2706 c->data.http.state = stream->txn->rsp.msg_state;
2707 break;
2708 default:
2709 break;
2710 }
2711}
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002712
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002713/* This function is called after the Lua execution. it
2714 * returns true if the parser state is consistent, otherwise,
2715 * it return false.
2716 *
2717 * In HTTP mode, the parser state must be in the same state
2718 * or greater when we exit the function. Even if we do a
2719 * control yield. This prevent to break the HTTP message
2720 * from the Lua code.
2721 */
2722static inline int consistency_check(struct stream *stream, int opt, struct hlua_consistency *c)
2723{
2724 if (c->mode != stream->be->mode)
2725 return 0;
2726
2727 switch (c->mode) {
2728 case PR_MODE_HTTP:
2729 if (c->data.http.dir != (opt & SMP_OPT_DIR))
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002730 return 0;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002731 if (c->data.http.dir == SMP_OPT_DIR_REQ)
2732 return stream->txn->req.msg_state >= c->data.http.state;
2733 else
2734 return stream->txn->rsp.msg_state >= c->data.http.state;
2735 default:
2736 return 1;
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002737 }
2738 return 1;
2739}
2740
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002741/* Returns the struct hlua_channel join to the class channel in the
2742 * stack entry "ud" or throws an argument error.
2743 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002744__LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002745{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02002746 return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002747}
2748
Willy Tarreau47860ed2015-03-10 14:07:50 +01002749/* Pushes the channel onto the top of the stack. If the stask does not have a
2750 * free slots, the function fails and returns 0;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002751 */
Willy Tarreau2a71af42015-03-10 13:51:50 +01002752static int hlua_channel_new(lua_State *L, struct channel *channel)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002753{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002754 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002755 if (!lua_checkstack(L, 3))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002756 return 0;
2757
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002758 lua_newtable(L);
Willy Tarreau47860ed2015-03-10 14:07:50 +01002759 lua_pushlightuserdata(L, channel);
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002760 lua_rawseti(L, -2, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002761
2762 /* Pop a class sesison metatable and affect it to the userdata. */
2763 lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref);
2764 lua_setmetatable(L, -2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002765 return 1;
2766}
2767
2768/* Duplicate all the data present in the input channel and put it
2769 * in a string LUA variables. Returns -1 and push a nil value in
2770 * the stack if the channel is closed and all the data are consumed,
2771 * returns 0 if no data are available, otherwise it returns the length
2772 * of the builded string.
2773 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002774static inline int _hlua_channel_dup(struct channel *chn, lua_State *L)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002775{
2776 char *blk1;
2777 char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02002778 size_t len1;
2779 size_t len2;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002780 int ret;
2781 luaL_Buffer b;
2782
Willy Tarreau06d80a92017-10-19 14:32:15 +02002783 ret = ci_getblk_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002784 if (unlikely(ret == 0))
2785 return 0;
2786
2787 if (unlikely(ret < 0)) {
2788 lua_pushnil(L);
2789 return -1;
2790 }
2791
2792 luaL_buffinit(L, &b);
2793 luaL_addlstring(&b, blk1, len1);
2794 if (unlikely(ret == 2))
2795 luaL_addlstring(&b, blk2, len2);
2796 luaL_pushresult(&b);
2797
2798 if (unlikely(ret == 2))
2799 return len1 + len2;
2800 return len1;
2801}
2802
2803/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2804 * a yield. This function keep the data in the buffer.
2805 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002806__LJMP static int hlua_channel_dup_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002807{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002808 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002809
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002810 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2811
Christopher Faulet3f829a42018-12-13 21:56:45 +01002812 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2813 WILL_LJMP(lua_error(L));
2814
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002815 if (_hlua_channel_dup(chn, L) == 0)
Willy Tarreau9635e032018-10-16 17:52:55 +02002816 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_dup_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002817 return 1;
2818}
2819
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002820/* Check arguments for the function "hlua_channel_dup_yield". */
2821__LJMP static int hlua_channel_dup(lua_State *L)
2822{
2823 MAY_LJMP(check_args(L, 1, "dup"));
2824 MAY_LJMP(hlua_checkchannel(L, 1));
2825 return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0));
2826}
2827
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002828/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2829 * a yield. This function consumes the data in the buffer. It returns
2830 * a string containing the data or a nil pointer if no data are available
2831 * and the channel is closed.
2832 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002833__LJMP static int hlua_channel_get_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002834{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002835 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002836 int ret;
2837
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002838 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002839
Christopher Faulet3f829a42018-12-13 21:56:45 +01002840 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2841 WILL_LJMP(lua_error(L));
2842
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002843 ret = _hlua_channel_dup(chn, L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002844 if (unlikely(ret == 0))
Willy Tarreau9635e032018-10-16 17:52:55 +02002845 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002846
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002847 if (unlikely(ret == -1))
2848 return 1;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002849
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002850 b_sub(&chn->buf, ret);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002851 return 1;
2852}
2853
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002854/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002855__LJMP static int hlua_channel_get(lua_State *L)
2856{
2857 MAY_LJMP(check_args(L, 1, "get"));
2858 MAY_LJMP(hlua_checkchannel(L, 1));
2859 return MAY_LJMP(hlua_channel_get_yield(L, 0, 0));
2860}
2861
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002862/* This functions consumes and returns one line. If the channel is closed,
2863 * and the last data does not contains a final '\n', the data are returned
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002864 * without the final '\n'. When no more data are available, it returns nil
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002865 * value.
2866 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002867__LJMP static int hlua_channel_getline_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002868{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002869 char *blk1;
2870 char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02002871 size_t len1;
2872 size_t len2;
2873 size_t len;
Willy Tarreau47860ed2015-03-10 14:07:50 +01002874 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002875 int ret;
2876 luaL_Buffer b;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002877
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002878 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2879
Christopher Faulet3f829a42018-12-13 21:56:45 +01002880 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2881 WILL_LJMP(lua_error(L));
2882
Willy Tarreau06d80a92017-10-19 14:32:15 +02002883 ret = ci_getline_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002884 if (ret == 0)
Willy Tarreau9635e032018-10-16 17:52:55 +02002885 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_getline_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002886
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002887 if (ret == -1) {
2888 lua_pushnil(L);
2889 return 1;
2890 }
2891
2892 luaL_buffinit(L, &b);
2893 luaL_addlstring(&b, blk1, len1);
2894 len = len1;
2895 if (unlikely(ret == 2)) {
2896 luaL_addlstring(&b, blk2, len2);
2897 len += len2;
2898 }
2899 luaL_pushresult(&b);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002900 b_rep_blk(&chn->buf, ci_head(chn), ci_head(chn) + len, NULL, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002901 return 1;
2902}
2903
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002904/* Check arguments for the function "hlua_channel_getline_yield". */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002905__LJMP static int hlua_channel_getline(lua_State *L)
2906{
2907 MAY_LJMP(check_args(L, 1, "getline"));
2908 MAY_LJMP(hlua_checkchannel(L, 1));
2909 return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0));
2910}
2911
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002912/* This function takes a string as input, and append it at the
2913 * input side of channel. If the data is too big, but a space
2914 * is probably available after sending some data, the function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002915 * yields. If the data is bigger than the buffer, or if the
2916 * channel is closed, it returns -1. Otherwise, it returns the
2917 * amount of data written.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002918 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002919__LJMP static int hlua_channel_append_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002920{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002921 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002922 size_t len;
2923 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
2924 int l = MAY_LJMP(luaL_checkinteger(L, 3));
2925 int ret;
2926 int max;
2927
Christopher Faulet3f829a42018-12-13 21:56:45 +01002928 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2929 WILL_LJMP(lua_error(L));
2930
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002931 /* Check if the buffer is available because HAProxy doesn't allocate
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002932 * the request buffer if its not required.
2933 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002934 if (chn->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +01002935 si_rx_buff_blk(chn_prod(chn));
Willy Tarreau9635e032018-10-16 17:52:55 +02002936 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002937 }
2938
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002939 max = channel_recv_limit(chn) - b_data(&chn->buf);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002940 if (max > len - l)
2941 max = len - l;
2942
Willy Tarreau06d80a92017-10-19 14:32:15 +02002943 ret = ci_putblk(chn, str + l, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002944 if (ret == -2 || ret == -3) {
2945 lua_pushinteger(L, -1);
2946 return 1;
2947 }
Willy Tarreaubc18da12015-03-13 14:00:47 +01002948 if (ret == -1) {
2949 chn->flags |= CF_WAKE_WRITE;
Willy Tarreau9635e032018-10-16 17:52:55 +02002950 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Willy Tarreaubc18da12015-03-13 14:00:47 +01002951 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002952 l += ret;
2953 lua_pop(L, 1);
2954 lua_pushinteger(L, l);
2955
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002956 max = channel_recv_limit(chn) - b_data(&chn->buf);
Willy Tarreaua79021a2018-06-15 18:07:57 +02002957 if (max == 0 && co_data(chn) == 0) {
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002958 /* There are no space available, and the output buffer is empty.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002959 * in this case, we cannot add more data, so we cannot yield,
2960 * we return the amount of copyied data.
2961 */
2962 return 1;
2963 }
2964 if (l < len)
Willy Tarreau9635e032018-10-16 17:52:55 +02002965 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002966 return 1;
2967}
2968
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002969/* Just a wrapper of "hlua_channel_append_yield". It returns the length
2970 * of the written string, or -1 if the channel is closed or if the
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002971 * buffer size is too little for the data.
2972 */
2973__LJMP static int hlua_channel_append(lua_State *L)
2974{
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002975 size_t len;
2976
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002977 MAY_LJMP(check_args(L, 2, "append"));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002978 MAY_LJMP(hlua_checkchannel(L, 1));
2979 MAY_LJMP(luaL_checklstring(L, 2, &len));
2980 MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002981 lua_pushinteger(L, 0);
2982
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002983 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002984}
2985
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002986/* Just a wrapper of "hlua_channel_append_yield". This wrapper starts
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002987 * his process by cleaning the buffer. The result is a replacement
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002988 * of the current data. It returns the length of the written string,
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002989 * or -1 if the channel is closed or if the buffer size is too
2990 * little for the data.
2991 */
2992__LJMP static int hlua_channel_set(lua_State *L)
2993{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002994 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002995
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002996 MAY_LJMP(check_args(L, 2, "set"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002997 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002998 lua_pushinteger(L, 0);
2999
Christopher Faulet3f829a42018-12-13 21:56:45 +01003000 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
3001 WILL_LJMP(lua_error(L));
3002
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003003 b_set_data(&chn->buf, co_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003004
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003005 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003006}
3007
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003008/* Append data in the output side of the buffer. This data is immediately
3009 * sent. The function returns the amount of data written. If the buffer
3010 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003011 * if the channel is closed.
3012 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003013__LJMP static int hlua_channel_send_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003014{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003015 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003016 size_t len;
3017 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
3018 int l = MAY_LJMP(luaL_checkinteger(L, 3));
3019 int max;
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003020 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003021
Christopher Faulet3f829a42018-12-13 21:56:45 +01003022 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
3023 WILL_LJMP(lua_error(L));
3024
Willy Tarreau47860ed2015-03-10 14:07:50 +01003025 if (unlikely(channel_output_closed(chn))) {
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003026 lua_pushinteger(L, -1);
3027 return 1;
3028 }
3029
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003030 /* Check if the buffer is available because HAProxy doesn't allocate
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01003031 * the request buffer if its not required.
3032 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003033 if (chn->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +01003034 si_rx_buff_blk(chn_prod(chn));
Willy Tarreau9635e032018-10-16 17:52:55 +02003035 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01003036 }
3037
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003038 /* The written data will be immediately sent, so we can check
3039 * the available space without taking in account the reserve.
3040 * The reserve is guaranteed for the processing of incoming
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003041 * data, because the buffer will be flushed.
3042 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003043 max = b_room(&chn->buf);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003044
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003045 /* If there is no space available, and the output buffer is empty.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003046 * in this case, we cannot add more data, so we cannot yield,
3047 * we return the amount of copyied data.
3048 */
Willy Tarreaua79021a2018-06-15 18:07:57 +02003049 if (max == 0 && co_data(chn) == 0)
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003050 return 1;
3051
3052 /* Adjust the real required length. */
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003053 if (max > len - l)
3054 max = len - l;
3055
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003056 /* The buffer available size may be not contiguous. This test
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003057 * detects a non contiguous buffer and realign it.
3058 */
Willy Tarreau3f679992018-06-15 15:06:42 +02003059 if (ci_space_for_replace(chn) < max)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003060 channel_slow_realign(chn, trash.area);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003061
3062 /* Copy input data in the buffer. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003063 max = b_rep_blk(&chn->buf, ci_head(chn), ci_head(chn), str + l, max);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003064
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003065 /* buffer replace considers that the input part is filled.
3066 * so, I must forward these new data in the output part.
3067 */
Willy Tarreaubcbd3932018-06-06 07:13:22 +02003068 c_adv(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003069
3070 l += max;
3071 lua_pop(L, 1);
3072 lua_pushinteger(L, l);
3073
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003074 /* If there is no space available, and the output buffer is empty.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003075 * in this case, we cannot add more data, so we cannot yield,
3076 * we return the amount of copyied data.
3077 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003078 max = b_room(&chn->buf);
Willy Tarreaua79021a2018-06-15 18:07:57 +02003079 if (max == 0 && co_data(chn) == 0)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003080 return 1;
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003081
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003082 if (l < len) {
3083 /* If we are waiting for space in the response buffer, we
3084 * must set the flag WAKERESWR. This flag required the task
3085 * wake up if any activity is detected on the response buffer.
3086 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003087 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003088 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01003089 else
3090 HLUA_SET_WAKEREQWR(hlua);
Willy Tarreau9635e032018-10-16 17:52:55 +02003091 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003092 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003093
3094 return 1;
3095}
3096
3097/* Just a wraper of "_hlua_channel_send". This wrapper permits
3098 * yield the LUA process, and resume it without checking the
3099 * input arguments.
3100 */
3101__LJMP static int hlua_channel_send(lua_State *L)
3102{
3103 MAY_LJMP(check_args(L, 2, "send"));
3104 lua_pushinteger(L, 0);
3105
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003106 return MAY_LJMP(hlua_channel_send_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003107}
3108
3109/* This function forward and amount of butes. The data pass from
3110 * the input side of the buffer to the output side, and can be
3111 * forwarded. This function never fails.
3112 *
3113 * The Lua function takes an amount of bytes to be forwarded in
3114 * imput. It returns the number of bytes forwarded.
3115 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003116__LJMP static int hlua_channel_forward_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003117{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003118 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003119 int len;
3120 int l;
3121 int max;
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003122 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003123
3124 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Faulet3f829a42018-12-13 21:56:45 +01003125
3126 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
3127 WILL_LJMP(lua_error(L));
3128
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003129 len = MAY_LJMP(luaL_checkinteger(L, 2));
3130 l = MAY_LJMP(luaL_checkinteger(L, -1));
3131
3132 max = len - l;
Willy Tarreaua79021a2018-06-15 18:07:57 +02003133 if (max > ci_data(chn))
3134 max = ci_data(chn);
Willy Tarreau47860ed2015-03-10 14:07:50 +01003135 channel_forward(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003136 l += max;
3137
3138 lua_pop(L, 1);
3139 lua_pushinteger(L, l);
3140
3141 /* Check if it miss bytes to forward. */
3142 if (l < len) {
3143 /* The the input channel or the output channel are closed, we
3144 * must return the amount of data forwarded.
3145 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003146 if (channel_input_closed(chn) || channel_output_closed(chn))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003147 return 1;
3148
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003149 /* If we are waiting for space data in the response buffer, we
3150 * must set the flag WAKERESWR. This flag required the task
3151 * wake up if any activity is detected on the response buffer.
3152 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003153 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003154 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01003155 else
3156 HLUA_SET_WAKEREQWR(hlua);
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003157
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003158 /* Otherwise, we can yield waiting for new data in the inpout side. */
Willy Tarreau9635e032018-10-16 17:52:55 +02003159 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_forward_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003160 }
3161
3162 return 1;
3163}
3164
3165/* Just check the input and prepare the stack for the previous
3166 * function "hlua_channel_forward_yield"
3167 */
3168__LJMP static int hlua_channel_forward(lua_State *L)
3169{
3170 MAY_LJMP(check_args(L, 2, "forward"));
3171 MAY_LJMP(hlua_checkchannel(L, 1));
3172 MAY_LJMP(luaL_checkinteger(L, 2));
3173
3174 lua_pushinteger(L, 0);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003175 return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003176}
3177
3178/* Just returns the number of bytes available in the input
3179 * side of the buffer. This function never fails.
3180 */
3181__LJMP static int hlua_channel_get_in_len(lua_State *L)
3182{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003183 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003184
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003185 MAY_LJMP(check_args(L, 1, "get_in_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003186 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Fauleta3ceac12018-12-14 13:39:09 +01003187 if (IS_HTX_STRM(chn_strm(chn))) {
3188 struct htx *htx = htxbuf(&chn->buf);
3189 lua_pushinteger(L, htx->data - co_data(chn));
3190 }
3191 else
3192 lua_pushinteger(L, ci_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003193 return 1;
3194}
3195
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003196/* Returns true if the channel is full. */
3197__LJMP static int hlua_channel_is_full(lua_State *L)
3198{
3199 struct channel *chn;
3200 int rem;
3201
3202 MAY_LJMP(check_args(L, 1, "is_full"));
3203 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3204
Christopher Fauleta3ceac12018-12-14 13:39:09 +01003205 if (IS_HTX_STRM(chn_strm(chn))) {
3206 struct htx *htx = htxbuf(&chn->buf);
3207
3208 rem = htx_free_data_space(htx);
3209 }
3210 else
3211 rem = b_room(&chn->buf);
3212
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003213 rem -= global.tune.maxrewrite; /* Rewrite reserved size */
3214
3215 lua_pushboolean(L, rem <= 0);
3216 return 1;
3217}
3218
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003219/* Just returns the number of bytes available in the output
3220 * side of the buffer. This function never fails.
3221 */
3222__LJMP static int hlua_channel_get_out_len(lua_State *L)
3223{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003224 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003225
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003226 MAY_LJMP(check_args(L, 1, "get_out_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003227 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Willy Tarreaua79021a2018-06-15 18:07:57 +02003228 lua_pushinteger(L, co_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003229 return 1;
3230}
3231
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003232/*
3233 *
3234 *
3235 * Class Fetches
3236 *
3237 *
3238 */
3239
3240/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003241 * a class stream, otherwise it throws an error.
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003242 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003243__LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003244{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003245 return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003246}
3247
3248/* This function creates and push in the stack a fetch object according
3249 * with a current TXN.
3250 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003251static int hlua_fetches_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003252{
Willy Tarreau7073c472015-04-06 11:15:40 +02003253 struct hlua_smp *hsmp;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003254
3255 /* Check stack size. */
3256 if (!lua_checkstack(L, 3))
3257 return 0;
3258
3259 /* Create the object: obj[0] = userdata.
3260 * Note that the base of the Fetches object is the
3261 * transaction object.
3262 */
3263 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003264 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003265 lua_rawseti(L, -2, 0);
3266
Willy Tarreau7073c472015-04-06 11:15:40 +02003267 hsmp->s = txn->s;
3268 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003269 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003270 hsmp->flags = flags;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003271
3272 /* Pop a class sesison metatable and affect it to the userdata. */
3273 lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref);
3274 lua_setmetatable(L, -2);
3275
3276 return 1;
3277}
3278
3279/* This function is an LUA binding. It is called with each sample-fetch.
3280 * It uses closure argument to store the associated sample-fetch. It
3281 * returns only one argument or throws an error. An error is thrown
3282 * only if an error is encountered during the argument parsing. If
3283 * the "sample-fetch" function fails, nil is returned.
3284 */
3285__LJMP static int hlua_run_sample_fetch(lua_State *L)
3286{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003287 struct hlua_smp *hsmp;
Willy Tarreau2ec22742015-03-10 14:27:20 +01003288 struct sample_fetch *f;
Frédéric Lécaillef874a832018-06-15 13:56:04 +02003289 struct arg args[ARGM_NBARGS + 1] = {{0}};
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003290 int i;
3291 struct sample smp;
3292
3293 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003294 f = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003295
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003296 /* Get traditional arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003297 hsmp = MAY_LJMP(hlua_checkfetches(L, 1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003298
Thierry FOURNIERca988662015-12-20 18:43:03 +01003299 /* Check execution authorization. */
3300 if (f->use & SMP_USE_HTTP_ANY &&
3301 !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) {
3302 lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which "
3303 "is not available in Lua services", f->kw);
3304 WILL_LJMP(lua_error(L));
3305 }
3306
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003307 /* Get extra arguments. */
3308 for (i = 0; i < lua_gettop(L) - 1; i++) {
3309 if (i >= ARGM_NBARGS)
3310 break;
3311 hlua_lua2arg(L, i + 2, &args[i]);
3312 }
3313 args[i].type = ARGT_STOP;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003314 args[i].data.str.area = NULL;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003315
3316 /* Check arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003317 MAY_LJMP(hlua_lua2arg_check(L, 2, args, f->arg_mask, hsmp->p));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003318
3319 /* Run the special args checker. */
Willy Tarreau2ec22742015-03-10 14:27:20 +01003320 if (f->val_args && !f->val_args(args, NULL)) {
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003321 lua_pushfstring(L, "error in arguments");
3322 WILL_LJMP(lua_error(L));
3323 }
3324
3325 /* Initialise the sample. */
3326 memset(&smp, 0, sizeof(smp));
3327
3328 /* Run the sample fetch process. */
Willy Tarreau1777ea62016-03-10 16:15:46 +01003329 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003330 if (!f->process(args, &smp, f->kw, f->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003331 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003332 lua_pushstring(L, "");
3333 else
3334 lua_pushnil(L);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003335 return 1;
3336 }
3337
3338 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003339 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003340 hlua_smp2lua_str(L, &smp);
3341 else
3342 hlua_smp2lua(L, &smp);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003343 return 1;
3344}
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003345
3346/*
3347 *
3348 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003349 * Class Converters
3350 *
3351 *
3352 */
3353
3354/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003355 * a class stream, otherwise it throws an error.
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003356 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003357__LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003358{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003359 return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003360}
3361
3362/* This function creates and push in the stack a Converters object
3363 * according with a current TXN.
3364 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003365static int hlua_converters_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003366{
Willy Tarreau7073c472015-04-06 11:15:40 +02003367 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003368
3369 /* Check stack size. */
3370 if (!lua_checkstack(L, 3))
3371 return 0;
3372
3373 /* Create the object: obj[0] = userdata.
3374 * Note that the base of the Converters object is the
3375 * same than the TXN object.
3376 */
3377 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003378 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003379 lua_rawseti(L, -2, 0);
3380
Willy Tarreau7073c472015-04-06 11:15:40 +02003381 hsmp->s = txn->s;
3382 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003383 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003384 hsmp->flags = flags;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003385
Willy Tarreau87b09662015-04-03 00:22:06 +02003386 /* Pop a class stream metatable and affect it to the table. */
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003387 lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref);
3388 lua_setmetatable(L, -2);
3389
3390 return 1;
3391}
3392
3393/* This function is an LUA binding. It is called with each converter.
3394 * It uses closure argument to store the associated converter. It
3395 * returns only one argument or throws an error. An error is thrown
3396 * only if an error is encountered during the argument parsing. If
3397 * the converter function function fails, nil is returned.
3398 */
3399__LJMP static int hlua_run_sample_conv(lua_State *L)
3400{
Willy Tarreauda5f1082015-04-06 11:17:13 +02003401 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003402 struct sample_conv *conv;
Frédéric Lécaillef874a832018-06-15 13:56:04 +02003403 struct arg args[ARGM_NBARGS + 1] = {{0}};
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003404 int i;
3405 struct sample smp;
3406
3407 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003408 conv = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003409
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003410 /* Get traditional arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003411 hsmp = MAY_LJMP(hlua_checkconverters(L, 1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003412
3413 /* Get extra arguments. */
3414 for (i = 0; i < lua_gettop(L) - 2; i++) {
3415 if (i >= ARGM_NBARGS)
3416 break;
3417 hlua_lua2arg(L, i + 3, &args[i]);
3418 }
3419 args[i].type = ARGT_STOP;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003420 args[i].data.str.area = NULL;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003421
3422 /* Check arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003423 MAY_LJMP(hlua_lua2arg_check(L, 3, args, conv->arg_mask, hsmp->p));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003424
3425 /* Run the special args checker. */
3426 if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) {
3427 hlua_pusherror(L, "error in arguments");
3428 WILL_LJMP(lua_error(L));
3429 }
3430
3431 /* Initialise the sample. */
3432 if (!hlua_lua2smp(L, 2, &smp)) {
3433 hlua_pusherror(L, "error in the input argument");
3434 WILL_LJMP(lua_error(L));
3435 }
3436
Willy Tarreau1777ea62016-03-10 16:15:46 +01003437 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
3438
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003439 /* Apply expected cast. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003440 if (!sample_casts[smp.data.type][conv->in_type]) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003441 hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'",
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003442 smp_to_type[smp.data.type], smp_to_type[conv->in_type]);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003443 WILL_LJMP(lua_error(L));
3444 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003445 if (sample_casts[smp.data.type][conv->in_type] != c_none &&
3446 !sample_casts[smp.data.type][conv->in_type](&smp)) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003447 hlua_pusherror(L, "error during the input argument casting");
3448 WILL_LJMP(lua_error(L));
3449 }
3450
3451 /* Run the sample conversion process. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003452 if (!conv->process(args, &smp, conv->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003453 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003454 lua_pushstring(L, "");
3455 else
Willy Tarreaua678b432015-08-28 10:14:59 +02003456 lua_pushnil(L);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003457 return 1;
3458 }
3459
3460 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003461 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003462 hlua_smp2lua_str(L, &smp);
3463 else
3464 hlua_smp2lua(L, &smp);
Willy Tarreaua678b432015-08-28 10:14:59 +02003465 return 1;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003466}
3467
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003468/*
3469 *
3470 *
3471 * Class AppletTCP
3472 *
3473 *
3474 */
3475
3476/* Returns a struct hlua_txn if the stack entry "ud" is
3477 * a class stream, otherwise it throws an error.
3478 */
3479__LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud)
3480{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003481 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003482}
3483
3484/* This function creates and push in the stack an Applet object
3485 * according with a current TXN.
3486 */
3487static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx)
3488{
3489 struct hlua_appctx *appctx;
3490 struct stream_interface *si = ctx->owner;
3491 struct stream *s = si_strm(si);
3492 struct proxy *p = s->be;
3493
3494 /* Check stack size. */
3495 if (!lua_checkstack(L, 3))
3496 return 0;
3497
3498 /* Create the object: obj[0] = userdata.
3499 * Note that the base of the Converters object is the
3500 * same than the TXN object.
3501 */
3502 lua_newtable(L);
3503 appctx = lua_newuserdata(L, sizeof(*appctx));
3504 lua_rawseti(L, -2, 0);
3505 appctx->appctx = ctx;
3506 appctx->htxn.s = s;
3507 appctx->htxn.p = p;
3508
3509 /* Create the "f" field that contains a list of fetches. */
3510 lua_pushstring(L, "f");
3511 if (!hlua_fetches_new(L, &appctx->htxn, 0))
3512 return 0;
3513 lua_settable(L, -3);
3514
3515 /* Create the "sf" field that contains a list of stringsafe fetches. */
3516 lua_pushstring(L, "sf");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003517 if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003518 return 0;
3519 lua_settable(L, -3);
3520
3521 /* Create the "c" field that contains a list of converters. */
3522 lua_pushstring(L, "c");
3523 if (!hlua_converters_new(L, &appctx->htxn, 0))
3524 return 0;
3525 lua_settable(L, -3);
3526
3527 /* Create the "sc" field that contains a list of stringsafe converters. */
3528 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003529 if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003530 return 0;
3531 lua_settable(L, -3);
3532
3533 /* Pop a class stream metatable and affect it to the table. */
3534 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref);
3535 lua_setmetatable(L, -2);
3536
3537 return 1;
3538}
3539
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003540__LJMP static int hlua_applet_tcp_set_var(lua_State *L)
3541{
3542 struct hlua_appctx *appctx;
3543 struct stream *s;
3544 const char *name;
3545 size_t len;
3546 struct sample smp;
3547
3548 MAY_LJMP(check_args(L, 3, "set_var"));
3549
3550 /* It is useles to retrieve the stream, but this function
3551 * runs only in a stream context.
3552 */
3553 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3554 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3555 s = appctx->htxn.s;
3556
3557 /* Converts the third argument in a sample. */
3558 hlua_lua2smp(L, 3, &smp);
3559
3560 /* Store the sample in a variable. */
3561 smp_set_owner(&smp, s->be, s->sess, s, 0);
3562 vars_set_by_name(name, len, &smp);
3563 return 0;
3564}
3565
3566__LJMP static int hlua_applet_tcp_unset_var(lua_State *L)
3567{
3568 struct hlua_appctx *appctx;
3569 struct stream *s;
3570 const char *name;
3571 size_t len;
3572 struct sample smp;
3573
3574 MAY_LJMP(check_args(L, 2, "unset_var"));
3575
3576 /* It is useles to retrieve the stream, but this function
3577 * runs only in a stream context.
3578 */
3579 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3580 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3581 s = appctx->htxn.s;
3582
3583 /* Unset the variable. */
3584 smp_set_owner(&smp, s->be, s->sess, s, 0);
3585 vars_unset_by_name(name, len, &smp);
3586 return 0;
3587}
3588
3589__LJMP static int hlua_applet_tcp_get_var(lua_State *L)
3590{
3591 struct hlua_appctx *appctx;
3592 struct stream *s;
3593 const char *name;
3594 size_t len;
3595 struct sample smp;
3596
3597 MAY_LJMP(check_args(L, 2, "get_var"));
3598
3599 /* It is useles to retrieve the stream, but this function
3600 * runs only in a stream context.
3601 */
3602 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3603 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3604 s = appctx->htxn.s;
3605
3606 smp_set_owner(&smp, s->be, s->sess, s, 0);
3607 if (!vars_get_by_name(name, len, &smp)) {
3608 lua_pushnil(L);
3609 return 1;
3610 }
3611
3612 return hlua_smp2lua(L, &smp);
3613}
3614
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003615__LJMP static int hlua_applet_tcp_set_priv(lua_State *L)
3616{
3617 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3618 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003619 struct hlua *hlua;
3620
3621 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003622 if (!s->hlua)
3623 return 0;
3624 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003625
3626 MAY_LJMP(check_args(L, 2, "set_priv"));
3627
3628 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02003629 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003630
3631 /* Get and store new value. */
3632 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
3633 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
3634
3635 return 0;
3636}
3637
3638__LJMP static int hlua_applet_tcp_get_priv(lua_State *L)
3639{
3640 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3641 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003642 struct hlua *hlua;
3643
3644 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003645 if (!s->hlua) {
3646 lua_pushnil(L);
3647 return 1;
3648 }
3649 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003650
3651 /* Push configuration index in the stack. */
3652 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
3653
3654 return 1;
3655}
3656
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003657/* If expected data not yet available, it returns a yield. This function
3658 * consumes the data in the buffer. It returns a string containing the
3659 * data. This string can be empty.
3660 */
3661__LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx)
3662{
3663 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3664 struct stream_interface *si = appctx->appctx->owner;
3665 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02003666 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003667 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02003668 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003669 size_t len2;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003670
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003671 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003672 ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003673
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003674 /* Data not yet available. return yield. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003675 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003676 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003677 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_getline_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003678 }
3679
3680 /* End of data: commit the total strings and return. */
3681 if (ret < 0) {
3682 luaL_pushresult(&appctx->b);
3683 return 1;
3684 }
3685
3686 /* Ensure that the block 2 length is usable. */
3687 if (ret == 1)
3688 len2 = 0;
3689
3690 /* dont check the max length read and dont check. */
3691 luaL_addlstring(&appctx->b, blk1, len1);
3692 luaL_addlstring(&appctx->b, blk2, len2);
3693
3694 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003695 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003696 luaL_pushresult(&appctx->b);
3697 return 1;
3698}
3699
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003700/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003701__LJMP static int hlua_applet_tcp_getline(lua_State *L)
3702{
3703 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3704
3705 /* Initialise the string catenation. */
3706 luaL_buffinit(L, &appctx->b);
3707
3708 return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0));
3709}
3710
3711/* If expected data not yet available, it returns a yield. This function
3712 * consumes the data in the buffer. It returns a string containing the
3713 * data. This string can be empty.
3714 */
3715__LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx)
3716{
3717 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3718 struct stream_interface *si = appctx->appctx->owner;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003719 size_t len = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003720 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02003721 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003722 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02003723 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003724 size_t len2;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003725
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003726 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003727 ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003728
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003729 /* Data not yet available. return yield. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003730 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003731 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003732 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003733 }
3734
3735 /* End of data: commit the total strings and return. */
3736 if (ret < 0) {
3737 luaL_pushresult(&appctx->b);
3738 return 1;
3739 }
3740
3741 /* Ensure that the block 2 length is usable. */
3742 if (ret == 1)
3743 len2 = 0;
3744
3745 if (len == -1) {
3746
3747 /* If len == -1, catenate all the data avalaile and
3748 * yield because we want to get all the data until
3749 * the end of data stream.
3750 */
3751 luaL_addlstring(&appctx->b, blk1, len1);
3752 luaL_addlstring(&appctx->b, blk2, len2);
Willy Tarreau06d80a92017-10-19 14:32:15 +02003753 co_skip(si_oc(si), len1 + len2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003754 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003755 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003756
3757 } else {
3758
3759 /* Copy the fisrt block caping to the length required. */
3760 if (len1 > len)
3761 len1 = len;
3762 luaL_addlstring(&appctx->b, blk1, len1);
3763 len -= len1;
3764
3765 /* Copy the second block. */
3766 if (len2 > len)
3767 len2 = len;
3768 luaL_addlstring(&appctx->b, blk2, len2);
3769 len -= len2;
3770
3771 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003772 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003773
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003774 /* If there is no other data available, yield waiting for new data. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003775 if (len > 0) {
3776 lua_pushinteger(L, len);
3777 lua_replace(L, 2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003778 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003779 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003780 }
3781
3782 /* return the result. */
3783 luaL_pushresult(&appctx->b);
3784 return 1;
3785 }
3786
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003787 /* we never execute this */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003788 hlua_pusherror(L, "Lua: internal error");
3789 WILL_LJMP(lua_error(L));
3790 return 0;
3791}
3792
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003793/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003794__LJMP static int hlua_applet_tcp_recv(lua_State *L)
3795{
3796 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3797 int len = -1;
3798
3799 if (lua_gettop(L) > 2)
3800 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
3801 if (lua_gettop(L) >= 2) {
3802 len = MAY_LJMP(luaL_checkinteger(L, 2));
3803 lua_pop(L, 1);
3804 }
3805
3806 /* Confirm or set the required length */
3807 lua_pushinteger(L, len);
3808
3809 /* Initialise the string catenation. */
3810 luaL_buffinit(L, &appctx->b);
3811
3812 return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0));
3813}
3814
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003815/* Append data in the output side of the buffer. This data is immediately
3816 * sent. The function returns the amount of data written. If the buffer
3817 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003818 * if the channel is closed.
3819 */
3820__LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx)
3821{
3822 size_t len;
3823 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3824 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
3825 int l = MAY_LJMP(luaL_checkinteger(L, 3));
3826 struct stream_interface *si = appctx->appctx->owner;
3827 struct channel *chn = si_ic(si);
3828 int max;
3829
3830 /* Get the max amount of data which can write as input in the channel. */
3831 max = channel_recv_max(chn);
3832 if (max > (len - l))
3833 max = len - l;
3834
3835 /* Copy data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003836 ci_putblk(chn, str + l, max);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003837
3838 /* update counters. */
3839 l += max;
3840 lua_pop(L, 1);
3841 lua_pushinteger(L, l);
3842
3843 /* If some data is not send, declares the situation to the
3844 * applet, and returns a yield.
3845 */
3846 if (l < len) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003847 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003848 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003849 }
3850
3851 return 1;
3852}
3853
3854/* Just a wraper of "hlua_applet_tcp_send_yield". This wrapper permits
3855 * yield the LUA process, and resume it without checking the
3856 * input arguments.
3857 */
3858__LJMP static int hlua_applet_tcp_send(lua_State *L)
3859{
3860 MAY_LJMP(check_args(L, 2, "send"));
3861 lua_pushinteger(L, 0);
3862
3863 return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0));
3864}
3865
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003866/*
3867 *
3868 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003869 * Class AppletHTTP
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003870 *
3871 *
3872 */
3873
3874/* Returns a struct hlua_txn if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003875 * a class stream, otherwise it throws an error.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003876 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003877__LJMP static struct hlua_appctx *hlua_checkapplet_http(lua_State *L, int ud)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003878{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003879 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003880}
3881
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003882/* This function creates and push in the stack an Applet object
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003883 * according with a current TXN.
3884 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003885static int hlua_applet_http_new(lua_State *L, struct appctx *ctx)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003886{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003887 struct hlua_appctx *appctx;
Thierry FOURNIER841475e2015-12-11 17:10:09 +01003888 struct hlua_txn htxn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003889 struct stream_interface *si = ctx->owner;
3890 struct stream *s = si_strm(si);
3891 struct proxy *px = s->be;
Christopher Fauleta2097962019-07-15 16:25:33 +02003892 struct htx *htx;
3893 struct htx_blk *blk;
3894 struct htx_sl *sl;
3895 struct ist path;
3896 unsigned long long len = 0;
3897 int32_t pos;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003898
3899 /* Check stack size. */
3900 if (!lua_checkstack(L, 3))
3901 return 0;
3902
3903 /* Create the object: obj[0] = userdata.
3904 * Note that the base of the Converters object is the
3905 * same than the TXN object.
3906 */
3907 lua_newtable(L);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003908 appctx = lua_newuserdata(L, sizeof(*appctx));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003909 lua_rawseti(L, -2, 0);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003910 appctx->appctx = ctx;
3911 appctx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */
Robin H. Johnson52f5db22017-01-01 13:10:52 -08003912 appctx->appctx->ctx.hlua_apphttp.reason = NULL; /* Use default reason based on status */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003913 appctx->htxn.s = s;
3914 appctx->htxn.p = px;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003915
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003916 /* Create the "f" field that contains a list of fetches. */
3917 lua_pushstring(L, "f");
3918 if (!hlua_fetches_new(L, &appctx->htxn, 0))
3919 return 0;
3920 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003921
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003922 /* Create the "sf" field that contains a list of stringsafe fetches. */
3923 lua_pushstring(L, "sf");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003924 if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003925 return 0;
3926 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003927
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003928 /* Create the "c" field that contains a list of converters. */
3929 lua_pushstring(L, "c");
3930 if (!hlua_converters_new(L, &appctx->htxn, 0))
3931 return 0;
3932 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003933
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003934 /* Create the "sc" field that contains a list of stringsafe converters. */
3935 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003936 if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003937 return 0;
3938 lua_settable(L, -3);
Willy Tarreaueee5b512015-04-03 23:46:31 +02003939
Christopher Fauleta2097962019-07-15 16:25:33 +02003940 htx = htxbuf(&s->req.buf);
3941 blk = htx_get_first_blk(htx);
3942 BUG_ON(htx_get_blk_type(blk) != HTX_BLK_REQ_SL);
3943 sl = htx_get_blk_ptr(htx, blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003944
Christopher Fauleta2097962019-07-15 16:25:33 +02003945 /* Stores the request method. */
3946 lua_pushstring(L, "method");
3947 lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl));
3948 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003949
Christopher Fauleta2097962019-07-15 16:25:33 +02003950 /* Stores the http version. */
3951 lua_pushstring(L, "version");
3952 lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl));
3953 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003954
Christopher Fauleta2097962019-07-15 16:25:33 +02003955 /* creates an array of headers. hlua_http_get_headers() crates and push
3956 * the array on the top of the stack.
3957 */
3958 lua_pushstring(L, "headers");
3959 htxn.s = s;
3960 htxn.p = px;
3961 htxn.dir = SMP_OPT_DIR_REQ;
3962 if (!hlua_http_get_headers(L, &htxn, &htxn.s->txn->req))
3963 return 0;
3964 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003965
Christopher Fauleta2097962019-07-15 16:25:33 +02003966 path = http_get_path(htx_sl_req_uri(sl));
3967 if (path.ptr) {
3968 char *p, *q, *end;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003969
Christopher Fauleta2097962019-07-15 16:25:33 +02003970 p = path.ptr;
3971 end = path.ptr + path.len;
3972 q = p;
3973 while (q < end && *q != '?')
3974 q++;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003975
Thierry FOURNIER7d388632017-02-22 02:06:16 +01003976 /* Stores the request path. */
Christopher Fauleta2097962019-07-15 16:25:33 +02003977 lua_pushstring(L, "path");
3978 lua_pushlstring(L, p, q - p);
Thierry FOURNIER7d388632017-02-22 02:06:16 +01003979 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003980
Christopher Fauleta2097962019-07-15 16:25:33 +02003981 /* Stores the query string. */
3982 lua_pushstring(L, "qs");
3983 if (*q == '?')
3984 q++;
3985 lua_pushlstring(L, q, end - q);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003986 lua_settable(L, -3);
Christopher Fauleta2097962019-07-15 16:25:33 +02003987 }
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003988
Christopher Fauleta2097962019-07-15 16:25:33 +02003989 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
3990 struct htx_blk *blk = htx_get_blk(htx, pos);
3991 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003992
Christopher Fauleta2097962019-07-15 16:25:33 +02003993 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
3994 break;
3995 if (type == HTX_BLK_DATA)
3996 len += htx_get_blksz(blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003997 }
Christopher Fauleta2097962019-07-15 16:25:33 +02003998 if (htx->extra != ULLONG_MAX)
3999 len += htx->extra;
4000
4001 /* Stores the request path. */
4002 lua_pushstring(L, "length");
4003 lua_pushinteger(L, len);
4004 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01004005
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004006 /* Create an empty array of HTTP request headers. */
4007 lua_pushstring(L, "response");
4008 lua_newtable(L);
4009 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01004010
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004011 /* Pop a class stream metatable and affect it to the table. */
4012 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref);
4013 lua_setmetatable(L, -2);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004014
4015 return 1;
4016}
4017
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004018__LJMP static int hlua_applet_http_set_var(lua_State *L)
4019{
4020 struct hlua_appctx *appctx;
4021 struct stream *s;
4022 const char *name;
4023 size_t len;
4024 struct sample smp;
4025
4026 MAY_LJMP(check_args(L, 3, "set_var"));
4027
4028 /* It is useles to retrieve the stream, but this function
4029 * runs only in a stream context.
4030 */
4031 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4032 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4033 s = appctx->htxn.s;
4034
4035 /* Converts the third argument in a sample. */
4036 hlua_lua2smp(L, 3, &smp);
4037
4038 /* Store the sample in a variable. */
4039 smp_set_owner(&smp, s->be, s->sess, s, 0);
4040 vars_set_by_name(name, len, &smp);
4041 return 0;
4042}
4043
4044__LJMP static int hlua_applet_http_unset_var(lua_State *L)
4045{
4046 struct hlua_appctx *appctx;
4047 struct stream *s;
4048 const char *name;
4049 size_t len;
4050 struct sample smp;
4051
4052 MAY_LJMP(check_args(L, 2, "unset_var"));
4053
4054 /* It is useles to retrieve the stream, but this function
4055 * runs only in a stream context.
4056 */
4057 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4058 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4059 s = appctx->htxn.s;
4060
4061 /* Unset the variable. */
4062 smp_set_owner(&smp, s->be, s->sess, s, 0);
4063 vars_unset_by_name(name, len, &smp);
4064 return 0;
4065}
4066
4067__LJMP static int hlua_applet_http_get_var(lua_State *L)
4068{
4069 struct hlua_appctx *appctx;
4070 struct stream *s;
4071 const char *name;
4072 size_t len;
4073 struct sample smp;
4074
4075 MAY_LJMP(check_args(L, 2, "get_var"));
4076
4077 /* It is useles to retrieve the stream, but this function
4078 * runs only in a stream context.
4079 */
4080 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4081 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4082 s = appctx->htxn.s;
4083
4084 smp_set_owner(&smp, s->be, s->sess, s, 0);
4085 if (!vars_get_by_name(name, len, &smp)) {
4086 lua_pushnil(L);
4087 return 1;
4088 }
4089
4090 return hlua_smp2lua(L, &smp);
4091}
4092
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004093__LJMP static int hlua_applet_http_set_priv(lua_State *L)
4094{
4095 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4096 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004097 struct hlua *hlua;
4098
4099 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004100 if (!s->hlua)
4101 return 0;
4102 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004103
4104 MAY_LJMP(check_args(L, 2, "set_priv"));
4105
4106 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02004107 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004108
4109 /* Get and store new value. */
4110 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
4111 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
4112
4113 return 0;
4114}
4115
4116__LJMP static int hlua_applet_http_get_priv(lua_State *L)
4117{
4118 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4119 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004120 struct hlua *hlua;
4121
4122 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004123 if (!s->hlua) {
4124 lua_pushnil(L);
4125 return 1;
4126 }
4127 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004128
4129 /* Push configuration index in the stack. */
4130 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
4131
4132 return 1;
4133}
4134
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004135/* If expected data not yet available, it returns a yield. This function
4136 * consumes the data in the buffer. It returns a string containing the
4137 * data. This string can be empty.
4138 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004139__LJMP static int hlua_applet_http_getline_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004140{
4141 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4142 struct stream_interface *si = appctx->appctx->owner;
4143 struct channel *req = si_oc(si);
4144 struct htx *htx;
4145 struct htx_blk *blk;
4146 size_t count;
4147 int stop = 0;
4148
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004149 htx = htx_from_buf(&req->buf);
4150 count = co_data(req);
Christopher Fauleta3f15502019-05-13 15:27:23 +02004151 blk = htx_get_first_blk(htx);
Christopher Fauletcc26b132018-12-18 21:20:57 +01004152
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004153 while (count && !stop && blk) {
4154 enum htx_blk_type type = htx_get_blk_type(blk);
4155 uint32_t sz = htx_get_blksz(blk);
4156 struct ist v;
4157 uint32_t vlen;
4158 char *nl;
4159
Christopher Faulete461e342018-12-18 16:43:35 +01004160 if (type == HTX_BLK_EOM) {
4161 stop = 1;
4162 break;
4163 }
4164
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004165 vlen = sz;
4166 if (vlen > count) {
4167 if (type != HTX_BLK_DATA)
4168 break;
4169 vlen = count;
4170 }
4171
4172 switch (type) {
4173 case HTX_BLK_UNUSED:
4174 break;
4175
4176 case HTX_BLK_DATA:
4177 v = htx_get_blk_value(htx, blk);
4178 v.len = vlen;
4179 nl = istchr(v, '\n');
4180 if (nl != NULL) {
4181 stop = 1;
4182 vlen = nl - v.ptr + 1;
4183 }
4184 luaL_addlstring(&appctx->b, v.ptr, vlen);
4185 break;
4186
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004187 case HTX_BLK_TLR:
4188 case HTX_BLK_EOM:
4189 stop = 1;
4190 break;
4191
4192 default:
4193 break;
4194 }
4195
4196 co_set_data(req, co_data(req) - vlen);
4197 count -= vlen;
4198 if (sz == vlen)
4199 blk = htx_remove_blk(htx, blk);
4200 else {
4201 htx_cut_data_blk(htx, blk, vlen);
4202 break;
4203 }
4204 }
4205
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004206 htx_to_buf(htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004207 if (!stop) {
4208 si_cant_get(si);
Christopher Fauleta2097962019-07-15 16:25:33 +02004209 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_getline_yield, TICK_ETERNITY, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004210 }
4211
4212 /* return the result. */
4213 luaL_pushresult(&appctx->b);
4214 return 1;
4215}
4216
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004217
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004218/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004219__LJMP static int hlua_applet_http_getline(lua_State *L)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004220{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004221 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004222
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004223 /* Initialise the string catenation. */
4224 luaL_buffinit(L, &appctx->b);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004225
Christopher Fauleta2097962019-07-15 16:25:33 +02004226 return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004227}
4228
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004229/* If expected data not yet available, it returns a yield. This function
4230 * consumes the data in the buffer. It returns a string containing the
4231 * data. This string can be empty.
4232 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004233__LJMP static int hlua_applet_http_recv_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004234{
4235 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4236 struct stream_interface *si = appctx->appctx->owner;
4237 struct channel *req = si_oc(si);
4238 struct htx *htx;
4239 struct htx_blk *blk;
4240 size_t count;
4241 int len;
4242
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004243 htx = htx_from_buf(&req->buf);
4244 len = MAY_LJMP(luaL_checkinteger(L, 2));
4245 count = co_data(req);
Christopher Faulet29f17582019-05-23 11:03:26 +02004246 blk = htx_get_head_blk(htx);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004247 while (count && len && blk) {
4248 enum htx_blk_type type = htx_get_blk_type(blk);
4249 uint32_t sz = htx_get_blksz(blk);
4250 struct ist v;
4251 uint32_t vlen;
4252
Christopher Faulete461e342018-12-18 16:43:35 +01004253 if (type == HTX_BLK_EOM) {
4254 len = 0;
4255 break;
4256 }
4257
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004258 vlen = sz;
4259 if (len > 0 && vlen > len)
4260 vlen = len;
4261 if (vlen > count) {
4262 if (type != HTX_BLK_DATA)
4263 break;
4264 vlen = count;
4265 }
4266
4267 switch (type) {
4268 case HTX_BLK_UNUSED:
4269 break;
4270
4271 case HTX_BLK_DATA:
4272 v = htx_get_blk_value(htx, blk);
4273 luaL_addlstring(&appctx->b, v.ptr, vlen);
4274 break;
4275
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004276 case HTX_BLK_TLR:
4277 case HTX_BLK_EOM:
4278 len = 0;
4279 break;
4280
4281 default:
4282 break;
4283 }
4284
4285 co_set_data(req, co_data(req) - vlen);
4286 count -= vlen;
4287 if (len > 0)
4288 len -= vlen;
4289 if (sz == vlen)
4290 blk = htx_remove_blk(htx, blk);
4291 else {
4292 htx_cut_data_blk(htx, blk, vlen);
4293 break;
4294 }
4295 }
4296
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004297 htx_to_buf(htx, &req->buf);
4298
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004299 /* If we are no other data available, yield waiting for new data. */
4300 if (len) {
4301 if (len > 0) {
4302 lua_pushinteger(L, len);
4303 lua_replace(L, 2);
4304 }
4305 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004306 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004307 }
4308
4309 /* return the result. */
4310 luaL_pushresult(&appctx->b);
4311 return 1;
4312}
4313
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004314/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004315__LJMP static int hlua_applet_http_recv(lua_State *L)
4316{
4317 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4318 int len = -1;
4319
4320 /* Check arguments. */
4321 if (lua_gettop(L) > 2)
4322 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
4323 if (lua_gettop(L) >= 2) {
4324 len = MAY_LJMP(luaL_checkinteger(L, 2));
4325 lua_pop(L, 1);
4326 }
4327
Christopher Fauleta2097962019-07-15 16:25:33 +02004328 lua_pushinteger(L, len);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004329
Christopher Fauleta2097962019-07-15 16:25:33 +02004330 /* Initialise the string catenation. */
4331 luaL_buffinit(L, &appctx->b);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004332
Christopher Fauleta2097962019-07-15 16:25:33 +02004333 return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004334}
4335
4336/* Append data in the output side of the buffer. This data is immediately
4337 * sent. The function returns the amount of data written. If the buffer
4338 * cannot contain the data, the function yields. The function returns -1
4339 * if the channel is closed.
4340 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004341__LJMP static int hlua_applet_http_send_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004342{
4343 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4344 struct stream_interface *si = appctx->appctx->owner;
4345 struct channel *res = si_ic(si);
4346 struct htx *htx = htx_from_buf(&res->buf);
4347 const char *data;
4348 size_t len;
4349 int l = MAY_LJMP(luaL_checkinteger(L, 3));
4350 int max;
4351
Christopher Faulet9060fc02019-07-03 11:39:30 +02004352 max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx));
Christopher Faulet4b0e9b22019-01-09 12:16:58 +01004353 if (!max)
4354 goto snd_yield;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004355
4356 data = MAY_LJMP(luaL_checklstring(L, 2, &len));
4357
4358 /* Get the max amount of data which can write as input in the channel. */
4359 if (max > (len - l))
4360 max = len - l;
4361
4362 /* Copy data. */
Willy Tarreau0a7ef022019-05-28 10:30:11 +02004363 max = htx_add_data(htx, ist2(data + l, max));
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01004364 channel_add_input(res, max);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004365
4366 /* update counters. */
4367 l += max;
4368 lua_pop(L, 1);
4369 lua_pushinteger(L, l);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004370
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004371 /* If some data is not send, declares the situation to the
4372 * applet, and returns a yield.
4373 */
4374 if (l < len) {
Christopher Faulet4b0e9b22019-01-09 12:16:58 +01004375 snd_yield:
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004376 htx_to_buf(htx, &res->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004377 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004378 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004379 }
4380
Christopher Fauleta2097962019-07-15 16:25:33 +02004381 htx_to_buf(htx, &res->buf);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004382 return 1;
4383}
4384
4385/* Just a wraper of "hlua_applet_send_yield". This wrapper permits
4386 * yield the LUA process, and resume it without checking the
4387 * input arguments.
4388 */
4389__LJMP static int hlua_applet_http_send(lua_State *L)
4390{
4391 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004392
4393 /* We want to send some data. Headers must be sent. */
4394 if (!(appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
4395 hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data.");
4396 WILL_LJMP(lua_error(L));
4397 }
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004398
Christopher Fauleta2097962019-07-15 16:25:33 +02004399 /* This interger is used for followinf the amount of data sent. */
4400 lua_pushinteger(L, 0);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004401
Christopher Fauleta2097962019-07-15 16:25:33 +02004402 return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004403}
4404
4405__LJMP static int hlua_applet_http_addheader(lua_State *L)
4406{
4407 const char *name;
4408 int ret;
4409
4410 MAY_LJMP(hlua_checkapplet_http(L, 1));
4411 name = MAY_LJMP(luaL_checkstring(L, 2));
4412 MAY_LJMP(luaL_checkstring(L, 3));
4413
4414 /* Push in the stack the "response" entry. */
4415 ret = lua_getfield(L, 1, "response");
4416 if (ret != LUA_TTABLE) {
4417 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] "
4418 "is expected as an array. %s found", lua_typename(L, ret));
4419 WILL_LJMP(lua_error(L));
4420 }
4421
4422 /* check if the header is already registered if it is not
4423 * the case, register it.
4424 */
4425 ret = lua_getfield(L, -1, name);
4426 if (ret == LUA_TNIL) {
4427
4428 /* Entry not found. */
4429 lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */
4430
4431 /* Insert the new header name in the array in the top of the stack.
4432 * It left the new array in the top of the stack.
4433 */
4434 lua_newtable(L);
4435 lua_pushvalue(L, 2);
4436 lua_pushvalue(L, -2);
4437 lua_settable(L, -4);
4438
4439 } else if (ret != LUA_TTABLE) {
4440
4441 /* corruption error. */
4442 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] "
4443 "is expected as an array. %s found", name, lua_typename(L, ret));
4444 WILL_LJMP(lua_error(L));
4445 }
4446
4447 /* Now the top od thestack is an array of values. We push
4448 * the header value as new entry.
4449 */
4450 lua_pushvalue(L, 3);
4451 ret = lua_rawlen(L, -2);
4452 lua_rawseti(L, -2, ret + 1);
4453 lua_pushboolean(L, 1);
4454 return 1;
4455}
4456
4457__LJMP static int hlua_applet_http_status(lua_State *L)
4458{
4459 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4460 int status = MAY_LJMP(luaL_checkinteger(L, 2));
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004461 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004462
4463 if (status < 100 || status > 599) {
4464 lua_pushboolean(L, 0);
4465 return 1;
4466 }
4467
4468 appctx->appctx->ctx.hlua_apphttp.status = status;
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004469 appctx->appctx->ctx.hlua_apphttp.reason = reason;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004470 lua_pushboolean(L, 1);
4471 return 1;
4472}
4473
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004474
Christopher Fauleta2097962019-07-15 16:25:33 +02004475__LJMP static int hlua_applet_http_send_response(lua_State *L)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004476{
4477 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4478 struct stream_interface *si = appctx->appctx->owner;
4479 struct channel *res = si_ic(si);
4480 struct htx *htx;
4481 struct htx_sl *sl;
4482 struct h1m h1m;
4483 const char *status, *reason;
4484 const char *name, *value;
4485 size_t nlen, vlen;
4486 unsigned int flags;
4487
4488 /* Send the message at once. */
4489 htx = htx_from_buf(&res->buf);
4490 h1m_init_res(&h1m);
4491
4492 /* Use the same http version than the request. */
4493 status = ultoa_r(appctx->appctx->ctx.hlua_apphttp.status, trash.area, trash.size);
4494 reason = appctx->appctx->ctx.hlua_apphttp.reason;
4495 if (reason == NULL)
4496 reason = http_get_reason(appctx->appctx->ctx.hlua_apphttp.status);
4497 if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) {
4498 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11);
4499 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason));
4500 }
4501 else {
4502 flags = HTX_SL_F_IS_RESP;
4503 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason));
4504 }
4505 if (!sl) {
4506 hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n",
4507 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4508 WILL_LJMP(lua_error(L));
4509 }
4510 sl->info.res.status = appctx->appctx->ctx.hlua_apphttp.status;
4511
4512 /* Get the array associated to the field "response" in the object AppletHTTP. */
4513 lua_pushvalue(L, 0);
4514 if (lua_getfield(L, 1, "response") != LUA_TTABLE) {
4515 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n",
4516 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4517 WILL_LJMP(lua_error(L));
4518 }
4519
4520 /* Browse the list of headers. */
4521 lua_pushnil(L);
4522 while(lua_next(L, -2) != 0) {
4523 /* We expect a string as -2. */
4524 if (lua_type(L, -2) != LUA_TSTRING) {
4525 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n",
4526 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4527 lua_typename(L, lua_type(L, -2)));
4528 WILL_LJMP(lua_error(L));
4529 }
4530 name = lua_tolstring(L, -2, &nlen);
4531
4532 /* We expect an array as -1. */
4533 if (lua_type(L, -1) != LUA_TTABLE) {
4534 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n",
4535 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4536 name,
4537 lua_typename(L, lua_type(L, -1)));
4538 WILL_LJMP(lua_error(L));
4539 }
4540
4541 /* Browse the table who is on the top of the stack. */
4542 lua_pushnil(L);
4543 while(lua_next(L, -2) != 0) {
4544 int id;
4545
4546 /* We expect a number as -2. */
4547 if (lua_type(L, -2) != LUA_TNUMBER) {
4548 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n",
4549 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4550 name,
4551 lua_typename(L, lua_type(L, -2)));
4552 WILL_LJMP(lua_error(L));
4553 }
4554 id = lua_tointeger(L, -2);
4555
4556 /* We expect a string as -2. */
4557 if (lua_type(L, -1) != LUA_TSTRING) {
4558 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n",
4559 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4560 name, id,
4561 lua_typename(L, lua_type(L, -1)));
4562 WILL_LJMP(lua_error(L));
4563 }
4564 value = lua_tolstring(L, -1, &vlen);
4565
4566 /* Simple Protocol checks. */
4567 if (isteqi(ist2(name, nlen), ist("transfer-encoding")))
4568 h1_parse_xfer_enc_header(&h1m, ist2(name, nlen));
4569 else if (isteqi(ist2(name, nlen), ist("content-length"))) {
4570 struct ist v = ist2(value, vlen);
4571 int ret;
4572
4573 ret = h1_parse_cont_len_header(&h1m, &v);
4574 if (ret < 0) {
4575 hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n",
4576 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4577 name);
4578 WILL_LJMP(lua_error(L));
4579 }
4580 else if (ret == 0)
4581 goto next; /* Skip it */
4582 }
4583
4584 /* Add a new header */
4585 if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) {
4586 hlua_pusherror(L, "Lua applet http '%s': Failed to add header '%s' in the response.\n",
4587 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4588 name);
4589 WILL_LJMP(lua_error(L));
4590 }
4591 next:
4592 /* Remove the array from the stack, and get next element with a remaining string. */
4593 lua_pop(L, 1);
4594 }
4595
4596 /* Remove the array from the stack, and get next element with a remaining string. */
4597 lua_pop(L, 1);
4598 }
4599
4600 if (h1m.flags & H1_MF_CHNK)
4601 h1m.flags &= ~H1_MF_CLEN;
4602 if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK))
4603 h1m.flags |= H1_MF_XFER_LEN;
4604
4605 /* Uset HTX start-line flags */
4606 if (h1m.flags & H1_MF_XFER_ENC)
4607 flags |= HTX_SL_F_XFER_ENC;
4608 if (h1m.flags & H1_MF_XFER_LEN) {
4609 flags |= HTX_SL_F_XFER_LEN;
4610 if (h1m.flags & H1_MF_CHNK)
4611 flags |= HTX_SL_F_CHNK;
4612 else if (h1m.flags & H1_MF_CLEN)
4613 flags |= HTX_SL_F_CLEN;
4614 if (h1m.body_len == 0)
4615 flags |= HTX_SL_F_BODYLESS;
4616 }
4617 sl->flags |= flags;
4618
4619 /* If we dont have a content-length set, and the HTTP version is 1.1
4620 * and the status code implies the presence of a message body, we must
4621 * announce a transfer encoding chunked. This is required by haproxy
4622 * for the keepalive compliance. If the applet annouces a transfer-encoding
4623 * chunked itslef, don't do anything.
4624 */
4625 if ((flags & (HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN)) == HTX_SL_F_VER_11 &&
4626 appctx->appctx->ctx.hlua_apphttp.status >= 200 &&
4627 appctx->appctx->ctx.hlua_apphttp.status != 204 &&
4628 appctx->appctx->ctx.hlua_apphttp.status != 304) {
4629 /* Add a new header */
4630 sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN);
4631 if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) {
4632 hlua_pusherror(L, "Lua applet http '%s': Failed to add header 'transfer-encoding' in the response.\n",
4633 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4634 WILL_LJMP(lua_error(L));
4635 }
4636 }
4637
4638 /* Finalize headers. */
4639 if (!htx_add_endof(htx, HTX_BLK_EOH)) {
4640 hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n",
4641 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4642 WILL_LJMP(lua_error(L));
4643 }
4644
4645 if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) {
4646 b_reset(&res->buf);
4647 hlua_pusherror(L, "Lua: 'start_response': response header block too big");
4648 WILL_LJMP(lua_error(L));
4649 }
4650
4651 htx_to_buf(htx, &res->buf);
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01004652 channel_add_input(res, htx->data);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004653
4654 /* Headers sent, set the flag. */
4655 appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT;
4656 return 0;
4657
4658}
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004659/* We will build the status line and the headers of the HTTP response.
4660 * We will try send at once if its not possible, we give back the hand
4661 * waiting for more room.
4662 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004663__LJMP static int hlua_applet_http_start_response_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004664{
4665 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4666 struct stream_interface *si = appctx->appctx->owner;
4667 struct channel *res = si_ic(si);
4668
4669 if (co_data(res)) {
4670 si_rx_room_blk(si);
Christopher Fauleta2097962019-07-15 16:25:33 +02004671 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_start_response_yield, TICK_ETERNITY, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004672 }
Christopher Fauleta2097962019-07-15 16:25:33 +02004673 return MAY_LJMP(hlua_applet_http_send_response(L));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004674}
4675
4676
Christopher Fauleta2097962019-07-15 16:25:33 +02004677__LJMP static int hlua_applet_http_start_response(lua_State *L)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004678{
Christopher Fauleta2097962019-07-15 16:25:33 +02004679 return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004680}
4681
Christopher Fauleta2097962019-07-15 16:25:33 +02004682/*
4683 *
4684 *
4685 * Class HTTP
4686 *
4687 *
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004688 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004689
4690/* Returns a struct hlua_txn if the stack entry "ud" is
4691 * a class stream, otherwise it throws an error.
4692 */
4693__LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004694{
Christopher Fauleta2097962019-07-15 16:25:33 +02004695 return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref));
4696}
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004697
Christopher Fauleta2097962019-07-15 16:25:33 +02004698/* This function creates and push in the stack a HTTP object
4699 * according with a current TXN.
4700 */
4701static int hlua_http_new(lua_State *L, struct hlua_txn *txn)
4702{
4703 struct hlua_txn *htxn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004704
Christopher Fauleta2097962019-07-15 16:25:33 +02004705 /* Check stack size. */
4706 if (!lua_checkstack(L, 3))
4707 return 0;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004708
Christopher Fauleta2097962019-07-15 16:25:33 +02004709 /* Create the object: obj[0] = userdata.
4710 * Note that the base of the Converters object is the
4711 * same than the TXN object.
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004712 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004713 lua_newtable(L);
4714 htxn = lua_newuserdata(L, sizeof(*htxn));
4715 lua_rawseti(L, -2, 0);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004716
4717 htxn->s = txn->s;
4718 htxn->p = txn->p;
Christopher Faulet256b69a2019-05-23 11:14:21 +02004719 htxn->dir = txn->dir;
4720 htxn->flags = txn->flags;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004721
4722 /* Pop a class stream metatable and affect it to the table. */
4723 lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref);
4724 lua_setmetatable(L, -2);
4725
4726 return 1;
4727}
4728
4729/* This function creates ans returns an array of HTTP headers.
4730 * This function does not fails. It is used as wrapper with the
4731 * 2 following functions.
4732 */
4733__LJMP static int hlua_http_get_headers(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg)
4734{
Christopher Fauleta2097962019-07-15 16:25:33 +02004735 struct htx *htx;
4736 int32_t pos;
4737
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004738 /* Create the table. */
4739 lua_newtable(L);
4740
4741 if (!htxn->s->txn)
4742 return 1;
4743
Christopher Fauleta2097962019-07-15 16:25:33 +02004744 htx = htxbuf(&msg->chn->buf);
4745 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
4746 struct htx_blk *blk = htx_get_blk(htx, pos);
4747 enum htx_blk_type type = htx_get_blk_type(blk);
4748 struct ist n, v;
4749 int len;
Christopher Faulet724a12c2018-12-13 22:12:15 +01004750
Christopher Fauleta2097962019-07-15 16:25:33 +02004751 if (type == HTX_BLK_HDR) {
4752 n = htx_get_blk_name(htx,blk);
4753 v = htx_get_blk_value(htx, blk);
Christopher Faulet724a12c2018-12-13 22:12:15 +01004754 }
Christopher Fauleta2097962019-07-15 16:25:33 +02004755 else if (type == HTX_BLK_EOH)
4756 break;
4757 else
4758 continue;
Christopher Faulet724a12c2018-12-13 22:12:15 +01004759
Christopher Fauleta2097962019-07-15 16:25:33 +02004760 /* Check for existing entry:
4761 * assume that the table is on the top of the stack, and
4762 * push the key in the stack, the function lua_gettable()
4763 * perform the lookup.
4764 */
4765 lua_pushlstring(L, n.ptr, n.len);
4766 lua_gettable(L, -2);
Christopher Faulet724a12c2018-12-13 22:12:15 +01004767
Christopher Fauleta2097962019-07-15 16:25:33 +02004768 switch (lua_type(L, -1)) {
4769 case LUA_TNIL:
4770 /* Table not found, create it. */
4771 lua_pop(L, 1); /* remove the nil value. */
4772 lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */
4773 lua_newtable(L); /* create and push empty table. */
4774 lua_pushlstring(L, v.ptr, v.len); /* push header value. */
4775 lua_rawseti(L, -2, 0); /* index header value (pop it). */
4776 lua_rawset(L, -3); /* index new table with header name (pop the values). */
Christopher Faulet724a12c2018-12-13 22:12:15 +01004777 break;
Christopher Faulet724a12c2018-12-13 22:12:15 +01004778
Christopher Fauleta2097962019-07-15 16:25:33 +02004779 case LUA_TTABLE:
4780 /* Entry found: push the value in the table. */
4781 len = lua_rawlen(L, -1);
4782 lua_pushlstring(L, v.ptr, v.len); /* push header value. */
4783 lua_rawseti(L, -2, len+1); /* index header value (pop it). */
4784 lua_pop(L, 1); /* remove the table (it is stored in the main table). */
4785 break;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004786
Christopher Fauleta2097962019-07-15 16:25:33 +02004787 default:
4788 /* Other cases are errors. */
4789 hlua_pusherror(L, "internal error during the parsing of headers.");
4790 WILL_LJMP(lua_error(L));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004791 }
4792 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004793 return 1;
4794}
4795
4796__LJMP static int hlua_http_req_get_headers(lua_State *L)
4797{
4798 struct hlua_txn *htxn;
4799
4800 MAY_LJMP(check_args(L, 1, "req_get_headers"));
4801 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4802
4803 return hlua_http_get_headers(L, htxn, &htxn->s->txn->req);
4804}
4805
4806__LJMP static int hlua_http_res_get_headers(lua_State *L)
4807{
4808 struct hlua_txn *htxn;
4809
4810 MAY_LJMP(check_args(L, 1, "res_get_headers"));
4811 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4812
4813 return hlua_http_get_headers(L, htxn, &htxn->s->txn->rsp);
4814}
4815
4816/* This function replace full header, or just a value in
4817 * the request or in the response. It is a wrapper fir the
4818 * 4 following functions.
4819 */
4820__LJMP static inline int hlua_http_rep_hdr(lua_State *L, struct hlua_txn *htxn,
4821 struct http_msg *msg, int action)
4822{
4823 size_t name_len;
4824 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
4825 const char *reg = MAY_LJMP(luaL_checkstring(L, 3));
4826 const char *value = MAY_LJMP(luaL_checkstring(L, 4));
Christopher Fauleta2097962019-07-15 16:25:33 +02004827 struct htx *htx;
Dragan Dosen26743032019-04-30 15:54:36 +02004828 struct my_regex *re;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004829
Dragan Dosen26743032019-04-30 15:54:36 +02004830 if (!(re = regex_comp(reg, 1, 1, NULL)))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004831 WILL_LJMP(luaL_argerror(L, 3, "invalid regex"));
4832
Christopher Fauleta2097962019-07-15 16:25:33 +02004833 htx = htxbuf(&msg->chn->buf);
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004834 http_transform_header_str(htxn->s, msg->chn, htx, ist2(name, name_len), value, re, action);
Dragan Dosen26743032019-04-30 15:54:36 +02004835 regex_free(re);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004836 return 0;
4837}
4838
4839__LJMP static int hlua_http_req_rep_hdr(lua_State *L)
4840{
4841 struct hlua_txn *htxn;
4842
4843 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
4844 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4845
4846 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_HDR));
4847}
4848
4849__LJMP static int hlua_http_res_rep_hdr(lua_State *L)
4850{
4851 struct hlua_txn *htxn;
4852
4853 MAY_LJMP(check_args(L, 4, "res_rep_hdr"));
4854 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4855
4856 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->rsp, ACT_HTTP_REPLACE_HDR));
4857}
4858
4859__LJMP static int hlua_http_req_rep_val(lua_State *L)
4860{
4861 struct hlua_txn *htxn;
4862
4863 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
4864 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4865
4866 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_VAL));
4867}
4868
4869__LJMP static int hlua_http_res_rep_val(lua_State *L)
4870{
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004871 struct hlua_txn *htxn;
4872
4873 MAY_LJMP(check_args(L, 4, "res_rep_val"));
4874 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4875
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02004876 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->rsp, ACT_HTTP_REPLACE_VAL));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004877}
4878
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004879/* This function deletes all the occurrences of an header.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004880 * It is a wrapper for the 2 following functions.
4881 */
4882__LJMP static inline int hlua_http_del_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg)
4883{
4884 size_t len;
4885 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Christopher Fauleta2097962019-07-15 16:25:33 +02004886 struct htx *htx = htxbuf(&msg->chn->buf);
4887 struct http_hdr_ctx ctx;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004888
Christopher Fauleta2097962019-07-15 16:25:33 +02004889 ctx.blk = NULL;
4890 while (http_find_header(htx, ist2(name, len), &ctx, 1))
4891 http_remove_header(htx, &ctx);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004892 return 0;
4893}
4894
4895__LJMP static int hlua_http_req_del_hdr(lua_State *L)
4896{
4897 struct hlua_txn *htxn;
4898
4899 MAY_LJMP(check_args(L, 2, "req_del_hdr"));
4900 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4901
Willy Tarreaueee5b512015-04-03 23:46:31 +02004902 return hlua_http_del_hdr(L, htxn, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004903}
4904
4905__LJMP static int hlua_http_res_del_hdr(lua_State *L)
4906{
4907 struct hlua_txn *htxn;
4908
4909 MAY_LJMP(check_args(L, 2, "req_del_hdr"));
4910 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4911
Willy Tarreaueee5b512015-04-03 23:46:31 +02004912 return hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004913}
4914
4915/* This function adds an header. It is a wrapper used by
4916 * the 2 following functions.
4917 */
4918__LJMP static inline int hlua_http_add_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg)
4919{
4920 size_t name_len;
4921 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
4922 size_t value_len;
4923 const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len));
Christopher Fauleta2097962019-07-15 16:25:33 +02004924 struct htx *htx = htxbuf(&msg->chn->buf);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004925
Christopher Fauleta2097962019-07-15 16:25:33 +02004926 lua_pushboolean(L, http_add_header(htx, ist2(name, name_len),
4927 ist2(value, value_len)));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004928 return 0;
4929}
4930
4931__LJMP static int hlua_http_req_add_hdr(lua_State *L)
4932{
4933 struct hlua_txn *htxn;
4934
4935 MAY_LJMP(check_args(L, 3, "req_add_hdr"));
4936 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4937
Willy Tarreaueee5b512015-04-03 23:46:31 +02004938 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004939}
4940
4941__LJMP static int hlua_http_res_add_hdr(lua_State *L)
4942{
4943 struct hlua_txn *htxn;
4944
4945 MAY_LJMP(check_args(L, 3, "res_add_hdr"));
4946 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4947
Willy Tarreaueee5b512015-04-03 23:46:31 +02004948 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004949}
4950
4951static int hlua_http_req_set_hdr(lua_State *L)
4952{
4953 struct hlua_txn *htxn;
4954
4955 MAY_LJMP(check_args(L, 3, "req_set_hdr"));
4956 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4957
Willy Tarreaueee5b512015-04-03 23:46:31 +02004958 hlua_http_del_hdr(L, htxn, &htxn->s->txn->req);
4959 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004960}
4961
4962static int hlua_http_res_set_hdr(lua_State *L)
4963{
4964 struct hlua_txn *htxn;
4965
4966 MAY_LJMP(check_args(L, 3, "res_set_hdr"));
4967 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4968
Willy Tarreaueee5b512015-04-03 23:46:31 +02004969 hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp);
4970 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004971}
4972
4973/* This function set the method. */
4974static int hlua_http_req_set_meth(lua_State *L)
4975{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02004976 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004977 size_t name_len;
4978 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004979
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004980 lua_pushboolean(L, http_req_replace_stline(0, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004981 return 1;
4982}
4983
4984/* This function set the method. */
4985static int hlua_http_req_set_path(lua_State *L)
4986{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02004987 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004988 size_t name_len;
4989 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004990
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004991 lua_pushboolean(L, http_req_replace_stline(1, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004992 return 1;
4993}
4994
4995/* This function set the query-string. */
4996static int hlua_http_req_set_query(lua_State *L)
4997{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02004998 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004999 size_t name_len;
5000 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005001
5002 /* Check length. */
5003 if (name_len > trash.size - 1) {
5004 lua_pushboolean(L, 0);
5005 return 1;
5006 }
5007
5008 /* Add the mark question as prefix. */
5009 chunk_reset(&trash);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005010 trash.area[trash.data++] = '?';
5011 memcpy(trash.area + trash.data, name, name_len);
5012 trash.data += name_len;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005013
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005014 lua_pushboolean(L,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005015 http_req_replace_stline(2, trash.area, trash.data, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005016 return 1;
5017}
5018
5019/* This function set the uri. */
5020static int hlua_http_req_set_uri(lua_State *L)
5021{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005022 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005023 size_t name_len;
5024 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005025
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005026 lua_pushboolean(L, http_req_replace_stline(3, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005027 return 1;
5028}
5029
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005030/* This function set the response code & optionally reason. */
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005031static int hlua_http_res_set_status(lua_State *L)
5032{
5033 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5034 unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2));
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005035 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005036
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005037 http_res_set_status(code, reason, htxn->s);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005038 return 0;
5039}
5040
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005041/*
5042 *
5043 *
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005044 * Class TXN
5045 *
5046 *
5047 */
5048
5049/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02005050 * a class stream, otherwise it throws an error.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005051 */
5052__LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud)
5053{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02005054 return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref));
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005055}
5056
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005057__LJMP static int hlua_set_var(lua_State *L)
5058{
5059 struct hlua_txn *htxn;
5060 const char *name;
5061 size_t len;
5062 struct sample smp;
5063
5064 MAY_LJMP(check_args(L, 3, "set_var"));
5065
5066 /* It is useles to retrieve the stream, but this function
5067 * runs only in a stream context.
5068 */
5069 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5070 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5071
5072 /* Converts the third argument in a sample. */
5073 hlua_lua2smp(L, 3, &smp);
5074
5075 /* Store the sample in a variable. */
Willy Tarreau7560dd42016-03-10 16:28:58 +01005076 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Willy Tarreau6204cd92016-03-10 16:33:04 +01005077 vars_set_by_name(name, len, &smp);
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005078 return 0;
5079}
5080
Christopher Faulet85d79c92016-11-09 16:54:56 +01005081__LJMP static int hlua_unset_var(lua_State *L)
5082{
5083 struct hlua_txn *htxn;
5084 const char *name;
5085 size_t len;
5086 struct sample smp;
5087
5088 MAY_LJMP(check_args(L, 2, "unset_var"));
5089
5090 /* It is useles to retrieve the stream, but this function
5091 * runs only in a stream context.
5092 */
5093 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5094 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5095
5096 /* Unset the variable. */
5097 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
5098 vars_unset_by_name(name, len, &smp);
5099 return 0;
5100}
5101
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005102__LJMP static int hlua_get_var(lua_State *L)
5103{
5104 struct hlua_txn *htxn;
5105 const char *name;
5106 size_t len;
5107 struct sample smp;
5108
5109 MAY_LJMP(check_args(L, 2, "get_var"));
5110
5111 /* It is useles to retrieve the stream, but this function
5112 * runs only in a stream context.
5113 */
5114 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5115 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5116
Willy Tarreau7560dd42016-03-10 16:28:58 +01005117 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Willy Tarreau6204cd92016-03-10 16:33:04 +01005118 if (!vars_get_by_name(name, len, &smp)) {
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005119 lua_pushnil(L);
5120 return 1;
5121 }
5122
5123 return hlua_smp2lua(L, &smp);
5124}
5125
Willy Tarreau59551662015-03-10 14:23:13 +01005126__LJMP static int hlua_set_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005127{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005128 struct hlua *hlua;
5129
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005130 MAY_LJMP(check_args(L, 2, "set_priv"));
5131
Willy Tarreau87b09662015-04-03 00:22:06 +02005132 /* It is useles to retrieve the stream, but this function
5133 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005134 */
5135 MAY_LJMP(hlua_checktxn(L, 1));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005136 hlua = hlua_gethlua(L);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005137
5138 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02005139 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005140
5141 /* Get and store new value. */
5142 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
5143 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
5144
5145 return 0;
5146}
5147
Willy Tarreau59551662015-03-10 14:23:13 +01005148__LJMP static int hlua_get_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005149{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005150 struct hlua *hlua;
5151
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005152 MAY_LJMP(check_args(L, 1, "get_priv"));
5153
Willy Tarreau87b09662015-04-03 00:22:06 +02005154 /* It is useles to retrieve the stream, but this function
5155 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005156 */
5157 MAY_LJMP(hlua_checktxn(L, 1));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005158 hlua = hlua_gethlua(L);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005159
5160 /* Push configuration index in the stack. */
5161 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
5162
5163 return 1;
5164}
5165
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005166/* Create stack entry containing a class TXN. This function
5167 * return 0 if the stack does not contains free slots,
5168 * otherwise it returns 1.
5169 */
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005170static int hlua_txn_new(lua_State *L, struct stream *s, struct proxy *p, int dir, int flags)
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005171{
Willy Tarreaude491382015-04-06 11:04:28 +02005172 struct hlua_txn *htxn;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005173
5174 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005175 if (!lua_checkstack(L, 3))
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005176 return 0;
5177
5178 /* NOTE: The allocation never fails. The failure
5179 * throw an error, and the function never returns.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08005180 * if the throw is not available, the process is aborted.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005181 */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005182 /* Create the object: obj[0] = userdata. */
5183 lua_newtable(L);
Willy Tarreaude491382015-04-06 11:04:28 +02005184 htxn = lua_newuserdata(L, sizeof(*htxn));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005185 lua_rawseti(L, -2, 0);
5186
Willy Tarreaude491382015-04-06 11:04:28 +02005187 htxn->s = s;
5188 htxn->p = p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01005189 htxn->dir = dir;
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005190 htxn->flags = flags;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005191
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005192 /* Create the "f" field that contains a list of fetches. */
5193 lua_pushstring(L, "f");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005194 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005195 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005196 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005197
5198 /* Create the "sf" field that contains a list of stringsafe fetches. */
5199 lua_pushstring(L, "sf");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005200 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP | HLUA_F_AS_STRING))
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005201 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005202 lua_rawset(L, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005203
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005204 /* Create the "c" field that contains a list of converters. */
5205 lua_pushstring(L, "c");
Willy Tarreaude491382015-04-06 11:04:28 +02005206 if (!hlua_converters_new(L, htxn, 0))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005207 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005208 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005209
5210 /* Create the "sc" field that contains a list of stringsafe converters. */
5211 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01005212 if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING))
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005213 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005214 lua_rawset(L, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005215
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005216 /* Create the "req" field that contains the request channel object. */
5217 lua_pushstring(L, "req");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005218 if (!hlua_channel_new(L, &s->req))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005219 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005220 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005221
5222 /* Create the "res" field that contains the response channel object. */
5223 lua_pushstring(L, "res");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005224 if (!hlua_channel_new(L, &s->res))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005225 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005226 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005227
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005228 /* Creates the HTTP object is the current proxy allows http. */
5229 lua_pushstring(L, "http");
5230 if (p->mode == PR_MODE_HTTP) {
Willy Tarreaude491382015-04-06 11:04:28 +02005231 if (!hlua_http_new(L, htxn))
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005232 return 0;
5233 }
5234 else
5235 lua_pushnil(L);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005236 lua_rawset(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005237
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005238 /* Pop a class sesison metatable and affect it to the userdata. */
5239 lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref);
5240 lua_setmetatable(L, -2);
5241
5242 return 1;
5243}
5244
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005245__LJMP static int hlua_txn_deflog(lua_State *L)
5246{
5247 const char *msg;
5248 struct hlua_txn *htxn;
5249
5250 MAY_LJMP(check_args(L, 2, "deflog"));
5251 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5252 msg = MAY_LJMP(luaL_checkstring(L, 2));
5253
5254 hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg);
5255 return 0;
5256}
5257
5258__LJMP static int hlua_txn_log(lua_State *L)
5259{
5260 int level;
5261 const char *msg;
5262 struct hlua_txn *htxn;
5263
5264 MAY_LJMP(check_args(L, 3, "log"));
5265 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5266 level = MAY_LJMP(luaL_checkinteger(L, 2));
5267 msg = MAY_LJMP(luaL_checkstring(L, 3));
5268
5269 if (level < 0 || level >= NB_LOG_LEVELS)
5270 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
5271
5272 hlua_sendlog(htxn->s->be, level, msg);
5273 return 0;
5274}
5275
5276__LJMP static int hlua_txn_log_debug(lua_State *L)
5277{
5278 const char *msg;
5279 struct hlua_txn *htxn;
5280
5281 MAY_LJMP(check_args(L, 2, "Debug"));
5282 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5283 msg = MAY_LJMP(luaL_checkstring(L, 2));
5284 hlua_sendlog(htxn->s->be, LOG_DEBUG, msg);
5285 return 0;
5286}
5287
5288__LJMP static int hlua_txn_log_info(lua_State *L)
5289{
5290 const char *msg;
5291 struct hlua_txn *htxn;
5292
5293 MAY_LJMP(check_args(L, 2, "Info"));
5294 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5295 msg = MAY_LJMP(luaL_checkstring(L, 2));
5296 hlua_sendlog(htxn->s->be, LOG_INFO, msg);
5297 return 0;
5298}
5299
5300__LJMP static int hlua_txn_log_warning(lua_State *L)
5301{
5302 const char *msg;
5303 struct hlua_txn *htxn;
5304
5305 MAY_LJMP(check_args(L, 2, "Warning"));
5306 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5307 msg = MAY_LJMP(luaL_checkstring(L, 2));
5308 hlua_sendlog(htxn->s->be, LOG_WARNING, msg);
5309 return 0;
5310}
5311
5312__LJMP static int hlua_txn_log_alert(lua_State *L)
5313{
5314 const char *msg;
5315 struct hlua_txn *htxn;
5316
5317 MAY_LJMP(check_args(L, 2, "Alert"));
5318 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5319 msg = MAY_LJMP(luaL_checkstring(L, 2));
5320 hlua_sendlog(htxn->s->be, LOG_ALERT, msg);
5321 return 0;
5322}
5323
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005324__LJMP static int hlua_txn_set_loglevel(lua_State *L)
5325{
5326 struct hlua_txn *htxn;
5327 int ll;
5328
5329 MAY_LJMP(check_args(L, 2, "set_loglevel"));
5330 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5331 ll = MAY_LJMP(luaL_checkinteger(L, 2));
5332
5333 if (ll < 0 || ll > 7)
5334 WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7"));
5335
5336 htxn->s->logs.level = ll;
5337 return 0;
5338}
5339
5340__LJMP static int hlua_txn_set_tos(lua_State *L)
5341{
5342 struct hlua_txn *htxn;
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005343 int tos;
5344
5345 MAY_LJMP(check_args(L, 2, "set_tos"));
5346 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5347 tos = MAY_LJMP(luaL_checkinteger(L, 2));
5348
Willy Tarreau1a18b542018-12-11 16:37:42 +01005349 conn_set_tos(objt_conn(htxn->s->sess->origin), tos);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005350 return 0;
5351}
5352
5353__LJMP static int hlua_txn_set_mark(lua_State *L)
5354{
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005355 struct hlua_txn *htxn;
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005356 int mark;
5357
5358 MAY_LJMP(check_args(L, 2, "set_mark"));
5359 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5360 mark = MAY_LJMP(luaL_checkinteger(L, 2));
5361
Willy Tarreau1a18b542018-12-11 16:37:42 +01005362 conn_set_tos(objt_conn(htxn->s->sess->origin), mark);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005363 return 0;
5364}
5365
Patrick Hemmer268a7072018-05-11 12:52:31 -04005366__LJMP static int hlua_txn_set_priority_class(lua_State *L)
5367{
5368 struct hlua_txn *htxn;
5369
5370 MAY_LJMP(check_args(L, 2, "set_priority_class"));
5371 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5372 htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2)));
5373 return 0;
5374}
5375
5376__LJMP static int hlua_txn_set_priority_offset(lua_State *L)
5377{
5378 struct hlua_txn *htxn;
5379
5380 MAY_LJMP(check_args(L, 2, "set_priority_offset"));
5381 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5382 htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2)));
5383 return 0;
5384}
5385
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005386/* This function is an Lua binding that send pending data
5387 * to the client, and close the stream interface.
5388 */
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005389__LJMP static int hlua_txn_done(lua_State *L)
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005390{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005391 struct hlua_txn *htxn;
Thierry FOURNIER9bd52d42016-07-14 11:45:33 +02005392 struct hlua *hlua;
Willy Tarreau81389672015-03-10 12:03:52 +01005393 struct channel *ic, *oc;
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005394
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005395 MAY_LJMP(check_args(L, 1, "close"));
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005396 htxn = MAY_LJMP(hlua_checktxn(L, 1));
Thierry FOURNIER9bd52d42016-07-14 11:45:33 +02005397 hlua = hlua_gethlua(L);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005398
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005399 /* If the flags NOTERM is set, we cannot terminate the http
5400 * session, so we just end the execution of the current
5401 * lua code.
5402 */
5403 if (htxn->flags & HLUA_TXN_NOTERM) {
5404 WILL_LJMP(hlua_done(L));
5405 return 0;
5406 }
5407
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005408 ic = &htxn->s->req;
5409 oc = &htxn->s->res;
Willy Tarreau81389672015-03-10 12:03:52 +01005410
Christopher Faulet4d0e2632019-07-16 10:52:40 +02005411 if (IS_HTX_STRM(htxn->s))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005412 http_reply_and_close(htxn->s, 0, NULL);
Christopher Faulet4d0e2632019-07-16 10:52:40 +02005413 else {
Christopher Faulet4d0e2632019-07-16 10:52:40 +02005414 channel_auto_read(ic);
5415 channel_abort(ic);
5416 channel_auto_close(ic);
5417 channel_erase(ic);
Thierry FOURNIER10ec2142015-08-24 17:23:45 +02005418
Christopher Faulet4d0e2632019-07-16 10:52:40 +02005419 oc->wex = tick_add_ifset(now_ms, oc->wto);
5420 channel_auto_read(oc);
5421 channel_auto_close(oc);
5422 channel_shutr_now(oc);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005423
Christopher Faulet4d0e2632019-07-16 10:52:40 +02005424 ic->analysers = 0;
5425 }
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005426
Thierry FOURNIER9bd52d42016-07-14 11:45:33 +02005427 hlua->flags |= HLUA_STOP;
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005428 WILL_LJMP(hlua_done(L));
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005429 return 0;
5430}
5431
5432__LJMP static int hlua_log(lua_State *L)
5433{
5434 int level;
5435 const char *msg;
5436
5437 MAY_LJMP(check_args(L, 2, "log"));
5438 level = MAY_LJMP(luaL_checkinteger(L, 1));
5439 msg = MAY_LJMP(luaL_checkstring(L, 2));
5440
5441 if (level < 0 || level >= NB_LOG_LEVELS)
5442 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
5443
5444 hlua_sendlog(NULL, level, msg);
5445 return 0;
5446}
5447
5448__LJMP static int hlua_log_debug(lua_State *L)
5449{
5450 const char *msg;
5451
5452 MAY_LJMP(check_args(L, 1, "debug"));
5453 msg = MAY_LJMP(luaL_checkstring(L, 1));
5454 hlua_sendlog(NULL, LOG_DEBUG, msg);
5455 return 0;
5456}
5457
5458__LJMP static int hlua_log_info(lua_State *L)
5459{
5460 const char *msg;
5461
5462 MAY_LJMP(check_args(L, 1, "info"));
5463 msg = MAY_LJMP(luaL_checkstring(L, 1));
5464 hlua_sendlog(NULL, LOG_INFO, msg);
5465 return 0;
5466}
5467
5468__LJMP static int hlua_log_warning(lua_State *L)
5469{
5470 const char *msg;
5471
5472 MAY_LJMP(check_args(L, 1, "warning"));
5473 msg = MAY_LJMP(luaL_checkstring(L, 1));
5474 hlua_sendlog(NULL, LOG_WARNING, msg);
5475 return 0;
5476}
5477
5478__LJMP static int hlua_log_alert(lua_State *L)
5479{
5480 const char *msg;
5481
5482 MAY_LJMP(check_args(L, 1, "alert"));
5483 msg = MAY_LJMP(luaL_checkstring(L, 1));
5484 hlua_sendlog(NULL, LOG_ALERT, msg);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005485 return 0;
5486}
5487
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005488__LJMP static int hlua_sleep_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005489{
5490 int wakeup_ms = lua_tointeger(L, -1);
5491 if (now_ms < wakeup_ms)
Willy Tarreau9635e032018-10-16 17:52:55 +02005492 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005493 return 0;
5494}
5495
5496__LJMP static int hlua_sleep(lua_State *L)
5497{
5498 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005499 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005500
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005501 MAY_LJMP(check_args(L, 1, "sleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005502
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005503 delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005504 wakeup_ms = tick_add(now_ms, delay);
5505 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005506
Willy Tarreau9635e032018-10-16 17:52:55 +02005507 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005508 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005509}
5510
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005511__LJMP static int hlua_msleep(lua_State *L)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005512{
5513 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005514 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005515
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005516 MAY_LJMP(check_args(L, 1, "msleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005517
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005518 delay = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005519 wakeup_ms = tick_add(now_ms, delay);
5520 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005521
Willy Tarreau9635e032018-10-16 17:52:55 +02005522 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005523 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005524}
5525
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01005526/* This functionis an LUA binding. it permits to give back
5527 * the hand at the HAProxy scheduler. It is used when the
5528 * LUA processing consumes a lot of time.
5529 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005530__LJMP static int hlua_yield_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005531{
5532 return 0;
5533}
5534
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01005535__LJMP static int hlua_yield(lua_State *L)
5536{
Willy Tarreau9635e032018-10-16 17:52:55 +02005537 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_yield_yield, TICK_ETERNITY, HLUA_CTRLYIELD));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005538 return 0;
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01005539}
5540
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005541/* This function change the nice of the currently executed
5542 * task. It is used set low or high priority at the current
5543 * task.
5544 */
Willy Tarreau59551662015-03-10 14:23:13 +01005545__LJMP static int hlua_set_nice(lua_State *L)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005546{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005547 struct hlua *hlua;
5548 int nice;
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005549
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005550 MAY_LJMP(check_args(L, 1, "set_nice"));
5551 hlua = hlua_gethlua(L);
5552 nice = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005553
5554 /* If he task is not set, I'm in a start mode. */
5555 if (!hlua || !hlua->task)
5556 return 0;
5557
5558 if (nice < -1024)
5559 nice = -1024;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005560 else if (nice > 1024)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005561 nice = 1024;
5562
5563 hlua->task->nice = nice;
5564 return 0;
5565}
5566
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08005567/* This function is used as a callback of a task. It is called by the
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005568 * HAProxy task subsystem when the task is awaked. The LUA runtime can
5569 * return an E_AGAIN signal, the emmiter of this signal must set a
5570 * signal to wake the task.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005571 *
5572 * Task wrapper are longjmp safe because the only one Lua code
5573 * executed is the safe hlua_ctx_resume();
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005574 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02005575static struct task *hlua_process_task(struct task *task, void *context, unsigned short state)
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005576{
Olivier Houchard9f6af332018-05-25 14:04:04 +02005577 struct hlua *hlua = context;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005578 enum hlua_exec status;
5579
Christopher Faulet5bc99722018-04-25 10:34:45 +02005580 if (task->thread_mask == MAX_THREADS_MASK)
5581 task_set_affinity(task, tid_bit);
5582
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005583 /* If it is the first call to the task, we must initialize the
5584 * execution timeouts.
5585 */
5586 if (!HLUA_IS_RUNNING(hlua))
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02005587 hlua->max_time = hlua_timeout_task;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005588
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005589 /* Execute the Lua code. */
5590 status = hlua_ctx_resume(hlua, 1);
5591
5592 switch (status) {
5593 /* finished or yield */
5594 case HLUA_E_OK:
5595 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02005596 task_destroy(task);
Tim Duesterhuscd235c62018-04-24 13:56:01 +02005597 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005598 break;
5599
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01005600 case HLUA_E_AGAIN: /* co process or timeout wake me later. */
Thierry FOURNIERcb146882017-12-10 17:10:57 +01005601 notification_gc(&hlua->com);
PiBa-NLfe971b32018-05-02 22:27:14 +02005602 task->expire = hlua->wake_time;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005603 break;
5604
5605 /* finished with error. */
5606 case HLUA_E_ERRMSG:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005607 SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005608 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02005609 task_destroy(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02005610 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005611 break;
5612
5613 case HLUA_E_ERR:
5614 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005615 SEND_ERR(NULL, "Lua task: unknown error.\n");
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005616 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02005617 task_destroy(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02005618 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005619 break;
5620 }
Emeric Brun253e53e2017-10-17 18:58:40 +02005621 return task;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005622}
5623
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01005624/* This function is an LUA binding that register LUA function to be
5625 * executed after the HAProxy configuration parsing and before the
5626 * HAProxy scheduler starts. This function expect only one LUA
5627 * argument that is a function. This function returns nothing, but
5628 * throws if an error is encountered.
5629 */
5630__LJMP static int hlua_register_init(lua_State *L)
5631{
5632 struct hlua_init_function *init;
5633 int ref;
5634
5635 MAY_LJMP(check_args(L, 1, "register_init"));
5636
5637 ref = MAY_LJMP(hlua_checkfunction(L, 1));
5638
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02005639 init = calloc(1, sizeof(*init));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01005640 if (!init)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01005641 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01005642
5643 init->function_ref = ref;
5644 LIST_ADDQ(&hlua_init_functions, &init->l);
5645 return 0;
5646}
5647
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005648/* This functio is an LUA binding. It permits to register a task
5649 * executed in parallel of the main HAroxy activity. The task is
5650 * created and it is set in the HAProxy scheduler. It can be called
5651 * from the "init" section, "post init" or during the runtime.
5652 *
5653 * Lua prototype:
5654 *
5655 * <none> core.register_task(<function>)
5656 */
5657static int hlua_register_task(lua_State *L)
5658{
5659 struct hlua *hlua;
5660 struct task *task;
5661 int ref;
5662
5663 MAY_LJMP(check_args(L, 1, "register_task"));
5664
5665 ref = MAY_LJMP(hlua_checkfunction(L, 1));
5666
Willy Tarreaubafbe012017-11-24 17:34:44 +01005667 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005668 if (!hlua)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01005669 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005670
Emeric Brunc60def82017-09-27 14:59:38 +02005671 task = task_new(MAX_THREADS_MASK);
Willy Tarreaue09101e2018-10-16 17:37:12 +02005672 if (!task)
5673 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
5674
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005675 task->context = hlua;
5676 task->process = hlua_process_task;
5677
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01005678 if (!hlua_ctx_init(hlua, task, 1))
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01005679 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005680
5681 /* Restore the function in the stack. */
5682 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref);
5683 hlua->nargs = 0;
5684
5685 /* Schedule task. */
5686 task_schedule(task, now_ms);
5687
5688 return 0;
5689}
5690
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005691/* Wrapper called by HAProxy to execute an LUA converter. This wrapper
5692 * doesn't allow "yield" functions because the HAProxy engine cannot
5693 * resume converters.
5694 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02005695static int hlua_sample_conv_wrapper(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005696{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02005697 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02005698 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01005699 const char *error;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005700
Willy Tarreaube508f12016-03-10 11:47:01 +01005701 if (!stream)
5702 return 0;
5703
Willy Tarreau87b09662015-04-03 00:22:06 +02005704 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01005705 * Lua context can be not initialized. This behavior
5706 * permits to save performances because a systematic
5707 * Lua initialization cause 5% performances loss.
5708 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005709 if (!stream->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01005710 stream->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005711 if (!stream->hlua) {
5712 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
5713 return 0;
5714 }
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01005715 if (!hlua_ctx_init(stream->hlua, stream->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005716 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
5717 return 0;
5718 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01005719 }
5720
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005721 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005722 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005723
5724 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005725 if (!SET_SAFE_LJMP(stream->hlua->T)) {
5726 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
5727 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01005728 else
5729 error = "critical error";
5730 SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005731 return 0;
5732 }
5733
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005734 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005735 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005736 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005737 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005738 return 0;
5739 }
5740
5741 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005742 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005743
5744 /* convert input sample and pust-it in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005745 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005746 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005747 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005748 return 0;
5749 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005750 hlua_smp2lua(stream->hlua->T, smp);
5751 stream->hlua->nargs = 1;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005752
5753 /* push keywords in the stack. */
5754 if (arg_p) {
5755 for (; arg_p->type != ARGT_STOP; arg_p++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005756 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005757 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005758 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005759 return 0;
5760 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005761 hlua_arg2lua(stream->hlua->T, arg_p);
5762 stream->hlua->nargs++;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005763 }
5764 }
5765
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005766 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005767 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005768
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005769 /* At this point the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005770 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005771 }
5772
5773 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005774 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005775 /* finished. */
5776 case HLUA_E_OK:
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02005777 /* If the stack is empty, the function fails. */
5778 if (lua_gettop(stream->hlua->T) <= 0)
5779 return 0;
5780
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005781 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005782 hlua_lua2smp(stream->hlua->T, -1, smp);
5783 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005784 return 1;
5785
5786 /* yield. */
5787 case HLUA_E_AGAIN:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005788 SEND_ERR(stream->be, "Lua converter '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005789 return 0;
5790
5791 /* finished with error. */
5792 case HLUA_E_ERRMSG:
5793 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005794 SEND_ERR(stream->be, "Lua converter '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005795 fcn->name, lua_tostring(stream->hlua->T, -1));
5796 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005797 return 0;
5798
Thierry Fournierd5b073c2018-05-21 19:42:47 +02005799 case HLUA_E_ETMOUT:
5800 SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name);
5801 return 0;
5802
5803 case HLUA_E_NOMEM:
5804 SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name);
5805 return 0;
5806
5807 case HLUA_E_YIELD:
5808 SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name);
5809 return 0;
5810
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005811 case HLUA_E_ERR:
5812 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005813 SEND_ERR(stream->be, "Lua converter '%s' returns an unknown error.\n", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005814
5815 default:
5816 return 0;
5817 }
5818}
5819
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005820/* Wrapper called by HAProxy to execute a sample-fetch. this wrapper
5821 * doesn't allow "yield" functions because the HAProxy engine cannot
Willy Tarreaube508f12016-03-10 11:47:01 +01005822 * resume sample-fetches. This function will be called by the sample
5823 * fetch engine to call lua-based fetch operations.
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005824 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +02005825static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp,
5826 const char *kw, void *private)
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005827{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02005828 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02005829 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01005830 const char *error;
Willy Tarreau83061a82018-07-13 11:56:34 +02005831 const struct buffer msg = { };
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005832
Willy Tarreaube508f12016-03-10 11:47:01 +01005833 if (!stream)
5834 return 0;
Christopher Fauletafd8f102018-11-08 11:34:21 +01005835
Willy Tarreau87b09662015-04-03 00:22:06 +02005836 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01005837 * Lua context can be not initialized. This behavior
5838 * permits to save performances because a systematic
5839 * Lua initialization cause 5% performances loss.
5840 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005841 if (!stream->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01005842 stream->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005843 if (!stream->hlua) {
5844 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
5845 return 0;
5846 }
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01005847 if (!hlua_ctx_init(stream->hlua, stream->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005848 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
5849 return 0;
5850 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01005851 }
5852
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005853 consistency_set(stream, smp->opt, &stream->hlua->cons);
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005854
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005855 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005856 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005857
5858 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005859 if (!SET_SAFE_LJMP(stream->hlua->T)) {
5860 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
5861 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01005862 else
5863 error = "critical error";
5864 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005865 return 0;
5866 }
5867
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005868 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005869 if (!lua_checkstack(stream->hlua->T, 2)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005870 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005871 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005872 return 0;
5873 }
5874
5875 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005876 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005877
5878 /* push arguments in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005879 if (!hlua_txn_new(stream->hlua->T, stream, smp->px, smp->opt & SMP_OPT_DIR,
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005880 HLUA_TXN_NOTERM)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005881 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005882 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005883 return 0;
5884 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005885 stream->hlua->nargs = 1;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005886
5887 /* push keywords in the stack. */
5888 for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) {
5889 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005890 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005891 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005892 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005893 return 0;
5894 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005895 hlua_arg2lua(stream->hlua->T, arg_p);
5896 stream->hlua->nargs++;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005897 }
5898
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005899 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005900 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005901
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005902 /* At this point the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005903 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005904 }
5905
5906 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005907 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005908 /* finished. */
5909 case HLUA_E_OK:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005910 if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) {
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01005911 si_retnclose(&stream->si[0], &msg);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02005912 return 0;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005913 }
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02005914 /* If the stack is empty, the function fails. */
5915 if (lua_gettop(stream->hlua->T) <= 0)
5916 return 0;
5917
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005918 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005919 hlua_lua2smp(stream->hlua->T, -1, smp);
5920 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005921
5922 /* Set the end of execution flag. */
5923 smp->flags &= ~SMP_F_MAY_CHANGE;
5924 return 1;
5925
5926 /* yield. */
5927 case HLUA_E_AGAIN:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005928 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01005929 si_retnclose(&stream->si[0], &msg);
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005930 SEND_ERR(smp->px, "Lua sample-fetch '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005931 return 0;
5932
5933 /* finished with error. */
5934 case HLUA_E_ERRMSG:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005935 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01005936 si_retnclose(&stream->si[0], &msg);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005937 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005938 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005939 fcn->name, lua_tostring(stream->hlua->T, -1));
5940 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005941 return 0;
5942
Thierry Fournierd5b073c2018-05-21 19:42:47 +02005943 case HLUA_E_ETMOUT:
5944 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01005945 si_retnclose(&stream->si[0], &msg);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02005946 SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name);
5947 return 0;
5948
5949 case HLUA_E_NOMEM:
5950 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01005951 si_retnclose(&stream->si[0], &msg);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02005952 SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name);
5953 return 0;
5954
5955 case HLUA_E_YIELD:
5956 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01005957 si_retnclose(&stream->si[0], &msg);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02005958 SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name);
5959 return 0;
5960
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005961 case HLUA_E_ERR:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005962 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01005963 si_retnclose(&stream->si[0], &msg);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005964 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005965 SEND_ERR(smp->px, "Lua sample-fetch '%s' returns an unknown error.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005966
5967 default:
5968 return 0;
5969 }
5970}
5971
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005972/* This function is an LUA binding used for registering
5973 * "sample-conv" functions. It expects a converter name used
5974 * in the haproxy configuration file, and an LUA function.
5975 */
5976__LJMP static int hlua_register_converters(lua_State *L)
5977{
5978 struct sample_conv_kw_list *sck;
5979 const char *name;
5980 int ref;
5981 int len;
5982 struct hlua_function *fcn;
5983
5984 MAY_LJMP(check_args(L, 2, "register_converters"));
5985
5986 /* First argument : converter name. */
5987 name = MAY_LJMP(luaL_checkstring(L, 1));
5988
5989 /* Second argument : lua function. */
5990 ref = MAY_LJMP(hlua_checkfunction(L, 2));
5991
5992 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02005993 sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005994 if (!sck)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01005995 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02005996 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005997 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01005998 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005999
6000 /* Fill fcn. */
6001 fcn->name = strdup(name);
6002 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006003 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006004 fcn->function_ref = ref;
6005
6006 /* List head */
6007 sck->list.n = sck->list.p = NULL;
6008
6009 /* converter keyword. */
6010 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006011 sck->kw[0].kw = calloc(1, len);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006012 if (!sck->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006013 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006014
6015 snprintf((char *)sck->kw[0].kw, len, "lua.%s", name);
6016 sck->kw[0].process = hlua_sample_conv_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01006017 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 +01006018 sck->kw[0].val_args = NULL;
6019 sck->kw[0].in_type = SMP_T_STR;
6020 sck->kw[0].out_type = SMP_T_STR;
6021 sck->kw[0].private = fcn;
6022
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006023 /* Register this new converter */
6024 sample_register_convs(sck);
6025
6026 return 0;
6027}
6028
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006029/* This function is an LUA binding used for registering
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006030 * "sample-fetch" functions. It expects a converter name used
6031 * in the haproxy configuration file, and an LUA function.
6032 */
6033__LJMP static int hlua_register_fetches(lua_State *L)
6034{
6035 const char *name;
6036 int ref;
6037 int len;
6038 struct sample_fetch_kw_list *sfk;
6039 struct hlua_function *fcn;
6040
6041 MAY_LJMP(check_args(L, 2, "register_fetches"));
6042
6043 /* First argument : sample-fetch name. */
6044 name = MAY_LJMP(luaL_checkstring(L, 1));
6045
6046 /* Second argument : lua function. */
6047 ref = MAY_LJMP(hlua_checkfunction(L, 2));
6048
6049 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006050 sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006051 if (!sfk)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006052 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006053 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006054 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006055 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006056
6057 /* Fill fcn. */
6058 fcn->name = strdup(name);
6059 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006060 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006061 fcn->function_ref = ref;
6062
6063 /* List head */
6064 sfk->list.n = sfk->list.p = NULL;
6065
6066 /* sample-fetch keyword. */
6067 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006068 sfk->kw[0].kw = calloc(1, len);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006069 if (!sfk->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006070 return luaL_error(L, "Lua out of memory error.");
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006071
6072 snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name);
6073 sfk->kw[0].process = hlua_sample_fetch_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01006074 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 +01006075 sfk->kw[0].val_args = NULL;
6076 sfk->kw[0].out_type = SMP_T_STR;
6077 sfk->kw[0].use = SMP_USE_HTTP_ANY;
6078 sfk->kw[0].val = 0;
6079 sfk->kw[0].private = fcn;
6080
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006081 /* Register this new fetch. */
6082 sample_register_fetches(sfk);
6083
6084 return 0;
6085}
6086
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006087/* This function is a wrapper to execute each LUA function declared
6088 * as an action wrapper during the initialisation period. This function
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006089 * return ACT_RET_CONT if the processing is finished (with or without
6090 * error) and return ACT_RET_YIELD if the function must be called again
6091 * because the LUA returns a yield.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006092 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006093static enum act_return hlua_action(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +02006094 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006095{
6096 char **arg;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006097 unsigned int analyzer;
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02006098 int dir;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006099 const char *error;
Willy Tarreau83061a82018-07-13 11:56:34 +02006100 const struct buffer msg = { };
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006101
6102 switch (rule->from) {
Thierry FOURNIER6e01f382015-11-02 09:52:54 +01006103 case ACT_F_TCP_REQ_CNT: analyzer = AN_REQ_INSPECT_FE ; dir = SMP_OPT_DIR_REQ; break;
6104 case ACT_F_TCP_RES_CNT: analyzer = AN_RES_INSPECT ; dir = SMP_OPT_DIR_RES; break;
6105 case ACT_F_HTTP_REQ: analyzer = AN_REQ_HTTP_PROCESS_FE; dir = SMP_OPT_DIR_REQ; break;
6106 case ACT_F_HTTP_RES: analyzer = AN_RES_HTTP_PROCESS_BE; dir = SMP_OPT_DIR_RES; break;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006107 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006108 SEND_ERR(px, "Lua: internal error while execute action.\n");
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006109 return ACT_RET_CONT;
6110 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006111
Willy Tarreau87b09662015-04-03 00:22:06 +02006112 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006113 * Lua context can be not initialized. This behavior
6114 * permits to save performances because a systematic
6115 * Lua initialization cause 5% performances loss.
6116 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006117 if (!s->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006118 s->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006119 if (!s->hlua) {
6120 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
6121 rule->arg.hlua_rule->fcn.name);
6122 return ACT_RET_CONT;
6123 }
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006124 if (!hlua_ctx_init(s->hlua, s->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006125 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
6126 rule->arg.hlua_rule->fcn.name);
6127 return ACT_RET_CONT;
6128 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006129 }
6130
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006131 consistency_set(s, dir, &s->hlua->cons);
6132
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006133 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006134 if (!HLUA_IS_RUNNING(s->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006135
6136 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006137 if (!SET_SAFE_LJMP(s->hlua->T)) {
6138 if (lua_type(s->hlua->T, -1) == LUA_TSTRING)
6139 error = lua_tostring(s->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006140 else
6141 error = "critical error";
6142 SEND_ERR(px, "Lua function '%s': %s.\n",
6143 rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006144 return ACT_RET_CONT;
6145 }
6146
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006147 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006148 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006149 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006150 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006151 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006152 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006153 }
6154
6155 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006156 lua_rawgeti(s->hlua->T, LUA_REGISTRYINDEX, rule->arg.hlua_rule->fcn.function_ref);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006157
Willy Tarreau87b09662015-04-03 00:22:06 +02006158 /* Create and and push object stream in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006159 if (!hlua_txn_new(s->hlua->T, s, px, dir, 0)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006160 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006161 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006162 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006163 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006164 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006165 s->hlua->nargs = 1;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006166
6167 /* push keywords in the stack. */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006168 for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006169 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006170 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006171 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006172 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006173 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006174 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006175 lua_pushstring(s->hlua->T, *arg);
6176 s->hlua->nargs++;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006177 }
6178
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006179 /* Now the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006180 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006181
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006182 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006183 s->hlua->max_time = hlua_timeout_session;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006184 }
6185
6186 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006187 switch (hlua_ctx_resume(s->hlua, !(flags & ACT_FLAG_FINAL))) {
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006188 /* finished. */
6189 case HLUA_E_OK:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006190 if (!consistency_check(s, dir, &s->hlua->cons)) {
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006191 si_retnclose(&s->si[0], &msg);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02006192 return ACT_RET_ERR;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006193 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006194 if (s->hlua->flags & HLUA_STOP)
Christopher Faulet8f1aa772019-07-04 11:27:15 +02006195 return ACT_RET_DONE;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006196 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006197
6198 /* yield. */
6199 case HLUA_E_AGAIN:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006200 /* Set timeout in the required channel. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006201 if (s->hlua->wake_time != TICK_ETERNITY) {
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006202 if (analyzer & (AN_REQ_INSPECT_FE|AN_REQ_HTTP_PROCESS_FE))
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006203 s->req.analyse_exp = s->hlua->wake_time;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006204 else if (analyzer & (AN_RES_INSPECT|AN_RES_HTTP_PROCESS_BE))
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006205 s->res.analyse_exp = s->hlua->wake_time;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006206 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006207 /* Some actions can be wake up when a "write" event
6208 * is detected on a response channel. This is useful
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006209 * only for actions targeted on the requests.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006210 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006211 if (HLUA_IS_WAKERESWR(s->hlua)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006212 s->res.flags |= CF_WAKE_WRITE;
Willy Tarreau76bd97f2015-03-10 17:16:10 +01006213 if ((analyzer & (AN_REQ_INSPECT_FE|AN_REQ_HTTP_PROCESS_FE)))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006214 s->res.analysers |= analyzer;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006215 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006216 if (HLUA_IS_WAKEREQWR(s->hlua))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006217 s->req.flags |= CF_WAKE_WRITE;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006218 /* We can quit the function without consistency check
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006219 * because HAProxy is not able to manipulate data, it
6220 * is only allowed to call me again. */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006221 return ACT_RET_YIELD;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006222
6223 /* finished with error. */
6224 case HLUA_E_ERRMSG:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006225 if (!consistency_check(s, dir, &s->hlua->cons)) {
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006226 si_retnclose(&s->si[0], &msg);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02006227 return ACT_RET_ERR;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006228 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006229 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006230 SEND_ERR(px, "Lua function '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006231 rule->arg.hlua_rule->fcn.name, lua_tostring(s->hlua->T, -1));
6232 lua_pop(s->hlua->T, 1);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006233 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006234
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006235 case HLUA_E_ETMOUT:
6236 if (!consistency_check(s, dir, &s->hlua->cons)) {
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006237 si_retnclose(&s->si[0], &msg);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006238 return ACT_RET_ERR;
6239 }
6240 SEND_ERR(px, "Lua function '%s': execution timeout.\n", rule->arg.hlua_rule->fcn.name);
6241 return 0;
6242
6243 case HLUA_E_NOMEM:
6244 if (!consistency_check(s, dir, &s->hlua->cons)) {
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006245 si_retnclose(&s->si[0], &msg);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006246 return ACT_RET_ERR;
6247 }
6248 SEND_ERR(px, "Lua function '%s': out of memory error.\n", rule->arg.hlua_rule->fcn.name);
6249 return 0;
6250
6251 case HLUA_E_YIELD:
6252 if (!consistency_check(s, dir, &s->hlua->cons)) {
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006253 si_retnclose(&s->si[0], &msg);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006254 return ACT_RET_ERR;
6255 }
6256 SEND_ERR(px, "Lua function '%s': aborting Lua processing on expired timeout.\n",
6257 rule->arg.hlua_rule->fcn.name);
6258 return 0;
6259
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006260 case HLUA_E_ERR:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006261 if (!consistency_check(s, dir, &s->hlua->cons)) {
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006262 si_retnclose(&s->si[0], &msg);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02006263 return ACT_RET_ERR;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006264 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006265 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006266 SEND_ERR(px, "Lua function '%s' return an unknown error.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006267 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006268
6269 default:
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006270 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006271 }
6272}
6273
Olivier Houchard9f6af332018-05-25 14:04:04 +02006274struct task *hlua_applet_wakeup(struct task *t, void *context, unsigned short state)
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006275{
Olivier Houchard9f6af332018-05-25 14:04:04 +02006276 struct appctx *ctx = context;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006277
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006278 appctx_wakeup(ctx);
Willy Tarreaud9587412017-08-23 16:07:33 +02006279 t->expire = TICK_ETERNITY;
Willy Tarreaud1aa41f2017-07-21 16:41:56 +02006280 return t;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006281}
6282
6283static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
6284{
6285 struct stream_interface *si = ctx->owner;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006286 struct hlua *hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006287 struct task *task;
6288 char **arg;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006289 const char *error;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006290
Willy Tarreaubafbe012017-11-24 17:34:44 +01006291 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006292 if (!hlua) {
6293 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
6294 ctx->rule->arg.hlua_rule->fcn.name);
6295 return 0;
6296 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006297 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006298 ctx->ctx.hlua_apptcp.hlua = hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006299 ctx->ctx.hlua_apptcp.flags = 0;
6300
6301 /* Create task used by signal to wakeup applets. */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01006302 task = task_new(tid_bit);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006303 if (!task) {
6304 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
6305 ctx->rule->arg.hlua_rule->fcn.name);
6306 return 0;
6307 }
6308 task->nice = 0;
6309 task->context = ctx;
6310 task->process = hlua_applet_wakeup;
6311 ctx->ctx.hlua_apptcp.task = task;
6312
6313 /* In the execution wrappers linked with a stream, the
6314 * Lua context can be not initialized. This behavior
6315 * permits to save performances because a systematic
6316 * Lua initialization cause 5% performances loss.
6317 */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006318 if (!hlua_ctx_init(hlua, task, 0)) {
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006319 SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n",
6320 ctx->rule->arg.hlua_rule->fcn.name);
6321 return 0;
6322 }
6323
6324 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02006325 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006326
6327 /* The following Lua calls can fail. */
6328 if (!SET_SAFE_LJMP(hlua->T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01006329 if (lua_type(hlua->T, -1) == LUA_TSTRING)
6330 error = lua_tostring(hlua->T, -1);
6331 else
6332 error = "critical error";
6333 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
6334 ctx->rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006335 return 0;
6336 }
6337
6338 /* Check stack available size. */
6339 if (!lua_checkstack(hlua->T, 1)) {
6340 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
6341 ctx->rule->arg.hlua_rule->fcn.name);
6342 RESET_SAFE_LJMP(hlua->T);
6343 return 0;
6344 }
6345
6346 /* Restore the function in the stack. */
6347 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref);
6348
6349 /* Create and and push object stream in the stack. */
6350 if (!hlua_applet_tcp_new(hlua->T, ctx)) {
6351 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
6352 ctx->rule->arg.hlua_rule->fcn.name);
6353 RESET_SAFE_LJMP(hlua->T);
6354 return 0;
6355 }
6356 hlua->nargs = 1;
6357
6358 /* push keywords in the stack. */
6359 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
6360 if (!lua_checkstack(hlua->T, 1)) {
6361 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
6362 ctx->rule->arg.hlua_rule->fcn.name);
6363 RESET_SAFE_LJMP(hlua->T);
6364 return 0;
6365 }
6366 lua_pushstring(hlua->T, *arg);
6367 hlua->nargs++;
6368 }
6369
6370 RESET_SAFE_LJMP(hlua->T);
6371
6372 /* Wakeup the applet ASAP. */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01006373 si_cant_get(si);
Willy Tarreau3367d412018-11-15 10:57:41 +01006374 si_rx_endp_more(si);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006375
6376 return 1;
6377}
6378
6379static void hlua_applet_tcp_fct(struct appctx *ctx)
6380{
6381 struct stream_interface *si = ctx->owner;
6382 struct stream *strm = si_strm(si);
6383 struct channel *res = si_ic(si);
6384 struct act_rule *rule = ctx->rule;
6385 struct proxy *px = strm->be;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006386 struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006387
6388 /* The applet execution is already done. */
Olivier Houchard594c8c52018-08-28 14:41:31 +02006389 if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) {
6390 /* eat the whole request */
6391 co_skip(si_oc(si), co_data(si_oc(si)));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006392 return;
Olivier Houchard594c8c52018-08-28 14:41:31 +02006393 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006394
6395 /* If the stream is disconnect or closed, ldo nothing. */
6396 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
6397 return;
6398
6399 /* Execute the function. */
6400 switch (hlua_ctx_resume(hlua, 1)) {
6401 /* finished. */
6402 case HLUA_E_OK:
6403 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
6404
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006405 /* eat the whole request */
Willy Tarreaua79021a2018-06-15 18:07:57 +02006406 co_skip(si_oc(si), co_data(si_oc(si)));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006407 res->flags |= CF_READ_NULL;
6408 si_shutr(si);
6409 return;
6410
6411 /* yield. */
6412 case HLUA_E_AGAIN:
Thierry Fournier0164f202016-02-20 17:47:43 +01006413 if (hlua->wake_time != TICK_ETERNITY)
6414 task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006415 return;
6416
6417 /* finished with error. */
6418 case HLUA_E_ERRMSG:
6419 /* Display log. */
6420 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
6421 rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1));
6422 lua_pop(hlua->T, 1);
6423 goto error;
6424
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006425 case HLUA_E_ETMOUT:
6426 SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n",
6427 rule->arg.hlua_rule->fcn.name);
6428 goto error;
6429
6430 case HLUA_E_NOMEM:
6431 SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n",
6432 rule->arg.hlua_rule->fcn.name);
6433 goto error;
6434
6435 case HLUA_E_YIELD: /* unexpected */
6436 SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n",
6437 rule->arg.hlua_rule->fcn.name);
6438 goto error;
6439
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006440 case HLUA_E_ERR:
6441 /* Display log. */
6442 SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n",
6443 rule->arg.hlua_rule->fcn.name);
6444 goto error;
6445
6446 default:
6447 goto error;
6448 }
6449
6450error:
6451
6452 /* For all other cases, just close the stream. */
6453 si_shutw(si);
6454 si_shutr(si);
6455 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
6456}
6457
6458static void hlua_applet_tcp_release(struct appctx *ctx)
6459{
Olivier Houchard3f795f72019-04-17 22:51:06 +02006460 task_destroy(ctx->ctx.hlua_apptcp.task);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006461 ctx->ctx.hlua_apptcp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006462 hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006463 ctx->ctx.hlua_apptcp.hlua = NULL;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006464}
6465
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006466/* The function returns 1 if the initialisation is complete, 0 if
6467 * an errors occurs and -1 if more data are required for initializing
6468 * the applet.
6469 */
6470static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
6471{
6472 struct stream_interface *si = ctx->owner;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006473 struct http_txn *txn;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006474 struct hlua *hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006475 char **arg;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006476 struct task *task;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006477 const char *error;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006478
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006479 txn = strm->txn;
Willy Tarreaubafbe012017-11-24 17:34:44 +01006480 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006481 if (!hlua) {
6482 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
6483 ctx->rule->arg.hlua_rule->fcn.name);
6484 return 0;
6485 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006486 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006487 ctx->ctx.hlua_apphttp.hlua = hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006488 ctx->ctx.hlua_apphttp.left_bytes = -1;
6489 ctx->ctx.hlua_apphttp.flags = 0;
6490
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +01006491 if (txn->req.flags & HTTP_MSGF_VER_11)
6492 ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11;
6493
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006494 /* Create task used by signal to wakeup applets. */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01006495 task = task_new(tid_bit);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006496 if (!task) {
6497 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
6498 ctx->rule->arg.hlua_rule->fcn.name);
6499 return 0;
6500 }
6501 task->nice = 0;
6502 task->context = ctx;
6503 task->process = hlua_applet_wakeup;
6504 ctx->ctx.hlua_apphttp.task = task;
6505
6506 /* In the execution wrappers linked with a stream, the
6507 * Lua context can be not initialized. This behavior
6508 * permits to save performances because a systematic
6509 * Lua initialization cause 5% performances loss.
6510 */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006511 if (!hlua_ctx_init(hlua, task, 0)) {
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006512 SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n",
6513 ctx->rule->arg.hlua_rule->fcn.name);
6514 return 0;
6515 }
6516
6517 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02006518 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006519
6520 /* The following Lua calls can fail. */
6521 if (!SET_SAFE_LJMP(hlua->T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01006522 if (lua_type(hlua->T, -1) == LUA_TSTRING)
6523 error = lua_tostring(hlua->T, -1);
6524 else
6525 error = "critical error";
6526 SEND_ERR(px, "Lua applet http '%s': %s.\n",
6527 ctx->rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006528 return 0;
6529 }
6530
6531 /* Check stack available size. */
6532 if (!lua_checkstack(hlua->T, 1)) {
6533 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
6534 ctx->rule->arg.hlua_rule->fcn.name);
6535 RESET_SAFE_LJMP(hlua->T);
6536 return 0;
6537 }
6538
6539 /* Restore the function in the stack. */
6540 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref);
6541
6542 /* Create and and push object stream in the stack. */
6543 if (!hlua_applet_http_new(hlua->T, ctx)) {
6544 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
6545 ctx->rule->arg.hlua_rule->fcn.name);
6546 RESET_SAFE_LJMP(hlua->T);
6547 return 0;
6548 }
6549 hlua->nargs = 1;
6550
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006551 /* push keywords in the stack. */
6552 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
6553 if (!lua_checkstack(hlua->T, 1)) {
6554 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
6555 ctx->rule->arg.hlua_rule->fcn.name);
6556 RESET_SAFE_LJMP(hlua->T);
6557 return 0;
6558 }
6559 lua_pushstring(hlua->T, *arg);
6560 hlua->nargs++;
6561 }
6562
6563 RESET_SAFE_LJMP(hlua->T);
6564
6565 /* Wakeup the applet when data is ready for read. */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01006566 si_cant_get(si);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006567
6568 return 1;
6569}
6570
Christopher Fauleta2097962019-07-15 16:25:33 +02006571static void hlua_applet_http_fct(struct appctx *ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01006572{
6573 struct stream_interface *si = ctx->owner;
6574 struct stream *strm = si_strm(si);
6575 struct channel *req = si_oc(si);
6576 struct channel *res = si_ic(si);
6577 struct act_rule *rule = ctx->rule;
6578 struct proxy *px = strm->be;
6579 struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua;
6580 struct htx *req_htx, *res_htx;
6581
6582 res_htx = htx_from_buf(&res->buf);
6583
6584 /* If the stream is disconnect or closed, ldo nothing. */
6585 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
6586 goto out;
6587
6588 /* Check if the input buffer is avalaible. */
6589 if (!b_size(&res->buf)) {
6590 si_rx_room_blk(si);
6591 goto out;
6592 }
6593 /* check that the output is not closed */
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01006594 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
Christopher Faulet9c832fc2018-12-14 13:34:05 +01006595 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
6596
6597 /* Set the currently running flag. */
6598 if (!HLUA_IS_RUNNING(hlua) &&
6599 !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
6600 struct htx_blk *blk;
6601 size_t count = co_data(req);
6602
6603 if (!count) {
6604 si_cant_get(si);
6605 goto out;
6606 }
6607
6608 /* We need to flush the request header. This left the body for
6609 * the Lua.
6610 */
6611 req_htx = htx_from_buf(&req->buf);
Christopher Fauleta3f15502019-05-13 15:27:23 +02006612 blk = htx_get_first_blk(req_htx);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01006613 while (count && blk) {
6614 enum htx_blk_type type = htx_get_blk_type(blk);
6615 uint32_t sz = htx_get_blksz(blk);
6616
6617 if (sz > count) {
6618 si_cant_get(si);
Christopher Faulet0ae79d02019-02-27 21:36:59 +01006619 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01006620 goto out;
6621 }
6622
6623 count -= sz;
6624 co_set_data(req, co_data(req) - sz);
6625 blk = htx_remove_blk(req_htx, blk);
6626
6627 if (type == HTX_BLK_EOH)
6628 break;
6629 }
Christopher Faulet0ae79d02019-02-27 21:36:59 +01006630 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01006631 }
6632
6633 /* Executes The applet if it is not done. */
6634 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
6635
6636 /* Execute the function. */
6637 switch (hlua_ctx_resume(hlua, 1)) {
6638 /* finished. */
6639 case HLUA_E_OK:
6640 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
6641 break;
6642
6643 /* yield. */
6644 case HLUA_E_AGAIN:
6645 if (hlua->wake_time != TICK_ETERNITY)
6646 task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time);
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01006647 goto out;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01006648
6649 /* finished with error. */
6650 case HLUA_E_ERRMSG:
6651 /* Display log. */
6652 SEND_ERR(px, "Lua applet http '%s': %s.\n",
6653 rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1));
6654 lua_pop(hlua->T, 1);
6655 goto error;
6656
6657 case HLUA_E_ETMOUT:
6658 SEND_ERR(px, "Lua applet http '%s': execution timeout.\n",
6659 rule->arg.hlua_rule->fcn.name);
6660 goto error;
6661
6662 case HLUA_E_NOMEM:
6663 SEND_ERR(px, "Lua applet http '%s': out of memory error.\n",
6664 rule->arg.hlua_rule->fcn.name);
6665 goto error;
6666
6667 case HLUA_E_YIELD: /* unexpected */
6668 SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n",
6669 rule->arg.hlua_rule->fcn.name);
6670 goto error;
6671
6672 case HLUA_E_ERR:
6673 /* Display log. */
6674 SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n",
6675 rule->arg.hlua_rule->fcn.name);
6676 goto error;
6677
6678 default:
6679 goto error;
6680 }
6681 }
6682
6683 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01006684 if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT)
6685 goto done;
6686
Christopher Faulet9c832fc2018-12-14 13:34:05 +01006687 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT))
6688 goto error;
6689
Christopher Faulet54b5e212019-06-04 10:08:28 +02006690 /* Don't add TLR because mux-h1 will take care of it */
Christopher Faulet9c832fc2018-12-14 13:34:05 +01006691 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
6692 si_rx_room_blk(si);
6693 goto out;
6694 }
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01006695 channel_add_input(res, 1);
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01006696 strm->txn->status = ctx->ctx.hlua_apphttp.status;
6697 ctx->ctx.hlua_apphttp.flags |= APPLET_RSP_SENT;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01006698 }
6699
6700 done:
6701 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01006702 if (!(res->flags & CF_SHUTR)) {
Christopher Faulet9c832fc2018-12-14 13:34:05 +01006703 res->flags |= CF_READ_NULL;
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01006704 si_shutr(si);
6705 }
6706
6707 /* eat the whole request */
6708 if (co_data(req)) {
6709 req_htx = htx_from_buf(&req->buf);
6710 co_htx_skip(req, req_htx, co_data(req));
6711 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01006712 }
6713 }
6714
6715 out:
Christopher Faulet9c832fc2018-12-14 13:34:05 +01006716 htx_to_buf(res_htx, &res->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01006717 return;
6718
6719 error:
6720
6721 /* If we are in HTTP mode, and we are not send any
6722 * data, return a 500 server error in best effort:
6723 * if there is no room available in the buffer,
6724 * just close the connection.
6725 */
6726 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
Christopher Fauletf7346382019-07-17 22:02:08 +02006727 struct buffer *err = &http_err_chunks[HTTP_ERR_500];
Christopher Faulet9c832fc2018-12-14 13:34:05 +01006728
6729 channel_erase(res);
6730 res->buf.data = b_data(err);
6731 memcpy(res->buf.area, b_head(err), b_data(err));
6732 res_htx = htx_from_buf(&res->buf);
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01006733 channel_add_input(res, res_htx->data);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01006734 }
6735 if (!(strm->flags & SF_ERR_MASK))
6736 strm->flags |= SF_ERR_RESOURCE;
6737 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
6738 goto done;
6739}
6740
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006741static void hlua_applet_http_release(struct appctx *ctx)
6742{
Olivier Houchard3f795f72019-04-17 22:51:06 +02006743 task_destroy(ctx->ctx.hlua_apphttp.task);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006744 ctx->ctx.hlua_apphttp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006745 hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006746 ctx->ctx.hlua_apphttp.hlua = NULL;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006747}
6748
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006749/* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in
6750 * succes case, else return ACT_RET_PRS_ERR.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006751 *
6752 * This function can fail with an abort() due to an Lua critical error.
6753 * We are in the configuration parsing process of HAProxy, this abort() is
6754 * tolerated.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006755 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006756static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px,
6757 struct act_rule *rule, char **err)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006758{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006759 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006760 int i;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006761
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006762 /* Memory for the rule. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006763 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006764 if (!rule->arg.hlua_rule) {
6765 memprintf(err, "out of memory error");
Thierry FOURNIERafa80492015-08-19 09:04:15 +02006766 return ACT_RET_PRS_ERR;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006767 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006768
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006769 /* Memory for arguments. */
6770 rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, sizeof(char *));
6771 if (!rule->arg.hlua_rule->args) {
6772 memprintf(err, "out of memory error");
6773 return ACT_RET_PRS_ERR;
6774 }
6775
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006776 /* Reference the Lua function and store the reference. */
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006777 rule->arg.hlua_rule->fcn = *fcn;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006778
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006779 /* Expect some arguments */
6780 for (i = 0; i < fcn->nargs; i++) {
Thierry FOURNIER1725c2e2019-01-06 19:38:49 +01006781 if (*args[*cur_arg] == '\0') {
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006782 memprintf(err, "expect %d arguments", fcn->nargs);
6783 return ACT_RET_PRS_ERR;
6784 }
Thierry FOURNIER1725c2e2019-01-06 19:38:49 +01006785 rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]);
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006786 if (!rule->arg.hlua_rule->args[i]) {
6787 memprintf(err, "out of memory error");
6788 return ACT_RET_PRS_ERR;
6789 }
6790 (*cur_arg)++;
6791 }
6792 rule->arg.hlua_rule->args[i] = NULL;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006793
Thierry FOURNIER42148732015-09-02 17:17:33 +02006794 rule->action = ACT_CUSTOM;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006795 rule->action_ptr = hlua_action;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02006796 return ACT_RET_PRS_OK;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006797}
6798
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006799static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px,
6800 struct act_rule *rule, char **err)
6801{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006802 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006803
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01006804 /* HTTP applets are forbidden in tcp-request rules.
6805 * HTTP applet request requires everything initilized by
6806 * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER).
6807 * The applet will be immediately initilized, but its before
6808 * the call of this analyzer.
6809 */
6810 if (rule->from != ACT_F_HTTP_REQ) {
6811 memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets");
6812 return ACT_RET_PRS_ERR;
6813 }
6814
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006815 /* Memory for the rule. */
6816 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
6817 if (!rule->arg.hlua_rule) {
6818 memprintf(err, "out of memory error");
6819 return ACT_RET_PRS_ERR;
6820 }
6821
6822 /* Reference the Lua function and store the reference. */
6823 rule->arg.hlua_rule->fcn = *fcn;
6824
6825 /* TODO: later accept arguments. */
6826 rule->arg.hlua_rule->args = NULL;
6827
6828 /* Add applet pointer in the rule. */
6829 rule->applet.obj_type = OBJ_TYPE_APPLET;
6830 rule->applet.name = fcn->name;
6831 rule->applet.init = hlua_applet_http_init;
6832 rule->applet.fct = hlua_applet_http_fct;
6833 rule->applet.release = hlua_applet_http_release;
6834 rule->applet.timeout = hlua_timeout_applet;
6835
6836 return ACT_RET_PRS_OK;
6837}
6838
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006839/* This function is an LUA binding used for registering
6840 * "sample-conv" functions. It expects a converter name used
6841 * in the haproxy configuration file, and an LUA function.
6842 */
6843__LJMP static int hlua_register_action(lua_State *L)
6844{
6845 struct action_kw_list *akl;
6846 const char *name;
6847 int ref;
6848 int len;
6849 struct hlua_function *fcn;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006850 int nargs;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006851
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006852 /* Initialise the number of expected arguments at 0. */
6853 nargs = 0;
6854
6855 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
6856 WILL_LJMP(luaL_error(L, "'register_action' needs between 3 and 4 arguments"));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006857
6858 /* First argument : converter name. */
6859 name = MAY_LJMP(luaL_checkstring(L, 1));
6860
6861 /* Second argument : environment. */
6862 if (lua_type(L, 2) != LUA_TTABLE)
6863 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
6864
6865 /* Third argument : lua function. */
6866 ref = MAY_LJMP(hlua_checkfunction(L, 3));
6867
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006868 /* Fourth argument : number of mandatory arguments expected on the configuration line. */
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006869 if (lua_gettop(L) >= 4)
6870 nargs = MAY_LJMP(luaL_checkinteger(L, 4));
6871
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006872 /* browse the second argument as an array. */
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006873 lua_pushnil(L);
6874 while (lua_next(L, 2) != 0) {
6875 if (lua_type(L, -1) != LUA_TSTRING)
6876 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
6877
6878 /* Check required environment. Only accepted "http" or "tcp". */
6879 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006880 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006881 if (!akl)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006882 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006883 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006884 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006885 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006886
6887 /* Fill fcn. */
6888 fcn->name = strdup(name);
6889 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006890 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006891 fcn->function_ref = ref;
6892
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006893 /* Set the expected number od arguments. */
6894 fcn->nargs = nargs;
6895
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006896 /* List head */
6897 akl->list.n = akl->list.p = NULL;
6898
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006899 /* action keyword. */
6900 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006901 akl->kw[0].kw = calloc(1, len);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006902 if (!akl->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006903 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006904
6905 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
6906
6907 akl->kw[0].match_pfx = 0;
6908 akl->kw[0].private = fcn;
6909 akl->kw[0].parse = action_register_lua;
6910
6911 /* select the action registering point. */
6912 if (strcmp(lua_tostring(L, -1), "tcp-req") == 0)
6913 tcp_req_cont_keywords_register(akl);
6914 else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0)
6915 tcp_res_cont_keywords_register(akl);
6916 else if (strcmp(lua_tostring(L, -1), "http-req") == 0)
6917 http_req_keywords_register(akl);
6918 else if (strcmp(lua_tostring(L, -1), "http-res") == 0)
6919 http_res_keywords_register(akl);
6920 else
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006921 WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. "
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006922 "'tcp-req', 'tcp-res', 'http-req' or 'http-res' "
6923 "are expected.", lua_tostring(L, -1)));
6924
6925 /* pop the environment string. */
6926 lua_pop(L, 1);
6927 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006928 return ACT_RET_PRS_OK;
6929}
6930
6931static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px,
6932 struct act_rule *rule, char **err)
6933{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006934 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006935
Christopher Faulet280f85b2019-07-15 15:02:04 +02006936 if (px->mode == PR_MODE_HTTP) {
6937 memprintf(err, "Lua TCP services cannot be used on HTTP proxies");
Christopher Fauletafd8f102018-11-08 11:34:21 +01006938 return ACT_RET_PRS_ERR;
6939 }
6940
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006941 /* Memory for the rule. */
6942 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
6943 if (!rule->arg.hlua_rule) {
6944 memprintf(err, "out of memory error");
6945 return ACT_RET_PRS_ERR;
6946 }
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006947
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006948 /* Reference the Lua function and store the reference. */
6949 rule->arg.hlua_rule->fcn = *fcn;
6950
6951 /* TODO: later accept arguments. */
6952 rule->arg.hlua_rule->args = NULL;
6953
6954 /* Add applet pointer in the rule. */
6955 rule->applet.obj_type = OBJ_TYPE_APPLET;
6956 rule->applet.name = fcn->name;
6957 rule->applet.init = hlua_applet_tcp_init;
6958 rule->applet.fct = hlua_applet_tcp_fct;
6959 rule->applet.release = hlua_applet_tcp_release;
6960 rule->applet.timeout = hlua_timeout_applet;
6961
6962 return 0;
6963}
6964
6965/* This function is an LUA binding used for registering
6966 * "sample-conv" functions. It expects a converter name used
6967 * in the haproxy configuration file, and an LUA function.
6968 */
6969__LJMP static int hlua_register_service(lua_State *L)
6970{
6971 struct action_kw_list *akl;
6972 const char *name;
6973 const char *env;
6974 int ref;
6975 int len;
6976 struct hlua_function *fcn;
6977
6978 MAY_LJMP(check_args(L, 3, "register_service"));
6979
6980 /* First argument : converter name. */
6981 name = MAY_LJMP(luaL_checkstring(L, 1));
6982
6983 /* Second argument : environment. */
6984 env = MAY_LJMP(luaL_checkstring(L, 2));
6985
6986 /* Third argument : lua function. */
6987 ref = MAY_LJMP(hlua_checkfunction(L, 3));
6988
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006989 /* Allocate and fill the sample fetch keyword struct. */
6990 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
6991 if (!akl)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006992 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006993 fcn = calloc(1, sizeof(*fcn));
6994 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006995 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006996
6997 /* Fill fcn. */
6998 len = strlen("<lua.>") + strlen(name) + 1;
6999 fcn->name = calloc(1, len);
7000 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007001 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007002 snprintf((char *)fcn->name, len, "<lua.%s>", name);
7003 fcn->function_ref = ref;
7004
7005 /* List head */
7006 akl->list.n = akl->list.p = NULL;
7007
7008 /* converter keyword. */
7009 len = strlen("lua.") + strlen(name) + 1;
7010 akl->kw[0].kw = calloc(1, len);
7011 if (!akl->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007012 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007013
7014 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
7015
Thierry FOURNIER / OZON.IO02564fd2016-11-12 11:07:05 +01007016 /* Check required environment. Only accepted "http" or "tcp". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007017 if (strcmp(env, "tcp") == 0)
7018 akl->kw[0].parse = action_register_service_tcp;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007019 else if (strcmp(env, "http") == 0)
7020 akl->kw[0].parse = action_register_service_http;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007021 else
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007022 WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. "
Eric Salamafe7456f2017-12-21 14:30:07 +01007023 "'tcp' or 'http' are expected.", env));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007024
7025 akl->kw[0].match_pfx = 0;
7026 akl->kw[0].private = fcn;
7027
7028 /* End of array. */
7029 memset(&akl->kw[1], 0, sizeof(*akl->kw));
7030
7031 /* Register this new converter */
7032 service_keywords_register(akl);
7033
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007034 return 0;
7035}
7036
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007037/* This function initialises Lua cli handler. It copies the
7038 * arguments in the Lua stack and create channel IO objects.
7039 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02007040static int hlua_cli_parse_fct(char **args, char *payload, struct appctx *appctx, void *private)
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007041{
7042 struct hlua *hlua;
7043 struct hlua_function *fcn;
7044 int i;
7045 const char *error;
7046
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007047 fcn = private;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007048 appctx->ctx.hlua_cli.fcn = private;
7049
Willy Tarreaubafbe012017-11-24 17:34:44 +01007050 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007051 if (!hlua) {
7052 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007053 return 1;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007054 }
7055 HLUA_INIT(hlua);
7056 appctx->ctx.hlua_cli.hlua = hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007057
7058 /* Create task used by signal to wakeup applets.
7059 * We use the same wakeup fonction than the Lua applet_tcp and
7060 * applet_http. It is absolutely compatible.
7061 */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01007062 appctx->ctx.hlua_cli.task = task_new(tid_bit);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007063 if (!appctx->ctx.hlua_cli.task) {
Thierry FOURNIERffbf5692016-12-16 11:14:06 +01007064 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007065 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007066 }
7067 appctx->ctx.hlua_cli.task->nice = 0;
7068 appctx->ctx.hlua_cli.task->context = appctx;
7069 appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup;
7070
7071 /* Initialises the Lua context */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01007072 if (!hlua_ctx_init(hlua, appctx->ctx.hlua_cli.task, 0)) {
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007073 SEND_ERR(NULL, "Lua cli '%s': can't initialize Lua context.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007074 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007075 }
7076
7077 /* The following Lua calls can fail. */
7078 if (!SET_SAFE_LJMP(hlua->T)) {
7079 if (lua_type(hlua->T, -1) == LUA_TSTRING)
7080 error = lua_tostring(hlua->T, -1);
7081 else
7082 error = "critical error";
7083 SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error);
7084 goto error;
7085 }
7086
7087 /* Check stack available size. */
7088 if (!lua_checkstack(hlua->T, 2)) {
7089 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7090 goto error;
7091 }
7092
7093 /* Restore the function in the stack. */
7094 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
7095
7096 /* Once the arguments parsed, the CLI is like an AppletTCP,
7097 * so push AppletTCP in the stack.
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007098 */
7099 if (!hlua_applet_tcp_new(hlua->T, appctx)) {
7100 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7101 goto error;
7102 }
7103 hlua->nargs = 1;
7104
7105 /* push keywords in the stack. */
7106 for (i = 0; *args[i]; i++) {
7107 /* Check stack available size. */
7108 if (!lua_checkstack(hlua->T, 1)) {
7109 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7110 goto error;
7111 }
7112 lua_pushstring(hlua->T, args[i]);
7113 hlua->nargs++;
7114 }
7115
7116 /* We must initialize the execution timeouts. */
7117 hlua->max_time = hlua_timeout_session;
7118
7119 /* At this point the execution is safe. */
7120 RESET_SAFE_LJMP(hlua->T);
7121
7122 /* It's ok */
7123 return 0;
7124
7125 /* It's not ok. */
7126error:
7127 RESET_SAFE_LJMP(hlua->T);
7128 hlua_ctx_destroy(hlua);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007129 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007130 return 1;
7131}
7132
7133static int hlua_cli_io_handler_fct(struct appctx *appctx)
7134{
7135 struct hlua *hlua;
7136 struct stream_interface *si;
7137 struct hlua_function *fcn;
7138
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007139 hlua = appctx->ctx.hlua_cli.hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007140 si = appctx->owner;
Willy Tarreau8ae4f752016-12-14 15:41:45 +01007141 fcn = appctx->ctx.hlua_cli.fcn;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007142
7143 /* If the stream is disconnect or closed, ldo nothing. */
7144 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
7145 return 1;
7146
7147 /* Execute the function. */
7148 switch (hlua_ctx_resume(hlua, 1)) {
7149
7150 /* finished. */
7151 case HLUA_E_OK:
7152 return 1;
7153
7154 /* yield. */
7155 case HLUA_E_AGAIN:
7156 /* We want write. */
7157 if (HLUA_IS_WAKERESWR(hlua))
Willy Tarreaudb398432018-11-15 11:08:52 +01007158 si_rx_room_blk(si);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007159 /* Set the timeout. */
7160 if (hlua->wake_time != TICK_ETERNITY)
7161 task_schedule(hlua->task, hlua->wake_time);
7162 return 0;
7163
7164 /* finished with error. */
7165 case HLUA_E_ERRMSG:
7166 /* Display log. */
7167 SEND_ERR(NULL, "Lua cli '%s': %s.\n",
7168 fcn->name, lua_tostring(hlua->T, -1));
7169 lua_pop(hlua->T, 1);
7170 return 1;
7171
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007172 case HLUA_E_ETMOUT:
7173 SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n",
7174 fcn->name);
7175 return 1;
7176
7177 case HLUA_E_NOMEM:
7178 SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n",
7179 fcn->name);
7180 return 1;
7181
7182 case HLUA_E_YIELD: /* unexpected */
7183 SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n",
7184 fcn->name);
7185 return 1;
7186
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007187 case HLUA_E_ERR:
7188 /* Display log. */
7189 SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n",
7190 fcn->name);
7191 return 1;
7192
7193 default:
7194 return 1;
7195 }
7196
7197 return 1;
7198}
7199
7200static void hlua_cli_io_release_fct(struct appctx *appctx)
7201{
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007202 hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007203 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007204}
7205
7206/* This function is an LUA binding used for registering
7207 * new keywords in the cli. It expects a list of keywords
7208 * which are the "path". It is limited to 5 keywords. A
7209 * description of the command, a function to be executed
7210 * for the parsing and a function for io handlers.
7211 */
7212__LJMP static int hlua_register_cli(lua_State *L)
7213{
7214 struct cli_kw_list *cli_kws;
7215 const char *message;
7216 int ref_io;
7217 int len;
7218 struct hlua_function *fcn;
7219 int index;
7220 int i;
7221
7222 MAY_LJMP(check_args(L, 3, "register_cli"));
7223
7224 /* First argument : an array of maximum 5 keywords. */
7225 if (!lua_istable(L, 1))
7226 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table"));
7227
7228 /* Second argument : string with contextual message. */
7229 message = MAY_LJMP(luaL_checkstring(L, 2));
7230
7231 /* Third and fourth argument : lua function. */
7232 ref_io = MAY_LJMP(hlua_checkfunction(L, 3));
7233
7234 /* Allocate and fill the sample fetch keyword struct. */
7235 cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2);
7236 if (!cli_kws)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007237 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007238 fcn = calloc(1, sizeof(*fcn));
7239 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007240 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007241
7242 /* Fill path. */
7243 index = 0;
7244 lua_pushnil(L);
7245 while(lua_next(L, 1) != 0) {
7246 if (index >= 5)
7247 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries"));
7248 if (lua_type(L, -1) != LUA_TSTRING)
7249 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings"));
7250 cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1));
7251 if (!cli_kws->kw[0].str_kw[index])
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007252 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007253 index++;
7254 lua_pop(L, 1);
7255 }
7256
7257 /* Copy help message. */
7258 cli_kws->kw[0].usage = strdup(message);
7259 if (!cli_kws->kw[0].usage)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007260 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007261
7262 /* Fill fcn io handler. */
7263 len = strlen("<lua.cli>") + 1;
7264 for (i = 0; i < index; i++)
7265 len += strlen(cli_kws->kw[0].str_kw[i]) + 1;
7266 fcn->name = calloc(1, len);
7267 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007268 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007269 strncat((char *)fcn->name, "<lua.cli", len);
7270 for (i = 0; i < index; i++) {
7271 strncat((char *)fcn->name, ".", len);
7272 strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len);
7273 }
7274 strncat((char *)fcn->name, ">", len);
7275 fcn->function_ref = ref_io;
7276
7277 /* Fill last entries. */
7278 cli_kws->kw[0].private = fcn;
7279 cli_kws->kw[0].parse = hlua_cli_parse_fct;
7280 cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct;
7281 cli_kws->kw[0].io_release = hlua_cli_io_release_fct;
7282
7283 /* Register this new converter */
7284 cli_register_kw(cli_kws);
7285
7286 return 0;
7287}
7288
Thierry FOURNIERbd413492015-03-03 16:52:26 +01007289static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx,
7290 struct proxy *defpx, const char *file, int line,
7291 char **err, unsigned int *timeout)
7292{
7293 const char *error;
7294
7295 error = parse_time_err(args[1], timeout, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02007296 if (error == PARSE_TIME_OVER) {
7297 memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)",
7298 args[1], args[0]);
7299 return -1;
7300 }
7301 else if (error == PARSE_TIME_UNDER) {
7302 memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)",
7303 args[1], args[0]);
7304 return -1;
7305 }
7306 else if (error) {
Thierry FOURNIERbd413492015-03-03 16:52:26 +01007307 memprintf(err, "%s: invalid timeout", args[0]);
7308 return -1;
7309 }
7310 return 0;
7311}
7312
7313static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx,
7314 struct proxy *defpx, const char *file, int line,
7315 char **err)
7316{
7317 return hlua_read_timeout(args, section_type, curpx, defpx,
7318 file, line, err, &hlua_timeout_session);
7319}
7320
7321static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx,
7322 struct proxy *defpx, const char *file, int line,
7323 char **err)
7324{
7325 return hlua_read_timeout(args, section_type, curpx, defpx,
7326 file, line, err, &hlua_timeout_task);
7327}
7328
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007329static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx,
7330 struct proxy *defpx, const char *file, int line,
7331 char **err)
7332{
7333 return hlua_read_timeout(args, section_type, curpx, defpx,
7334 file, line, err, &hlua_timeout_applet);
7335}
7336
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01007337static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx,
7338 struct proxy *defpx, const char *file, int line,
7339 char **err)
7340{
7341 char *error;
7342
7343 hlua_nb_instruction = strtoll(args[1], &error, 10);
7344 if (*error != '\0') {
7345 memprintf(err, "%s: invalid number", args[0]);
7346 return -1;
7347 }
7348 return 0;
7349}
7350
Willy Tarreau32f61e22015-03-18 17:54:59 +01007351static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx,
7352 struct proxy *defpx, const char *file, int line,
7353 char **err)
7354{
7355 char *error;
7356
7357 if (*(args[1]) == 0) {
7358 memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]);
7359 return -1;
7360 }
7361 hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L;
7362 if (*error != '\0') {
7363 memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error);
7364 return -1;
7365 }
7366 return 0;
7367}
7368
7369
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007370/* This function is called by the main configuration key "lua-load". It loads and
7371 * execute an lua file during the parsing of the HAProxy configuration file. It is
7372 * the main lua entry point.
7373 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007374 * This function runs with the HAProxy keywords API. It returns -1 if an error
7375 * occurs, otherwise it returns 0.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007376 *
7377 * In some error case, LUA set an error message in top of the stack. This function
7378 * returns this error message in the HAProxy logs and pop it from the stack.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007379 *
7380 * This function can fail with an abort() due to an Lua critical error.
7381 * We are in the configuration parsing process of HAProxy, this abort() is
7382 * tolerated.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007383 */
7384static int hlua_load(char **args, int section_type, struct proxy *curpx,
7385 struct proxy *defpx, const char *file, int line,
7386 char **err)
7387{
7388 int error;
7389
7390 /* Just load and compile the file. */
7391 error = luaL_loadfile(gL.T, args[1]);
7392 if (error) {
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007393 memprintf(err, "error in Lua file '%s': %s", args[1], lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007394 lua_pop(gL.T, 1);
7395 return -1;
7396 }
7397
7398 /* If no syntax error where detected, execute the code. */
7399 error = lua_pcall(gL.T, 0, LUA_MULTRET, 0);
7400 switch (error) {
7401 case LUA_OK:
7402 break;
7403 case LUA_ERRRUN:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007404 memprintf(err, "Lua runtime error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007405 lua_pop(gL.T, 1);
7406 return -1;
7407 case LUA_ERRMEM:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007408 memprintf(err, "Lua out of memory error.n");
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007409 return -1;
7410 case LUA_ERRERR:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007411 memprintf(err, "Lua message handler error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007412 lua_pop(gL.T, 1);
7413 return -1;
7414 case LUA_ERRGCMM:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007415 memprintf(err, "Lua garbage collector error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007416 lua_pop(gL.T, 1);
7417 return -1;
7418 default:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007419 memprintf(err, "Lua unknonwn error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007420 lua_pop(gL.T, 1);
7421 return -1;
7422 }
7423
7424 return 0;
7425}
7426
7427/* configuration keywords declaration */
7428static struct cfg_kw_list cfg_kws = {{ },{
Thierry FOURNIERbd413492015-03-03 16:52:26 +01007429 { CFG_GLOBAL, "lua-load", hlua_load },
7430 { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout },
7431 { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout },
Thierry FOURNIER56da1012015-10-01 08:42:31 +02007432 { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout },
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01007433 { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield },
Willy Tarreau32f61e22015-03-18 17:54:59 +01007434 { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem },
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007435 { 0, NULL, NULL },
7436}};
7437
Willy Tarreau0108d902018-11-25 19:14:37 +01007438INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
7439
Christopher Fauletafd8f102018-11-08 11:34:21 +01007440
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007441/* This function can fail with an abort() due to an Lua critical error.
7442 * We are in the initialisation process of HAProxy, this abort() is
7443 * tolerated.
7444 */
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007445int hlua_post_init()
7446{
7447 struct hlua_init_function *init;
7448 const char *msg;
7449 enum hlua_exec ret;
Thierry Fournierfd107a22016-02-19 19:57:23 +01007450 const char *error;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007451
Thierry Fournier3d4a6752016-02-19 20:53:30 +01007452 /* Call post initialisation function in safe environement. */
7453 if (!SET_SAFE_LJMP(gL.T)) {
7454 if (lua_type(gL.T, -1) == LUA_TSTRING)
7455 error = lua_tostring(gL.T, -1);
7456 else
7457 error = "critical error";
7458 fprintf(stderr, "Lua post-init: %s.\n", error);
7459 exit(1);
7460 }
Frédéric Lécaille54f2bcf2018-08-29 13:46:24 +02007461
7462#if USE_OPENSSL
7463 /* Initialize SSL server. */
7464 if (socket_ssl.xprt->prepare_srv) {
7465 int saved_used_backed = global.ssl_used_backend;
7466 // don't affect maxconn automatic computation
7467 socket_ssl.xprt->prepare_srv(&socket_ssl);
7468 global.ssl_used_backend = saved_used_backed;
7469 }
7470#endif
7471
Thierry Fournier3d4a6752016-02-19 20:53:30 +01007472 hlua_fcn_post_init(gL.T);
7473 RESET_SAFE_LJMP(gL.T);
7474
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007475 list_for_each_entry(init, &hlua_init_functions, l) {
7476 lua_rawgeti(gL.T, LUA_REGISTRYINDEX, init->function_ref);
7477 ret = hlua_ctx_resume(&gL, 0);
7478 switch (ret) {
7479 case HLUA_E_OK:
7480 lua_pop(gL.T, -1);
7481 return 1;
7482 case HLUA_E_AGAIN:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007483 ha_alert("Lua init: yield not allowed.\n");
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007484 return 0;
7485 case HLUA_E_ERRMSG:
7486 msg = lua_tostring(gL.T, -1);
Christopher Faulet767a84b2017-11-24 16:50:31 +01007487 ha_alert("lua init: %s.\n", msg);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007488 return 0;
7489 case HLUA_E_ERR:
7490 default:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007491 ha_alert("Lua init: unknown runtime error.\n");
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007492 return 0;
7493 }
7494 }
7495 return 1;
7496}
7497
Willy Tarreau32f61e22015-03-18 17:54:59 +01007498/* The memory allocator used by the Lua stack. <ud> is a pointer to the
7499 * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize>
7500 * is the previously allocated size or the kind of object in case of a new
7501 * allocation. <nsize> is the requested new size.
7502 */
7503static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
7504{
7505 struct hlua_mem_allocator *zone = ud;
7506
7507 if (nsize == 0) {
7508 /* it's a free */
7509 if (ptr)
7510 zone->allocated -= osize;
7511 free(ptr);
7512 return NULL;
7513 }
7514
7515 if (!ptr) {
7516 /* it's a new allocation */
7517 if (zone->limit && zone->allocated + nsize > zone->limit)
7518 return NULL;
7519
7520 ptr = malloc(nsize);
7521 if (ptr)
7522 zone->allocated += nsize;
7523 return ptr;
7524 }
7525
7526 /* it's a realloc */
7527 if (zone->limit && zone->allocated + nsize - osize > zone->limit)
7528 return NULL;
7529
7530 ptr = realloc(ptr, nsize);
7531 if (ptr)
7532 zone->allocated += nsize - osize;
7533 return ptr;
7534}
7535
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007536/* Ithis function can fail with an abort() due to an Lua critical error.
7537 * We are in the initialisation process of HAProxy, this abort() is
7538 * tolerated.
7539 */
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01007540void hlua_init(void)
7541{
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007542 int i;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01007543 int idx;
7544 struct sample_fetch *sf;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007545 struct sample_conv *sc;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01007546 char *p;
Thierry Fournierfd107a22016-02-19 19:57:23 +01007547 const char *error_msg;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01007548#ifdef USE_OPENSSL
Willy Tarreau80f5fae2015-02-27 16:38:20 +01007549 struct srv_kw *kw;
7550 int tmp_error;
7551 char *error;
Thierry FOURNIER36d13742015-03-17 16:48:53 +01007552 char *args[] = { /* SSL client configuration. */
7553 "ssl",
7554 "verify",
7555 "none",
Thierry FOURNIER36d13742015-03-17 16:48:53 +01007556 NULL
7557 };
Willy Tarreau80f5fae2015-02-27 16:38:20 +01007558#endif
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007559
Thierry FOURNIER380d0932015-01-23 14:27:52 +01007560 /* Init main lua stack. */
7561 gL.Mref = LUA_REFNIL;
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01007562 gL.flags = 0;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01007563 LIST_INIT(&gL.com);
Willy Tarreau42ef75f2017-04-12 21:40:29 +02007564 gL.T = lua_newstate(hlua_alloc, &hlua_global_allocator);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01007565 hlua_sethlua(&gL);
7566 gL.Tref = LUA_REFNIL;
7567 gL.task = NULL;
7568
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007569 /* From this point, until the end of the initialisation function,
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007570 * the Lua function can fail with an abort. We are in the initialisation
7571 * process of HAProxy, this abort() is tolerated.
7572 */
7573
Thierry FOURNIER380d0932015-01-23 14:27:52 +01007574 /* Initialise lua. */
7575 luaL_openlibs(gL.T);
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007576
Thierry Fournier75933d42016-01-21 09:30:18 +01007577 /* Set safe environment for the initialisation. */
7578 if (!SET_SAFE_LJMP(gL.T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01007579 if (lua_type(gL.T, -1) == LUA_TSTRING)
7580 error_msg = lua_tostring(gL.T, -1);
7581 else
7582 error_msg = "critical error";
7583 fprintf(stderr, "Lua init: %s.\n", error_msg);
Thierry Fournier75933d42016-01-21 09:30:18 +01007584 exit(1);
7585 }
7586
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007587 /*
7588 *
7589 * Create "core" object.
7590 *
7591 */
7592
Thierry FOURNIERa2d8c652015-03-11 17:29:39 +01007593 /* This table entry is the object "core" base. */
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007594 lua_newtable(gL.T);
7595
7596 /* Push the loglevel constants. */
Willy Tarreau80f5fae2015-02-27 16:38:20 +01007597 for (i = 0; i < NB_LOG_LEVELS; i++)
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007598 hlua_class_const_int(gL.T, log_levels[i], i);
7599
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007600 /* Register special functions. */
7601 hlua_class_function(gL.T, "register_init", hlua_register_init);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01007602 hlua_class_function(gL.T, "register_task", hlua_register_task);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01007603 hlua_class_function(gL.T, "register_fetches", hlua_register_fetches);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01007604 hlua_class_function(gL.T, "register_converters", hlua_register_converters);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007605 hlua_class_function(gL.T, "register_action", hlua_register_action);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007606 hlua_class_function(gL.T, "register_service", hlua_register_service);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007607 hlua_class_function(gL.T, "register_cli", hlua_register_cli);
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01007608 hlua_class_function(gL.T, "yield", hlua_yield);
Willy Tarreau59551662015-03-10 14:23:13 +01007609 hlua_class_function(gL.T, "set_nice", hlua_set_nice);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01007610 hlua_class_function(gL.T, "sleep", hlua_sleep);
7611 hlua_class_function(gL.T, "msleep", hlua_msleep);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01007612 hlua_class_function(gL.T, "add_acl", hlua_add_acl);
7613 hlua_class_function(gL.T, "del_acl", hlua_del_acl);
7614 hlua_class_function(gL.T, "set_map", hlua_set_map);
7615 hlua_class_function(gL.T, "del_map", hlua_del_map);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007616 hlua_class_function(gL.T, "tcp", hlua_socket_new);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01007617 hlua_class_function(gL.T, "log", hlua_log);
7618 hlua_class_function(gL.T, "Debug", hlua_log_debug);
7619 hlua_class_function(gL.T, "Info", hlua_log_info);
7620 hlua_class_function(gL.T, "Warning", hlua_log_warning);
7621 hlua_class_function(gL.T, "Alert", hlua_log_alert);
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02007622 hlua_class_function(gL.T, "done", hlua_done);
Thierry Fournierfb0b5462016-01-21 09:28:58 +01007623 hlua_fcn_reg_core_fcn(gL.T);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007624
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007625 lua_setglobal(gL.T, "core");
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01007626
7627 /*
7628 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02007629 * Register class Map
7630 *
7631 */
7632
7633 /* This table entry is the object "Map" base. */
7634 lua_newtable(gL.T);
7635
7636 /* register pattern types. */
7637 for (i=0; i<PAT_MATCH_NUM; i++)
7638 hlua_class_const_int(gL.T, pat_match_names[i], i);
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01007639 for (i=0; i<PAT_MATCH_NUM; i++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007640 snprintf(trash.area, trash.size, "_%s", pat_match_names[i]);
7641 hlua_class_const_int(gL.T, trash.area, i);
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01007642 }
Thierry FOURNIER3def3932015-04-07 11:27:54 +02007643
7644 /* register constructor. */
7645 hlua_class_function(gL.T, "new", hlua_map_new);
7646
7647 /* Create and fill the metatable. */
7648 lua_newtable(gL.T);
7649
7650 /* Create and fille the __index entry. */
7651 lua_pushstring(gL.T, "__index");
7652 lua_newtable(gL.T);
7653
7654 /* Register . */
7655 hlua_class_function(gL.T, "lookup", hlua_map_lookup);
7656 hlua_class_function(gL.T, "slookup", hlua_map_slookup);
7657
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007658 lua_rawset(gL.T, -3);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02007659
Thierry Fournier45e78d72016-02-19 18:34:46 +01007660 /* Register previous table in the registry with reference and named entry.
7661 * The function hlua_register_metatable() pops the stack, so we
7662 * previously create a copy of the table.
7663 */
Thierry FOURNIER3def3932015-04-07 11:27:54 +02007664 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007665 class_map_ref = hlua_register_metatable(gL.T, CLASS_MAP);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02007666
7667 /* Assign the metatable to the mai Map object. */
7668 lua_setmetatable(gL.T, -2);
7669
7670 /* Set a name to the table. */
7671 lua_setglobal(gL.T, "Map");
7672
7673 /*
7674 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01007675 * Register class Channel
7676 *
7677 */
7678
7679 /* Create and fill the metatable. */
7680 lua_newtable(gL.T);
7681
7682 /* Create and fille the __index entry. */
7683 lua_pushstring(gL.T, "__index");
7684 lua_newtable(gL.T);
7685
7686 /* Register . */
7687 hlua_class_function(gL.T, "get", hlua_channel_get);
7688 hlua_class_function(gL.T, "dup", hlua_channel_dup);
7689 hlua_class_function(gL.T, "getline", hlua_channel_getline);
7690 hlua_class_function(gL.T, "set", hlua_channel_set);
7691 hlua_class_function(gL.T, "append", hlua_channel_append);
7692 hlua_class_function(gL.T, "send", hlua_channel_send);
7693 hlua_class_function(gL.T, "forward", hlua_channel_forward);
7694 hlua_class_function(gL.T, "get_in_len", hlua_channel_get_in_len);
7695 hlua_class_function(gL.T, "get_out_len", hlua_channel_get_out_len);
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01007696 hlua_class_function(gL.T, "is_full", hlua_channel_is_full);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01007697
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007698 lua_rawset(gL.T, -3);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01007699
7700 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007701 class_channel_ref = hlua_register_metatable(gL.T, CLASS_CHANNEL);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01007702
7703 /*
7704 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01007705 * Register class Fetches
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01007706 *
7707 */
7708
7709 /* Create and fill the metatable. */
7710 lua_newtable(gL.T);
7711
7712 /* Create and fille the __index entry. */
7713 lua_pushstring(gL.T, "__index");
7714 lua_newtable(gL.T);
7715
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01007716 /* Browse existing fetches and create the associated
7717 * object method.
7718 */
7719 sf = NULL;
7720 while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) {
7721
7722 /* Dont register the keywork if the arguments check function are
7723 * not safe during the runtime.
7724 */
7725 if ((sf->val_args != NULL) &&
7726 (sf->val_args != val_payload_lv) &&
7727 (sf->val_args != val_hdr))
7728 continue;
7729
7730 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
7731 * by an underscore.
7732 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007733 strncpy(trash.area, sf->kw, trash.size);
7734 trash.area[trash.size - 1] = '\0';
7735 for (p = trash.area; *p; p++)
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01007736 if (*p == '.' || *p == '-' || *p == '+')
7737 *p = '_';
7738
7739 /* Register the function. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007740 lua_pushstring(gL.T, trash.area);
Willy Tarreau2ec22742015-03-10 14:27:20 +01007741 lua_pushlightuserdata(gL.T, sf);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01007742 lua_pushcclosure(gL.T, hlua_run_sample_fetch, 1);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007743 lua_rawset(gL.T, -3);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01007744 }
7745
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007746 lua_rawset(gL.T, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01007747
7748 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007749 class_fetches_ref = hlua_register_metatable(gL.T, CLASS_FETCHES);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01007750
7751 /*
7752 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007753 * Register class Converters
7754 *
7755 */
7756
7757 /* Create and fill the metatable. */
7758 lua_newtable(gL.T);
7759
7760 /* Create and fill the __index entry. */
7761 lua_pushstring(gL.T, "__index");
7762 lua_newtable(gL.T);
7763
7764 /* Browse existing converters and create the associated
7765 * object method.
7766 */
7767 sc = NULL;
7768 while ((sc = sample_conv_getnext(sc, &idx)) != NULL) {
7769 /* Dont register the keywork if the arguments check function are
7770 * not safe during the runtime.
7771 */
7772 if (sc->val_args != NULL)
7773 continue;
7774
7775 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
7776 * by an underscore.
7777 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007778 strncpy(trash.area, sc->kw, trash.size);
7779 trash.area[trash.size - 1] = '\0';
7780 for (p = trash.area; *p; p++)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007781 if (*p == '.' || *p == '-' || *p == '+')
7782 *p = '_';
7783
7784 /* Register the function. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007785 lua_pushstring(gL.T, trash.area);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007786 lua_pushlightuserdata(gL.T, sc);
7787 lua_pushcclosure(gL.T, hlua_run_sample_conv, 1);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007788 lua_rawset(gL.T, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007789 }
7790
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007791 lua_rawset(gL.T, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007792
7793 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007794 class_converters_ref = hlua_register_metatable(gL.T, CLASS_CONVERTERS);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007795
7796 /*
7797 *
Thierry FOURNIER08504f42015-03-16 14:17:08 +01007798 * Register class HTTP
7799 *
7800 */
7801
7802 /* Create and fill the metatable. */
7803 lua_newtable(gL.T);
7804
7805 /* Create and fille the __index entry. */
7806 lua_pushstring(gL.T, "__index");
7807 lua_newtable(gL.T);
7808
7809 /* Register Lua functions. */
7810 hlua_class_function(gL.T, "req_get_headers",hlua_http_req_get_headers);
7811 hlua_class_function(gL.T, "req_del_header", hlua_http_req_del_hdr);
7812 hlua_class_function(gL.T, "req_rep_header", hlua_http_req_rep_hdr);
7813 hlua_class_function(gL.T, "req_rep_value", hlua_http_req_rep_val);
7814 hlua_class_function(gL.T, "req_add_header", hlua_http_req_add_hdr);
7815 hlua_class_function(gL.T, "req_set_header", hlua_http_req_set_hdr);
7816 hlua_class_function(gL.T, "req_set_method", hlua_http_req_set_meth);
7817 hlua_class_function(gL.T, "req_set_path", hlua_http_req_set_path);
7818 hlua_class_function(gL.T, "req_set_query", hlua_http_req_set_query);
7819 hlua_class_function(gL.T, "req_set_uri", hlua_http_req_set_uri);
7820
7821 hlua_class_function(gL.T, "res_get_headers",hlua_http_res_get_headers);
7822 hlua_class_function(gL.T, "res_del_header", hlua_http_res_del_hdr);
7823 hlua_class_function(gL.T, "res_rep_header", hlua_http_res_rep_hdr);
7824 hlua_class_function(gL.T, "res_rep_value", hlua_http_res_rep_val);
7825 hlua_class_function(gL.T, "res_add_header", hlua_http_res_add_hdr);
7826 hlua_class_function(gL.T, "res_set_header", hlua_http_res_set_hdr);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02007827 hlua_class_function(gL.T, "res_set_status", hlua_http_res_set_status);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01007828
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007829 lua_rawset(gL.T, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01007830
7831 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007832 class_http_ref = hlua_register_metatable(gL.T, CLASS_HTTP);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01007833
7834 /*
7835 *
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007836 * Register class AppletTCP
7837 *
7838 */
7839
7840 /* Create and fill the metatable. */
7841 lua_newtable(gL.T);
7842
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007843 /* Create and fille the __index entry. */
7844 lua_pushstring(gL.T, "__index");
7845 lua_newtable(gL.T);
7846
7847 /* Register Lua functions. */
Thierry FOURNIER / OZON.IO3e1d7912016-12-12 12:29:34 +01007848 hlua_class_function(gL.T, "getline", hlua_applet_tcp_getline);
7849 hlua_class_function(gL.T, "receive", hlua_applet_tcp_recv);
7850 hlua_class_function(gL.T, "send", hlua_applet_tcp_send);
7851 hlua_class_function(gL.T, "set_priv", hlua_applet_tcp_set_priv);
7852 hlua_class_function(gL.T, "get_priv", hlua_applet_tcp_get_priv);
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01007853 hlua_class_function(gL.T, "set_var", hlua_applet_tcp_set_var);
7854 hlua_class_function(gL.T, "unset_var", hlua_applet_tcp_unset_var);
7855 hlua_class_function(gL.T, "get_var", hlua_applet_tcp_get_var);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007856
7857 lua_settable(gL.T, -3);
7858
7859 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007860 class_applet_tcp_ref = hlua_register_metatable(gL.T, CLASS_APPLET_TCP);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007861
7862 /*
7863 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007864 * Register class AppletHTTP
7865 *
7866 */
7867
7868 /* Create and fill the metatable. */
7869 lua_newtable(gL.T);
7870
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007871 /* Create and fille the __index entry. */
7872 lua_pushstring(gL.T, "__index");
7873 lua_newtable(gL.T);
7874
7875 /* Register Lua functions. */
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01007876 hlua_class_function(gL.T, "set_priv", hlua_applet_http_set_priv);
7877 hlua_class_function(gL.T, "get_priv", hlua_applet_http_get_priv);
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01007878 hlua_class_function(gL.T, "set_var", hlua_applet_http_set_var);
7879 hlua_class_function(gL.T, "unset_var", hlua_applet_http_unset_var);
7880 hlua_class_function(gL.T, "get_var", hlua_applet_http_get_var);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007881 hlua_class_function(gL.T, "getline", hlua_applet_http_getline);
7882 hlua_class_function(gL.T, "receive", hlua_applet_http_recv);
7883 hlua_class_function(gL.T, "send", hlua_applet_http_send);
7884 hlua_class_function(gL.T, "add_header", hlua_applet_http_addheader);
7885 hlua_class_function(gL.T, "set_status", hlua_applet_http_status);
7886 hlua_class_function(gL.T, "start_response", hlua_applet_http_start_response);
7887
7888 lua_settable(gL.T, -3);
7889
7890 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007891 class_applet_http_ref = hlua_register_metatable(gL.T, CLASS_APPLET_HTTP);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007892
7893 /*
7894 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01007895 * Register class TXN
7896 *
7897 */
7898
7899 /* Create and fill the metatable. */
7900 lua_newtable(gL.T);
7901
7902 /* Create and fille the __index entry. */
7903 lua_pushstring(gL.T, "__index");
7904 lua_newtable(gL.T);
7905
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01007906 /* Register Lua functions. */
Patrick Hemmer268a7072018-05-11 12:52:31 -04007907 hlua_class_function(gL.T, "set_priv", hlua_set_priv);
7908 hlua_class_function(gL.T, "get_priv", hlua_get_priv);
7909 hlua_class_function(gL.T, "set_var", hlua_set_var);
7910 hlua_class_function(gL.T, "unset_var", hlua_unset_var);
7911 hlua_class_function(gL.T, "get_var", hlua_get_var);
7912 hlua_class_function(gL.T, "done", hlua_txn_done);
7913 hlua_class_function(gL.T, "set_loglevel", hlua_txn_set_loglevel);
7914 hlua_class_function(gL.T, "set_tos", hlua_txn_set_tos);
7915 hlua_class_function(gL.T, "set_mark", hlua_txn_set_mark);
7916 hlua_class_function(gL.T, "set_priority_class", hlua_txn_set_priority_class);
7917 hlua_class_function(gL.T, "set_priority_offset", hlua_txn_set_priority_offset);
7918 hlua_class_function(gL.T, "deflog", hlua_txn_deflog);
7919 hlua_class_function(gL.T, "log", hlua_txn_log);
7920 hlua_class_function(gL.T, "Debug", hlua_txn_log_debug);
7921 hlua_class_function(gL.T, "Info", hlua_txn_log_info);
7922 hlua_class_function(gL.T, "Warning", hlua_txn_log_warning);
7923 hlua_class_function(gL.T, "Alert", hlua_txn_log_alert);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01007924
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007925 lua_rawset(gL.T, -3);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01007926
7927 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007928 class_txn_ref = hlua_register_metatable(gL.T, CLASS_TXN);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007929
7930 /*
7931 *
7932 * Register class Socket
7933 *
7934 */
7935
7936 /* Create and fill the metatable. */
7937 lua_newtable(gL.T);
7938
7939 /* Create and fille the __index entry. */
7940 lua_pushstring(gL.T, "__index");
7941 lua_newtable(gL.T);
7942
Baptiste Assmann84bb4932015-03-02 21:40:06 +01007943#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007944 hlua_class_function(gL.T, "connect_ssl", hlua_socket_connect_ssl);
Baptiste Assmann84bb4932015-03-02 21:40:06 +01007945#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007946 hlua_class_function(gL.T, "connect", hlua_socket_connect);
7947 hlua_class_function(gL.T, "send", hlua_socket_send);
7948 hlua_class_function(gL.T, "receive", hlua_socket_receive);
7949 hlua_class_function(gL.T, "close", hlua_socket_close);
7950 hlua_class_function(gL.T, "getpeername", hlua_socket_getpeername);
7951 hlua_class_function(gL.T, "getsockname", hlua_socket_getsockname);
7952 hlua_class_function(gL.T, "setoption", hlua_socket_setoption);
7953 hlua_class_function(gL.T, "settimeout", hlua_socket_settimeout);
7954
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007955 lua_rawset(gL.T, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007956
7957 /* Register the garbage collector entry. */
7958 lua_pushstring(gL.T, "__gc");
7959 lua_pushcclosure(gL.T, hlua_socket_gc, 0);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007960 lua_rawset(gL.T, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007961
7962 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007963 class_socket_ref = hlua_register_metatable(gL.T, CLASS_SOCKET);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007964
7965 /* Proxy and server configuration initialisation. */
7966 memset(&socket_proxy, 0, sizeof(socket_proxy));
7967 init_new_proxy(&socket_proxy);
7968 socket_proxy.parent = NULL;
7969 socket_proxy.last_change = now.tv_sec;
7970 socket_proxy.id = "LUA-SOCKET";
7971 socket_proxy.cap = PR_CAP_FE | PR_CAP_BE;
7972 socket_proxy.maxconn = 0;
7973 socket_proxy.accept = NULL;
7974 socket_proxy.options2 |= PR_O2_INDEPSTR;
7975 socket_proxy.srv = NULL;
7976 socket_proxy.conn_retries = 0;
7977 socket_proxy.timeout.connect = 5000; /* By default the timeout connection is 5s. */
7978
7979 /* Init TCP server: unchanged parameters */
7980 memset(&socket_tcp, 0, sizeof(socket_tcp));
7981 socket_tcp.next = NULL;
7982 socket_tcp.proxy = &socket_proxy;
7983 socket_tcp.obj_type = OBJ_TYPE_SERVER;
7984 LIST_INIT(&socket_tcp.actconns);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04007985 socket_tcp.pendconns = EB_ROOT;
Christopher Faulet40a007c2017-07-03 15:41:01 +02007986 socket_tcp.priv_conns = NULL;
7987 socket_tcp.idle_conns = NULL;
7988 socket_tcp.safe_conns = NULL;
Emeric Brun52a91d32017-08-31 14:41:55 +02007989 socket_tcp.next_state = SRV_ST_RUNNING; /* early server setup */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007990 socket_tcp.last_change = 0;
7991 socket_tcp.id = "LUA-TCP-CONN";
7992 socket_tcp.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
7993 socket_tcp.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
7994 socket_tcp.pp_opts = 0; /* Remove proxy protocol. */
7995
7996 /* XXX: Copy default parameter from default server,
7997 * but the default server is not initialized.
7998 */
7999 socket_tcp.maxqueue = socket_proxy.defsrv.maxqueue;
8000 socket_tcp.minconn = socket_proxy.defsrv.minconn;
8001 socket_tcp.maxconn = socket_proxy.defsrv.maxconn;
8002 socket_tcp.slowstart = socket_proxy.defsrv.slowstart;
8003 socket_tcp.onerror = socket_proxy.defsrv.onerror;
8004 socket_tcp.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
8005 socket_tcp.onmarkedup = socket_proxy.defsrv.onmarkedup;
8006 socket_tcp.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
8007 socket_tcp.uweight = socket_proxy.defsrv.iweight;
8008 socket_tcp.iweight = socket_proxy.defsrv.iweight;
8009
8010 socket_tcp.check.status = HCHK_STATUS_INI;
8011 socket_tcp.check.rise = socket_proxy.defsrv.check.rise;
8012 socket_tcp.check.fall = socket_proxy.defsrv.check.fall;
8013 socket_tcp.check.health = socket_tcp.check.rise; /* socket, but will fall down at first failure */
8014 socket_tcp.check.server = &socket_tcp;
8015
8016 socket_tcp.agent.status = HCHK_STATUS_INI;
8017 socket_tcp.agent.rise = socket_proxy.defsrv.agent.rise;
8018 socket_tcp.agent.fall = socket_proxy.defsrv.agent.fall;
8019 socket_tcp.agent.health = socket_tcp.agent.rise; /* socket, but will fall down at first failure */
8020 socket_tcp.agent.server = &socket_tcp;
8021
Willy Tarreaua261e9b2016-12-22 20:44:00 +01008022 socket_tcp.xprt = xprt_get(XPRT_RAW);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008023
8024#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008025 /* Init TCP server: unchanged parameters */
8026 memset(&socket_ssl, 0, sizeof(socket_ssl));
8027 socket_ssl.next = NULL;
8028 socket_ssl.proxy = &socket_proxy;
8029 socket_ssl.obj_type = OBJ_TYPE_SERVER;
8030 LIST_INIT(&socket_ssl.actconns);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04008031 socket_ssl.pendconns = EB_ROOT;
Willy Tarreaub784b352019-02-07 14:48:24 +01008032 socket_ssl.priv_conns = NULL;
8033 socket_ssl.idle_conns = NULL;
8034 socket_ssl.safe_conns = NULL;
Emeric Brun52a91d32017-08-31 14:41:55 +02008035 socket_ssl.next_state = SRV_ST_RUNNING; /* early server setup */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008036 socket_ssl.last_change = 0;
8037 socket_ssl.id = "LUA-SSL-CONN";
8038 socket_ssl.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8039 socket_ssl.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8040 socket_ssl.pp_opts = 0; /* Remove proxy protocol. */
8041
8042 /* XXX: Copy default parameter from default server,
8043 * but the default server is not initialized.
8044 */
8045 socket_ssl.maxqueue = socket_proxy.defsrv.maxqueue;
8046 socket_ssl.minconn = socket_proxy.defsrv.minconn;
8047 socket_ssl.maxconn = socket_proxy.defsrv.maxconn;
8048 socket_ssl.slowstart = socket_proxy.defsrv.slowstart;
8049 socket_ssl.onerror = socket_proxy.defsrv.onerror;
8050 socket_ssl.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
8051 socket_ssl.onmarkedup = socket_proxy.defsrv.onmarkedup;
8052 socket_ssl.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
8053 socket_ssl.uweight = socket_proxy.defsrv.iweight;
8054 socket_ssl.iweight = socket_proxy.defsrv.iweight;
8055
8056 socket_ssl.check.status = HCHK_STATUS_INI;
8057 socket_ssl.check.rise = socket_proxy.defsrv.check.rise;
8058 socket_ssl.check.fall = socket_proxy.defsrv.check.fall;
8059 socket_ssl.check.health = socket_ssl.check.rise; /* socket, but will fall down at first failure */
8060 socket_ssl.check.server = &socket_ssl;
8061
8062 socket_ssl.agent.status = HCHK_STATUS_INI;
8063 socket_ssl.agent.rise = socket_proxy.defsrv.agent.rise;
8064 socket_ssl.agent.fall = socket_proxy.defsrv.agent.fall;
8065 socket_ssl.agent.health = socket_ssl.agent.rise; /* socket, but will fall down at first failure */
8066 socket_ssl.agent.server = &socket_ssl;
8067
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008068 socket_ssl.use_ssl = 1;
Willy Tarreaua261e9b2016-12-22 20:44:00 +01008069 socket_ssl.xprt = xprt_get(XPRT_SSL);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008070
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008071 for (idx = 0; args[idx] != NULL; idx++) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008072 if ((kw = srv_find_kw(args[idx])) != NULL) { /* Maybe it's registered server keyword */
8073 /*
8074 *
8075 * If the keyword is not known, we can search in the registered
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08008076 * server keywords. This is useful to configure special SSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008077 * features like client certificates and ssl_verify.
8078 *
8079 */
8080 tmp_error = kw->parse(args, &idx, &socket_proxy, &socket_ssl, &error);
8081 if (tmp_error != 0) {
8082 fprintf(stderr, "INTERNAL ERROR: %s\n", error);
8083 abort(); /* This must be never arrives because the command line
8084 not editable by the user. */
8085 }
8086 idx += kw->skip;
8087 }
8088 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008089#endif
Thierry Fournier75933d42016-01-21 09:30:18 +01008090
8091 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01008092}
Willy Tarreaubb57d942016-12-21 19:04:56 +01008093
Willy Tarreau80713382018-11-26 10:19:54 +01008094static void hlua_register_build_options(void)
8095{
Willy Tarreaubb57d942016-12-21 19:04:56 +01008096 char *ptr = NULL;
Willy Tarreau80713382018-11-26 10:19:54 +01008097
Willy Tarreaubb57d942016-12-21 19:04:56 +01008098 memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE);
8099 hap_register_build_opts(ptr, 1);
8100}
Willy Tarreau80713382018-11-26 10:19:54 +01008101
8102INITCALL0(STG_REGISTER, hlua_register_build_options);