blob: 8c9b2b161ed0227ed0edfd3eeb5d891bd37091c2 [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>
55#include <proto/proto_http.h>
56#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);
4834 htx_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 Fauleta2097962019-07-15 16:25:33 +02004980 lua_pushboolean(L, htx_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 Fauleta2097962019-07-15 16:25:33 +02004991 lua_pushboolean(L, htx_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 Fauleta2097962019-07-15 16:25:33 +02005015 htx_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 Fauleta2097962019-07-15 16:25:33 +02005026 lua_pushboolean(L, htx_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 Fauleta2097962019-07-15 16:25:33 +02005037 htx_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))
5412 htx_reply_and_close(htxn->s, 0, NULL);
5413 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;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006480
Willy Tarreau0078bfc2015-10-07 20:20:28 +02006481 /* We want two things in HTTP mode :
6482 * - enforce server-close mode if we were in keep-alive, so that the
6483 * applet is released after each response ;
6484 * - enable request body transfer to the applet in order to resync
6485 * with the response body.
6486 */
6487 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)
6488 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreau0078bfc2015-10-07 20:20:28 +02006489
Willy Tarreaubafbe012017-11-24 17:34:44 +01006490 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006491 if (!hlua) {
6492 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
6493 ctx->rule->arg.hlua_rule->fcn.name);
6494 return 0;
6495 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006496 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006497 ctx->ctx.hlua_apphttp.hlua = hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006498 ctx->ctx.hlua_apphttp.left_bytes = -1;
6499 ctx->ctx.hlua_apphttp.flags = 0;
6500
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +01006501 if (txn->req.flags & HTTP_MSGF_VER_11)
6502 ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11;
6503
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006504 /* Create task used by signal to wakeup applets. */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01006505 task = task_new(tid_bit);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006506 if (!task) {
6507 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
6508 ctx->rule->arg.hlua_rule->fcn.name);
6509 return 0;
6510 }
6511 task->nice = 0;
6512 task->context = ctx;
6513 task->process = hlua_applet_wakeup;
6514 ctx->ctx.hlua_apphttp.task = task;
6515
6516 /* In the execution wrappers linked with a stream, the
6517 * Lua context can be not initialized. This behavior
6518 * permits to save performances because a systematic
6519 * Lua initialization cause 5% performances loss.
6520 */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006521 if (!hlua_ctx_init(hlua, task, 0)) {
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006522 SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n",
6523 ctx->rule->arg.hlua_rule->fcn.name);
6524 return 0;
6525 }
6526
6527 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02006528 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006529
6530 /* The following Lua calls can fail. */
6531 if (!SET_SAFE_LJMP(hlua->T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01006532 if (lua_type(hlua->T, -1) == LUA_TSTRING)
6533 error = lua_tostring(hlua->T, -1);
6534 else
6535 error = "critical error";
6536 SEND_ERR(px, "Lua applet http '%s': %s.\n",
6537 ctx->rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006538 return 0;
6539 }
6540
6541 /* Check stack available size. */
6542 if (!lua_checkstack(hlua->T, 1)) {
6543 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
6544 ctx->rule->arg.hlua_rule->fcn.name);
6545 RESET_SAFE_LJMP(hlua->T);
6546 return 0;
6547 }
6548
6549 /* Restore the function in the stack. */
6550 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref);
6551
6552 /* Create and and push object stream in the stack. */
6553 if (!hlua_applet_http_new(hlua->T, ctx)) {
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 hlua->nargs = 1;
6560
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006561 /* push keywords in the stack. */
6562 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
6563 if (!lua_checkstack(hlua->T, 1)) {
6564 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
6565 ctx->rule->arg.hlua_rule->fcn.name);
6566 RESET_SAFE_LJMP(hlua->T);
6567 return 0;
6568 }
6569 lua_pushstring(hlua->T, *arg);
6570 hlua->nargs++;
6571 }
6572
6573 RESET_SAFE_LJMP(hlua->T);
6574
6575 /* Wakeup the applet when data is ready for read. */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01006576 si_cant_get(si);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006577
6578 return 1;
6579}
6580
Christopher Fauleta2097962019-07-15 16:25:33 +02006581static void hlua_applet_http_fct(struct appctx *ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01006582{
6583 struct stream_interface *si = ctx->owner;
6584 struct stream *strm = si_strm(si);
6585 struct channel *req = si_oc(si);
6586 struct channel *res = si_ic(si);
6587 struct act_rule *rule = ctx->rule;
6588 struct proxy *px = strm->be;
6589 struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua;
6590 struct htx *req_htx, *res_htx;
6591
6592 res_htx = htx_from_buf(&res->buf);
6593
6594 /* If the stream is disconnect or closed, ldo nothing. */
6595 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
6596 goto out;
6597
6598 /* Check if the input buffer is avalaible. */
6599 if (!b_size(&res->buf)) {
6600 si_rx_room_blk(si);
6601 goto out;
6602 }
6603 /* check that the output is not closed */
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01006604 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
Christopher Faulet9c832fc2018-12-14 13:34:05 +01006605 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
6606
6607 /* Set the currently running flag. */
6608 if (!HLUA_IS_RUNNING(hlua) &&
6609 !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
6610 struct htx_blk *blk;
6611 size_t count = co_data(req);
6612
6613 if (!count) {
6614 si_cant_get(si);
6615 goto out;
6616 }
6617
6618 /* We need to flush the request header. This left the body for
6619 * the Lua.
6620 */
6621 req_htx = htx_from_buf(&req->buf);
Christopher Fauleta3f15502019-05-13 15:27:23 +02006622 blk = htx_get_first_blk(req_htx);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01006623 while (count && blk) {
6624 enum htx_blk_type type = htx_get_blk_type(blk);
6625 uint32_t sz = htx_get_blksz(blk);
6626
6627 if (sz > count) {
6628 si_cant_get(si);
Christopher Faulet0ae79d02019-02-27 21:36:59 +01006629 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01006630 goto out;
6631 }
6632
6633 count -= sz;
6634 co_set_data(req, co_data(req) - sz);
6635 blk = htx_remove_blk(req_htx, blk);
6636
6637 if (type == HTX_BLK_EOH)
6638 break;
6639 }
Christopher Faulet0ae79d02019-02-27 21:36:59 +01006640 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01006641 }
6642
6643 /* Executes The applet if it is not done. */
6644 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
6645
6646 /* Execute the function. */
6647 switch (hlua_ctx_resume(hlua, 1)) {
6648 /* finished. */
6649 case HLUA_E_OK:
6650 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
6651 break;
6652
6653 /* yield. */
6654 case HLUA_E_AGAIN:
6655 if (hlua->wake_time != TICK_ETERNITY)
6656 task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time);
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01006657 goto out;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01006658
6659 /* finished with error. */
6660 case HLUA_E_ERRMSG:
6661 /* Display log. */
6662 SEND_ERR(px, "Lua applet http '%s': %s.\n",
6663 rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1));
6664 lua_pop(hlua->T, 1);
6665 goto error;
6666
6667 case HLUA_E_ETMOUT:
6668 SEND_ERR(px, "Lua applet http '%s': execution timeout.\n",
6669 rule->arg.hlua_rule->fcn.name);
6670 goto error;
6671
6672 case HLUA_E_NOMEM:
6673 SEND_ERR(px, "Lua applet http '%s': out of memory error.\n",
6674 rule->arg.hlua_rule->fcn.name);
6675 goto error;
6676
6677 case HLUA_E_YIELD: /* unexpected */
6678 SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n",
6679 rule->arg.hlua_rule->fcn.name);
6680 goto error;
6681
6682 case HLUA_E_ERR:
6683 /* Display log. */
6684 SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n",
6685 rule->arg.hlua_rule->fcn.name);
6686 goto error;
6687
6688 default:
6689 goto error;
6690 }
6691 }
6692
6693 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01006694 if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT)
6695 goto done;
6696
Christopher Faulet9c832fc2018-12-14 13:34:05 +01006697 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT))
6698 goto error;
6699
Christopher Faulet54b5e212019-06-04 10:08:28 +02006700 /* Don't add TLR because mux-h1 will take care of it */
Christopher Faulet9c832fc2018-12-14 13:34:05 +01006701 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
6702 si_rx_room_blk(si);
6703 goto out;
6704 }
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01006705 channel_add_input(res, 1);
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01006706 strm->txn->status = ctx->ctx.hlua_apphttp.status;
6707 ctx->ctx.hlua_apphttp.flags |= APPLET_RSP_SENT;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01006708 }
6709
6710 done:
6711 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01006712 if (!(res->flags & CF_SHUTR)) {
Christopher Faulet9c832fc2018-12-14 13:34:05 +01006713 res->flags |= CF_READ_NULL;
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01006714 si_shutr(si);
6715 }
6716
6717 /* eat the whole request */
6718 if (co_data(req)) {
6719 req_htx = htx_from_buf(&req->buf);
6720 co_htx_skip(req, req_htx, co_data(req));
6721 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01006722 }
6723 }
6724
6725 out:
Christopher Faulet9c832fc2018-12-14 13:34:05 +01006726 htx_to_buf(res_htx, &res->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01006727 return;
6728
6729 error:
6730
6731 /* If we are in HTTP mode, and we are not send any
6732 * data, return a 500 server error in best effort:
6733 * if there is no room available in the buffer,
6734 * just close the connection.
6735 */
6736 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
6737 struct buffer *err = &htx_err_chunks[HTTP_ERR_500];
6738
6739 channel_erase(res);
6740 res->buf.data = b_data(err);
6741 memcpy(res->buf.area, b_head(err), b_data(err));
6742 res_htx = htx_from_buf(&res->buf);
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01006743 channel_add_input(res, res_htx->data);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01006744 }
6745 if (!(strm->flags & SF_ERR_MASK))
6746 strm->flags |= SF_ERR_RESOURCE;
6747 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
6748 goto done;
6749}
6750
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006751static void hlua_applet_http_release(struct appctx *ctx)
6752{
Olivier Houchard3f795f72019-04-17 22:51:06 +02006753 task_destroy(ctx->ctx.hlua_apphttp.task);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006754 ctx->ctx.hlua_apphttp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006755 hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006756 ctx->ctx.hlua_apphttp.hlua = NULL;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006757}
6758
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006759/* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in
6760 * succes case, else return ACT_RET_PRS_ERR.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006761 *
6762 * This function can fail with an abort() due to an Lua critical error.
6763 * We are in the configuration parsing process of HAProxy, this abort() is
6764 * tolerated.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006765 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006766static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px,
6767 struct act_rule *rule, char **err)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006768{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006769 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006770 int i;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006771
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006772 /* Memory for the rule. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006773 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006774 if (!rule->arg.hlua_rule) {
6775 memprintf(err, "out of memory error");
Thierry FOURNIERafa80492015-08-19 09:04:15 +02006776 return ACT_RET_PRS_ERR;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006777 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006778
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006779 /* Memory for arguments. */
6780 rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, sizeof(char *));
6781 if (!rule->arg.hlua_rule->args) {
6782 memprintf(err, "out of memory error");
6783 return ACT_RET_PRS_ERR;
6784 }
6785
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006786 /* Reference the Lua function and store the reference. */
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006787 rule->arg.hlua_rule->fcn = *fcn;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006788
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006789 /* Expect some arguments */
6790 for (i = 0; i < fcn->nargs; i++) {
Thierry FOURNIER1725c2e2019-01-06 19:38:49 +01006791 if (*args[*cur_arg] == '\0') {
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006792 memprintf(err, "expect %d arguments", fcn->nargs);
6793 return ACT_RET_PRS_ERR;
6794 }
Thierry FOURNIER1725c2e2019-01-06 19:38:49 +01006795 rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]);
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006796 if (!rule->arg.hlua_rule->args[i]) {
6797 memprintf(err, "out of memory error");
6798 return ACT_RET_PRS_ERR;
6799 }
6800 (*cur_arg)++;
6801 }
6802 rule->arg.hlua_rule->args[i] = NULL;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006803
Thierry FOURNIER42148732015-09-02 17:17:33 +02006804 rule->action = ACT_CUSTOM;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006805 rule->action_ptr = hlua_action;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02006806 return ACT_RET_PRS_OK;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006807}
6808
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006809static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px,
6810 struct act_rule *rule, char **err)
6811{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006812 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006813
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01006814 /* HTTP applets are forbidden in tcp-request rules.
6815 * HTTP applet request requires everything initilized by
6816 * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER).
6817 * The applet will be immediately initilized, but its before
6818 * the call of this analyzer.
6819 */
6820 if (rule->from != ACT_F_HTTP_REQ) {
6821 memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets");
6822 return ACT_RET_PRS_ERR;
6823 }
6824
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006825 /* Memory for the rule. */
6826 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
6827 if (!rule->arg.hlua_rule) {
6828 memprintf(err, "out of memory error");
6829 return ACT_RET_PRS_ERR;
6830 }
6831
6832 /* Reference the Lua function and store the reference. */
6833 rule->arg.hlua_rule->fcn = *fcn;
6834
6835 /* TODO: later accept arguments. */
6836 rule->arg.hlua_rule->args = NULL;
6837
6838 /* Add applet pointer in the rule. */
6839 rule->applet.obj_type = OBJ_TYPE_APPLET;
6840 rule->applet.name = fcn->name;
6841 rule->applet.init = hlua_applet_http_init;
6842 rule->applet.fct = hlua_applet_http_fct;
6843 rule->applet.release = hlua_applet_http_release;
6844 rule->applet.timeout = hlua_timeout_applet;
6845
6846 return ACT_RET_PRS_OK;
6847}
6848
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006849/* This function is an LUA binding used for registering
6850 * "sample-conv" functions. It expects a converter name used
6851 * in the haproxy configuration file, and an LUA function.
6852 */
6853__LJMP static int hlua_register_action(lua_State *L)
6854{
6855 struct action_kw_list *akl;
6856 const char *name;
6857 int ref;
6858 int len;
6859 struct hlua_function *fcn;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006860 int nargs;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006861
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006862 /* Initialise the number of expected arguments at 0. */
6863 nargs = 0;
6864
6865 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
6866 WILL_LJMP(luaL_error(L, "'register_action' needs between 3 and 4 arguments"));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006867
6868 /* First argument : converter name. */
6869 name = MAY_LJMP(luaL_checkstring(L, 1));
6870
6871 /* Second argument : environment. */
6872 if (lua_type(L, 2) != LUA_TTABLE)
6873 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
6874
6875 /* Third argument : lua function. */
6876 ref = MAY_LJMP(hlua_checkfunction(L, 3));
6877
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006878 /* Fourth argument : number of mandatory arguments expected on the configuration line. */
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006879 if (lua_gettop(L) >= 4)
6880 nargs = MAY_LJMP(luaL_checkinteger(L, 4));
6881
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006882 /* browse the second argument as an array. */
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006883 lua_pushnil(L);
6884 while (lua_next(L, 2) != 0) {
6885 if (lua_type(L, -1) != LUA_TSTRING)
6886 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
6887
6888 /* Check required environment. Only accepted "http" or "tcp". */
6889 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006890 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006891 if (!akl)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006892 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006893 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006894 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006895 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006896
6897 /* Fill fcn. */
6898 fcn->name = strdup(name);
6899 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006900 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006901 fcn->function_ref = ref;
6902
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006903 /* Set the expected number od arguments. */
6904 fcn->nargs = nargs;
6905
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006906 /* List head */
6907 akl->list.n = akl->list.p = NULL;
6908
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006909 /* action keyword. */
6910 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006911 akl->kw[0].kw = calloc(1, len);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006912 if (!akl->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006913 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006914
6915 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
6916
6917 akl->kw[0].match_pfx = 0;
6918 akl->kw[0].private = fcn;
6919 akl->kw[0].parse = action_register_lua;
6920
6921 /* select the action registering point. */
6922 if (strcmp(lua_tostring(L, -1), "tcp-req") == 0)
6923 tcp_req_cont_keywords_register(akl);
6924 else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0)
6925 tcp_res_cont_keywords_register(akl);
6926 else if (strcmp(lua_tostring(L, -1), "http-req") == 0)
6927 http_req_keywords_register(akl);
6928 else if (strcmp(lua_tostring(L, -1), "http-res") == 0)
6929 http_res_keywords_register(akl);
6930 else
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006931 WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. "
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006932 "'tcp-req', 'tcp-res', 'http-req' or 'http-res' "
6933 "are expected.", lua_tostring(L, -1)));
6934
6935 /* pop the environment string. */
6936 lua_pop(L, 1);
6937 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006938 return ACT_RET_PRS_OK;
6939}
6940
6941static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px,
6942 struct act_rule *rule, char **err)
6943{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006944 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006945
Christopher Faulet280f85b2019-07-15 15:02:04 +02006946 if (px->mode == PR_MODE_HTTP) {
6947 memprintf(err, "Lua TCP services cannot be used on HTTP proxies");
Christopher Fauletafd8f102018-11-08 11:34:21 +01006948 return ACT_RET_PRS_ERR;
6949 }
6950
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006951 /* Memory for the rule. */
6952 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
6953 if (!rule->arg.hlua_rule) {
6954 memprintf(err, "out of memory error");
6955 return ACT_RET_PRS_ERR;
6956 }
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006957
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006958 /* Reference the Lua function and store the reference. */
6959 rule->arg.hlua_rule->fcn = *fcn;
6960
6961 /* TODO: later accept arguments. */
6962 rule->arg.hlua_rule->args = NULL;
6963
6964 /* Add applet pointer in the rule. */
6965 rule->applet.obj_type = OBJ_TYPE_APPLET;
6966 rule->applet.name = fcn->name;
6967 rule->applet.init = hlua_applet_tcp_init;
6968 rule->applet.fct = hlua_applet_tcp_fct;
6969 rule->applet.release = hlua_applet_tcp_release;
6970 rule->applet.timeout = hlua_timeout_applet;
6971
6972 return 0;
6973}
6974
6975/* This function is an LUA binding used for registering
6976 * "sample-conv" functions. It expects a converter name used
6977 * in the haproxy configuration file, and an LUA function.
6978 */
6979__LJMP static int hlua_register_service(lua_State *L)
6980{
6981 struct action_kw_list *akl;
6982 const char *name;
6983 const char *env;
6984 int ref;
6985 int len;
6986 struct hlua_function *fcn;
6987
6988 MAY_LJMP(check_args(L, 3, "register_service"));
6989
6990 /* First argument : converter name. */
6991 name = MAY_LJMP(luaL_checkstring(L, 1));
6992
6993 /* Second argument : environment. */
6994 env = MAY_LJMP(luaL_checkstring(L, 2));
6995
6996 /* Third argument : lua function. */
6997 ref = MAY_LJMP(hlua_checkfunction(L, 3));
6998
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006999 /* Allocate and fill the sample fetch keyword struct. */
7000 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
7001 if (!akl)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007002 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007003 fcn = calloc(1, sizeof(*fcn));
7004 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007005 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007006
7007 /* Fill fcn. */
7008 len = strlen("<lua.>") + strlen(name) + 1;
7009 fcn->name = calloc(1, len);
7010 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007011 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007012 snprintf((char *)fcn->name, len, "<lua.%s>", name);
7013 fcn->function_ref = ref;
7014
7015 /* List head */
7016 akl->list.n = akl->list.p = NULL;
7017
7018 /* converter keyword. */
7019 len = strlen("lua.") + strlen(name) + 1;
7020 akl->kw[0].kw = calloc(1, len);
7021 if (!akl->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007022 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007023
7024 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
7025
Thierry FOURNIER / OZON.IO02564fd2016-11-12 11:07:05 +01007026 /* Check required environment. Only accepted "http" or "tcp". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007027 if (strcmp(env, "tcp") == 0)
7028 akl->kw[0].parse = action_register_service_tcp;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007029 else if (strcmp(env, "http") == 0)
7030 akl->kw[0].parse = action_register_service_http;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007031 else
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007032 WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. "
Eric Salamafe7456f2017-12-21 14:30:07 +01007033 "'tcp' or 'http' are expected.", env));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007034
7035 akl->kw[0].match_pfx = 0;
7036 akl->kw[0].private = fcn;
7037
7038 /* End of array. */
7039 memset(&akl->kw[1], 0, sizeof(*akl->kw));
7040
7041 /* Register this new converter */
7042 service_keywords_register(akl);
7043
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007044 return 0;
7045}
7046
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007047/* This function initialises Lua cli handler. It copies the
7048 * arguments in the Lua stack and create channel IO objects.
7049 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02007050static int hlua_cli_parse_fct(char **args, char *payload, struct appctx *appctx, void *private)
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007051{
7052 struct hlua *hlua;
7053 struct hlua_function *fcn;
7054 int i;
7055 const char *error;
7056
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007057 fcn = private;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007058 appctx->ctx.hlua_cli.fcn = private;
7059
Willy Tarreaubafbe012017-11-24 17:34:44 +01007060 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007061 if (!hlua) {
7062 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007063 return 1;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007064 }
7065 HLUA_INIT(hlua);
7066 appctx->ctx.hlua_cli.hlua = hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007067
7068 /* Create task used by signal to wakeup applets.
7069 * We use the same wakeup fonction than the Lua applet_tcp and
7070 * applet_http. It is absolutely compatible.
7071 */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01007072 appctx->ctx.hlua_cli.task = task_new(tid_bit);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007073 if (!appctx->ctx.hlua_cli.task) {
Thierry FOURNIERffbf5692016-12-16 11:14:06 +01007074 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007075 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007076 }
7077 appctx->ctx.hlua_cli.task->nice = 0;
7078 appctx->ctx.hlua_cli.task->context = appctx;
7079 appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup;
7080
7081 /* Initialises the Lua context */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01007082 if (!hlua_ctx_init(hlua, appctx->ctx.hlua_cli.task, 0)) {
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007083 SEND_ERR(NULL, "Lua cli '%s': can't initialize Lua context.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007084 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007085 }
7086
7087 /* The following Lua calls can fail. */
7088 if (!SET_SAFE_LJMP(hlua->T)) {
7089 if (lua_type(hlua->T, -1) == LUA_TSTRING)
7090 error = lua_tostring(hlua->T, -1);
7091 else
7092 error = "critical error";
7093 SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error);
7094 goto error;
7095 }
7096
7097 /* Check stack available size. */
7098 if (!lua_checkstack(hlua->T, 2)) {
7099 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7100 goto error;
7101 }
7102
7103 /* Restore the function in the stack. */
7104 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
7105
7106 /* Once the arguments parsed, the CLI is like an AppletTCP,
7107 * so push AppletTCP in the stack.
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007108 */
7109 if (!hlua_applet_tcp_new(hlua->T, appctx)) {
7110 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7111 goto error;
7112 }
7113 hlua->nargs = 1;
7114
7115 /* push keywords in the stack. */
7116 for (i = 0; *args[i]; i++) {
7117 /* Check stack available size. */
7118 if (!lua_checkstack(hlua->T, 1)) {
7119 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7120 goto error;
7121 }
7122 lua_pushstring(hlua->T, args[i]);
7123 hlua->nargs++;
7124 }
7125
7126 /* We must initialize the execution timeouts. */
7127 hlua->max_time = hlua_timeout_session;
7128
7129 /* At this point the execution is safe. */
7130 RESET_SAFE_LJMP(hlua->T);
7131
7132 /* It's ok */
7133 return 0;
7134
7135 /* It's not ok. */
7136error:
7137 RESET_SAFE_LJMP(hlua->T);
7138 hlua_ctx_destroy(hlua);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007139 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007140 return 1;
7141}
7142
7143static int hlua_cli_io_handler_fct(struct appctx *appctx)
7144{
7145 struct hlua *hlua;
7146 struct stream_interface *si;
7147 struct hlua_function *fcn;
7148
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007149 hlua = appctx->ctx.hlua_cli.hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007150 si = appctx->owner;
Willy Tarreau8ae4f752016-12-14 15:41:45 +01007151 fcn = appctx->ctx.hlua_cli.fcn;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007152
7153 /* If the stream is disconnect or closed, ldo nothing. */
7154 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
7155 return 1;
7156
7157 /* Execute the function. */
7158 switch (hlua_ctx_resume(hlua, 1)) {
7159
7160 /* finished. */
7161 case HLUA_E_OK:
7162 return 1;
7163
7164 /* yield. */
7165 case HLUA_E_AGAIN:
7166 /* We want write. */
7167 if (HLUA_IS_WAKERESWR(hlua))
Willy Tarreaudb398432018-11-15 11:08:52 +01007168 si_rx_room_blk(si);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007169 /* Set the timeout. */
7170 if (hlua->wake_time != TICK_ETERNITY)
7171 task_schedule(hlua->task, hlua->wake_time);
7172 return 0;
7173
7174 /* finished with error. */
7175 case HLUA_E_ERRMSG:
7176 /* Display log. */
7177 SEND_ERR(NULL, "Lua cli '%s': %s.\n",
7178 fcn->name, lua_tostring(hlua->T, -1));
7179 lua_pop(hlua->T, 1);
7180 return 1;
7181
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007182 case HLUA_E_ETMOUT:
7183 SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n",
7184 fcn->name);
7185 return 1;
7186
7187 case HLUA_E_NOMEM:
7188 SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n",
7189 fcn->name);
7190 return 1;
7191
7192 case HLUA_E_YIELD: /* unexpected */
7193 SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n",
7194 fcn->name);
7195 return 1;
7196
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007197 case HLUA_E_ERR:
7198 /* Display log. */
7199 SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n",
7200 fcn->name);
7201 return 1;
7202
7203 default:
7204 return 1;
7205 }
7206
7207 return 1;
7208}
7209
7210static void hlua_cli_io_release_fct(struct appctx *appctx)
7211{
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007212 hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007213 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007214}
7215
7216/* This function is an LUA binding used for registering
7217 * new keywords in the cli. It expects a list of keywords
7218 * which are the "path". It is limited to 5 keywords. A
7219 * description of the command, a function to be executed
7220 * for the parsing and a function for io handlers.
7221 */
7222__LJMP static int hlua_register_cli(lua_State *L)
7223{
7224 struct cli_kw_list *cli_kws;
7225 const char *message;
7226 int ref_io;
7227 int len;
7228 struct hlua_function *fcn;
7229 int index;
7230 int i;
7231
7232 MAY_LJMP(check_args(L, 3, "register_cli"));
7233
7234 /* First argument : an array of maximum 5 keywords. */
7235 if (!lua_istable(L, 1))
7236 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table"));
7237
7238 /* Second argument : string with contextual message. */
7239 message = MAY_LJMP(luaL_checkstring(L, 2));
7240
7241 /* Third and fourth argument : lua function. */
7242 ref_io = MAY_LJMP(hlua_checkfunction(L, 3));
7243
7244 /* Allocate and fill the sample fetch keyword struct. */
7245 cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2);
7246 if (!cli_kws)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007247 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007248 fcn = calloc(1, sizeof(*fcn));
7249 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007250 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007251
7252 /* Fill path. */
7253 index = 0;
7254 lua_pushnil(L);
7255 while(lua_next(L, 1) != 0) {
7256 if (index >= 5)
7257 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries"));
7258 if (lua_type(L, -1) != LUA_TSTRING)
7259 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings"));
7260 cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1));
7261 if (!cli_kws->kw[0].str_kw[index])
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007262 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007263 index++;
7264 lua_pop(L, 1);
7265 }
7266
7267 /* Copy help message. */
7268 cli_kws->kw[0].usage = strdup(message);
7269 if (!cli_kws->kw[0].usage)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007270 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007271
7272 /* Fill fcn io handler. */
7273 len = strlen("<lua.cli>") + 1;
7274 for (i = 0; i < index; i++)
7275 len += strlen(cli_kws->kw[0].str_kw[i]) + 1;
7276 fcn->name = calloc(1, len);
7277 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007278 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007279 strncat((char *)fcn->name, "<lua.cli", len);
7280 for (i = 0; i < index; i++) {
7281 strncat((char *)fcn->name, ".", len);
7282 strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len);
7283 }
7284 strncat((char *)fcn->name, ">", len);
7285 fcn->function_ref = ref_io;
7286
7287 /* Fill last entries. */
7288 cli_kws->kw[0].private = fcn;
7289 cli_kws->kw[0].parse = hlua_cli_parse_fct;
7290 cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct;
7291 cli_kws->kw[0].io_release = hlua_cli_io_release_fct;
7292
7293 /* Register this new converter */
7294 cli_register_kw(cli_kws);
7295
7296 return 0;
7297}
7298
Thierry FOURNIERbd413492015-03-03 16:52:26 +01007299static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx,
7300 struct proxy *defpx, const char *file, int line,
7301 char **err, unsigned int *timeout)
7302{
7303 const char *error;
7304
7305 error = parse_time_err(args[1], timeout, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02007306 if (error == PARSE_TIME_OVER) {
7307 memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)",
7308 args[1], args[0]);
7309 return -1;
7310 }
7311 else if (error == PARSE_TIME_UNDER) {
7312 memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)",
7313 args[1], args[0]);
7314 return -1;
7315 }
7316 else if (error) {
Thierry FOURNIERbd413492015-03-03 16:52:26 +01007317 memprintf(err, "%s: invalid timeout", args[0]);
7318 return -1;
7319 }
7320 return 0;
7321}
7322
7323static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx,
7324 struct proxy *defpx, const char *file, int line,
7325 char **err)
7326{
7327 return hlua_read_timeout(args, section_type, curpx, defpx,
7328 file, line, err, &hlua_timeout_session);
7329}
7330
7331static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx,
7332 struct proxy *defpx, const char *file, int line,
7333 char **err)
7334{
7335 return hlua_read_timeout(args, section_type, curpx, defpx,
7336 file, line, err, &hlua_timeout_task);
7337}
7338
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007339static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx,
7340 struct proxy *defpx, const char *file, int line,
7341 char **err)
7342{
7343 return hlua_read_timeout(args, section_type, curpx, defpx,
7344 file, line, err, &hlua_timeout_applet);
7345}
7346
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01007347static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx,
7348 struct proxy *defpx, const char *file, int line,
7349 char **err)
7350{
7351 char *error;
7352
7353 hlua_nb_instruction = strtoll(args[1], &error, 10);
7354 if (*error != '\0') {
7355 memprintf(err, "%s: invalid number", args[0]);
7356 return -1;
7357 }
7358 return 0;
7359}
7360
Willy Tarreau32f61e22015-03-18 17:54:59 +01007361static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx,
7362 struct proxy *defpx, const char *file, int line,
7363 char **err)
7364{
7365 char *error;
7366
7367 if (*(args[1]) == 0) {
7368 memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]);
7369 return -1;
7370 }
7371 hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L;
7372 if (*error != '\0') {
7373 memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error);
7374 return -1;
7375 }
7376 return 0;
7377}
7378
7379
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007380/* This function is called by the main configuration key "lua-load". It loads and
7381 * execute an lua file during the parsing of the HAProxy configuration file. It is
7382 * the main lua entry point.
7383 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007384 * This function runs with the HAProxy keywords API. It returns -1 if an error
7385 * occurs, otherwise it returns 0.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007386 *
7387 * In some error case, LUA set an error message in top of the stack. This function
7388 * returns this error message in the HAProxy logs and pop it from the stack.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007389 *
7390 * This function can fail with an abort() due to an Lua critical error.
7391 * We are in the configuration parsing process of HAProxy, this abort() is
7392 * tolerated.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007393 */
7394static int hlua_load(char **args, int section_type, struct proxy *curpx,
7395 struct proxy *defpx, const char *file, int line,
7396 char **err)
7397{
7398 int error;
7399
7400 /* Just load and compile the file. */
7401 error = luaL_loadfile(gL.T, args[1]);
7402 if (error) {
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007403 memprintf(err, "error in Lua file '%s': %s", args[1], lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007404 lua_pop(gL.T, 1);
7405 return -1;
7406 }
7407
7408 /* If no syntax error where detected, execute the code. */
7409 error = lua_pcall(gL.T, 0, LUA_MULTRET, 0);
7410 switch (error) {
7411 case LUA_OK:
7412 break;
7413 case LUA_ERRRUN:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007414 memprintf(err, "Lua runtime error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007415 lua_pop(gL.T, 1);
7416 return -1;
7417 case LUA_ERRMEM:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007418 memprintf(err, "Lua out of memory error.n");
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007419 return -1;
7420 case LUA_ERRERR:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007421 memprintf(err, "Lua message handler error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007422 lua_pop(gL.T, 1);
7423 return -1;
7424 case LUA_ERRGCMM:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007425 memprintf(err, "Lua garbage collector error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007426 lua_pop(gL.T, 1);
7427 return -1;
7428 default:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007429 memprintf(err, "Lua unknonwn error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007430 lua_pop(gL.T, 1);
7431 return -1;
7432 }
7433
7434 return 0;
7435}
7436
7437/* configuration keywords declaration */
7438static struct cfg_kw_list cfg_kws = {{ },{
Thierry FOURNIERbd413492015-03-03 16:52:26 +01007439 { CFG_GLOBAL, "lua-load", hlua_load },
7440 { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout },
7441 { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout },
Thierry FOURNIER56da1012015-10-01 08:42:31 +02007442 { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout },
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01007443 { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield },
Willy Tarreau32f61e22015-03-18 17:54:59 +01007444 { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem },
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007445 { 0, NULL, NULL },
7446}};
7447
Willy Tarreau0108d902018-11-25 19:14:37 +01007448INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
7449
Christopher Fauletafd8f102018-11-08 11:34:21 +01007450
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007451/* This function can fail with an abort() due to an Lua critical error.
7452 * We are in the initialisation process of HAProxy, this abort() is
7453 * tolerated.
7454 */
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007455int hlua_post_init()
7456{
7457 struct hlua_init_function *init;
7458 const char *msg;
7459 enum hlua_exec ret;
Thierry Fournierfd107a22016-02-19 19:57:23 +01007460 const char *error;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007461
Thierry Fournier3d4a6752016-02-19 20:53:30 +01007462 /* Call post initialisation function in safe environement. */
7463 if (!SET_SAFE_LJMP(gL.T)) {
7464 if (lua_type(gL.T, -1) == LUA_TSTRING)
7465 error = lua_tostring(gL.T, -1);
7466 else
7467 error = "critical error";
7468 fprintf(stderr, "Lua post-init: %s.\n", error);
7469 exit(1);
7470 }
Frédéric Lécaille54f2bcf2018-08-29 13:46:24 +02007471
7472#if USE_OPENSSL
7473 /* Initialize SSL server. */
7474 if (socket_ssl.xprt->prepare_srv) {
7475 int saved_used_backed = global.ssl_used_backend;
7476 // don't affect maxconn automatic computation
7477 socket_ssl.xprt->prepare_srv(&socket_ssl);
7478 global.ssl_used_backend = saved_used_backed;
7479 }
7480#endif
7481
Thierry Fournier3d4a6752016-02-19 20:53:30 +01007482 hlua_fcn_post_init(gL.T);
7483 RESET_SAFE_LJMP(gL.T);
7484
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007485 list_for_each_entry(init, &hlua_init_functions, l) {
7486 lua_rawgeti(gL.T, LUA_REGISTRYINDEX, init->function_ref);
7487 ret = hlua_ctx_resume(&gL, 0);
7488 switch (ret) {
7489 case HLUA_E_OK:
7490 lua_pop(gL.T, -1);
7491 return 1;
7492 case HLUA_E_AGAIN:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007493 ha_alert("Lua init: yield not allowed.\n");
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007494 return 0;
7495 case HLUA_E_ERRMSG:
7496 msg = lua_tostring(gL.T, -1);
Christopher Faulet767a84b2017-11-24 16:50:31 +01007497 ha_alert("lua init: %s.\n", msg);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007498 return 0;
7499 case HLUA_E_ERR:
7500 default:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007501 ha_alert("Lua init: unknown runtime error.\n");
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007502 return 0;
7503 }
7504 }
7505 return 1;
7506}
7507
Willy Tarreau32f61e22015-03-18 17:54:59 +01007508/* The memory allocator used by the Lua stack. <ud> is a pointer to the
7509 * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize>
7510 * is the previously allocated size or the kind of object in case of a new
7511 * allocation. <nsize> is the requested new size.
7512 */
7513static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
7514{
7515 struct hlua_mem_allocator *zone = ud;
7516
7517 if (nsize == 0) {
7518 /* it's a free */
7519 if (ptr)
7520 zone->allocated -= osize;
7521 free(ptr);
7522 return NULL;
7523 }
7524
7525 if (!ptr) {
7526 /* it's a new allocation */
7527 if (zone->limit && zone->allocated + nsize > zone->limit)
7528 return NULL;
7529
7530 ptr = malloc(nsize);
7531 if (ptr)
7532 zone->allocated += nsize;
7533 return ptr;
7534 }
7535
7536 /* it's a realloc */
7537 if (zone->limit && zone->allocated + nsize - osize > zone->limit)
7538 return NULL;
7539
7540 ptr = realloc(ptr, nsize);
7541 if (ptr)
7542 zone->allocated += nsize - osize;
7543 return ptr;
7544}
7545
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007546/* Ithis function can fail with an abort() due to an Lua critical error.
7547 * We are in the initialisation process of HAProxy, this abort() is
7548 * tolerated.
7549 */
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01007550void hlua_init(void)
7551{
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007552 int i;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01007553 int idx;
7554 struct sample_fetch *sf;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007555 struct sample_conv *sc;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01007556 char *p;
Thierry Fournierfd107a22016-02-19 19:57:23 +01007557 const char *error_msg;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01007558#ifdef USE_OPENSSL
Willy Tarreau80f5fae2015-02-27 16:38:20 +01007559 struct srv_kw *kw;
7560 int tmp_error;
7561 char *error;
Thierry FOURNIER36d13742015-03-17 16:48:53 +01007562 char *args[] = { /* SSL client configuration. */
7563 "ssl",
7564 "verify",
7565 "none",
Thierry FOURNIER36d13742015-03-17 16:48:53 +01007566 NULL
7567 };
Willy Tarreau80f5fae2015-02-27 16:38:20 +01007568#endif
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007569
Thierry FOURNIER380d0932015-01-23 14:27:52 +01007570 /* Init main lua stack. */
7571 gL.Mref = LUA_REFNIL;
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01007572 gL.flags = 0;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01007573 LIST_INIT(&gL.com);
Willy Tarreau42ef75f2017-04-12 21:40:29 +02007574 gL.T = lua_newstate(hlua_alloc, &hlua_global_allocator);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01007575 hlua_sethlua(&gL);
7576 gL.Tref = LUA_REFNIL;
7577 gL.task = NULL;
7578
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007579 /* From this point, until the end of the initialisation function,
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007580 * the Lua function can fail with an abort. We are in the initialisation
7581 * process of HAProxy, this abort() is tolerated.
7582 */
7583
Thierry FOURNIER380d0932015-01-23 14:27:52 +01007584 /* Initialise lua. */
7585 luaL_openlibs(gL.T);
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007586
Thierry Fournier75933d42016-01-21 09:30:18 +01007587 /* Set safe environment for the initialisation. */
7588 if (!SET_SAFE_LJMP(gL.T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01007589 if (lua_type(gL.T, -1) == LUA_TSTRING)
7590 error_msg = lua_tostring(gL.T, -1);
7591 else
7592 error_msg = "critical error";
7593 fprintf(stderr, "Lua init: %s.\n", error_msg);
Thierry Fournier75933d42016-01-21 09:30:18 +01007594 exit(1);
7595 }
7596
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007597 /*
7598 *
7599 * Create "core" object.
7600 *
7601 */
7602
Thierry FOURNIERa2d8c652015-03-11 17:29:39 +01007603 /* This table entry is the object "core" base. */
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007604 lua_newtable(gL.T);
7605
7606 /* Push the loglevel constants. */
Willy Tarreau80f5fae2015-02-27 16:38:20 +01007607 for (i = 0; i < NB_LOG_LEVELS; i++)
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007608 hlua_class_const_int(gL.T, log_levels[i], i);
7609
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007610 /* Register special functions. */
7611 hlua_class_function(gL.T, "register_init", hlua_register_init);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01007612 hlua_class_function(gL.T, "register_task", hlua_register_task);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01007613 hlua_class_function(gL.T, "register_fetches", hlua_register_fetches);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01007614 hlua_class_function(gL.T, "register_converters", hlua_register_converters);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007615 hlua_class_function(gL.T, "register_action", hlua_register_action);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007616 hlua_class_function(gL.T, "register_service", hlua_register_service);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007617 hlua_class_function(gL.T, "register_cli", hlua_register_cli);
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01007618 hlua_class_function(gL.T, "yield", hlua_yield);
Willy Tarreau59551662015-03-10 14:23:13 +01007619 hlua_class_function(gL.T, "set_nice", hlua_set_nice);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01007620 hlua_class_function(gL.T, "sleep", hlua_sleep);
7621 hlua_class_function(gL.T, "msleep", hlua_msleep);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01007622 hlua_class_function(gL.T, "add_acl", hlua_add_acl);
7623 hlua_class_function(gL.T, "del_acl", hlua_del_acl);
7624 hlua_class_function(gL.T, "set_map", hlua_set_map);
7625 hlua_class_function(gL.T, "del_map", hlua_del_map);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007626 hlua_class_function(gL.T, "tcp", hlua_socket_new);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01007627 hlua_class_function(gL.T, "log", hlua_log);
7628 hlua_class_function(gL.T, "Debug", hlua_log_debug);
7629 hlua_class_function(gL.T, "Info", hlua_log_info);
7630 hlua_class_function(gL.T, "Warning", hlua_log_warning);
7631 hlua_class_function(gL.T, "Alert", hlua_log_alert);
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02007632 hlua_class_function(gL.T, "done", hlua_done);
Thierry Fournierfb0b5462016-01-21 09:28:58 +01007633 hlua_fcn_reg_core_fcn(gL.T);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007634
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007635 lua_setglobal(gL.T, "core");
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01007636
7637 /*
7638 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02007639 * Register class Map
7640 *
7641 */
7642
7643 /* This table entry is the object "Map" base. */
7644 lua_newtable(gL.T);
7645
7646 /* register pattern types. */
7647 for (i=0; i<PAT_MATCH_NUM; i++)
7648 hlua_class_const_int(gL.T, pat_match_names[i], i);
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01007649 for (i=0; i<PAT_MATCH_NUM; i++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007650 snprintf(trash.area, trash.size, "_%s", pat_match_names[i]);
7651 hlua_class_const_int(gL.T, trash.area, i);
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01007652 }
Thierry FOURNIER3def3932015-04-07 11:27:54 +02007653
7654 /* register constructor. */
7655 hlua_class_function(gL.T, "new", hlua_map_new);
7656
7657 /* Create and fill the metatable. */
7658 lua_newtable(gL.T);
7659
7660 /* Create and fille the __index entry. */
7661 lua_pushstring(gL.T, "__index");
7662 lua_newtable(gL.T);
7663
7664 /* Register . */
7665 hlua_class_function(gL.T, "lookup", hlua_map_lookup);
7666 hlua_class_function(gL.T, "slookup", hlua_map_slookup);
7667
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007668 lua_rawset(gL.T, -3);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02007669
Thierry Fournier45e78d72016-02-19 18:34:46 +01007670 /* Register previous table in the registry with reference and named entry.
7671 * The function hlua_register_metatable() pops the stack, so we
7672 * previously create a copy of the table.
7673 */
Thierry FOURNIER3def3932015-04-07 11:27:54 +02007674 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007675 class_map_ref = hlua_register_metatable(gL.T, CLASS_MAP);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02007676
7677 /* Assign the metatable to the mai Map object. */
7678 lua_setmetatable(gL.T, -2);
7679
7680 /* Set a name to the table. */
7681 lua_setglobal(gL.T, "Map");
7682
7683 /*
7684 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01007685 * Register class Channel
7686 *
7687 */
7688
7689 /* Create and fill the metatable. */
7690 lua_newtable(gL.T);
7691
7692 /* Create and fille the __index entry. */
7693 lua_pushstring(gL.T, "__index");
7694 lua_newtable(gL.T);
7695
7696 /* Register . */
7697 hlua_class_function(gL.T, "get", hlua_channel_get);
7698 hlua_class_function(gL.T, "dup", hlua_channel_dup);
7699 hlua_class_function(gL.T, "getline", hlua_channel_getline);
7700 hlua_class_function(gL.T, "set", hlua_channel_set);
7701 hlua_class_function(gL.T, "append", hlua_channel_append);
7702 hlua_class_function(gL.T, "send", hlua_channel_send);
7703 hlua_class_function(gL.T, "forward", hlua_channel_forward);
7704 hlua_class_function(gL.T, "get_in_len", hlua_channel_get_in_len);
7705 hlua_class_function(gL.T, "get_out_len", hlua_channel_get_out_len);
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01007706 hlua_class_function(gL.T, "is_full", hlua_channel_is_full);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01007707
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007708 lua_rawset(gL.T, -3);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01007709
7710 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007711 class_channel_ref = hlua_register_metatable(gL.T, CLASS_CHANNEL);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01007712
7713 /*
7714 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01007715 * Register class Fetches
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01007716 *
7717 */
7718
7719 /* Create and fill the metatable. */
7720 lua_newtable(gL.T);
7721
7722 /* Create and fille the __index entry. */
7723 lua_pushstring(gL.T, "__index");
7724 lua_newtable(gL.T);
7725
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01007726 /* Browse existing fetches and create the associated
7727 * object method.
7728 */
7729 sf = NULL;
7730 while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) {
7731
7732 /* Dont register the keywork if the arguments check function are
7733 * not safe during the runtime.
7734 */
7735 if ((sf->val_args != NULL) &&
7736 (sf->val_args != val_payload_lv) &&
7737 (sf->val_args != val_hdr))
7738 continue;
7739
7740 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
7741 * by an underscore.
7742 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007743 strncpy(trash.area, sf->kw, trash.size);
7744 trash.area[trash.size - 1] = '\0';
7745 for (p = trash.area; *p; p++)
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01007746 if (*p == '.' || *p == '-' || *p == '+')
7747 *p = '_';
7748
7749 /* Register the function. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007750 lua_pushstring(gL.T, trash.area);
Willy Tarreau2ec22742015-03-10 14:27:20 +01007751 lua_pushlightuserdata(gL.T, sf);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01007752 lua_pushcclosure(gL.T, hlua_run_sample_fetch, 1);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007753 lua_rawset(gL.T, -3);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01007754 }
7755
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007756 lua_rawset(gL.T, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01007757
7758 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007759 class_fetches_ref = hlua_register_metatable(gL.T, CLASS_FETCHES);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01007760
7761 /*
7762 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007763 * Register class Converters
7764 *
7765 */
7766
7767 /* Create and fill the metatable. */
7768 lua_newtable(gL.T);
7769
7770 /* Create and fill the __index entry. */
7771 lua_pushstring(gL.T, "__index");
7772 lua_newtable(gL.T);
7773
7774 /* Browse existing converters and create the associated
7775 * object method.
7776 */
7777 sc = NULL;
7778 while ((sc = sample_conv_getnext(sc, &idx)) != NULL) {
7779 /* Dont register the keywork if the arguments check function are
7780 * not safe during the runtime.
7781 */
7782 if (sc->val_args != NULL)
7783 continue;
7784
7785 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
7786 * by an underscore.
7787 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007788 strncpy(trash.area, sc->kw, trash.size);
7789 trash.area[trash.size - 1] = '\0';
7790 for (p = trash.area; *p; p++)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007791 if (*p == '.' || *p == '-' || *p == '+')
7792 *p = '_';
7793
7794 /* Register the function. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007795 lua_pushstring(gL.T, trash.area);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007796 lua_pushlightuserdata(gL.T, sc);
7797 lua_pushcclosure(gL.T, hlua_run_sample_conv, 1);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007798 lua_rawset(gL.T, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007799 }
7800
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007801 lua_rawset(gL.T, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007802
7803 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007804 class_converters_ref = hlua_register_metatable(gL.T, CLASS_CONVERTERS);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007805
7806 /*
7807 *
Thierry FOURNIER08504f42015-03-16 14:17:08 +01007808 * Register class HTTP
7809 *
7810 */
7811
7812 /* Create and fill the metatable. */
7813 lua_newtable(gL.T);
7814
7815 /* Create and fille the __index entry. */
7816 lua_pushstring(gL.T, "__index");
7817 lua_newtable(gL.T);
7818
7819 /* Register Lua functions. */
7820 hlua_class_function(gL.T, "req_get_headers",hlua_http_req_get_headers);
7821 hlua_class_function(gL.T, "req_del_header", hlua_http_req_del_hdr);
7822 hlua_class_function(gL.T, "req_rep_header", hlua_http_req_rep_hdr);
7823 hlua_class_function(gL.T, "req_rep_value", hlua_http_req_rep_val);
7824 hlua_class_function(gL.T, "req_add_header", hlua_http_req_add_hdr);
7825 hlua_class_function(gL.T, "req_set_header", hlua_http_req_set_hdr);
7826 hlua_class_function(gL.T, "req_set_method", hlua_http_req_set_meth);
7827 hlua_class_function(gL.T, "req_set_path", hlua_http_req_set_path);
7828 hlua_class_function(gL.T, "req_set_query", hlua_http_req_set_query);
7829 hlua_class_function(gL.T, "req_set_uri", hlua_http_req_set_uri);
7830
7831 hlua_class_function(gL.T, "res_get_headers",hlua_http_res_get_headers);
7832 hlua_class_function(gL.T, "res_del_header", hlua_http_res_del_hdr);
7833 hlua_class_function(gL.T, "res_rep_header", hlua_http_res_rep_hdr);
7834 hlua_class_function(gL.T, "res_rep_value", hlua_http_res_rep_val);
7835 hlua_class_function(gL.T, "res_add_header", hlua_http_res_add_hdr);
7836 hlua_class_function(gL.T, "res_set_header", hlua_http_res_set_hdr);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02007837 hlua_class_function(gL.T, "res_set_status", hlua_http_res_set_status);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01007838
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007839 lua_rawset(gL.T, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01007840
7841 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007842 class_http_ref = hlua_register_metatable(gL.T, CLASS_HTTP);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01007843
7844 /*
7845 *
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007846 * Register class AppletTCP
7847 *
7848 */
7849
7850 /* Create and fill the metatable. */
7851 lua_newtable(gL.T);
7852
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007853 /* Create and fille the __index entry. */
7854 lua_pushstring(gL.T, "__index");
7855 lua_newtable(gL.T);
7856
7857 /* Register Lua functions. */
Thierry FOURNIER / OZON.IO3e1d7912016-12-12 12:29:34 +01007858 hlua_class_function(gL.T, "getline", hlua_applet_tcp_getline);
7859 hlua_class_function(gL.T, "receive", hlua_applet_tcp_recv);
7860 hlua_class_function(gL.T, "send", hlua_applet_tcp_send);
7861 hlua_class_function(gL.T, "set_priv", hlua_applet_tcp_set_priv);
7862 hlua_class_function(gL.T, "get_priv", hlua_applet_tcp_get_priv);
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01007863 hlua_class_function(gL.T, "set_var", hlua_applet_tcp_set_var);
7864 hlua_class_function(gL.T, "unset_var", hlua_applet_tcp_unset_var);
7865 hlua_class_function(gL.T, "get_var", hlua_applet_tcp_get_var);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007866
7867 lua_settable(gL.T, -3);
7868
7869 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007870 class_applet_tcp_ref = hlua_register_metatable(gL.T, CLASS_APPLET_TCP);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007871
7872 /*
7873 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007874 * Register class AppletHTTP
7875 *
7876 */
7877
7878 /* Create and fill the metatable. */
7879 lua_newtable(gL.T);
7880
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007881 /* Create and fille the __index entry. */
7882 lua_pushstring(gL.T, "__index");
7883 lua_newtable(gL.T);
7884
7885 /* Register Lua functions. */
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01007886 hlua_class_function(gL.T, "set_priv", hlua_applet_http_set_priv);
7887 hlua_class_function(gL.T, "get_priv", hlua_applet_http_get_priv);
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01007888 hlua_class_function(gL.T, "set_var", hlua_applet_http_set_var);
7889 hlua_class_function(gL.T, "unset_var", hlua_applet_http_unset_var);
7890 hlua_class_function(gL.T, "get_var", hlua_applet_http_get_var);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007891 hlua_class_function(gL.T, "getline", hlua_applet_http_getline);
7892 hlua_class_function(gL.T, "receive", hlua_applet_http_recv);
7893 hlua_class_function(gL.T, "send", hlua_applet_http_send);
7894 hlua_class_function(gL.T, "add_header", hlua_applet_http_addheader);
7895 hlua_class_function(gL.T, "set_status", hlua_applet_http_status);
7896 hlua_class_function(gL.T, "start_response", hlua_applet_http_start_response);
7897
7898 lua_settable(gL.T, -3);
7899
7900 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007901 class_applet_http_ref = hlua_register_metatable(gL.T, CLASS_APPLET_HTTP);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007902
7903 /*
7904 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01007905 * Register class TXN
7906 *
7907 */
7908
7909 /* Create and fill the metatable. */
7910 lua_newtable(gL.T);
7911
7912 /* Create and fille the __index entry. */
7913 lua_pushstring(gL.T, "__index");
7914 lua_newtable(gL.T);
7915
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01007916 /* Register Lua functions. */
Patrick Hemmer268a7072018-05-11 12:52:31 -04007917 hlua_class_function(gL.T, "set_priv", hlua_set_priv);
7918 hlua_class_function(gL.T, "get_priv", hlua_get_priv);
7919 hlua_class_function(gL.T, "set_var", hlua_set_var);
7920 hlua_class_function(gL.T, "unset_var", hlua_unset_var);
7921 hlua_class_function(gL.T, "get_var", hlua_get_var);
7922 hlua_class_function(gL.T, "done", hlua_txn_done);
7923 hlua_class_function(gL.T, "set_loglevel", hlua_txn_set_loglevel);
7924 hlua_class_function(gL.T, "set_tos", hlua_txn_set_tos);
7925 hlua_class_function(gL.T, "set_mark", hlua_txn_set_mark);
7926 hlua_class_function(gL.T, "set_priority_class", hlua_txn_set_priority_class);
7927 hlua_class_function(gL.T, "set_priority_offset", hlua_txn_set_priority_offset);
7928 hlua_class_function(gL.T, "deflog", hlua_txn_deflog);
7929 hlua_class_function(gL.T, "log", hlua_txn_log);
7930 hlua_class_function(gL.T, "Debug", hlua_txn_log_debug);
7931 hlua_class_function(gL.T, "Info", hlua_txn_log_info);
7932 hlua_class_function(gL.T, "Warning", hlua_txn_log_warning);
7933 hlua_class_function(gL.T, "Alert", hlua_txn_log_alert);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01007934
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007935 lua_rawset(gL.T, -3);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01007936
7937 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007938 class_txn_ref = hlua_register_metatable(gL.T, CLASS_TXN);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007939
7940 /*
7941 *
7942 * Register class Socket
7943 *
7944 */
7945
7946 /* Create and fill the metatable. */
7947 lua_newtable(gL.T);
7948
7949 /* Create and fille the __index entry. */
7950 lua_pushstring(gL.T, "__index");
7951 lua_newtable(gL.T);
7952
Baptiste Assmann84bb4932015-03-02 21:40:06 +01007953#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007954 hlua_class_function(gL.T, "connect_ssl", hlua_socket_connect_ssl);
Baptiste Assmann84bb4932015-03-02 21:40:06 +01007955#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007956 hlua_class_function(gL.T, "connect", hlua_socket_connect);
7957 hlua_class_function(gL.T, "send", hlua_socket_send);
7958 hlua_class_function(gL.T, "receive", hlua_socket_receive);
7959 hlua_class_function(gL.T, "close", hlua_socket_close);
7960 hlua_class_function(gL.T, "getpeername", hlua_socket_getpeername);
7961 hlua_class_function(gL.T, "getsockname", hlua_socket_getsockname);
7962 hlua_class_function(gL.T, "setoption", hlua_socket_setoption);
7963 hlua_class_function(gL.T, "settimeout", hlua_socket_settimeout);
7964
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007965 lua_rawset(gL.T, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007966
7967 /* Register the garbage collector entry. */
7968 lua_pushstring(gL.T, "__gc");
7969 lua_pushcclosure(gL.T, hlua_socket_gc, 0);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007970 lua_rawset(gL.T, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007971
7972 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007973 class_socket_ref = hlua_register_metatable(gL.T, CLASS_SOCKET);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007974
7975 /* Proxy and server configuration initialisation. */
7976 memset(&socket_proxy, 0, sizeof(socket_proxy));
7977 init_new_proxy(&socket_proxy);
7978 socket_proxy.parent = NULL;
7979 socket_proxy.last_change = now.tv_sec;
7980 socket_proxy.id = "LUA-SOCKET";
7981 socket_proxy.cap = PR_CAP_FE | PR_CAP_BE;
7982 socket_proxy.maxconn = 0;
7983 socket_proxy.accept = NULL;
7984 socket_proxy.options2 |= PR_O2_INDEPSTR;
7985 socket_proxy.srv = NULL;
7986 socket_proxy.conn_retries = 0;
7987 socket_proxy.timeout.connect = 5000; /* By default the timeout connection is 5s. */
7988
7989 /* Init TCP server: unchanged parameters */
7990 memset(&socket_tcp, 0, sizeof(socket_tcp));
7991 socket_tcp.next = NULL;
7992 socket_tcp.proxy = &socket_proxy;
7993 socket_tcp.obj_type = OBJ_TYPE_SERVER;
7994 LIST_INIT(&socket_tcp.actconns);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04007995 socket_tcp.pendconns = EB_ROOT;
Christopher Faulet40a007c2017-07-03 15:41:01 +02007996 socket_tcp.priv_conns = NULL;
7997 socket_tcp.idle_conns = NULL;
7998 socket_tcp.safe_conns = NULL;
Emeric Brun52a91d32017-08-31 14:41:55 +02007999 socket_tcp.next_state = SRV_ST_RUNNING; /* early server setup */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008000 socket_tcp.last_change = 0;
8001 socket_tcp.id = "LUA-TCP-CONN";
8002 socket_tcp.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8003 socket_tcp.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8004 socket_tcp.pp_opts = 0; /* Remove proxy protocol. */
8005
8006 /* XXX: Copy default parameter from default server,
8007 * but the default server is not initialized.
8008 */
8009 socket_tcp.maxqueue = socket_proxy.defsrv.maxqueue;
8010 socket_tcp.minconn = socket_proxy.defsrv.minconn;
8011 socket_tcp.maxconn = socket_proxy.defsrv.maxconn;
8012 socket_tcp.slowstart = socket_proxy.defsrv.slowstart;
8013 socket_tcp.onerror = socket_proxy.defsrv.onerror;
8014 socket_tcp.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
8015 socket_tcp.onmarkedup = socket_proxy.defsrv.onmarkedup;
8016 socket_tcp.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
8017 socket_tcp.uweight = socket_proxy.defsrv.iweight;
8018 socket_tcp.iweight = socket_proxy.defsrv.iweight;
8019
8020 socket_tcp.check.status = HCHK_STATUS_INI;
8021 socket_tcp.check.rise = socket_proxy.defsrv.check.rise;
8022 socket_tcp.check.fall = socket_proxy.defsrv.check.fall;
8023 socket_tcp.check.health = socket_tcp.check.rise; /* socket, but will fall down at first failure */
8024 socket_tcp.check.server = &socket_tcp;
8025
8026 socket_tcp.agent.status = HCHK_STATUS_INI;
8027 socket_tcp.agent.rise = socket_proxy.defsrv.agent.rise;
8028 socket_tcp.agent.fall = socket_proxy.defsrv.agent.fall;
8029 socket_tcp.agent.health = socket_tcp.agent.rise; /* socket, but will fall down at first failure */
8030 socket_tcp.agent.server = &socket_tcp;
8031
Willy Tarreaua261e9b2016-12-22 20:44:00 +01008032 socket_tcp.xprt = xprt_get(XPRT_RAW);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008033
8034#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008035 /* Init TCP server: unchanged parameters */
8036 memset(&socket_ssl, 0, sizeof(socket_ssl));
8037 socket_ssl.next = NULL;
8038 socket_ssl.proxy = &socket_proxy;
8039 socket_ssl.obj_type = OBJ_TYPE_SERVER;
8040 LIST_INIT(&socket_ssl.actconns);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04008041 socket_ssl.pendconns = EB_ROOT;
Willy Tarreaub784b352019-02-07 14:48:24 +01008042 socket_ssl.priv_conns = NULL;
8043 socket_ssl.idle_conns = NULL;
8044 socket_ssl.safe_conns = NULL;
Emeric Brun52a91d32017-08-31 14:41:55 +02008045 socket_ssl.next_state = SRV_ST_RUNNING; /* early server setup */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008046 socket_ssl.last_change = 0;
8047 socket_ssl.id = "LUA-SSL-CONN";
8048 socket_ssl.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8049 socket_ssl.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8050 socket_ssl.pp_opts = 0; /* Remove proxy protocol. */
8051
8052 /* XXX: Copy default parameter from default server,
8053 * but the default server is not initialized.
8054 */
8055 socket_ssl.maxqueue = socket_proxy.defsrv.maxqueue;
8056 socket_ssl.minconn = socket_proxy.defsrv.minconn;
8057 socket_ssl.maxconn = socket_proxy.defsrv.maxconn;
8058 socket_ssl.slowstart = socket_proxy.defsrv.slowstart;
8059 socket_ssl.onerror = socket_proxy.defsrv.onerror;
8060 socket_ssl.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
8061 socket_ssl.onmarkedup = socket_proxy.defsrv.onmarkedup;
8062 socket_ssl.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
8063 socket_ssl.uweight = socket_proxy.defsrv.iweight;
8064 socket_ssl.iweight = socket_proxy.defsrv.iweight;
8065
8066 socket_ssl.check.status = HCHK_STATUS_INI;
8067 socket_ssl.check.rise = socket_proxy.defsrv.check.rise;
8068 socket_ssl.check.fall = socket_proxy.defsrv.check.fall;
8069 socket_ssl.check.health = socket_ssl.check.rise; /* socket, but will fall down at first failure */
8070 socket_ssl.check.server = &socket_ssl;
8071
8072 socket_ssl.agent.status = HCHK_STATUS_INI;
8073 socket_ssl.agent.rise = socket_proxy.defsrv.agent.rise;
8074 socket_ssl.agent.fall = socket_proxy.defsrv.agent.fall;
8075 socket_ssl.agent.health = socket_ssl.agent.rise; /* socket, but will fall down at first failure */
8076 socket_ssl.agent.server = &socket_ssl;
8077
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008078 socket_ssl.use_ssl = 1;
Willy Tarreaua261e9b2016-12-22 20:44:00 +01008079 socket_ssl.xprt = xprt_get(XPRT_SSL);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008080
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008081 for (idx = 0; args[idx] != NULL; idx++) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008082 if ((kw = srv_find_kw(args[idx])) != NULL) { /* Maybe it's registered server keyword */
8083 /*
8084 *
8085 * If the keyword is not known, we can search in the registered
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08008086 * server keywords. This is useful to configure special SSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008087 * features like client certificates and ssl_verify.
8088 *
8089 */
8090 tmp_error = kw->parse(args, &idx, &socket_proxy, &socket_ssl, &error);
8091 if (tmp_error != 0) {
8092 fprintf(stderr, "INTERNAL ERROR: %s\n", error);
8093 abort(); /* This must be never arrives because the command line
8094 not editable by the user. */
8095 }
8096 idx += kw->skip;
8097 }
8098 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008099#endif
Thierry Fournier75933d42016-01-21 09:30:18 +01008100
8101 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01008102}
Willy Tarreaubb57d942016-12-21 19:04:56 +01008103
Willy Tarreau80713382018-11-26 10:19:54 +01008104static void hlua_register_build_options(void)
8105{
Willy Tarreaubb57d942016-12-21 19:04:56 +01008106 char *ptr = NULL;
Willy Tarreau80713382018-11-26 10:19:54 +01008107
Willy Tarreaubb57d942016-12-21 19:04:56 +01008108 memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE);
8109 hap_register_build_opts(ptr, 1);
8110}
Willy Tarreau80713382018-11-26 10:19:54 +01008111
8112INITCALL0(STG_REGISTER, hlua_register_build_options);