blob: 3de0bea1aa82bb3bdf461ae4541112f081ffc1ee [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>
Thierry FOURNIERbabae282015-09-17 11:36:37 +020014#include <setjmp.h>
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +010015
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +010016#include <lauxlib.h>
17#include <lua.h>
18#include <lualib.h>
19
Thierry FOURNIER463119c2015-03-10 00:35:36 +010020#if !defined(LUA_VERSION_NUM) || LUA_VERSION_NUM < 503
21#error "Requires Lua 5.3 or later."
Cyril Bontédc0306e2015-03-02 00:08:40 +010022#endif
23
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020024#include <haproxy/api.h>
Willy Tarreau8d2b7772020-05-27 10:58:19 +020025#include <import/ebpttree.h>
Thierry FOURNIER380d0932015-01-23 14:27:52 +010026
27#include <common/cfgparse.h>
Willy Tarreau3f567e42020-05-28 15:29:19 +020028#include <haproxy/thread.h>
Willy Tarreau7cd8b6e2020-06-02 17:32:26 +020029#include <haproxy/regex.h>
Willy Tarreau374b4422020-06-02 17:46:16 +020030#include <haproxy/xref.h>
Willy Tarreau5413a872020-06-02 19:33:08 +020031#include <haproxy/h1.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020032#include <haproxy/tools.h>
Thierry FOURNIER380d0932015-01-23 14:27:52 +010033
William Lallemand9ed62032016-11-21 17:49:11 +010034#include <types/cli.h>
Thierry FOURNIER380d0932015-01-23 14:27:52 +010035#include <types/hlua.h>
36#include <types/proxy.h>
William Lallemand9ed62032016-11-21 17:49:11 +010037#include <types/stats.h>
Thierry FOURNIER380d0932015-01-23 14:27:52 +010038
Thierry FOURNIER55da1652015-01-23 11:36:30 +010039#include <proto/arg.h>
Willy Tarreau8a8d83b2015-04-13 13:24:54 +020040#include <proto/applet.h>
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010041#include <proto/channel.h>
William Lallemand9ed62032016-11-21 17:49:11 +010042#include <proto/cli.h>
Willy Tarreaua71f6422016-11-16 17:00:14 +010043#include <proto/connection.h>
William Lallemand9ed62032016-11-21 17:49:11 +010044#include <proto/stats.h>
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +010045#include <proto/hlua.h>
Thierry Fournierfb0b5462016-01-21 09:28:58 +010046#include <proto/hlua_fcn.h>
Willy Tarreau79e57332018-10-02 16:01:16 +020047#include <proto/http_fetch.h>
Christopher Faulet724a12c2018-12-13 22:12:15 +010048#include <proto/http_htx.h>
Willy Tarreau61c112a2018-10-02 16:43:32 +020049#include <proto/http_rules.h>
Thierry FOURNIER3def3932015-04-07 11:27:54 +020050#include <proto/map.h>
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010051#include <proto/obj_type.h>
Patrick Hemmer268a7072018-05-11 12:52:31 -040052#include <proto/queue.h>
Thierry FOURNIER83758bb2015-02-04 13:21:04 +010053#include <proto/pattern.h>
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +010054#include <proto/payload.h>
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020055#include <proto/http_ana.h>
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +010056#include <proto/sample.h>
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010057#include <proto/server.h>
Willy Tarreaufeb76402015-04-03 14:10:06 +020058#include <proto/session.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020059#include <proto/stream.h>
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010060#include <proto/stream_interface.h>
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +010061#include <proto/task.h>
Willy Tarreau39713102016-11-25 15:49:32 +010062#include <proto/tcp_rules.h>
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +020063#include <proto/vars.h>
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +010064
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +010065/* Lua uses longjmp to perform yield or throwing errors. This
66 * macro is used only for identifying the function that can
67 * not return because a longjmp is executed.
68 * __LJMP marks a prototype of hlua file that can use longjmp.
69 * WILL_LJMP() marks an lua function that will use longjmp.
70 * MAY_LJMP() marks an lua function that may use longjmp.
71 */
72#define __LJMP
Willy Tarreau4e7cc332018-10-20 17:45:48 +020073#define WILL_LJMP(func) do { func; my_unreachable(); } while(0)
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +010074#define MAY_LJMP(func) func
75
Thierry FOURNIERbabae282015-09-17 11:36:37 +020076/* This couple of function executes securely some Lua calls outside of
77 * the lua runtime environment. Each Lua call can return a longjmp
78 * if it encounter a memory error.
79 *
80 * Lua documentation extract:
81 *
82 * If an error happens outside any protected environment, Lua calls
83 * a panic function (see lua_atpanic) and then calls abort, thus
84 * exiting the host application. Your panic function can avoid this
85 * exit by never returning (e.g., doing a long jump to your own
86 * recovery point outside Lua).
87 *
88 * The panic function runs as if it were a message handler (see
89 * §2.3); in particular, the error message is at the top of the
90 * stack. However, there is no guarantee about stack space. To push
91 * anything on the stack, the panic function must first check the
92 * available space (see §4.2).
93 *
94 * We must check all the Lua entry point. This includes:
95 * - The include/proto/hlua.h exported functions
96 * - the task wrapper function
97 * - The action wrapper function
98 * - The converters wrapper function
99 * - The sample-fetch wrapper functions
100 *
101 * It is tolerated that the initilisation function returns an abort.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800102 * Before each Lua abort, an error message is written on stderr.
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200103 *
104 * The macro SET_SAFE_LJMP initialise the longjmp. The Macro
105 * RESET_SAFE_LJMP reset the longjmp. These function must be macro
106 * because they must be exists in the program stack when the longjmp
107 * is called.
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +0200108 *
109 * Note that the Lua processing is not really thread safe. It provides
110 * heavy system which consists to add our own lock function in the Lua
111 * code and recompile the library. This system will probably not accepted
112 * by maintainers of various distribs.
113 *
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500114 * Our main execution point of the Lua is the function lua_resume(). A
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +0200115 * 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; }
Willy Tarreau9d6bb5a2020-02-06 15:55:41 +0100128static int hlua_panic_ljmp(lua_State *L) { WILL_LJMP(longjmp(safe_ljmp_env, 1)); }
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200129
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;
Christopher Faulet700d9e82020-01-31 12:21:52 +0100193static int class_txn_reply_ref;
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +0100194
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100195/* Global Lua execution timeout. By default Lua, execution linked
Willy Tarreau87b09662015-04-03 00:22:06 +0200196 * with stream (actions, sample-fetches and converters) have a
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100197 * short timeout. Lua linked with tasks doesn't have a timeout
198 * because a task may remain alive during all the haproxy execution.
199 */
200static unsigned int hlua_timeout_session = 4000; /* session timeout. */
201static unsigned int hlua_timeout_task = TICK_ETERNITY; /* task timeout. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200202static unsigned int hlua_timeout_applet = 4000; /* applet timeout. */
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100203
Thierry FOURNIERee9f8022015-03-03 17:37:37 +0100204/* Interrupts the Lua processing each "hlua_nb_instruction" instructions.
205 * it is used for preventing infinite loops.
206 *
207 * I test the scheer with an infinite loop containing one incrementation
208 * and one test. I run this loop between 10 seconds, I raise a ceil of
209 * 710M loops from one interrupt each 9000 instructions, so I fix the value
210 * to one interrupt each 10 000 instructions.
211 *
212 * configured | Number of
213 * instructions | loops executed
214 * between two | in milions
215 * forced yields |
216 * ---------------+---------------
217 * 10 | 160
218 * 500 | 670
219 * 1000 | 680
220 * 5000 | 700
221 * 7000 | 700
222 * 8000 | 700
223 * 9000 | 710 <- ceil
224 * 10000 | 710
225 * 100000 | 710
226 * 1000000 | 710
227 *
228 */
229static unsigned int hlua_nb_instruction = 10000;
230
Willy Tarreau32f61e22015-03-18 17:54:59 +0100231/* Descriptor for the memory allocation state. If limit is not null, it will
232 * be enforced on any memory allocation.
233 */
234struct hlua_mem_allocator {
235 size_t allocated;
236 size_t limit;
237};
238
239static struct hlua_mem_allocator hlua_global_allocator;
240
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100241/* These functions converts types between HAProxy internal args or
242 * sample and LUA types. Another function permits to check if the
243 * LUA stack contains arguments according with an required ARG_T
244 * format.
245 */
246static int hlua_arg2lua(lua_State *L, const struct arg *arg);
247static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100248__LJMP static int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp,
David Carlier0c437f42016-04-27 16:21:56 +0100249 uint64_t mask, struct proxy *p);
Willy Tarreau5eadada2015-03-10 17:28:54 +0100250static int hlua_smp2lua(lua_State *L, struct sample *smp);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100251static int hlua_smp2lua_str(lua_State *L, struct sample *smp);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100252static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp);
253
Christopher Faulet9d1332b2020-02-24 16:46:16 +0100254__LJMP static int hlua_http_get_headers(lua_State *L, struct http_msg *msg);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200255
Thierry FOURNIER23bc3752015-09-11 19:15:43 +0200256#define SEND_ERR(__be, __fmt, __args...) \
257 do { \
258 send_log(__be, LOG_ERR, __fmt, ## __args); \
259 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) \
Christopher Faulet767a84b2017-11-24 16:50:31 +0100260 ha_alert(__fmt, ## __args); \
Thierry FOURNIER23bc3752015-09-11 19:15:43 +0200261 } while (0)
262
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100263/* Used to check an Lua function type in the stack. It creates and
264 * returns a reference of the function. This function throws an
265 * error if the rgument is not a "function".
266 */
267__LJMP unsigned int hlua_checkfunction(lua_State *L, int argno)
268{
269 if (!lua_isfunction(L, argno)) {
Thierry FOURNIERfd1e9552018-02-23 18:41:18 +0100270 const char *msg = lua_pushfstring(L, "function expected, got %s", luaL_typename(L, argno));
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100271 WILL_LJMP(luaL_argerror(L, argno, msg));
272 }
273 lua_pushvalue(L, argno);
274 return luaL_ref(L, LUA_REGISTRYINDEX);
275}
276
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200277/* Return the string that is of the top of the stack. */
278const char *hlua_get_top_error_string(lua_State *L)
279{
280 if (lua_gettop(L) < 1)
281 return "unknown error";
282 if (lua_type(L, -1) != LUA_TSTRING)
283 return "unknown error";
284 return lua_tostring(L, -1);
285}
286
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200287__LJMP static const char *hlua_traceback(lua_State *L)
288{
289 lua_Debug ar;
290 int level = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +0200291 struct buffer *msg = get_trash_chunk();
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200292 int filled = 0;
293
294 while (lua_getstack(L, level++, &ar)) {
295
296 /* Add separator */
297 if (filled)
298 chunk_appendf(msg, ", ");
299 filled = 1;
300
301 /* Fill fields:
302 * 'S': fills in the fields source, short_src, linedefined, lastlinedefined, and what;
303 * 'l': fills in the field currentline;
304 * 'n': fills in the field name and namewhat;
305 * 't': fills in the field istailcall;
306 */
307 lua_getinfo(L, "Slnt", &ar);
308
309 /* Append code localisation */
310 if (ar.currentline > 0)
311 chunk_appendf(msg, "%s:%d ", ar.short_src, ar.currentline);
312 else
313 chunk_appendf(msg, "%s ", ar.short_src);
314
315 /*
316 * Get function name
317 *
318 * if namewhat is no empty, name is defined.
319 * what contains "Lua" for Lua function, "C" for C function,
320 * or "main" for main code.
321 */
322 if (*ar.namewhat != '\0' && ar.name != NULL) /* is there a name from code? */
323 chunk_appendf(msg, "%s '%s'", ar.namewhat, ar.name); /* use it */
324
325 else if (*ar.what == 'm') /* "main", the code is not executed in a function */
326 chunk_appendf(msg, "main chunk");
327
328 else if (*ar.what != 'C') /* for Lua functions, use <file:line> */
329 chunk_appendf(msg, "C function line %d", ar.linedefined);
330
331 else /* nothing left... */
332 chunk_appendf(msg, "?");
333
334
335 /* Display tailed call */
336 if (ar.istailcall)
337 chunk_appendf(msg, " ...");
338 }
339
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200340 return msg->area;
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200341}
342
343
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100344/* This function check the number of arguments available in the
345 * stack. If the number of arguments available is not the same
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500346 * then <nb> an error is thrown.
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100347 */
348__LJMP static inline void check_args(lua_State *L, int nb, char *fcn)
349{
350 if (lua_gettop(L) == nb)
351 return;
352 WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb));
353}
354
Mark Lakes22154b42018-01-29 14:38:40 -0800355/* This function pushes an error string prefixed by the file name
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100356 * and the line number where the error is encountered.
357 */
358static int hlua_pusherror(lua_State *L, const char *fmt, ...)
359{
360 va_list argp;
361 va_start(argp, fmt);
362 luaL_where(L, 1);
363 lua_pushvfstring(L, fmt, argp);
364 va_end(argp);
365 lua_concat(L, 2);
366 return 1;
367}
368
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100369/* This functions is used with sample fetch and converters. It
370 * converts the HAProxy configuration argument in a lua stack
371 * values.
372 *
373 * It takes an array of "arg", and each entry of the array is
374 * converted and pushed in the LUA stack.
375 */
376static int hlua_arg2lua(lua_State *L, const struct arg *arg)
377{
378 switch (arg->type) {
379 case ARGT_SINT:
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100380 case ARGT_TIME:
381 case ARGT_SIZE:
Thierry FOURNIERf90838b2015-03-06 13:48:32 +0100382 lua_pushinteger(L, arg->data.sint);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100383 break;
384
385 case ARGT_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200386 lua_pushlstring(L, arg->data.str.area, arg->data.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100387 break;
388
389 case ARGT_IPV4:
390 case ARGT_IPV6:
391 case ARGT_MSK4:
392 case ARGT_MSK6:
393 case ARGT_FE:
394 case ARGT_BE:
395 case ARGT_TAB:
396 case ARGT_SRV:
397 case ARGT_USR:
398 case ARGT_MAP:
399 default:
400 lua_pushnil(L);
401 break;
402 }
403 return 1;
404}
405
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500406/* This function take one entry in an LUA stack at the index "ud",
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100407 * and try to convert it in an HAProxy argument entry. This is useful
Ilya Shipitsind4259502020-04-08 01:07:56 +0500408 * with sample fetch wrappers. The input arguments are given to the
409 * lua wrapper and converted as arg list by the function.
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100410 */
411static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg)
412{
413 switch (lua_type(L, ud)) {
414
415 case LUA_TNUMBER:
416 case LUA_TBOOLEAN:
417 arg->type = ARGT_SINT;
418 arg->data.sint = lua_tointeger(L, ud);
419 break;
420
421 case LUA_TSTRING:
422 arg->type = ARGT_STR;
Tim Duesterhus2e89dec2019-09-29 23:03:08 +0200423 arg->data.str.area = (char *)lua_tolstring(L, ud, &arg->data.str.data);
Tim Duesterhus29d2e8a2019-09-29 23:03:07 +0200424 /* We don't know the actual size of the underlying allocation, so be conservative. */
425 arg->data.str.size = arg->data.str.data;
426 arg->data.str.head = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100427 break;
428
429 case LUA_TUSERDATA:
430 case LUA_TNIL:
431 case LUA_TTABLE:
432 case LUA_TFUNCTION:
433 case LUA_TTHREAD:
434 case LUA_TLIGHTUSERDATA:
435 arg->type = ARGT_SINT;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +0200436 arg->data.sint = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100437 break;
438 }
439 return 1;
440}
441
442/* the following functions are used to convert a struct sample
443 * in Lua type. This useful to convert the return of the
Ilya Shipitsind4259502020-04-08 01:07:56 +0500444 * fetches or converters.
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100445 */
Willy Tarreau5eadada2015-03-10 17:28:54 +0100446static int hlua_smp2lua(lua_State *L, struct sample *smp)
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100447{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200448 switch (smp->data.type) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100449 case SMP_T_SINT:
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100450 case SMP_T_BOOL:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200451 lua_pushinteger(L, smp->data.u.sint);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100452 break;
453
454 case SMP_T_BIN:
455 case SMP_T_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200456 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100457 break;
458
459 case SMP_T_METH:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200460 switch (smp->data.u.meth.meth) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100461 case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break;
462 case HTTP_METH_GET: lua_pushstring(L, "GET"); break;
463 case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break;
464 case HTTP_METH_POST: lua_pushstring(L, "POST"); break;
465 case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break;
466 case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break;
467 case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break;
468 case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break;
469 case HTTP_METH_OTHER:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200470 lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100471 break;
472 default:
473 lua_pushnil(L);
474 break;
475 }
476 break;
477
478 case SMP_T_IPV4:
479 case SMP_T_IPV6:
480 case SMP_T_ADDR: /* This type is never used to qualify a sample. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200481 if (sample_casts[smp->data.type][SMP_T_STR] &&
482 sample_casts[smp->data.type][SMP_T_STR](smp))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200483 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Willy Tarreau5eadada2015-03-10 17:28:54 +0100484 else
485 lua_pushnil(L);
486 break;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100487 default:
488 lua_pushnil(L);
489 break;
490 }
491 return 1;
492}
493
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100494/* the following functions are used to convert a struct sample
495 * in Lua strings. This is useful to convert the return of the
Ilya Shipitsind4259502020-04-08 01:07:56 +0500496 * fetches or converters.
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100497 */
498static int hlua_smp2lua_str(lua_State *L, struct sample *smp)
499{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200500 switch (smp->data.type) {
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100501
502 case SMP_T_BIN:
503 case SMP_T_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200504 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100505 break;
506
507 case SMP_T_METH:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200508 switch (smp->data.u.meth.meth) {
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100509 case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break;
510 case HTTP_METH_GET: lua_pushstring(L, "GET"); break;
511 case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break;
512 case HTTP_METH_POST: lua_pushstring(L, "POST"); break;
513 case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break;
514 case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break;
515 case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break;
516 case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break;
517 case HTTP_METH_OTHER:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200518 lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100519 break;
520 default:
521 lua_pushstring(L, "");
522 break;
523 }
524 break;
525
526 case SMP_T_SINT:
527 case SMP_T_BOOL:
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100528 case SMP_T_IPV4:
529 case SMP_T_IPV6:
530 case SMP_T_ADDR: /* This type is never used to qualify a sample. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200531 if (sample_casts[smp->data.type][SMP_T_STR] &&
532 sample_casts[smp->data.type][SMP_T_STR](smp))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200533 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100534 else
535 lua_pushstring(L, "");
536 break;
537 default:
538 lua_pushstring(L, "");
539 break;
540 }
541 return 1;
542}
543
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100544/* the following functions are used to convert an Lua type in a
545 * struct sample. This is useful to provide data from a converter
546 * to the LUA code.
547 */
548static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp)
549{
550 switch (lua_type(L, ud)) {
551
552 case LUA_TNUMBER:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200553 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200554 smp->data.u.sint = lua_tointeger(L, ud);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100555 break;
556
557
558 case LUA_TBOOLEAN:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200559 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200560 smp->data.u.sint = lua_toboolean(L, ud);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100561 break;
562
563 case LUA_TSTRING:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200564 smp->data.type = SMP_T_STR;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100565 smp->flags |= SMP_F_CONST;
Tim Duesterhus2e89dec2019-09-29 23:03:08 +0200566 smp->data.u.str.area = (char *)lua_tolstring(L, ud, &smp->data.u.str.data);
Tim Duesterhus29d2e8a2019-09-29 23:03:07 +0200567 /* We don't know the actual size of the underlying allocation, so be conservative. */
568 smp->data.u.str.size = smp->data.u.str.data;
569 smp->data.u.str.head = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100570 break;
571
572 case LUA_TUSERDATA:
573 case LUA_TNIL:
574 case LUA_TTABLE:
575 case LUA_TFUNCTION:
576 case LUA_TTHREAD:
577 case LUA_TLIGHTUSERDATA:
Thierry FOURNIER93405e12015-08-26 14:19:03 +0200578 case LUA_TNONE:
579 default:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200580 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200581 smp->data.u.sint = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100582 break;
583 }
584 return 1;
585}
586
Ilya Shipitsind4259502020-04-08 01:07:56 +0500587/* This function check the "argp" built by another conversion function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800588 * is in accord with the expected argp defined by the "mask". The function
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100589 * returns true or false. It can be adjust the types if there compatibles.
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100590 *
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500591 * This function assumes that the argp argument contains ARGM_NBARGS + 1
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100592 * entries.
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100593 */
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100594__LJMP int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp,
David Carlier0c437f42016-04-27 16:21:56 +0100595 uint64_t mask, struct proxy *p)
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100596{
597 int min_arg;
598 int idx;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100599 struct proxy *px;
600 char *sname, *pname;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100601
602 idx = 0;
603 min_arg = ARGM(mask);
604 mask >>= ARGM_BITS;
605
606 while (1) {
607
608 /* Check oversize. */
609 if (idx >= ARGM_NBARGS && argp[idx].type != ARGT_STOP) {
Cyril Bonté577a36a2015-03-02 00:08:38 +0100610 WILL_LJMP(luaL_argerror(L, first + idx, "Malformed argument mask"));
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100611 }
612
613 /* Check for mandatory arguments. */
614 if (argp[idx].type == ARGT_STOP) {
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100615 if (idx < min_arg) {
616
617 /* If miss other argument than the first one, we return an error. */
618 if (idx > 0)
619 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
620
621 /* If first argument have a certain type, some default values
622 * may be used. See the function smp_resolve_args().
623 */
624 switch (mask & ARGT_MASK) {
625
626 case ARGT_FE:
627 if (!(p->cap & PR_CAP_FE))
628 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
629 argp[idx].data.prx = p;
630 argp[idx].type = ARGT_FE;
631 argp[idx+1].type = ARGT_STOP;
632 break;
633
634 case ARGT_BE:
635 if (!(p->cap & PR_CAP_BE))
636 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
637 argp[idx].data.prx = p;
638 argp[idx].type = ARGT_BE;
639 argp[idx+1].type = ARGT_STOP;
640 break;
641
642 case ARGT_TAB:
643 argp[idx].data.prx = p;
644 argp[idx].type = ARGT_TAB;
645 argp[idx+1].type = ARGT_STOP;
646 break;
647
648 default:
649 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
650 break;
651 }
652 }
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100653 return 0;
654 }
655
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500656 /* Check for exceed the number of required argument. */
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100657 if ((mask & ARGT_MASK) == ARGT_STOP &&
658 argp[idx].type != ARGT_STOP) {
659 WILL_LJMP(luaL_argerror(L, first + idx, "Last argument expected"));
660 }
661
662 if ((mask & ARGT_MASK) == ARGT_STOP &&
663 argp[idx].type == ARGT_STOP) {
664 return 0;
665 }
666
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100667 /* Convert some argument types. */
668 switch (mask & ARGT_MASK) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100669 case ARGT_SINT:
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100670 if (argp[idx].type != ARGT_SINT)
671 WILL_LJMP(luaL_argerror(L, first + idx, "integer expected"));
672 argp[idx].type = ARGT_SINT;
673 break;
674
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100675 case ARGT_TIME:
676 if (argp[idx].type != ARGT_SINT)
677 WILL_LJMP(luaL_argerror(L, first + idx, "integer expected"));
Thierry FOURNIER29176f32015-07-07 00:41:29 +0200678 argp[idx].type = ARGT_TIME;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100679 break;
680
681 case ARGT_SIZE:
682 if (argp[idx].type != ARGT_SINT)
683 WILL_LJMP(luaL_argerror(L, first + idx, "integer expected"));
Thierry FOURNIER29176f32015-07-07 00:41:29 +0200684 argp[idx].type = ARGT_SIZE;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100685 break;
686
687 case ARGT_FE:
688 if (argp[idx].type != ARGT_STR)
689 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200690 memcpy(trash.area, argp[idx].data.str.area,
691 argp[idx].data.str.data);
692 trash.area[argp[idx].data.str.data] = 0;
693 argp[idx].data.prx = proxy_fe_by_name(trash.area);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100694 if (!argp[idx].data.prx)
695 WILL_LJMP(luaL_argerror(L, first + idx, "frontend doesn't exist"));
696 argp[idx].type = ARGT_FE;
697 break;
698
699 case ARGT_BE:
700 if (argp[idx].type != ARGT_STR)
701 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200702 memcpy(trash.area, argp[idx].data.str.area,
703 argp[idx].data.str.data);
704 trash.area[argp[idx].data.str.data] = 0;
705 argp[idx].data.prx = proxy_be_by_name(trash.area);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100706 if (!argp[idx].data.prx)
707 WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist"));
708 argp[idx].type = ARGT_BE;
709 break;
710
711 case ARGT_TAB:
712 if (argp[idx].type != ARGT_STR)
713 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200714 memcpy(trash.area, argp[idx].data.str.area,
715 argp[idx].data.str.data);
716 trash.area[argp[idx].data.str.data] = 0;
Tim Duesterhus9fe7c632019-09-29 23:03:09 +0200717 argp[idx].data.t = stktable_find_by_name(trash.area);
718 if (!argp[idx].data.t)
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100719 WILL_LJMP(luaL_argerror(L, first + idx, "table doesn't exist"));
720 argp[idx].type = ARGT_TAB;
721 break;
722
723 case ARGT_SRV:
724 if (argp[idx].type != ARGT_STR)
725 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200726 memcpy(trash.area, argp[idx].data.str.area,
727 argp[idx].data.str.data);
728 trash.area[argp[idx].data.str.data] = 0;
729 sname = strrchr(trash.area, '/');
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100730 if (sname) {
731 *sname++ = '\0';
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200732 pname = trash.area;
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200733 px = proxy_be_by_name(pname);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100734 if (!px)
735 WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist"));
736 }
737 else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200738 sname = trash.area;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100739 px = p;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100740 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100741 argp[idx].data.srv = findserver(px, sname);
742 if (!argp[idx].data.srv)
743 WILL_LJMP(luaL_argerror(L, first + idx, "server doesn't exist"));
744 argp[idx].type = ARGT_SRV;
745 break;
746
747 case ARGT_IPV4:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200748 memcpy(trash.area, argp[idx].data.str.area,
749 argp[idx].data.str.data);
750 trash.area[argp[idx].data.str.data] = 0;
751 if (inet_pton(AF_INET, trash.area, &argp[idx].data.ipv4))
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100752 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 address"));
753 argp[idx].type = ARGT_IPV4;
754 break;
755
756 case ARGT_MSK4:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200757 memcpy(trash.area, argp[idx].data.str.area,
758 argp[idx].data.str.data);
759 trash.area[argp[idx].data.str.data] = 0;
760 if (!str2mask(trash.area, &argp[idx].data.ipv4))
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100761 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 mask"));
762 argp[idx].type = ARGT_MSK4;
763 break;
764
765 case ARGT_IPV6:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200766 memcpy(trash.area, argp[idx].data.str.area,
767 argp[idx].data.str.data);
768 trash.area[argp[idx].data.str.data] = 0;
769 if (inet_pton(AF_INET6, trash.area, &argp[idx].data.ipv6))
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100770 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 address"));
771 argp[idx].type = ARGT_IPV6;
772 break;
773
774 case ARGT_MSK6:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200775 memcpy(trash.area, argp[idx].data.str.area,
776 argp[idx].data.str.data);
777 trash.area[argp[idx].data.str.data] = 0;
778 if (!str2mask6(trash.area, &argp[idx].data.ipv6))
Tim Duesterhusb814da62018-01-25 16:24:50 +0100779 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 mask"));
780 argp[idx].type = ARGT_MSK6;
781 break;
782
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100783 case ARGT_MAP:
784 case ARGT_REG:
785 case ARGT_USR:
786 WILL_LJMP(luaL_argerror(L, first + idx, "type not yet supported"));
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100787 break;
788 }
789
790 /* Check for type of argument. */
791 if ((mask & ARGT_MASK) != argp[idx].type) {
792 const char *msg = lua_pushfstring(L, "'%s' expected, got '%s'",
793 arg_type_names[(mask & ARGT_MASK)],
794 arg_type_names[argp[idx].type & ARGT_MASK]);
795 WILL_LJMP(luaL_argerror(L, first + idx, msg));
796 }
797
798 /* Next argument. */
799 mask >>= ARGT_BITS;
800 idx++;
801 }
802}
803
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100804/*
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500805 * The following functions are used to make correspondence between the the
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100806 * executed lua pointer and the "struct hlua *" that contain the context.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100807 *
808 * - hlua_gethlua : return the hlua context associated with an lua_State.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100809 * - hlua_sethlua : create the association between hlua context and lua_state.
810 */
811static inline struct hlua *hlua_gethlua(lua_State *L)
812{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +0100813 struct hlua **hlua = lua_getextraspace(L);
814 return *hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100815}
816static inline void hlua_sethlua(struct hlua *hlua)
817{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +0100818 struct hlua **hlua_store = lua_getextraspace(hlua->T);
819 *hlua_store = hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100820}
821
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100822/* This function is used to send logs. It try to send on screen (stderr)
823 * and on the default syslog server.
824 */
825static inline void hlua_sendlog(struct proxy *px, int level, const char *msg)
826{
827 struct tm tm;
828 char *p;
829
830 /* Cleanup the log message. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200831 p = trash.area;
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100832 for (; *msg != '\0'; msg++, p++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200833 if (p >= trash.area + trash.size - 1) {
Thierry FOURNIERccf00632015-09-16 12:47:03 +0200834 /* Break the message if exceed the buffer size. */
835 *(p-4) = ' ';
836 *(p-3) = '.';
837 *(p-2) = '.';
838 *(p-1) = '.';
839 break;
840 }
Willy Tarreau90807112020-02-25 08:16:33 +0100841 if (isprint((unsigned char)*msg))
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100842 *p = *msg;
843 else
844 *p = '.';
845 }
846 *p = '\0';
847
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200848 send_log(px, level, "%s\n", trash.area);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100849 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
Willy Tarreaua678b432015-08-28 10:14:59 +0200850 get_localtime(date.tv_sec, &tm);
851 fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n",
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100852 log_levels[level], tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec,
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200853 (int)getpid(), trash.area);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100854 fflush(stderr);
855 }
856}
857
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100858/* This function just ensure that the yield will be always
859 * returned with a timeout and permit to set some flags
860 */
861__LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx,
Thierry FOURNIERf90838b2015-03-06 13:48:32 +0100862 lua_KFunction k, int timeout, unsigned int flags)
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100863{
864 struct hlua *hlua = hlua_gethlua(L);
865
866 /* Set the wake timeout. If timeout is required, we set
867 * the expiration time.
868 */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +0200869 hlua->wake_time = timeout;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100870
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +0100871 hlua->flags |= flags;
872
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100873 /* Process the yield. */
Willy Tarreau9635e032018-10-16 17:52:55 +0200874 MAY_LJMP(lua_yieldk(L, nresults, ctx, k));
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100875}
876
Willy Tarreau87b09662015-04-03 00:22:06 +0200877/* This function initialises the Lua environment stored in the stream.
878 * It must be called at the start of the stream. This function creates
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100879 * an LUA coroutine. It can not be use to crete the main LUA context.
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200880 *
881 * This function is particular. it initialises a new Lua thread. If the
882 * initialisation fails (example: out of memory error), the lua function
883 * throws an error (longjmp).
884 *
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100885 * In some case (at least one), this function can be called from safe
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500886 * environment, so we must not initialise it. While the support of
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100887 * threads appear, the safe environment set a lock to ensure only one
888 * Lua execution at a time. If we initialize safe environment in another
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500889 * safe environment, we have a dead lock.
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100890 *
891 * set "already_safe" true if the context is initialized form safe
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500892 * Lua function.
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100893 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800894 * This function manipulates two Lua stacks: the main and the thread. Only
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200895 * the main stack can fail. The thread is not manipulated. This function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800896 * MUST NOT manipulate the created thread stack state, because it is not
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500897 * protected against errors thrown by the thread stack.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100898 */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100899int hlua_ctx_init(struct hlua *lua, struct task *task, int already_safe)
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100900{
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100901 if (!already_safe) {
902 if (!SET_SAFE_LJMP(gL.T)) {
903 lua->Tref = LUA_REFNIL;
904 return 0;
905 }
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200906 }
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100907 lua->Mref = LUA_REFNIL;
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +0100908 lua->flags = 0;
Willy Tarreauf31af932020-01-14 09:59:38 +0100909 lua->gc_count = 0;
Christopher Fauletbc275a92020-02-26 14:55:16 +0100910 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100911 LIST_INIT(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100912 lua->T = lua_newthread(gL.T);
913 if (!lua->T) {
914 lua->Tref = LUA_REFNIL;
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100915 if (!already_safe)
916 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100917 return 0;
918 }
919 hlua_sethlua(lua);
920 lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
921 lua->task = task;
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100922 if (!already_safe)
923 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100924 return 1;
925}
926
Willy Tarreau87b09662015-04-03 00:22:06 +0200927/* Used to destroy the Lua coroutine when the attached stream or task
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100928 * is destroyed. The destroy also the memory context. The struct "lua"
929 * is not freed.
930 */
931void hlua_ctx_destroy(struct hlua *lua)
932{
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100933 if (!lua)
Thierry FOURNIERa718b292015-03-04 16:48:34 +0100934 return;
935
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100936 if (!lua->T)
937 goto end;
938
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100939 /* Purge all the pending signals. */
Thierry FOURNIERd6975962017-07-12 14:31:10 +0200940 notification_purge(&lua->com);
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100941
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200942 if (!SET_SAFE_LJMP(lua->T))
943 return;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100944 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200945 RESET_SAFE_LJMP(lua->T);
Thierry FOURNIER5a50a852015-09-23 16:59:28 +0200946
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200947 if (!SET_SAFE_LJMP(gL.T))
948 return;
949 luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref);
950 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER5a50a852015-09-23 16:59:28 +0200951 /* Forces a garbage collecting process. If the Lua program is finished
952 * without error, we run the GC on the thread pointer. Its freed all
953 * the unused memory.
954 * If the thread is finnish with an error or is currently yielded,
955 * it seems that the GC applied on the thread doesn't clean anything,
956 * so e run the GC on the main thread.
957 * NOTE: maybe this action locks all the Lua threads untiml the en of
958 * the garbage collection.
959 */
Willy Tarreauf31af932020-01-14 09:59:38 +0100960 if (lua->gc_count) {
Thierry FOURNIER7bd10d52017-07-17 00:44:40 +0200961 if (!SET_SAFE_LJMP(gL.T))
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200962 return;
Thierry FOURNIER7bd10d52017-07-17 00:44:40 +0200963 lua_gc(gL.T, LUA_GCCOLLECT, 0);
964 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +0200965 }
Thierry FOURNIER5a50a852015-09-23 16:59:28 +0200966
Thierry FOURNIERa7b536b2015-09-21 22:50:24 +0200967 lua->T = NULL;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100968
969end:
Willy Tarreaubafbe012017-11-24 17:34:44 +0100970 pool_free(pool_head_hlua, lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100971}
972
973/* This function is used to restore the Lua context when a coroutine
974 * fails. This function copy the common memory between old coroutine
975 * and the new coroutine. The old coroutine is destroyed, and its
976 * replaced by the new coroutine.
977 * If the flag "keep_msg" is set, the last entry of the old is assumed
978 * as string error message and it is copied in the new stack.
979 */
980static int hlua_ctx_renew(struct hlua *lua, int keep_msg)
981{
982 lua_State *T;
983 int new_ref;
984
985 /* Renew the main LUA stack doesn't have sense. */
986 if (lua == &gL)
987 return 0;
988
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100989 /* New Lua coroutine. */
990 T = lua_newthread(gL.T);
991 if (!T)
992 return 0;
993
994 /* Copy last error message. */
995 if (keep_msg)
996 lua_xmove(lua->T, T, 1);
997
998 /* Copy data between the coroutines. */
999 lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref);
1000 lua_xmove(lua->T, T, 1);
1001 new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Valur poped. */
1002
1003 /* Destroy old data. */
1004 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
1005
1006 /* The thread is garbage collected by Lua. */
1007 luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref);
1008
1009 /* Fill the struct with the new coroutine values. */
1010 lua->Mref = new_ref;
1011 lua->T = T;
1012 lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
1013
1014 /* Set context. */
1015 hlua_sethlua(lua);
1016
1017 return 1;
1018}
1019
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001020void hlua_hook(lua_State *L, lua_Debug *ar)
1021{
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001022 struct hlua *hlua = hlua_gethlua(L);
1023
1024 /* Lua cannot yield when its returning from a function,
1025 * so, we can fix the interrupt hook to 1 instruction,
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001026 * expecting that the function is finished.
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001027 */
1028 if (lua_gethookmask(L) & LUA_MASKRET) {
1029 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1);
1030 return;
1031 }
1032
1033 /* restore the interrupt condition. */
1034 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
1035
1036 /* If we interrupt the Lua processing in yieldable state, we yield.
1037 * If the state is not yieldable, trying yield causes an error.
1038 */
1039 if (lua_isyieldable(L))
Willy Tarreau9635e032018-10-16 17:52:55 +02001040 MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD));
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001041
Thierry FOURNIERa85cfb12015-03-13 14:50:06 +01001042 /* If we cannot yield, update the clock and check the timeout. */
1043 tv_update_date(0, 1);
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001044 hlua->run_time += now_ms - hlua->start_time;
1045 if (hlua->max_time && hlua->run_time >= hlua->max_time) {
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001046 lua_pushfstring(L, "execution timeout");
1047 WILL_LJMP(lua_error(L));
1048 }
1049
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001050 /* Update the start time. */
1051 hlua->start_time = now_ms;
1052
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001053 /* Try to interrupt the process at the end of the current
1054 * unyieldable function.
1055 */
1056 lua_sethook(hlua->T, hlua_hook, LUA_MASKRET|LUA_MASKCOUNT, hlua_nb_instruction);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001057}
1058
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001059/* This function start or resumes the Lua stack execution. If the flag
1060 * "yield_allowed" if no set and the LUA stack execution returns a yield
1061 * The function return an error.
1062 *
1063 * The function can returns 4 values:
1064 * - HLUA_E_OK : The execution is terminated without any errors.
1065 * - HLUA_E_AGAIN : The execution must continue at the next associated
1066 * task wakeup.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001067 * - HLUA_E_ERRMSG : An error has occurred, an error message is set in
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001068 * the top of the stack.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001069 * - HLUA_E_ERR : An error has occurred without error message.
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001070 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001071 * If an error occurred, the stack is renewed and it is ready to run new
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001072 * LUA code.
1073 */
1074static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed)
1075{
1076 int ret;
1077 const char *msg;
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001078 const char *trace;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001079
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001080 /* Initialise run time counter. */
1081 if (!HLUA_IS_RUNNING(lua))
1082 lua->run_time = 0;
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001083
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001084 /* Lock the whole Lua execution. This lock must be before the
1085 * label "resume_execution".
1086 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001087 HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock);
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001088
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001089resume_execution:
1090
1091 /* This hook interrupts the Lua processing each 'hlua_nb_instruction'
1092 * instructions. it is used for preventing infinite loops.
1093 */
1094 lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
1095
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +01001096 /* Remove all flags except the running flags. */
Thierry FOURNIER2f3867f2015-09-28 01:02:01 +02001097 HLUA_SET_RUN(lua);
1098 HLUA_CLR_CTRLYIELD(lua);
1099 HLUA_CLR_WAKERESWR(lua);
1100 HLUA_CLR_WAKEREQWR(lua);
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +01001101
Christopher Fauletbc275a92020-02-26 14:55:16 +01001102 /* Update the start time and reset wake_time. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001103 lua->start_time = now_ms;
Christopher Fauletbc275a92020-02-26 14:55:16 +01001104 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001105
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001106 /* Call the function. */
1107 ret = lua_resume(lua->T, gL.T, lua->nargs);
1108 switch (ret) {
1109
1110 case LUA_OK:
1111 ret = HLUA_E_OK;
1112 break;
1113
1114 case LUA_YIELD:
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001115 /* Check if the execution timeout is expired. It it is the case, we
1116 * break the Lua execution.
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001117 */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001118 tv_update_date(0, 1);
1119 lua->run_time += now_ms - lua->start_time;
1120 if (lua->max_time && lua->run_time > lua->max_time) {
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001121 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001122 ret = HLUA_E_ETMOUT;
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001123 break;
1124 }
1125 /* Process the forced yield. if the general yield is not allowed or
1126 * if no task were associated this the current Lua execution
1127 * coroutine, we resume the execution. Else we want to return in the
1128 * scheduler and we want to be waked up again, to continue the
1129 * current Lua execution. So we schedule our own task.
1130 */
1131 if (HLUA_IS_CTRLYIELDING(lua)) {
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001132 if (!yield_allowed || !lua->task)
1133 goto resume_execution;
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001134 task_wakeup(lua->task, TASK_WOKEN_MSG);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001135 }
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001136 if (!yield_allowed) {
1137 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001138 ret = HLUA_E_YIELD;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001139 break;
1140 }
1141 ret = HLUA_E_AGAIN;
1142 break;
1143
1144 case LUA_ERRRUN:
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001145
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001146 /* Special exit case. The traditional exit is returned as an error
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001147 * because the errors ares the only one mean to return immediately
1148 * from and lua execution.
1149 */
1150 if (lua->flags & HLUA_EXIT) {
1151 ret = HLUA_E_OK;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01001152 hlua_ctx_renew(lua, 1);
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001153 break;
1154 }
1155
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001156 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001157 if (!lua_checkstack(lua->T, 1)) {
1158 ret = HLUA_E_ERR;
1159 break;
1160 }
1161 msg = lua_tostring(lua->T, -1);
1162 lua_settop(lua->T, 0); /* Empty the stack. */
1163 lua_pop(lua->T, 1);
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001164 trace = hlua_traceback(lua->T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001165 if (msg)
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001166 lua_pushfstring(lua->T, "runtime error: %s from %s", msg, trace);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001167 else
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001168 lua_pushfstring(lua->T, "unknown runtime error from %s", trace);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001169 ret = HLUA_E_ERRMSG;
1170 break;
1171
1172 case LUA_ERRMEM:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001173 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001174 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001175 ret = HLUA_E_NOMEM;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001176 break;
1177
1178 case LUA_ERRERR:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001179 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001180 if (!lua_checkstack(lua->T, 1)) {
1181 ret = HLUA_E_ERR;
1182 break;
1183 }
1184 msg = lua_tostring(lua->T, -1);
1185 lua_settop(lua->T, 0); /* Empty the stack. */
1186 lua_pop(lua->T, 1);
1187 if (msg)
1188 lua_pushfstring(lua->T, "message handler error: %s", msg);
1189 else
1190 lua_pushfstring(lua->T, "message handler error");
1191 ret = HLUA_E_ERRMSG;
1192 break;
1193
1194 default:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001195 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001196 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001197 ret = HLUA_E_ERR;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001198 break;
1199 }
1200
1201 switch (ret) {
1202 case HLUA_E_AGAIN:
1203 break;
1204
1205 case HLUA_E_ERRMSG:
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001206 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001207 hlua_ctx_renew(lua, 1);
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001208 HLUA_CLR_RUN(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001209 break;
1210
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001211 case HLUA_E_ETMOUT:
1212 case HLUA_E_NOMEM:
1213 case HLUA_E_YIELD:
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001214 case HLUA_E_ERR:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001215 HLUA_CLR_RUN(lua);
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001216 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001217 hlua_ctx_renew(lua, 0);
1218 break;
1219
1220 case HLUA_E_OK:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001221 HLUA_CLR_RUN(lua);
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001222 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001223 break;
1224 }
1225
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001226 /* This is the main exit point, remove the Lua lock. */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001227 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock);
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001228
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001229 return ret;
1230}
1231
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001232/* This function exit the current code. */
1233__LJMP static int hlua_done(lua_State *L)
1234{
1235 struct hlua *hlua = hlua_gethlua(L);
1236
1237 hlua->flags |= HLUA_EXIT;
1238 WILL_LJMP(lua_error(L));
1239
1240 return 0;
1241}
1242
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001243/* This function is an LUA binding. It provides a function
1244 * for deleting ACL from a referenced ACL file.
1245 */
1246__LJMP static int hlua_del_acl(lua_State *L)
1247{
1248 const char *name;
1249 const char *key;
1250 struct pat_ref *ref;
1251
1252 MAY_LJMP(check_args(L, 2, "del_acl"));
1253
1254 name = MAY_LJMP(luaL_checkstring(L, 1));
1255 key = MAY_LJMP(luaL_checkstring(L, 2));
1256
1257 ref = pat_ref_lookup(name);
1258 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001259 WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001260
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001261 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001262 pat_ref_delete(ref, key);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001263 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001264 return 0;
1265}
1266
1267/* This function is an LUA binding. It provides a function
1268 * for deleting map entry from a referenced map file.
1269 */
1270static int hlua_del_map(lua_State *L)
1271{
1272 const char *name;
1273 const char *key;
1274 struct pat_ref *ref;
1275
1276 MAY_LJMP(check_args(L, 2, "del_map"));
1277
1278 name = MAY_LJMP(luaL_checkstring(L, 1));
1279 key = MAY_LJMP(luaL_checkstring(L, 2));
1280
1281 ref = pat_ref_lookup(name);
1282 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001283 WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001284
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001285 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001286 pat_ref_delete(ref, key);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001287 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001288 return 0;
1289}
1290
1291/* This function is an LUA binding. It provides a function
1292 * for adding ACL pattern from a referenced ACL file.
1293 */
1294static int hlua_add_acl(lua_State *L)
1295{
1296 const char *name;
1297 const char *key;
1298 struct pat_ref *ref;
1299
1300 MAY_LJMP(check_args(L, 2, "add_acl"));
1301
1302 name = MAY_LJMP(luaL_checkstring(L, 1));
1303 key = MAY_LJMP(luaL_checkstring(L, 2));
1304
1305 ref = pat_ref_lookup(name);
1306 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001307 WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001308
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001309 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001310 if (pat_ref_find_elt(ref, key) == NULL)
1311 pat_ref_add(ref, key, NULL, NULL);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001312 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001313 return 0;
1314}
1315
1316/* This function is an LUA binding. It provides a function
1317 * for setting map pattern and sample from a referenced map
1318 * file.
1319 */
1320static int hlua_set_map(lua_State *L)
1321{
1322 const char *name;
1323 const char *key;
1324 const char *value;
1325 struct pat_ref *ref;
1326
1327 MAY_LJMP(check_args(L, 3, "set_map"));
1328
1329 name = MAY_LJMP(luaL_checkstring(L, 1));
1330 key = MAY_LJMP(luaL_checkstring(L, 2));
1331 value = MAY_LJMP(luaL_checkstring(L, 3));
1332
1333 ref = pat_ref_lookup(name);
1334 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001335 WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001336
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001337 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001338 if (pat_ref_find_elt(ref, key) != NULL)
1339 pat_ref_set(ref, key, value, NULL);
1340 else
1341 pat_ref_add(ref, key, value, NULL);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001342 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001343 return 0;
1344}
1345
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001346/* A class is a lot of memory that contain data. This data can be a table,
1347 * an integer or user data. This data is associated with a metatable. This
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001348 * metatable have an original version registered in the global context with
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001349 * the name of the object (_G[<name>] = <metable> ).
1350 *
1351 * A metable is a table that modify the standard behavior of a standard
1352 * access to the associated data. The entries of this new metatable are
1353 * defined as is:
1354 *
1355 * http://lua-users.org/wiki/MetatableEvents
1356 *
1357 * __index
1358 *
1359 * we access an absent field in a table, the result is nil. This is
1360 * true, but it is not the whole truth. Actually, such access triggers
1361 * the interpreter to look for an __index metamethod: If there is no
1362 * such method, as usually happens, then the access results in nil;
1363 * otherwise, the metamethod will provide the result.
1364 *
1365 * Control 'prototype' inheritance. When accessing "myTable[key]" and
1366 * the key does not appear in the table, but the metatable has an __index
1367 * property:
1368 *
1369 * - if the value is a function, the function is called, passing in the
1370 * table and the key; the return value of that function is returned as
1371 * the result.
1372 *
1373 * - if the value is another table, the value of the key in that table is
1374 * asked for and returned (and if it doesn't exist in that table, but that
1375 * table's metatable has an __index property, then it continues on up)
1376 *
1377 * - Use "rawget(myTable,key)" to skip this metamethod.
1378 *
1379 * http://www.lua.org/pil/13.4.1.html
1380 *
1381 * __newindex
1382 *
1383 * Like __index, but control property assignment.
1384 *
1385 * __mode - Control weak references. A string value with one or both
1386 * of the characters 'k' and 'v' which specifies that the the
1387 * keys and/or values in the table are weak references.
1388 *
1389 * __call - Treat a table like a function. When a table is followed by
1390 * parenthesis such as "myTable( 'foo' )" and the metatable has
1391 * a __call key pointing to a function, that function is invoked
1392 * (passing any specified arguments) and the return value is
1393 * returned.
1394 *
1395 * __metatable - Hide the metatable. When "getmetatable( myTable )" is
1396 * called, if the metatable for myTable has a __metatable
1397 * key, the value of that key is returned instead of the
1398 * actual metatable.
1399 *
1400 * __tostring - Control string representation. When the builtin
1401 * "tostring( myTable )" function is called, if the metatable
1402 * for myTable has a __tostring property set to a function,
1403 * that function is invoked (passing myTable to it) and the
1404 * return value is used as the string representation.
1405 *
1406 * __len - Control table length. When the table length is requested using
1407 * the length operator ( '#' ), if the metatable for myTable has
1408 * a __len key pointing to a function, that function is invoked
1409 * (passing myTable to it) and the return value used as the value
1410 * of "#myTable".
1411 *
1412 * __gc - Userdata finalizer code. When userdata is set to be garbage
1413 * collected, if the metatable has a __gc field pointing to a
1414 * function, that function is first invoked, passing the userdata
1415 * to it. The __gc metamethod is not called for tables.
1416 * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html)
1417 *
1418 * Special metamethods for redefining standard operators:
1419 * http://www.lua.org/pil/13.1.html
1420 *
1421 * __add "+"
1422 * __sub "-"
1423 * __mul "*"
1424 * __div "/"
1425 * __unm "!"
1426 * __pow "^"
1427 * __concat ".."
1428 *
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001429 * Special methods for redefining standard relations
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001430 * http://www.lua.org/pil/13.2.html
1431 *
1432 * __eq "=="
1433 * __lt "<"
1434 * __le "<="
1435 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001436
1437/*
1438 *
1439 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001440 * Class Map
1441 *
1442 *
1443 */
1444
1445/* Returns a struct hlua_map if the stack entry "ud" is
1446 * a class session, otherwise it throws an error.
1447 */
1448__LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud)
1449{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001450 return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001451}
1452
1453/* This function is the map constructor. It don't need
1454 * the class Map object. It creates and return a new Map
1455 * object. It must be called only during "body" or "init"
1456 * context because it process some filesystem accesses.
1457 */
1458__LJMP static int hlua_map_new(struct lua_State *L)
1459{
1460 const char *fn;
1461 int match = PAT_MATCH_STR;
1462 struct sample_conv conv;
1463 const char *file = "";
1464 int line = 0;
1465 lua_Debug ar;
1466 char *err = NULL;
1467 struct arg args[2];
1468
1469 if (lua_gettop(L) < 1 || lua_gettop(L) > 2)
1470 WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument."));
1471
1472 fn = MAY_LJMP(luaL_checkstring(L, 1));
1473
1474 if (lua_gettop(L) >= 2) {
1475 match = MAY_LJMP(luaL_checkinteger(L, 2));
1476 if (match < 0 || match >= PAT_MATCH_NUM)
1477 WILL_LJMP(luaL_error(L, "'new' needs a valid match method."));
1478 }
1479
1480 /* Get Lua filename and line number. */
1481 if (lua_getstack(L, 1, &ar)) { /* check function at level */
1482 lua_getinfo(L, "Sl", &ar); /* get info about it */
1483 if (ar.currentline > 0) { /* is there info? */
1484 file = ar.short_src;
1485 line = ar.currentline;
1486 }
1487 }
1488
1489 /* fill fake sample_conv struct. */
1490 conv.kw = ""; /* unused. */
1491 conv.process = NULL; /* unused. */
1492 conv.arg_mask = 0; /* unused. */
1493 conv.val_args = NULL; /* unused. */
1494 conv.out_type = SMP_T_STR;
1495 conv.private = (void *)(long)match;
1496 switch (match) {
1497 case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break;
1498 case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break;
1499 case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break;
1500 case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break;
1501 case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break;
1502 case PAT_MATCH_END: conv.in_type = SMP_T_STR; break;
1503 case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001504 case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001505 case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break;
1506 default:
1507 WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode."));
1508 }
1509
1510 /* fill fake args. */
1511 args[0].type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001512 args[0].data.str.area = (char *)fn;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001513 args[1].type = ARGT_STOP;
1514
1515 /* load the map. */
1516 if (!sample_load_map(args, &conv, file, line, &err)) {
1517 /* error case: we cant use luaL_error because we must
1518 * free the err variable.
1519 */
1520 luaL_where(L, 1);
1521 lua_pushfstring(L, "'new': %s.", err);
1522 lua_concat(L, 2);
1523 free(err);
1524 WILL_LJMP(lua_error(L));
1525 }
1526
1527 /* create the lua object. */
1528 lua_newtable(L);
1529 lua_pushlightuserdata(L, args[0].data.map);
1530 lua_rawseti(L, -2, 0);
1531
1532 /* Pop a class Map metatable and affect it to the userdata. */
1533 lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref);
1534 lua_setmetatable(L, -2);
1535
1536
1537 return 1;
1538}
1539
1540__LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str)
1541{
1542 struct map_descriptor *desc;
1543 struct pattern *pat;
1544 struct sample smp;
1545
1546 MAY_LJMP(check_args(L, 2, "lookup"));
1547 desc = MAY_LJMP(hlua_checkmap(L, 1));
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001548 if (desc->pat.expect_type == SMP_T_SINT) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001549 smp.data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001550 smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001551 }
1552 else {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001553 smp.data.type = SMP_T_STR;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001554 smp.flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001555 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 +02001556 }
1557
1558 pat = pattern_exec_match(&desc->pat, &smp, 1);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001559 if (!pat || !pat->data) {
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001560 if (str)
1561 lua_pushstring(L, "");
1562 else
1563 lua_pushnil(L);
1564 return 1;
1565 }
1566
1567 /* The Lua pattern must return a string, so we can't check the returned type */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001568 lua_pushlstring(L, pat->data->u.str.area, pat->data->u.str.data);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001569 return 1;
1570}
1571
1572__LJMP static int hlua_map_lookup(struct lua_State *L)
1573{
1574 return _hlua_map_lookup(L, 0);
1575}
1576
1577__LJMP static int hlua_map_slookup(struct lua_State *L)
1578{
1579 return _hlua_map_lookup(L, 1);
1580}
1581
1582/*
1583 *
1584 *
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001585 * Class Socket
1586 *
1587 *
1588 */
1589
1590__LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud)
1591{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001592 return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001593}
1594
1595/* This function is the handler called for each I/O on the established
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001596 * connection. It is used for notify space available to send or data
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001597 * received.
1598 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001599static void hlua_socket_handler(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001600{
Willy Tarreau00a37f02015-04-13 12:05:19 +02001601 struct stream_interface *si = appctx->owner;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001602
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001603 if (appctx->ctx.hlua_cosocket.die) {
1604 si_shutw(si);
1605 si_shutr(si);
1606 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);
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001609 stream_shutdown(si_strm(si), SF_ERR_KILLED);
1610 }
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
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02001620 /* if the connection is not established, inform the stream that we want
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001621 * to be notified whenever the connection completes.
1622 */
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02001623 if (si_opposite(si)->state < SI_ST_EST) {
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;
Willy Tarreauf31af932020-01-14 09:59:38 +01001709 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001710
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001711 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001712
1713 /* Check if we run on the same thread than the xreator thread.
1714 * We cannot access to the socket if the thread is different.
1715 */
1716 if (socket->tid != tid)
1717 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1718
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001719 peer = xref_get_peer_and_lock(&socket->xref);
1720 if (!peer)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001721 return 0;
Willy Tarreauf31af932020-01-14 09:59:38 +01001722
1723 hlua->gc_count--;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001724 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001725
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001726 /* Set the flag which destroy the session. */
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001727 appctx->ctx.hlua_cosocket.die = 1;
1728 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001729
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001730 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001731 xref_disconnect(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001732 return 0;
1733}
1734
sada05ed3302018-05-11 11:48:18 -07001735/* The close function calls close_helper.
1736 */
1737__LJMP static int hlua_socket_close(lua_State *L)
1738{
1739 MAY_LJMP(check_args(L, 1, "close"));
1740 return hlua_socket_close_helper(L);
1741}
1742
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001743/* This Lua function assumes that the stack contain three parameters.
1744 * 1 - USERDATA containing a struct socket
1745 * 2 - INTEGER with values of the macro defined below
1746 * If the integer is -1, we must read at most one line.
1747 * If the integer is -2, we ust read all the data until the
1748 * end of the stream.
1749 * If the integer is positive value, we must read a number of
1750 * bytes corresponding to this value.
1751 */
1752#define HLSR_READ_LINE (-1)
1753#define HLSR_READ_ALL (-2)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001754__LJMP static int hlua_socket_receive_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001755{
1756 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
1757 int wanted = lua_tointeger(L, 2);
1758 struct hlua *hlua = hlua_gethlua(L);
1759 struct appctx *appctx;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001760 size_t len;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001761 int nblk;
Willy Tarreau206ba832018-06-14 15:27:31 +02001762 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001763 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02001764 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001765 size_t len2;
Thierry FOURNIER00543922015-03-09 18:35:06 +01001766 int skip_at_end = 0;
Willy Tarreau81389672015-03-10 12:03:52 +01001767 struct channel *oc;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001768 struct stream_interface *si;
1769 struct stream *s;
1770 struct xref *peer;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001771 int missing_bytes;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001772
1773 /* Check if this lua stack is schedulable. */
1774 if (!hlua || !hlua->task)
1775 WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in "
1776 "'frontend', 'backend' or 'task'"));
1777
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001778 /* Check if we run on the same thread than the xreator thread.
1779 * We cannot access to the socket if the thread is different.
1780 */
1781 if (socket->tid != tid)
1782 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1783
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001784 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001785 peer = xref_get_peer_and_lock(&socket->xref);
1786 if (!peer)
1787 goto no_peer;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001788 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
1789 si = appctx->owner;
1790 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001791
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001792 oc = &s->res;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001793 if (wanted == HLSR_READ_LINE) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001794 /* Read line. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001795 nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001796 if (nblk < 0) /* Connection close. */
1797 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001798 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001799 goto connection_empty;
Thierry FOURNIER00543922015-03-09 18:35:06 +01001800
1801 /* remove final \r\n. */
1802 if (nblk == 1) {
1803 if (blk1[len1-1] == '\n') {
1804 len1--;
1805 skip_at_end++;
1806 if (blk1[len1-1] == '\r') {
1807 len1--;
1808 skip_at_end++;
1809 }
1810 }
1811 }
1812 else {
1813 if (blk2[len2-1] == '\n') {
1814 len2--;
1815 skip_at_end++;
1816 if (blk2[len2-1] == '\r') {
1817 len2--;
1818 skip_at_end++;
1819 }
1820 }
1821 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001822 }
1823
1824 else if (wanted == HLSR_READ_ALL) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001825 /* Read all the available data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001826 nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001827 if (nblk < 0) /* Connection close. */
1828 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001829 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001830 goto connection_empty;
1831 }
1832
1833 else {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001834 /* Read a block of data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001835 nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001836 if (nblk < 0) /* Connection close. */
1837 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001838 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001839 goto connection_empty;
1840
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001841 missing_bytes = wanted - socket->b.n;
1842 if (len1 > missing_bytes) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001843 nblk = 1;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001844 len1 = missing_bytes;
1845 } if (nblk == 2 && len1 + len2 > missing_bytes)
1846 len2 = missing_bytes - len1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001847 }
1848
1849 len = len1;
1850
1851 luaL_addlstring(&socket->b, blk1, len1);
1852 if (nblk == 2) {
1853 len += len2;
1854 luaL_addlstring(&socket->b, blk2, len2);
1855 }
1856
1857 /* Consume data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001858 co_skip(oc, len + skip_at_end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001859
1860 /* Don't wait anything. */
Thierry FOURNIER7e4ee472018-05-25 15:03:50 +02001861 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001862
1863 /* If the pattern reclaim to read all the data
1864 * in the connection, got out.
1865 */
1866 if (wanted == HLSR_READ_ALL)
1867 goto connection_empty;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001868 else if (wanted >= 0 && socket->b.n < wanted)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001869 goto connection_empty;
1870
1871 /* Return result. */
1872 luaL_pushresult(&socket->b);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001873 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001874 return 1;
1875
1876connection_closed:
1877
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001878 xref_unlock(&socket->xref, peer);
1879
1880no_peer:
1881
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001882 /* If the buffer containds data. */
1883 if (socket->b.n > 0) {
1884 luaL_pushresult(&socket->b);
1885 return 1;
1886 }
1887 lua_pushnil(L);
1888 lua_pushstring(L, "connection closed.");
1889 return 2;
1890
1891connection_empty:
1892
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001893 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) {
1894 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001895 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001896 }
1897 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02001898 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_receive_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001899 return 0;
1900}
1901
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001902/* This Lua function gets two parameters. The first one can be string
1903 * or a number. If the string is "*l", the user requires one line. If
1904 * the string is "*a", the user requires all the contents of the stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001905 * If the value is a number, the user require a number of bytes equal
1906 * to the value. The default value is "*l" (a line).
1907 *
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001908 * This parameter with a variable type is converted in integer. This
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001909 * integer takes this values:
1910 * -1 : read a line
1911 * -2 : read all the stream
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001912 * >0 : amount of bytes.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001913 *
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001914 * The second parameter is optional. It contains a string that must be
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001915 * concatenated with the read data.
1916 */
1917__LJMP static int hlua_socket_receive(struct lua_State *L)
1918{
1919 int wanted = HLSR_READ_LINE;
1920 const char *pattern;
1921 int type;
1922 char *error;
1923 size_t len;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001924 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001925
1926 if (lua_gettop(L) < 1 || lua_gettop(L) > 3)
1927 WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments."));
1928
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001929 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001930
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001931 /* Check if we run on the same thread than the xreator thread.
1932 * We cannot access to the socket if the thread is different.
1933 */
1934 if (socket->tid != tid)
1935 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1936
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001937 /* check for pattern. */
1938 if (lua_gettop(L) >= 2) {
1939 type = lua_type(L, 2);
1940 if (type == LUA_TSTRING) {
1941 pattern = lua_tostring(L, 2);
1942 if (strcmp(pattern, "*a") == 0)
1943 wanted = HLSR_READ_ALL;
1944 else if (strcmp(pattern, "*l") == 0)
1945 wanted = HLSR_READ_LINE;
1946 else {
1947 wanted = strtoll(pattern, &error, 10);
1948 if (*error != '\0')
1949 WILL_LJMP(luaL_error(L, "Unsupported pattern."));
1950 }
1951 }
1952 else if (type == LUA_TNUMBER) {
1953 wanted = lua_tointeger(L, 2);
1954 if (wanted < 0)
1955 WILL_LJMP(luaL_error(L, "Unsupported size."));
1956 }
1957 }
1958
1959 /* Set pattern. */
1960 lua_pushinteger(L, wanted);
Tim Duesterhusc6e377e2018-01-04 19:32:13 +01001961
1962 /* Check if we would replace the top by itself. */
1963 if (lua_gettop(L) != 2)
1964 lua_replace(L, 2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001965
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001966 /* init buffer, and fill it with prefix. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001967 luaL_buffinit(L, &socket->b);
1968
1969 /* Check prefix. */
1970 if (lua_gettop(L) >= 3) {
1971 if (lua_type(L, 3) != LUA_TSTRING)
1972 WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix"));
1973 pattern = lua_tolstring(L, 3, &len);
1974 luaL_addlstring(&socket->b, pattern, len);
1975 }
1976
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001977 return __LJMP(hlua_socket_receive_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001978}
1979
1980/* Write the Lua input string in the output buffer.
Mark Lakes22154b42018-01-29 14:38:40 -08001981 * This function returns a yield if no space is available.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001982 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001983static int hlua_socket_write_yield(struct lua_State *L,int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001984{
1985 struct hlua_socket *socket;
1986 struct hlua *hlua = hlua_gethlua(L);
1987 struct appctx *appctx;
1988 size_t buf_len;
1989 const char *buf;
1990 int len;
1991 int send_len;
1992 int sent;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001993 struct xref *peer;
1994 struct stream_interface *si;
1995 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001996
1997 /* Check if this lua stack is schedulable. */
1998 if (!hlua || !hlua->task)
1999 WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in "
2000 "'frontend', 'backend' or 'task'"));
2001
2002 /* Get object */
2003 socket = MAY_LJMP(hlua_checksocket(L, 1));
2004 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002005 sent = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002006
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002007 /* Check if we run on the same thread than the xreator thread.
2008 * We cannot access to the socket if the thread is different.
2009 */
2010 if (socket->tid != tid)
2011 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2012
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002013 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002014 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002015 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002016 lua_pushinteger(L, -1);
2017 return 1;
2018 }
2019 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2020 si = appctx->owner;
2021 s = si_strm(si);
2022
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002023 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002024 if (channel_output_closed(&s->req)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002025 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002026 lua_pushinteger(L, -1);
2027 return 1;
2028 }
2029
2030 /* Update the input buffer data. */
2031 buf += sent;
2032 send_len = buf_len - sent;
2033
2034 /* All the data are sent. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002035 if (sent >= buf_len) {
2036 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002037 return 1; /* Implicitly return the length sent. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002038 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002039
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002040 /* Check if the buffer is available because HAProxy doesn't allocate
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01002041 * the request buffer if its not required.
2042 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002043 if (s->req.buf.size == 0) {
Willy Tarreau581abd32018-10-25 10:21:41 +02002044 if (!si_alloc_ibuf(si, &appctx->buffer_wait))
Christopher Faulet33834b12016-12-19 09:29:06 +01002045 goto hlua_socket_write_yield_return;
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01002046 }
2047
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002048 /* Check for available space. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002049 len = b_room(&s->req.buf);
Christopher Faulet33834b12016-12-19 09:29:06 +01002050 if (len <= 0) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002051 goto hlua_socket_write_yield_return;
Christopher Faulet33834b12016-12-19 09:29:06 +01002052 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002053
2054 /* send data */
2055 if (len < send_len)
2056 send_len = len;
Thierry FOURNIER66b89192018-05-27 01:14:47 +02002057 len = ci_putblk(&s->req, buf, send_len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002058
2059 /* "Not enough space" (-1), "Buffer too little to contain
2060 * the data" (-2) are not expected because the available length
2061 * is tested.
2062 * Other unknown error are also not expected.
2063 */
2064 if (len <= 0) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01002065 if (len == -1)
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002066 s->req.flags |= CF_WAKE_WRITE;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002067
sada05ed3302018-05-11 11:48:18 -07002068 MAY_LJMP(hlua_socket_close_helper(L));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002069 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002070 lua_pushinteger(L, -1);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002071 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002072 return 1;
2073 }
2074
2075 /* update buffers. */
Thierry FOURNIER101b9762018-05-27 01:27:40 +02002076 appctx_wakeup(appctx);
Willy Tarreaude70fa12015-09-26 11:25:05 +02002077
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002078 s->req.rex = TICK_ETERNITY;
2079 s->res.wex = TICK_ETERNITY;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002080
2081 /* Update length sent. */
2082 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002083 lua_pushinteger(L, sent + len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002084
2085 /* All the data buffer is sent ? */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002086 if (sent + len >= buf_len) {
2087 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002088 return 1;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002089 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002090
2091hlua_socket_write_yield_return:
Thierry FOURNIERba42fcd2018-05-27 00:59:48 +02002092 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2093 xref_unlock(&socket->xref, peer);
2094 WILL_LJMP(luaL_error(L, "out of memory"));
2095 }
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002096 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002097 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_write_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002098 return 0;
2099}
2100
2101/* This function initiate the send of data. It just check the input
2102 * parameters and push an integer in the Lua stack that contain the
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002103 * amount of data written to the buffer. This is used by the function
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002104 * "hlua_socket_write_yield" that can yield.
2105 *
2106 * The Lua function gets between 3 and 4 parameters. The first one is
2107 * the associated object. The second is a string buffer. The third is
2108 * a facultative integer that represents where is the buffer position
2109 * of the start of the data that can send. The first byte is the
2110 * position "1". The default value is "1". The fourth argument is a
2111 * facultative integer that represents where is the buffer position
2112 * of the end of the data that can send. The default is the last byte.
2113 */
2114static int hlua_socket_send(struct lua_State *L)
2115{
2116 int i;
2117 int j;
2118 const char *buf;
2119 size_t buf_len;
2120
2121 /* Check number of arguments. */
2122 if (lua_gettop(L) < 2 || lua_gettop(L) > 4)
2123 WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments"));
2124
2125 /* Get the string. */
2126 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
2127
2128 /* Get and check j. */
2129 if (lua_gettop(L) == 4) {
2130 j = MAY_LJMP(luaL_checkinteger(L, 4));
2131 if (j < 0)
2132 j = buf_len + j + 1;
2133 if (j > buf_len)
2134 j = buf_len + 1;
2135 lua_pop(L, 1);
2136 }
2137 else
2138 j = buf_len;
2139
2140 /* Get and check i. */
2141 if (lua_gettop(L) == 3) {
2142 i = MAY_LJMP(luaL_checkinteger(L, 3));
2143 if (i < 0)
2144 i = buf_len + i + 1;
2145 if (i > buf_len)
2146 i = buf_len + 1;
2147 lua_pop(L, 1);
2148 } else
2149 i = 1;
2150
2151 /* Check bth i and j. */
2152 if (i > j) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002153 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002154 return 1;
2155 }
2156 if (i == 0 && j == 0) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002157 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002158 return 1;
2159 }
2160 if (i == 0)
2161 i = 1;
2162 if (j == 0)
2163 j = 1;
2164
2165 /* Pop the string. */
2166 lua_pop(L, 1);
2167
2168 /* Update the buffer length. */
2169 buf += i - 1;
2170 buf_len = j - i + 1;
2171 lua_pushlstring(L, buf, buf_len);
2172
2173 /* This unsigned is used to remember the amount of sent data. */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002174 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002175
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002176 return MAY_LJMP(hlua_socket_write_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002177}
2178
Willy Tarreau22b0a682015-06-17 19:43:49 +02002179#define SOCKET_INFO_MAX_LEN sizeof("[0000:0000:0000:0000:0000:0000:0000:0000]:12345")
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002180__LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr)
2181{
2182 static char buffer[SOCKET_INFO_MAX_LEN];
2183 int ret;
2184 int len;
2185 char *p;
2186
2187 ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1);
2188 if (ret <= 0) {
2189 lua_pushnil(L);
2190 return 1;
2191 }
2192
2193 if (ret == AF_UNIX) {
2194 lua_pushstring(L, buffer+1);
2195 return 1;
2196 }
2197 else if (ret == AF_INET6) {
2198 buffer[0] = '[';
2199 len = strlen(buffer);
2200 buffer[len] = ']';
2201 len++;
2202 buffer[len] = ':';
2203 len++;
2204 p = buffer;
2205 }
2206 else if (ret == AF_INET) {
2207 p = buffer + 1;
2208 len = strlen(p);
2209 p[len] = ':';
2210 len++;
2211 }
2212 else {
2213 lua_pushnil(L);
2214 return 1;
2215 }
2216
2217 if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) {
2218 lua_pushnil(L);
2219 return 1;
2220 }
2221
2222 lua_pushstring(L, p);
2223 return 1;
2224}
2225
2226/* Returns information about the peer of the connection. */
2227__LJMP static int hlua_socket_getpeername(struct lua_State *L)
2228{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002229 struct hlua_socket *socket;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002230 struct xref *peer;
2231 struct appctx *appctx;
2232 struct stream_interface *si;
2233 struct stream *s;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002234 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002235
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002236 MAY_LJMP(check_args(L, 1, "getpeername"));
2237
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002238 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002239
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002240 /* Check if we run on the same thread than the xreator thread.
2241 * We cannot access to the socket if the thread is different.
2242 */
2243 if (socket->tid != tid)
2244 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2245
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002246 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002247 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002248 if (!peer) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002249 lua_pushnil(L);
2250 return 1;
2251 }
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002252 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2253 si = appctx->owner;
2254 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002255
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002256 if (!s->target_addr) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002257 xref_unlock(&socket->xref, peer);
Willy Tarreaua71f6422016-11-16 17:00:14 +01002258 lua_pushnil(L);
2259 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002260 }
2261
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002262 ret = MAY_LJMP(hlua_socket_info(L, s->target_addr));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002263 xref_unlock(&socket->xref, peer);
2264 return ret;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002265}
2266
2267/* Returns information about my connection side. */
2268static int hlua_socket_getsockname(struct lua_State *L)
2269{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002270 struct hlua_socket *socket;
2271 struct connection *conn;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002272 struct appctx *appctx;
2273 struct xref *peer;
2274 struct stream_interface *si;
2275 struct stream *s;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002276 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002277
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002278 MAY_LJMP(check_args(L, 1, "getsockname"));
2279
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002280 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002281
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002282 /* Check if we run on the same thread than the xreator thread.
2283 * We cannot access to the socket if the thread is different.
2284 */
2285 if (socket->tid != tid)
2286 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2287
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002288 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002289 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002290 if (!peer) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002291 lua_pushnil(L);
2292 return 1;
2293 }
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002294 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2295 si = appctx->owner;
2296 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002297
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002298 conn = cs_conn(objt_cs(s->si[1].end));
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002299 if (!conn || !conn_get_src(conn)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002300 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002301 lua_pushnil(L);
2302 return 1;
2303 }
2304
Willy Tarreau9da9a6f2019-07-17 14:49:44 +02002305 ret = hlua_socket_info(L, conn->src);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002306 xref_unlock(&socket->xref, peer);
2307 return ret;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002308}
2309
2310/* This struct define the applet. */
Willy Tarreau30576452015-04-13 13:50:30 +02002311static struct applet update_applet = {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002312 .obj_type = OBJ_TYPE_APPLET,
2313 .name = "<LUA_TCP>",
2314 .fct = hlua_socket_handler,
2315 .release = hlua_socket_release,
2316};
2317
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002318__LJMP static int hlua_socket_connect_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002319{
2320 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
2321 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002322 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002323 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002324 struct stream_interface *si;
2325 struct stream *s;
2326
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002327 /* Check if we run on the same thread than the xreator thread.
2328 * We cannot access to the socket if the thread is different.
2329 */
2330 if (socket->tid != tid)
2331 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2332
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002333 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002334 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002335 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002336 lua_pushnil(L);
2337 lua_pushstring(L, "Can't connect");
2338 return 2;
2339 }
2340 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2341 si = appctx->owner;
2342 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002343
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002344 /* Check if we run on the same thread than the xreator thread.
2345 * We cannot access to the socket if the thread is different.
2346 */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002347 if (socket->tid != tid) {
2348 xref_unlock(&socket->xref, peer);
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002349 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002350 }
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002351
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002352 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002353 if (!hlua || channel_output_closed(&s->req)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002354 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002355 lua_pushnil(L);
2356 lua_pushstring(L, "Can't connect");
2357 return 2;
2358 }
2359
Willy Tarreaue09101e2018-10-16 17:37:12 +02002360 appctx = __objt_appctx(s->si[0].end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002361
2362 /* Check for connection established. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002363 if (appctx->ctx.hlua_cosocket.connected) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002364 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002365 lua_pushinteger(L, 1);
2366 return 1;
2367 }
2368
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002369 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2370 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002371 WILL_LJMP(luaL_error(L, "out of memory error"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002372 }
2373 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002374 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002375 return 0;
2376}
2377
2378/* This function fail or initite the connection. */
2379__LJMP static int hlua_socket_connect(struct lua_State *L)
2380{
2381 struct hlua_socket *socket;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002382 int port = -1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002383 const char *ip;
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002384 struct hlua *hlua;
2385 struct appctx *appctx;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002386 int low, high;
2387 struct sockaddr_storage *addr;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002388 struct xref *peer;
2389 struct stream_interface *si;
2390 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002391
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002392 if (lua_gettop(L) < 2)
2393 WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments"));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002394
2395 /* Get args. */
2396 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002397
2398 /* Check if we run on the same thread than the xreator thread.
2399 * We cannot access to the socket if the thread is different.
2400 */
2401 if (socket->tid != tid)
2402 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2403
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002404 ip = MAY_LJMP(luaL_checkstring(L, 2));
Tim Duesterhus6edab862018-01-06 19:04:45 +01002405 if (lua_gettop(L) >= 3) {
2406 luaL_Buffer b;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002407 port = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002408
Tim Duesterhus6edab862018-01-06 19:04:45 +01002409 /* Force the ip to end with a colon, to support IPv6 addresses
2410 * that are not enclosed within square brackets.
2411 */
2412 if (port > 0) {
2413 luaL_buffinit(L, &b);
2414 luaL_addstring(&b, ip);
2415 luaL_addchar(&b, ':');
2416 luaL_pushresult(&b);
2417 ip = lua_tolstring(L, lua_gettop(L), NULL);
2418 }
2419 }
2420
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002421 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002422 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002423 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002424 lua_pushnil(L);
2425 return 1;
2426 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002427
2428 /* Parse ip address. */
Willy Tarreau48ef4c92017-01-06 18:32:38 +01002429 addr = str2sa_range(ip, NULL, &low, &high, NULL, NULL, NULL, 0);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002430 if (!addr) {
2431 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002432 WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002433 }
2434 if (low != high) {
2435 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002436 WILL_LJMP(luaL_error(L, "connect: port ranges not supported : address '%s'", ip));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002437 }
Willy Tarreau9da9a6f2019-07-17 14:49:44 +02002438
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002439 /* Set port. */
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002440 if (low == 0) {
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002441 if (addr->ss_family == AF_INET) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002442 if (port == -1) {
2443 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002444 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002445 }
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002446 ((struct sockaddr_in *)addr)->sin_port = htons(port);
2447 } else if (addr->ss_family == AF_INET6) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002448 if (port == -1) {
2449 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002450 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002451 }
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002452 ((struct sockaddr_in6 *)addr)->sin6_port = htons(port);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002453 }
2454 }
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002455
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002456 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2457 si = appctx->owner;
2458 s = si_strm(si);
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002459
2460 if (!sockaddr_alloc(&s->target_addr)) {
2461 xref_unlock(&socket->xref, peer);
2462 WILL_LJMP(luaL_error(L, "connect: internal error"));
2463 }
2464 *s->target_addr = *addr;
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002465 s->flags |= SF_ADDR_SET;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002466
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002467 hlua = hlua_gethlua(L);
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002468
2469 /* inform the stream that we want to be notified whenever the
2470 * connection completes.
2471 */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01002472 si_cant_get(&s->si[0]);
Willy Tarreau3367d412018-11-15 10:57:41 +01002473 si_rx_endp_more(&s->si[0]);
Thierry FOURNIER8c8fbbe2015-09-26 17:02:35 +02002474 appctx_wakeup(appctx);
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002475
Willy Tarreauf31af932020-01-14 09:59:38 +01002476 hlua->gc_count++;
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +02002477
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002478 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2479 xref_unlock(&socket->xref, peer);
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002480 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002481 }
2482 xref_unlock(&socket->xref, peer);
PiBa-NL706d5ee2018-05-05 23:51:42 +02002483
2484 task_wakeup(s->task, TASK_WOKEN_INIT);
2485 /* Return yield waiting for connection. */
2486
Willy Tarreau9635e032018-10-16 17:52:55 +02002487 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002488
2489 return 0;
2490}
2491
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002492#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002493__LJMP static int hlua_socket_connect_ssl(struct lua_State *L)
2494{
2495 struct hlua_socket *socket;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002496 struct xref *peer;
2497 struct appctx *appctx;
2498 struct stream_interface *si;
2499 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002500
2501 MAY_LJMP(check_args(L, 3, "connect_ssl"));
2502 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002503
2504 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002505 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002506 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002507 lua_pushnil(L);
2508 return 1;
2509 }
2510 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2511 si = appctx->owner;
2512 s = si_strm(si);
2513
2514 s->target = &socket_ssl.obj_type;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002515 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002516 return MAY_LJMP(hlua_socket_connect(L));
2517}
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002518#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002519
2520__LJMP static int hlua_socket_setoption(struct lua_State *L)
2521{
2522 return 0;
2523}
2524
2525__LJMP static int hlua_socket_settimeout(struct lua_State *L)
2526{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002527 struct hlua_socket *socket;
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002528 int tmout;
Mark Lakes56cc1252018-03-27 09:48:06 +02002529 double dtmout;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002530 struct xref *peer;
2531 struct appctx *appctx;
2532 struct stream_interface *si;
2533 struct stream *s;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002534
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002535 MAY_LJMP(check_args(L, 2, "settimeout"));
2536
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002537 socket = MAY_LJMP(hlua_checksocket(L, 1));
Mark Lakes56cc1252018-03-27 09:48:06 +02002538
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002539 /* convert the timeout to millis */
2540 dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002541
Thierry Fournier17a921b2018-03-08 09:59:02 +01002542 /* Check for negative values */
Mark Lakes56cc1252018-03-27 09:48:06 +02002543 if (dtmout < 0)
Thierry Fournier17a921b2018-03-08 09:59:02 +01002544 WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values"));
2545
Mark Lakes56cc1252018-03-27 09:48:06 +02002546 if (dtmout > INT_MAX) /* overflow check */
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002547 WILL_LJMP(luaL_error(L, "settimeout: cannot set values larger than %d ms", INT_MAX));
Mark Lakes56cc1252018-03-27 09:48:06 +02002548
2549 tmout = MS_TO_TICKS((int)dtmout);
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002550 if (tmout == 0)
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05002551 tmout++; /* very small timeouts are adjusted to a minimum of 1ms */
Mark Lakes56cc1252018-03-27 09:48:06 +02002552
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002553 /* Check if we run on the same thread than the xreator thread.
2554 * We cannot access to the socket if the thread is different.
2555 */
2556 if (socket->tid != tid)
2557 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2558
Mark Lakes56cc1252018-03-27 09:48:06 +02002559 /* check for connection break. If some data were read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002560 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002561 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002562 hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts.");
2563 WILL_LJMP(lua_error(L));
2564 return 0;
2565 }
2566 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2567 si = appctx->owner;
2568 s = si_strm(si);
2569
Cyril Bonté7bb63452018-08-17 23:51:02 +02002570 s->sess->fe->timeout.connect = tmout;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002571 s->req.rto = tmout;
2572 s->req.wto = tmout;
2573 s->res.rto = tmout;
2574 s->res.wto = tmout;
Cyril Bonté7bb63452018-08-17 23:51:02 +02002575 s->req.rex = tick_add_ifset(now_ms, tmout);
2576 s->req.wex = tick_add_ifset(now_ms, tmout);
2577 s->res.rex = tick_add_ifset(now_ms, tmout);
2578 s->res.wex = tick_add_ifset(now_ms, tmout);
2579
2580 s->task->expire = tick_add_ifset(now_ms, tmout);
2581 task_queue(s->task);
2582
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002583 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002584
Thierry Fourniere9636f12018-03-08 09:54:32 +01002585 lua_pushinteger(L, 1);
Tim Duesterhus119a5f12018-01-06 19:16:25 +01002586 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002587}
2588
2589__LJMP static int hlua_socket_new(lua_State *L)
2590{
2591 struct hlua_socket *socket;
2592 struct appctx *appctx;
Willy Tarreau15b5e142015-04-04 14:38:25 +02002593 struct session *sess;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002594 struct stream *strm;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002595
2596 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002597 if (!lua_checkstack(L, 3)) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002598 hlua_pusherror(L, "socket: full stack");
2599 goto out_fail_conf;
2600 }
2601
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002602 /* Create the object: obj[0] = userdata. */
2603 lua_newtable(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002604 socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket)));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002605 lua_rawseti(L, -2, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002606 memset(socket, 0, sizeof(*socket));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002607 socket->tid = tid;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002608
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05002609 /* Check if the various memory pools are initialized. */
Willy Tarreaubafbe012017-11-24 17:34:44 +01002610 if (!pool_head_stream || !pool_head_buffer) {
Thierry FOURNIER4a6170c2015-03-09 17:07:10 +01002611 hlua_pusherror(L, "socket: uninitialized pools.");
2612 goto out_fail_conf;
2613 }
2614
Willy Tarreau87b09662015-04-03 00:22:06 +02002615 /* Pop a class stream metatable and affect it to the userdata. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002616 lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref);
2617 lua_setmetatable(L, -2);
2618
Willy Tarreaud420a972015-04-06 00:39:18 +02002619 /* Create the applet context */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01002620 appctx = appctx_new(&update_applet, tid_bit);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002621 if (!appctx) {
2622 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaufeb76402015-04-03 14:10:06 +02002623 goto out_fail_conf;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002624 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002625
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002626 appctx->ctx.hlua_cosocket.connected = 0;
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02002627 appctx->ctx.hlua_cosocket.die = 0;
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002628 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write);
2629 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read);
Willy Tarreaub2bf8332015-04-04 15:58:58 +02002630
Willy Tarreaud420a972015-04-06 00:39:18 +02002631 /* Now create a session, task and stream for this applet */
2632 sess = session_new(&socket_proxy, NULL, &appctx->obj_type);
2633 if (!sess) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002634 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002635 goto out_fail_sess;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002636 }
2637
Willy Tarreau87787ac2017-08-28 16:22:54 +02002638 strm = stream_new(sess, &appctx->obj_type);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002639 if (!strm) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002640 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002641 goto out_fail_stream;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002642 }
2643
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002644 /* Initialise cross reference between stream and Lua socket object. */
2645 xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002646
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002647 /* Configure "right" stream interface. this "si" is used to connect
2648 * and retrieve data from the server. The connection is initialized
2649 * with the "struct server".
2650 */
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002651 si_set_state(&strm->si[1], SI_ST_ASS);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002652
2653 /* Force destination server. */
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002654 strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_BE_ASSIGNED;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002655 strm->target = &socket_tcp.obj_type;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002656
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002657 return 1;
2658
Willy Tarreaud420a972015-04-06 00:39:18 +02002659 out_fail_stream:
Willy Tarreau11c36242015-04-04 15:54:03 +02002660 session_free(sess);
Willy Tarreaud420a972015-04-06 00:39:18 +02002661 out_fail_sess:
2662 appctx_free(appctx);
2663 out_fail_conf:
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002664 WILL_LJMP(lua_error(L));
2665 return 0;
2666}
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01002667
2668/*
2669 *
2670 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002671 * Class Channel
2672 *
2673 *
2674 */
2675
2676/* Returns the struct hlua_channel join to the class channel in the
2677 * stack entry "ud" or throws an argument error.
2678 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002679__LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002680{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02002681 return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002682}
2683
Willy Tarreau47860ed2015-03-10 14:07:50 +01002684/* Pushes the channel onto the top of the stack. If the stask does not have a
2685 * free slots, the function fails and returns 0;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002686 */
Willy Tarreau2a71af42015-03-10 13:51:50 +01002687static int hlua_channel_new(lua_State *L, struct channel *channel)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002688{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002689 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002690 if (!lua_checkstack(L, 3))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002691 return 0;
2692
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002693 lua_newtable(L);
Willy Tarreau47860ed2015-03-10 14:07:50 +01002694 lua_pushlightuserdata(L, channel);
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002695 lua_rawseti(L, -2, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002696
2697 /* Pop a class sesison metatable and affect it to the userdata. */
2698 lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref);
2699 lua_setmetatable(L, -2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002700 return 1;
2701}
2702
2703/* Duplicate all the data present in the input channel and put it
2704 * in a string LUA variables. Returns -1 and push a nil value in
2705 * the stack if the channel is closed and all the data are consumed,
2706 * returns 0 if no data are available, otherwise it returns the length
Ilya Shipitsind4259502020-04-08 01:07:56 +05002707 * of the built string.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002708 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002709static inline int _hlua_channel_dup(struct channel *chn, lua_State *L)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002710{
2711 char *blk1;
2712 char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02002713 size_t len1;
2714 size_t len2;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002715 int ret;
2716 luaL_Buffer b;
2717
Willy Tarreau06d80a92017-10-19 14:32:15 +02002718 ret = ci_getblk_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002719 if (unlikely(ret == 0))
2720 return 0;
2721
2722 if (unlikely(ret < 0)) {
2723 lua_pushnil(L);
2724 return -1;
2725 }
2726
2727 luaL_buffinit(L, &b);
2728 luaL_addlstring(&b, blk1, len1);
2729 if (unlikely(ret == 2))
2730 luaL_addlstring(&b, blk2, len2);
2731 luaL_pushresult(&b);
2732
2733 if (unlikely(ret == 2))
2734 return len1 + len2;
2735 return len1;
2736}
2737
2738/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2739 * a yield. This function keep the data in the buffer.
2740 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002741__LJMP static int hlua_channel_dup_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002742{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002743 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002744
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002745 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2746
Christopher Faulet3f829a42018-12-13 21:56:45 +01002747 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2748 WILL_LJMP(lua_error(L));
2749
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002750 if (_hlua_channel_dup(chn, L) == 0)
Willy Tarreau9635e032018-10-16 17:52:55 +02002751 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_dup_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002752 return 1;
2753}
2754
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002755/* Check arguments for the function "hlua_channel_dup_yield". */
2756__LJMP static int hlua_channel_dup(lua_State *L)
2757{
2758 MAY_LJMP(check_args(L, 1, "dup"));
2759 MAY_LJMP(hlua_checkchannel(L, 1));
2760 return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0));
2761}
2762
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002763/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2764 * a yield. This function consumes the data in the buffer. It returns
2765 * a string containing the data or a nil pointer if no data are available
2766 * and the channel is closed.
2767 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002768__LJMP static int hlua_channel_get_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002769{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002770 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002771 int ret;
2772
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002773 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002774
Christopher Faulet3f829a42018-12-13 21:56:45 +01002775 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2776 WILL_LJMP(lua_error(L));
2777
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002778 ret = _hlua_channel_dup(chn, L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002779 if (unlikely(ret == 0))
Willy Tarreau9635e032018-10-16 17:52:55 +02002780 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002781
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002782 if (unlikely(ret == -1))
2783 return 1;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002784
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002785 b_sub(&chn->buf, ret);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002786 return 1;
2787}
2788
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002789/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002790__LJMP static int hlua_channel_get(lua_State *L)
2791{
2792 MAY_LJMP(check_args(L, 1, "get"));
2793 MAY_LJMP(hlua_checkchannel(L, 1));
2794 return MAY_LJMP(hlua_channel_get_yield(L, 0, 0));
2795}
2796
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002797/* This functions consumes and returns one line. If the channel is closed,
2798 * and the last data does not contains a final '\n', the data are returned
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002799 * without the final '\n'. When no more data are available, it returns nil
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002800 * value.
2801 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002802__LJMP static int hlua_channel_getline_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002803{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002804 char *blk1;
2805 char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02002806 size_t len1;
2807 size_t len2;
2808 size_t len;
Willy Tarreau47860ed2015-03-10 14:07:50 +01002809 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002810 int ret;
2811 luaL_Buffer b;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002812
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002813 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2814
Christopher Faulet3f829a42018-12-13 21:56:45 +01002815 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2816 WILL_LJMP(lua_error(L));
2817
Willy Tarreau06d80a92017-10-19 14:32:15 +02002818 ret = ci_getline_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002819 if (ret == 0)
Willy Tarreau9635e032018-10-16 17:52:55 +02002820 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_getline_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002821
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002822 if (ret == -1) {
2823 lua_pushnil(L);
2824 return 1;
2825 }
2826
2827 luaL_buffinit(L, &b);
2828 luaL_addlstring(&b, blk1, len1);
2829 len = len1;
2830 if (unlikely(ret == 2)) {
2831 luaL_addlstring(&b, blk2, len2);
2832 len += len2;
2833 }
2834 luaL_pushresult(&b);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002835 b_rep_blk(&chn->buf, ci_head(chn), ci_head(chn) + len, NULL, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002836 return 1;
2837}
2838
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002839/* Check arguments for the function "hlua_channel_getline_yield". */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002840__LJMP static int hlua_channel_getline(lua_State *L)
2841{
2842 MAY_LJMP(check_args(L, 1, "getline"));
2843 MAY_LJMP(hlua_checkchannel(L, 1));
2844 return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0));
2845}
2846
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002847/* This function takes a string as input, and append it at the
2848 * input side of channel. If the data is too big, but a space
2849 * is probably available after sending some data, the function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002850 * yields. If the data is bigger than the buffer, or if the
2851 * channel is closed, it returns -1. Otherwise, it returns the
2852 * amount of data written.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002853 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002854__LJMP static int hlua_channel_append_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002855{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002856 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002857 size_t len;
2858 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
2859 int l = MAY_LJMP(luaL_checkinteger(L, 3));
2860 int ret;
2861 int max;
2862
Christopher Faulet3f829a42018-12-13 21:56:45 +01002863 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2864 WILL_LJMP(lua_error(L));
2865
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002866 /* Check if the buffer is available because HAProxy doesn't allocate
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002867 * the request buffer if its not required.
2868 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002869 if (chn->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +01002870 si_rx_buff_blk(chn_prod(chn));
Willy Tarreau9635e032018-10-16 17:52:55 +02002871 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002872 }
2873
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002874 max = channel_recv_limit(chn) - b_data(&chn->buf);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002875 if (max > len - l)
2876 max = len - l;
2877
Willy Tarreau06d80a92017-10-19 14:32:15 +02002878 ret = ci_putblk(chn, str + l, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002879 if (ret == -2 || ret == -3) {
2880 lua_pushinteger(L, -1);
2881 return 1;
2882 }
Willy Tarreaubc18da12015-03-13 14:00:47 +01002883 if (ret == -1) {
2884 chn->flags |= CF_WAKE_WRITE;
Willy Tarreau9635e032018-10-16 17:52:55 +02002885 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Willy Tarreaubc18da12015-03-13 14:00:47 +01002886 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002887 l += ret;
2888 lua_pop(L, 1);
2889 lua_pushinteger(L, l);
2890
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002891 max = channel_recv_limit(chn) - b_data(&chn->buf);
Willy Tarreaua79021a2018-06-15 18:07:57 +02002892 if (max == 0 && co_data(chn) == 0) {
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002893 /* There are no space available, and the output buffer is empty.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002894 * in this case, we cannot add more data, so we cannot yield,
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05002895 * we return the amount of copied data.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002896 */
2897 return 1;
2898 }
2899 if (l < len)
Willy Tarreau9635e032018-10-16 17:52:55 +02002900 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002901 return 1;
2902}
2903
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002904/* Just a wrapper of "hlua_channel_append_yield". It returns the length
2905 * of the written string, or -1 if the channel is closed or if the
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002906 * buffer size is too little for the data.
2907 */
2908__LJMP static int hlua_channel_append(lua_State *L)
2909{
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002910 size_t len;
2911
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002912 MAY_LJMP(check_args(L, 2, "append"));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002913 MAY_LJMP(hlua_checkchannel(L, 1));
2914 MAY_LJMP(luaL_checklstring(L, 2, &len));
2915 MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002916 lua_pushinteger(L, 0);
2917
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002918 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002919}
2920
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002921/* Just a wrapper of "hlua_channel_append_yield". This wrapper starts
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002922 * his process by cleaning the buffer. The result is a replacement
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002923 * of the current data. It returns the length of the written string,
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002924 * or -1 if the channel is closed or if the buffer size is too
2925 * little for the data.
2926 */
2927__LJMP static int hlua_channel_set(lua_State *L)
2928{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002929 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002930
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002931 MAY_LJMP(check_args(L, 2, "set"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002932 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002933 lua_pushinteger(L, 0);
2934
Christopher Faulet3f829a42018-12-13 21:56:45 +01002935 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2936 WILL_LJMP(lua_error(L));
2937
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002938 b_set_data(&chn->buf, co_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002939
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002940 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002941}
2942
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002943/* Append data in the output side of the buffer. This data is immediately
2944 * sent. The function returns the amount of data written. If the buffer
2945 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002946 * if the channel is closed.
2947 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002948__LJMP static int hlua_channel_send_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002949{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002950 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002951 size_t len;
2952 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
2953 int l = MAY_LJMP(luaL_checkinteger(L, 3));
2954 int max;
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01002955 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002956
Christopher Faulet3f829a42018-12-13 21:56:45 +01002957 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2958 WILL_LJMP(lua_error(L));
2959
Willy Tarreau47860ed2015-03-10 14:07:50 +01002960 if (unlikely(channel_output_closed(chn))) {
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002961 lua_pushinteger(L, -1);
2962 return 1;
2963 }
2964
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002965 /* Check if the buffer is available because HAProxy doesn't allocate
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01002966 * the request buffer if its not required.
2967 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002968 if (chn->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +01002969 si_rx_buff_blk(chn_prod(chn));
Willy Tarreau9635e032018-10-16 17:52:55 +02002970 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01002971 }
2972
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002973 /* The written data will be immediately sent, so we can check
2974 * the available space without taking in account the reserve.
2975 * The reserve is guaranteed for the processing of incoming
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002976 * data, because the buffer will be flushed.
2977 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002978 max = b_room(&chn->buf);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002979
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002980 /* If there is no space available, and the output buffer is empty.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002981 * in this case, we cannot add more data, so we cannot yield,
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05002982 * we return the amount of copied data.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002983 */
Willy Tarreaua79021a2018-06-15 18:07:57 +02002984 if (max == 0 && co_data(chn) == 0)
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002985 return 1;
2986
2987 /* Adjust the real required length. */
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002988 if (max > len - l)
2989 max = len - l;
2990
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002991 /* The buffer available size may be not contiguous. This test
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002992 * detects a non contiguous buffer and realign it.
2993 */
Willy Tarreau3f679992018-06-15 15:06:42 +02002994 if (ci_space_for_replace(chn) < max)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002995 channel_slow_realign(chn, trash.area);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002996
2997 /* Copy input data in the buffer. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002998 max = b_rep_blk(&chn->buf, ci_head(chn), ci_head(chn), str + l, max);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002999
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003000 /* buffer replace considers that the input part is filled.
3001 * so, I must forward these new data in the output part.
3002 */
Willy Tarreaubcbd3932018-06-06 07:13:22 +02003003 c_adv(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003004
3005 l += max;
3006 lua_pop(L, 1);
3007 lua_pushinteger(L, l);
3008
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003009 /* If there is no space available, and the output buffer is empty.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003010 * in this case, we cannot add more data, so we cannot yield,
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003011 * we return the amount of copied data.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003012 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003013 max = b_room(&chn->buf);
Willy Tarreaua79021a2018-06-15 18:07:57 +02003014 if (max == 0 && co_data(chn) == 0)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003015 return 1;
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003016
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003017 if (l < len) {
3018 /* If we are waiting for space in the response buffer, we
3019 * must set the flag WAKERESWR. This flag required the task
3020 * wake up if any activity is detected on the response buffer.
3021 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003022 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003023 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01003024 else
3025 HLUA_SET_WAKEREQWR(hlua);
Willy Tarreau9635e032018-10-16 17:52:55 +02003026 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003027 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003028
3029 return 1;
3030}
3031
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003032/* Just a wrapper of "_hlua_channel_send". This wrapper permits
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003033 * yield the LUA process, and resume it without checking the
3034 * input arguments.
3035 */
3036__LJMP static int hlua_channel_send(lua_State *L)
3037{
3038 MAY_LJMP(check_args(L, 2, "send"));
3039 lua_pushinteger(L, 0);
3040
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003041 return MAY_LJMP(hlua_channel_send_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003042}
3043
3044/* This function forward and amount of butes. The data pass from
3045 * the input side of the buffer to the output side, and can be
3046 * forwarded. This function never fails.
3047 *
3048 * The Lua function takes an amount of bytes to be forwarded in
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003049 * input. It returns the number of bytes forwarded.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003050 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003051__LJMP static int hlua_channel_forward_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003052{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003053 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003054 int len;
3055 int l;
3056 int max;
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003057 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003058
3059 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Faulet3f829a42018-12-13 21:56:45 +01003060
3061 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
3062 WILL_LJMP(lua_error(L));
3063
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003064 len = MAY_LJMP(luaL_checkinteger(L, 2));
3065 l = MAY_LJMP(luaL_checkinteger(L, -1));
3066
3067 max = len - l;
Willy Tarreaua79021a2018-06-15 18:07:57 +02003068 if (max > ci_data(chn))
3069 max = ci_data(chn);
Willy Tarreau47860ed2015-03-10 14:07:50 +01003070 channel_forward(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003071 l += max;
3072
3073 lua_pop(L, 1);
3074 lua_pushinteger(L, l);
3075
3076 /* Check if it miss bytes to forward. */
3077 if (l < len) {
3078 /* The the input channel or the output channel are closed, we
3079 * must return the amount of data forwarded.
3080 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003081 if (channel_input_closed(chn) || channel_output_closed(chn))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003082 return 1;
3083
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003084 /* If we are waiting for space data in the response buffer, we
3085 * must set the flag WAKERESWR. This flag required the task
3086 * wake up if any activity is detected on the response buffer.
3087 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003088 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003089 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01003090 else
3091 HLUA_SET_WAKEREQWR(hlua);
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003092
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003093 /* Otherwise, we can yield waiting for new data in the inpout side. */
Willy Tarreau9635e032018-10-16 17:52:55 +02003094 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_forward_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003095 }
3096
3097 return 1;
3098}
3099
3100/* Just check the input and prepare the stack for the previous
3101 * function "hlua_channel_forward_yield"
3102 */
3103__LJMP static int hlua_channel_forward(lua_State *L)
3104{
3105 MAY_LJMP(check_args(L, 2, "forward"));
3106 MAY_LJMP(hlua_checkchannel(L, 1));
3107 MAY_LJMP(luaL_checkinteger(L, 2));
3108
3109 lua_pushinteger(L, 0);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003110 return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003111}
3112
3113/* Just returns the number of bytes available in the input
3114 * side of the buffer. This function never fails.
3115 */
3116__LJMP static int hlua_channel_get_in_len(lua_State *L)
3117{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003118 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003119
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003120 MAY_LJMP(check_args(L, 1, "get_in_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003121 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Fauleta3ceac12018-12-14 13:39:09 +01003122 if (IS_HTX_STRM(chn_strm(chn))) {
3123 struct htx *htx = htxbuf(&chn->buf);
3124 lua_pushinteger(L, htx->data - co_data(chn));
3125 }
3126 else
3127 lua_pushinteger(L, ci_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003128 return 1;
3129}
3130
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003131/* Returns true if the channel is full. */
3132__LJMP static int hlua_channel_is_full(lua_State *L)
3133{
3134 struct channel *chn;
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003135
3136 MAY_LJMP(check_args(L, 1, "is_full"));
3137 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Faulet0ec740e2020-02-26 11:59:19 +01003138 /* ignore the reserve, we are not on a producer side (ie in an
3139 * applet).
3140 */
3141 lua_pushboolean(L, channel_full(chn, 0));
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003142 return 1;
3143}
3144
Christopher Faulet2ac9ba22020-02-25 10:15:50 +01003145/* Returns true if the channel is the response channel. */
3146__LJMP static int hlua_channel_is_resp(lua_State *L)
3147{
3148 struct channel *chn;
3149
3150 MAY_LJMP(check_args(L, 1, "is_resp"));
3151 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3152
3153 lua_pushboolean(L, !!(chn->flags & CF_ISRESP));
3154 return 1;
3155}
3156
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003157/* Just returns the number of bytes available in the output
3158 * side of the buffer. This function never fails.
3159 */
3160__LJMP static int hlua_channel_get_out_len(lua_State *L)
3161{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003162 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003163
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003164 MAY_LJMP(check_args(L, 1, "get_out_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003165 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Willy Tarreaua79021a2018-06-15 18:07:57 +02003166 lua_pushinteger(L, co_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003167 return 1;
3168}
3169
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003170/*
3171 *
3172 *
3173 * Class Fetches
3174 *
3175 *
3176 */
3177
3178/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003179 * a class stream, otherwise it throws an error.
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003180 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003181__LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003182{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003183 return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003184}
3185
3186/* This function creates and push in the stack a fetch object according
3187 * with a current TXN.
3188 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003189static int hlua_fetches_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003190{
Willy Tarreau7073c472015-04-06 11:15:40 +02003191 struct hlua_smp *hsmp;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003192
3193 /* Check stack size. */
3194 if (!lua_checkstack(L, 3))
3195 return 0;
3196
3197 /* Create the object: obj[0] = userdata.
3198 * Note that the base of the Fetches object is the
3199 * transaction object.
3200 */
3201 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003202 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003203 lua_rawseti(L, -2, 0);
3204
Willy Tarreau7073c472015-04-06 11:15:40 +02003205 hsmp->s = txn->s;
3206 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003207 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003208 hsmp->flags = flags;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003209
3210 /* Pop a class sesison metatable and affect it to the userdata. */
3211 lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref);
3212 lua_setmetatable(L, -2);
3213
3214 return 1;
3215}
3216
3217/* This function is an LUA binding. It is called with each sample-fetch.
3218 * It uses closure argument to store the associated sample-fetch. It
3219 * returns only one argument or throws an error. An error is thrown
3220 * only if an error is encountered during the argument parsing. If
3221 * the "sample-fetch" function fails, nil is returned.
3222 */
3223__LJMP static int hlua_run_sample_fetch(lua_State *L)
3224{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003225 struct hlua_smp *hsmp;
Willy Tarreau2ec22742015-03-10 14:27:20 +01003226 struct sample_fetch *f;
Frédéric Lécaillef874a832018-06-15 13:56:04 +02003227 struct arg args[ARGM_NBARGS + 1] = {{0}};
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003228 int i;
3229 struct sample smp;
3230
3231 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003232 f = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003233
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003234 /* Get traditional arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003235 hsmp = MAY_LJMP(hlua_checkfetches(L, 1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003236
Thierry FOURNIERca988662015-12-20 18:43:03 +01003237 /* Check execution authorization. */
3238 if (f->use & SMP_USE_HTTP_ANY &&
3239 !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) {
3240 lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which "
3241 "is not available in Lua services", f->kw);
3242 WILL_LJMP(lua_error(L));
3243 }
3244
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003245 /* Get extra arguments. */
3246 for (i = 0; i < lua_gettop(L) - 1; i++) {
3247 if (i >= ARGM_NBARGS)
3248 break;
3249 hlua_lua2arg(L, i + 2, &args[i]);
3250 }
3251 args[i].type = ARGT_STOP;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003252 args[i].data.str.area = NULL;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003253
3254 /* Check arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003255 MAY_LJMP(hlua_lua2arg_check(L, 2, args, f->arg_mask, hsmp->p));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003256
3257 /* Run the special args checker. */
Willy Tarreau2ec22742015-03-10 14:27:20 +01003258 if (f->val_args && !f->val_args(args, NULL)) {
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003259 lua_pushfstring(L, "error in arguments");
3260 WILL_LJMP(lua_error(L));
3261 }
3262
3263 /* Initialise the sample. */
3264 memset(&smp, 0, sizeof(smp));
3265
3266 /* Run the sample fetch process. */
Willy Tarreau1777ea62016-03-10 16:15:46 +01003267 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003268 if (!f->process(args, &smp, f->kw, f->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003269 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003270 lua_pushstring(L, "");
3271 else
3272 lua_pushnil(L);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003273 return 1;
3274 }
3275
3276 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003277 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003278 hlua_smp2lua_str(L, &smp);
3279 else
3280 hlua_smp2lua(L, &smp);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003281 return 1;
3282}
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003283
3284/*
3285 *
3286 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003287 * Class Converters
3288 *
3289 *
3290 */
3291
3292/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003293 * a class stream, otherwise it throws an error.
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003294 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003295__LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003296{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003297 return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003298}
3299
3300/* This function creates and push in the stack a Converters object
3301 * according with a current TXN.
3302 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003303static int hlua_converters_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003304{
Willy Tarreau7073c472015-04-06 11:15:40 +02003305 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003306
3307 /* Check stack size. */
3308 if (!lua_checkstack(L, 3))
3309 return 0;
3310
3311 /* Create the object: obj[0] = userdata.
3312 * Note that the base of the Converters object is the
3313 * same than the TXN object.
3314 */
3315 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003316 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003317 lua_rawseti(L, -2, 0);
3318
Willy Tarreau7073c472015-04-06 11:15:40 +02003319 hsmp->s = txn->s;
3320 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003321 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003322 hsmp->flags = flags;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003323
Willy Tarreau87b09662015-04-03 00:22:06 +02003324 /* Pop a class stream metatable and affect it to the table. */
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003325 lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref);
3326 lua_setmetatable(L, -2);
3327
3328 return 1;
3329}
3330
3331/* This function is an LUA binding. It is called with each converter.
3332 * It uses closure argument to store the associated converter. It
3333 * returns only one argument or throws an error. An error is thrown
3334 * only if an error is encountered during the argument parsing. If
3335 * the converter function function fails, nil is returned.
3336 */
3337__LJMP static int hlua_run_sample_conv(lua_State *L)
3338{
Willy Tarreauda5f1082015-04-06 11:17:13 +02003339 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003340 struct sample_conv *conv;
Frédéric Lécaillef874a832018-06-15 13:56:04 +02003341 struct arg args[ARGM_NBARGS + 1] = {{0}};
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003342 int i;
3343 struct sample smp;
3344
3345 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003346 conv = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003347
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003348 /* Get traditional arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003349 hsmp = MAY_LJMP(hlua_checkconverters(L, 1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003350
3351 /* Get extra arguments. */
3352 for (i = 0; i < lua_gettop(L) - 2; i++) {
3353 if (i >= ARGM_NBARGS)
3354 break;
3355 hlua_lua2arg(L, i + 3, &args[i]);
3356 }
3357 args[i].type = ARGT_STOP;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003358 args[i].data.str.area = NULL;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003359
3360 /* Check arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003361 MAY_LJMP(hlua_lua2arg_check(L, 3, args, conv->arg_mask, hsmp->p));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003362
3363 /* Run the special args checker. */
3364 if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) {
3365 hlua_pusherror(L, "error in arguments");
3366 WILL_LJMP(lua_error(L));
3367 }
3368
3369 /* Initialise the sample. */
3370 if (!hlua_lua2smp(L, 2, &smp)) {
3371 hlua_pusherror(L, "error in the input argument");
3372 WILL_LJMP(lua_error(L));
3373 }
3374
Willy Tarreau1777ea62016-03-10 16:15:46 +01003375 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
3376
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003377 /* Apply expected cast. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003378 if (!sample_casts[smp.data.type][conv->in_type]) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003379 hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'",
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003380 smp_to_type[smp.data.type], smp_to_type[conv->in_type]);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003381 WILL_LJMP(lua_error(L));
3382 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003383 if (sample_casts[smp.data.type][conv->in_type] != c_none &&
3384 !sample_casts[smp.data.type][conv->in_type](&smp)) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003385 hlua_pusherror(L, "error during the input argument casting");
3386 WILL_LJMP(lua_error(L));
3387 }
3388
3389 /* Run the sample conversion process. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003390 if (!conv->process(args, &smp, conv->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003391 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003392 lua_pushstring(L, "");
3393 else
Willy Tarreaua678b432015-08-28 10:14:59 +02003394 lua_pushnil(L);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003395 return 1;
3396 }
3397
3398 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003399 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003400 hlua_smp2lua_str(L, &smp);
3401 else
3402 hlua_smp2lua(L, &smp);
Willy Tarreaua678b432015-08-28 10:14:59 +02003403 return 1;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003404}
3405
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003406/*
3407 *
3408 *
3409 * Class AppletTCP
3410 *
3411 *
3412 */
3413
3414/* Returns a struct hlua_txn if the stack entry "ud" is
3415 * a class stream, otherwise it throws an error.
3416 */
3417__LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud)
3418{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003419 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003420}
3421
3422/* This function creates and push in the stack an Applet object
3423 * according with a current TXN.
3424 */
3425static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx)
3426{
3427 struct hlua_appctx *appctx;
3428 struct stream_interface *si = ctx->owner;
3429 struct stream *s = si_strm(si);
3430 struct proxy *p = s->be;
3431
3432 /* Check stack size. */
3433 if (!lua_checkstack(L, 3))
3434 return 0;
3435
3436 /* Create the object: obj[0] = userdata.
3437 * Note that the base of the Converters object is the
3438 * same than the TXN object.
3439 */
3440 lua_newtable(L);
3441 appctx = lua_newuserdata(L, sizeof(*appctx));
3442 lua_rawseti(L, -2, 0);
3443 appctx->appctx = ctx;
3444 appctx->htxn.s = s;
3445 appctx->htxn.p = p;
3446
3447 /* Create the "f" field that contains a list of fetches. */
3448 lua_pushstring(L, "f");
3449 if (!hlua_fetches_new(L, &appctx->htxn, 0))
3450 return 0;
3451 lua_settable(L, -3);
3452
3453 /* Create the "sf" field that contains a list of stringsafe fetches. */
3454 lua_pushstring(L, "sf");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003455 if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003456 return 0;
3457 lua_settable(L, -3);
3458
3459 /* Create the "c" field that contains a list of converters. */
3460 lua_pushstring(L, "c");
3461 if (!hlua_converters_new(L, &appctx->htxn, 0))
3462 return 0;
3463 lua_settable(L, -3);
3464
3465 /* Create the "sc" field that contains a list of stringsafe converters. */
3466 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003467 if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003468 return 0;
3469 lua_settable(L, -3);
3470
3471 /* Pop a class stream metatable and affect it to the table. */
3472 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref);
3473 lua_setmetatable(L, -2);
3474
3475 return 1;
3476}
3477
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003478__LJMP static int hlua_applet_tcp_set_var(lua_State *L)
3479{
3480 struct hlua_appctx *appctx;
3481 struct stream *s;
3482 const char *name;
3483 size_t len;
3484 struct sample smp;
3485
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003486 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
3487 WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments"));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003488
3489 /* It is useles to retrieve the stream, but this function
3490 * runs only in a stream context.
3491 */
3492 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3493 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3494 s = appctx->htxn.s;
3495
3496 /* Converts the third argument in a sample. */
3497 hlua_lua2smp(L, 3, &smp);
3498
3499 /* Store the sample in a variable. */
3500 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003501
3502 if (lua_gettop(L) == 4 && lua_toboolean(L, 4))
3503 lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0);
3504 else
3505 lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0);
3506
Tim Duesterhus84ebc132020-05-19 13:49:41 +02003507 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003508}
3509
3510__LJMP static int hlua_applet_tcp_unset_var(lua_State *L)
3511{
3512 struct hlua_appctx *appctx;
3513 struct stream *s;
3514 const char *name;
3515 size_t len;
3516 struct sample smp;
3517
3518 MAY_LJMP(check_args(L, 2, "unset_var"));
3519
3520 /* It is useles to retrieve the stream, but this function
3521 * runs only in a stream context.
3522 */
3523 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3524 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3525 s = appctx->htxn.s;
3526
3527 /* Unset the variable. */
3528 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus84ebc132020-05-19 13:49:41 +02003529 lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0);
3530 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003531}
3532
3533__LJMP static int hlua_applet_tcp_get_var(lua_State *L)
3534{
3535 struct hlua_appctx *appctx;
3536 struct stream *s;
3537 const char *name;
3538 size_t len;
3539 struct sample smp;
3540
3541 MAY_LJMP(check_args(L, 2, "get_var"));
3542
3543 /* It is useles to retrieve the stream, but this function
3544 * runs only in a stream context.
3545 */
3546 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3547 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3548 s = appctx->htxn.s;
3549
3550 smp_set_owner(&smp, s->be, s->sess, s, 0);
3551 if (!vars_get_by_name(name, len, &smp)) {
3552 lua_pushnil(L);
3553 return 1;
3554 }
3555
3556 return hlua_smp2lua(L, &smp);
3557}
3558
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003559__LJMP static int hlua_applet_tcp_set_priv(lua_State *L)
3560{
3561 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3562 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003563 struct hlua *hlua;
3564
3565 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003566 if (!s->hlua)
3567 return 0;
3568 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003569
3570 MAY_LJMP(check_args(L, 2, "set_priv"));
3571
3572 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02003573 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003574
3575 /* Get and store new value. */
3576 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
3577 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
3578
3579 return 0;
3580}
3581
3582__LJMP static int hlua_applet_tcp_get_priv(lua_State *L)
3583{
3584 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3585 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003586 struct hlua *hlua;
3587
3588 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003589 if (!s->hlua) {
3590 lua_pushnil(L);
3591 return 1;
3592 }
3593 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003594
3595 /* Push configuration index in the stack. */
3596 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
3597
3598 return 1;
3599}
3600
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003601/* If expected data not yet available, it returns a yield. This function
3602 * consumes the data in the buffer. It returns a string containing the
3603 * data. This string can be empty.
3604 */
3605__LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx)
3606{
3607 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3608 struct stream_interface *si = appctx->appctx->owner;
3609 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02003610 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003611 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02003612 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003613 size_t len2;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003614
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003615 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003616 ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003617
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003618 /* Data not yet available. return yield. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003619 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003620 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003621 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_getline_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003622 }
3623
3624 /* End of data: commit the total strings and return. */
3625 if (ret < 0) {
3626 luaL_pushresult(&appctx->b);
3627 return 1;
3628 }
3629
3630 /* Ensure that the block 2 length is usable. */
3631 if (ret == 1)
3632 len2 = 0;
3633
3634 /* dont check the max length read and dont check. */
3635 luaL_addlstring(&appctx->b, blk1, len1);
3636 luaL_addlstring(&appctx->b, blk2, len2);
3637
3638 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003639 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003640 luaL_pushresult(&appctx->b);
3641 return 1;
3642}
3643
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003644/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003645__LJMP static int hlua_applet_tcp_getline(lua_State *L)
3646{
3647 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3648
3649 /* Initialise the string catenation. */
3650 luaL_buffinit(L, &appctx->b);
3651
3652 return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0));
3653}
3654
3655/* If expected data not yet available, it returns a yield. This function
3656 * consumes the data in the buffer. It returns a string containing the
3657 * data. This string can be empty.
3658 */
3659__LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx)
3660{
3661 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3662 struct stream_interface *si = appctx->appctx->owner;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003663 size_t len = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003664 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02003665 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003666 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02003667 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003668 size_t len2;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003669
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003670 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003671 ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003672
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003673 /* Data not yet available. return yield. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003674 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003675 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003676 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003677 }
3678
3679 /* End of data: commit the total strings and return. */
3680 if (ret < 0) {
3681 luaL_pushresult(&appctx->b);
3682 return 1;
3683 }
3684
3685 /* Ensure that the block 2 length is usable. */
3686 if (ret == 1)
3687 len2 = 0;
3688
3689 if (len == -1) {
3690
3691 /* If len == -1, catenate all the data avalaile and
3692 * yield because we want to get all the data until
3693 * the end of data stream.
3694 */
3695 luaL_addlstring(&appctx->b, blk1, len1);
3696 luaL_addlstring(&appctx->b, blk2, len2);
Willy Tarreau06d80a92017-10-19 14:32:15 +02003697 co_skip(si_oc(si), len1 + len2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003698 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003699 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003700
3701 } else {
3702
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003703 /* Copy the first block caping to the length required. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003704 if (len1 > len)
3705 len1 = len;
3706 luaL_addlstring(&appctx->b, blk1, len1);
3707 len -= len1;
3708
3709 /* Copy the second block. */
3710 if (len2 > len)
3711 len2 = len;
3712 luaL_addlstring(&appctx->b, blk2, len2);
3713 len -= len2;
3714
3715 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003716 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003717
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003718 /* If there is no other data available, yield waiting for new data. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003719 if (len > 0) {
3720 lua_pushinteger(L, len);
3721 lua_replace(L, 2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003722 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003723 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003724 }
3725
3726 /* return the result. */
3727 luaL_pushresult(&appctx->b);
3728 return 1;
3729 }
3730
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003731 /* we never execute this */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003732 hlua_pusherror(L, "Lua: internal error");
3733 WILL_LJMP(lua_error(L));
3734 return 0;
3735}
3736
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003737/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003738__LJMP static int hlua_applet_tcp_recv(lua_State *L)
3739{
3740 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3741 int len = -1;
3742
3743 if (lua_gettop(L) > 2)
3744 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
3745 if (lua_gettop(L) >= 2) {
3746 len = MAY_LJMP(luaL_checkinteger(L, 2));
3747 lua_pop(L, 1);
3748 }
3749
3750 /* Confirm or set the required length */
3751 lua_pushinteger(L, len);
3752
3753 /* Initialise the string catenation. */
3754 luaL_buffinit(L, &appctx->b);
3755
3756 return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0));
3757}
3758
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003759/* Append data in the output side of the buffer. This data is immediately
3760 * sent. The function returns the amount of data written. If the buffer
3761 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003762 * if the channel is closed.
3763 */
3764__LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx)
3765{
3766 size_t len;
3767 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3768 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
3769 int l = MAY_LJMP(luaL_checkinteger(L, 3));
3770 struct stream_interface *si = appctx->appctx->owner;
3771 struct channel *chn = si_ic(si);
3772 int max;
3773
3774 /* Get the max amount of data which can write as input in the channel. */
3775 max = channel_recv_max(chn);
3776 if (max > (len - l))
3777 max = len - l;
3778
3779 /* Copy data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003780 ci_putblk(chn, str + l, max);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003781
3782 /* update counters. */
3783 l += max;
3784 lua_pop(L, 1);
3785 lua_pushinteger(L, l);
3786
3787 /* If some data is not send, declares the situation to the
3788 * applet, and returns a yield.
3789 */
3790 if (l < len) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003791 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003792 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003793 }
3794
3795 return 1;
3796}
3797
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003798/* Just a wrapper of "hlua_applet_tcp_send_yield". This wrapper permits
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003799 * yield the LUA process, and resume it without checking the
3800 * input arguments.
3801 */
3802__LJMP static int hlua_applet_tcp_send(lua_State *L)
3803{
3804 MAY_LJMP(check_args(L, 2, "send"));
3805 lua_pushinteger(L, 0);
3806
3807 return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0));
3808}
3809
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003810/*
3811 *
3812 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003813 * Class AppletHTTP
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003814 *
3815 *
3816 */
3817
3818/* Returns a struct hlua_txn if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003819 * a class stream, otherwise it throws an error.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003820 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003821__LJMP static struct hlua_appctx *hlua_checkapplet_http(lua_State *L, int ud)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003822{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003823 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003824}
3825
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003826/* This function creates and push in the stack an Applet object
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003827 * according with a current TXN.
3828 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003829static int hlua_applet_http_new(lua_State *L, struct appctx *ctx)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003830{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003831 struct hlua_appctx *appctx;
Thierry FOURNIER841475e2015-12-11 17:10:09 +01003832 struct hlua_txn htxn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003833 struct stream_interface *si = ctx->owner;
3834 struct stream *s = si_strm(si);
3835 struct proxy *px = s->be;
Christopher Fauleta2097962019-07-15 16:25:33 +02003836 struct htx *htx;
3837 struct htx_blk *blk;
3838 struct htx_sl *sl;
3839 struct ist path;
3840 unsigned long long len = 0;
3841 int32_t pos;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003842
3843 /* Check stack size. */
3844 if (!lua_checkstack(L, 3))
3845 return 0;
3846
3847 /* Create the object: obj[0] = userdata.
3848 * Note that the base of the Converters object is the
3849 * same than the TXN object.
3850 */
3851 lua_newtable(L);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003852 appctx = lua_newuserdata(L, sizeof(*appctx));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003853 lua_rawseti(L, -2, 0);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003854 appctx->appctx = ctx;
3855 appctx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */
Robin H. Johnson52f5db22017-01-01 13:10:52 -08003856 appctx->appctx->ctx.hlua_apphttp.reason = NULL; /* Use default reason based on status */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003857 appctx->htxn.s = s;
3858 appctx->htxn.p = px;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003859
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003860 /* Create the "f" field that contains a list of fetches. */
3861 lua_pushstring(L, "f");
3862 if (!hlua_fetches_new(L, &appctx->htxn, 0))
3863 return 0;
3864 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003865
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003866 /* Create the "sf" field that contains a list of stringsafe fetches. */
3867 lua_pushstring(L, "sf");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003868 if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003869 return 0;
3870 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003871
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003872 /* Create the "c" field that contains a list of converters. */
3873 lua_pushstring(L, "c");
3874 if (!hlua_converters_new(L, &appctx->htxn, 0))
3875 return 0;
3876 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003877
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003878 /* Create the "sc" field that contains a list of stringsafe converters. */
3879 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003880 if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003881 return 0;
3882 lua_settable(L, -3);
Willy Tarreaueee5b512015-04-03 23:46:31 +02003883
Christopher Fauleta2097962019-07-15 16:25:33 +02003884 htx = htxbuf(&s->req.buf);
3885 blk = htx_get_first_blk(htx);
Christopher Fauletea009732019-11-18 15:50:25 +01003886 BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL);
Christopher Fauleta2097962019-07-15 16:25:33 +02003887 sl = htx_get_blk_ptr(htx, blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003888
Christopher Fauleta2097962019-07-15 16:25:33 +02003889 /* Stores the request method. */
3890 lua_pushstring(L, "method");
3891 lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl));
3892 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003893
Christopher Fauleta2097962019-07-15 16:25:33 +02003894 /* Stores the http version. */
3895 lua_pushstring(L, "version");
3896 lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl));
3897 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003898
Christopher Fauleta2097962019-07-15 16:25:33 +02003899 /* creates an array of headers. hlua_http_get_headers() crates and push
3900 * the array on the top of the stack.
3901 */
3902 lua_pushstring(L, "headers");
3903 htxn.s = s;
3904 htxn.p = px;
3905 htxn.dir = SMP_OPT_DIR_REQ;
Christopher Faulet9d1332b2020-02-24 16:46:16 +01003906 if (!hlua_http_get_headers(L, &htxn.s->txn->req))
Christopher Fauleta2097962019-07-15 16:25:33 +02003907 return 0;
3908 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003909
Christopher Fauleta2097962019-07-15 16:25:33 +02003910 path = http_get_path(htx_sl_req_uri(sl));
Tim Duesterhused526372020-03-05 17:56:33 +01003911 if (isttest(path)) {
Christopher Fauleta2097962019-07-15 16:25:33 +02003912 char *p, *q, *end;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003913
Christopher Fauleta2097962019-07-15 16:25:33 +02003914 p = path.ptr;
3915 end = path.ptr + path.len;
3916 q = p;
3917 while (q < end && *q != '?')
3918 q++;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003919
Thierry FOURNIER7d388632017-02-22 02:06:16 +01003920 /* Stores the request path. */
Christopher Fauleta2097962019-07-15 16:25:33 +02003921 lua_pushstring(L, "path");
3922 lua_pushlstring(L, p, q - p);
Thierry FOURNIER7d388632017-02-22 02:06:16 +01003923 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003924
Christopher Fauleta2097962019-07-15 16:25:33 +02003925 /* Stores the query string. */
3926 lua_pushstring(L, "qs");
3927 if (*q == '?')
3928 q++;
3929 lua_pushlstring(L, q, end - q);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003930 lua_settable(L, -3);
Christopher Fauleta2097962019-07-15 16:25:33 +02003931 }
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003932
Christopher Fauleta2097962019-07-15 16:25:33 +02003933 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
3934 struct htx_blk *blk = htx_get_blk(htx, pos);
3935 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003936
Christopher Fauleta2097962019-07-15 16:25:33 +02003937 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
3938 break;
3939 if (type == HTX_BLK_DATA)
3940 len += htx_get_blksz(blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003941 }
Christopher Fauleta2097962019-07-15 16:25:33 +02003942 if (htx->extra != ULLONG_MAX)
3943 len += htx->extra;
3944
3945 /* Stores the request path. */
3946 lua_pushstring(L, "length");
3947 lua_pushinteger(L, len);
3948 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003949
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003950 /* Create an empty array of HTTP request headers. */
3951 lua_pushstring(L, "response");
3952 lua_newtable(L);
3953 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003954
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003955 /* Pop a class stream metatable and affect it to the table. */
3956 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref);
3957 lua_setmetatable(L, -2);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003958
3959 return 1;
3960}
3961
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003962__LJMP static int hlua_applet_http_set_var(lua_State *L)
3963{
3964 struct hlua_appctx *appctx;
3965 struct stream *s;
3966 const char *name;
3967 size_t len;
3968 struct sample smp;
3969
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003970 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
3971 WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments"));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003972
3973 /* It is useles to retrieve the stream, but this function
3974 * runs only in a stream context.
3975 */
3976 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
3977 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3978 s = appctx->htxn.s;
3979
3980 /* Converts the third argument in a sample. */
3981 hlua_lua2smp(L, 3, &smp);
3982
3983 /* Store the sample in a variable. */
3984 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003985
3986 if (lua_gettop(L) == 4 && lua_toboolean(L, 4))
3987 lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0);
3988 else
3989 lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0);
3990
Tim Duesterhus84ebc132020-05-19 13:49:41 +02003991 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003992}
3993
3994__LJMP static int hlua_applet_http_unset_var(lua_State *L)
3995{
3996 struct hlua_appctx *appctx;
3997 struct stream *s;
3998 const char *name;
3999 size_t len;
4000 struct sample smp;
4001
4002 MAY_LJMP(check_args(L, 2, "unset_var"));
4003
4004 /* It is useles to retrieve the stream, but this function
4005 * runs only in a stream context.
4006 */
4007 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4008 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4009 s = appctx->htxn.s;
4010
4011 /* Unset the variable. */
4012 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus84ebc132020-05-19 13:49:41 +02004013 lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0);
4014 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004015}
4016
4017__LJMP static int hlua_applet_http_get_var(lua_State *L)
4018{
4019 struct hlua_appctx *appctx;
4020 struct stream *s;
4021 const char *name;
4022 size_t len;
4023 struct sample smp;
4024
4025 MAY_LJMP(check_args(L, 2, "get_var"));
4026
4027 /* It is useles to retrieve the stream, but this function
4028 * runs only in a stream context.
4029 */
4030 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4031 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4032 s = appctx->htxn.s;
4033
4034 smp_set_owner(&smp, s->be, s->sess, s, 0);
4035 if (!vars_get_by_name(name, len, &smp)) {
4036 lua_pushnil(L);
4037 return 1;
4038 }
4039
4040 return hlua_smp2lua(L, &smp);
4041}
4042
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004043__LJMP static int hlua_applet_http_set_priv(lua_State *L)
4044{
4045 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4046 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004047 struct hlua *hlua;
4048
4049 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004050 if (!s->hlua)
4051 return 0;
4052 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004053
4054 MAY_LJMP(check_args(L, 2, "set_priv"));
4055
4056 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02004057 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004058
4059 /* Get and store new value. */
4060 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
4061 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
4062
4063 return 0;
4064}
4065
4066__LJMP static int hlua_applet_http_get_priv(lua_State *L)
4067{
4068 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4069 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004070 struct hlua *hlua;
4071
4072 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004073 if (!s->hlua) {
4074 lua_pushnil(L);
4075 return 1;
4076 }
4077 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004078
4079 /* Push configuration index in the stack. */
4080 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
4081
4082 return 1;
4083}
4084
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004085/* If expected data not yet available, it returns a yield. This function
4086 * consumes the data in the buffer. It returns a string containing the
4087 * data. This string can be empty.
4088 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004089__LJMP static int hlua_applet_http_getline_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004090{
4091 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4092 struct stream_interface *si = appctx->appctx->owner;
4093 struct channel *req = si_oc(si);
4094 struct htx *htx;
4095 struct htx_blk *blk;
4096 size_t count;
4097 int stop = 0;
4098
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004099 htx = htx_from_buf(&req->buf);
4100 count = co_data(req);
Christopher Fauleta3f15502019-05-13 15:27:23 +02004101 blk = htx_get_first_blk(htx);
Christopher Fauletcc26b132018-12-18 21:20:57 +01004102
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004103 while (count && !stop && blk) {
4104 enum htx_blk_type type = htx_get_blk_type(blk);
4105 uint32_t sz = htx_get_blksz(blk);
4106 struct ist v;
4107 uint32_t vlen;
4108 char *nl;
4109
Christopher Faulete461e342018-12-18 16:43:35 +01004110 if (type == HTX_BLK_EOM) {
4111 stop = 1;
4112 break;
4113 }
4114
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004115 vlen = sz;
4116 if (vlen > count) {
4117 if (type != HTX_BLK_DATA)
4118 break;
4119 vlen = count;
4120 }
4121
4122 switch (type) {
4123 case HTX_BLK_UNUSED:
4124 break;
4125
4126 case HTX_BLK_DATA:
4127 v = htx_get_blk_value(htx, blk);
4128 v.len = vlen;
4129 nl = istchr(v, '\n');
4130 if (nl != NULL) {
4131 stop = 1;
4132 vlen = nl - v.ptr + 1;
4133 }
4134 luaL_addlstring(&appctx->b, v.ptr, vlen);
4135 break;
4136
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004137 case HTX_BLK_TLR:
4138 case HTX_BLK_EOM:
4139 stop = 1;
4140 break;
4141
4142 default:
4143 break;
4144 }
4145
4146 co_set_data(req, co_data(req) - vlen);
4147 count -= vlen;
4148 if (sz == vlen)
4149 blk = htx_remove_blk(htx, blk);
4150 else {
4151 htx_cut_data_blk(htx, blk, vlen);
4152 break;
4153 }
4154 }
4155
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004156 htx_to_buf(htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004157 if (!stop) {
4158 si_cant_get(si);
Christopher Fauleta2097962019-07-15 16:25:33 +02004159 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_getline_yield, TICK_ETERNITY, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004160 }
4161
4162 /* return the result. */
4163 luaL_pushresult(&appctx->b);
4164 return 1;
4165}
4166
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004167
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004168/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004169__LJMP static int hlua_applet_http_getline(lua_State *L)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004170{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004171 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004172
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004173 /* Initialise the string catenation. */
4174 luaL_buffinit(L, &appctx->b);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004175
Christopher Fauleta2097962019-07-15 16:25:33 +02004176 return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004177}
4178
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004179/* If expected data not yet available, it returns a yield. This function
4180 * consumes the data in the buffer. It returns a string containing the
4181 * data. This string can be empty.
4182 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004183__LJMP static int hlua_applet_http_recv_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004184{
4185 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4186 struct stream_interface *si = appctx->appctx->owner;
4187 struct channel *req = si_oc(si);
4188 struct htx *htx;
4189 struct htx_blk *blk;
4190 size_t count;
4191 int len;
4192
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004193 htx = htx_from_buf(&req->buf);
4194 len = MAY_LJMP(luaL_checkinteger(L, 2));
4195 count = co_data(req);
Christopher Faulet29f17582019-05-23 11:03:26 +02004196 blk = htx_get_head_blk(htx);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004197 while (count && len && blk) {
4198 enum htx_blk_type type = htx_get_blk_type(blk);
4199 uint32_t sz = htx_get_blksz(blk);
4200 struct ist v;
4201 uint32_t vlen;
4202
Christopher Faulete461e342018-12-18 16:43:35 +01004203 if (type == HTX_BLK_EOM) {
4204 len = 0;
4205 break;
4206 }
4207
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004208 vlen = sz;
4209 if (len > 0 && vlen > len)
4210 vlen = len;
4211 if (vlen > count) {
4212 if (type != HTX_BLK_DATA)
4213 break;
4214 vlen = count;
4215 }
4216
4217 switch (type) {
4218 case HTX_BLK_UNUSED:
4219 break;
4220
4221 case HTX_BLK_DATA:
4222 v = htx_get_blk_value(htx, blk);
4223 luaL_addlstring(&appctx->b, v.ptr, vlen);
4224 break;
4225
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004226 case HTX_BLK_TLR:
4227 case HTX_BLK_EOM:
4228 len = 0;
4229 break;
4230
4231 default:
4232 break;
4233 }
4234
4235 co_set_data(req, co_data(req) - vlen);
4236 count -= vlen;
4237 if (len > 0)
4238 len -= vlen;
4239 if (sz == vlen)
4240 blk = htx_remove_blk(htx, blk);
4241 else {
4242 htx_cut_data_blk(htx, blk, vlen);
4243 break;
4244 }
4245 }
4246
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004247 htx_to_buf(htx, &req->buf);
4248
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004249 /* If we are no other data available, yield waiting for new data. */
4250 if (len) {
4251 if (len > 0) {
4252 lua_pushinteger(L, len);
4253 lua_replace(L, 2);
4254 }
4255 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004256 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004257 }
4258
4259 /* return the result. */
4260 luaL_pushresult(&appctx->b);
4261 return 1;
4262}
4263
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004264/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004265__LJMP static int hlua_applet_http_recv(lua_State *L)
4266{
4267 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4268 int len = -1;
4269
4270 /* Check arguments. */
4271 if (lua_gettop(L) > 2)
4272 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
4273 if (lua_gettop(L) >= 2) {
4274 len = MAY_LJMP(luaL_checkinteger(L, 2));
4275 lua_pop(L, 1);
4276 }
4277
Christopher Fauleta2097962019-07-15 16:25:33 +02004278 lua_pushinteger(L, len);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004279
Christopher Fauleta2097962019-07-15 16:25:33 +02004280 /* Initialise the string catenation. */
4281 luaL_buffinit(L, &appctx->b);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004282
Christopher Fauleta2097962019-07-15 16:25:33 +02004283 return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004284}
4285
4286/* Append data in the output side of the buffer. This data is immediately
4287 * sent. The function returns the amount of data written. If the buffer
4288 * cannot contain the data, the function yields. The function returns -1
4289 * if the channel is closed.
4290 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004291__LJMP static int hlua_applet_http_send_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004292{
4293 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4294 struct stream_interface *si = appctx->appctx->owner;
4295 struct channel *res = si_ic(si);
4296 struct htx *htx = htx_from_buf(&res->buf);
4297 const char *data;
4298 size_t len;
4299 int l = MAY_LJMP(luaL_checkinteger(L, 3));
4300 int max;
4301
Christopher Faulet9060fc02019-07-03 11:39:30 +02004302 max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx));
Christopher Faulet4b0e9b22019-01-09 12:16:58 +01004303 if (!max)
4304 goto snd_yield;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004305
4306 data = MAY_LJMP(luaL_checklstring(L, 2, &len));
4307
4308 /* Get the max amount of data which can write as input in the channel. */
4309 if (max > (len - l))
4310 max = len - l;
4311
4312 /* Copy data. */
Willy Tarreau0a7ef022019-05-28 10:30:11 +02004313 max = htx_add_data(htx, ist2(data + l, max));
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01004314 channel_add_input(res, max);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004315
4316 /* update counters. */
4317 l += max;
4318 lua_pop(L, 1);
4319 lua_pushinteger(L, l);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004320
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004321 /* If some data is not send, declares the situation to the
4322 * applet, and returns a yield.
4323 */
4324 if (l < len) {
Christopher Faulet4b0e9b22019-01-09 12:16:58 +01004325 snd_yield:
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004326 htx_to_buf(htx, &res->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004327 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004328 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004329 }
4330
Christopher Fauleta2097962019-07-15 16:25:33 +02004331 htx_to_buf(htx, &res->buf);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004332 return 1;
4333}
4334
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004335/* Just a wrapper of "hlua_applet_send_yield". This wrapper permits
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004336 * yield the LUA process, and resume it without checking the
4337 * input arguments.
4338 */
4339__LJMP static int hlua_applet_http_send(lua_State *L)
4340{
4341 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004342
4343 /* We want to send some data. Headers must be sent. */
4344 if (!(appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
4345 hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data.");
4346 WILL_LJMP(lua_error(L));
4347 }
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004348
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004349 /* This integer is used for followinf the amount of data sent. */
Christopher Fauleta2097962019-07-15 16:25:33 +02004350 lua_pushinteger(L, 0);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004351
Christopher Fauleta2097962019-07-15 16:25:33 +02004352 return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004353}
4354
4355__LJMP static int hlua_applet_http_addheader(lua_State *L)
4356{
4357 const char *name;
4358 int ret;
4359
4360 MAY_LJMP(hlua_checkapplet_http(L, 1));
4361 name = MAY_LJMP(luaL_checkstring(L, 2));
4362 MAY_LJMP(luaL_checkstring(L, 3));
4363
4364 /* Push in the stack the "response" entry. */
4365 ret = lua_getfield(L, 1, "response");
4366 if (ret != LUA_TTABLE) {
4367 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] "
4368 "is expected as an array. %s found", lua_typename(L, ret));
4369 WILL_LJMP(lua_error(L));
4370 }
4371
4372 /* check if the header is already registered if it is not
4373 * the case, register it.
4374 */
4375 ret = lua_getfield(L, -1, name);
4376 if (ret == LUA_TNIL) {
4377
4378 /* Entry not found. */
4379 lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */
4380
4381 /* Insert the new header name in the array in the top of the stack.
4382 * It left the new array in the top of the stack.
4383 */
4384 lua_newtable(L);
4385 lua_pushvalue(L, 2);
4386 lua_pushvalue(L, -2);
4387 lua_settable(L, -4);
4388
4389 } else if (ret != LUA_TTABLE) {
4390
4391 /* corruption error. */
4392 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] "
4393 "is expected as an array. %s found", name, lua_typename(L, ret));
4394 WILL_LJMP(lua_error(L));
4395 }
4396
4397 /* Now the top od thestack is an array of values. We push
4398 * the header value as new entry.
4399 */
4400 lua_pushvalue(L, 3);
4401 ret = lua_rawlen(L, -2);
4402 lua_rawseti(L, -2, ret + 1);
4403 lua_pushboolean(L, 1);
4404 return 1;
4405}
4406
4407__LJMP static int hlua_applet_http_status(lua_State *L)
4408{
4409 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4410 int status = MAY_LJMP(luaL_checkinteger(L, 2));
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004411 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004412
4413 if (status < 100 || status > 599) {
4414 lua_pushboolean(L, 0);
4415 return 1;
4416 }
4417
4418 appctx->appctx->ctx.hlua_apphttp.status = status;
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004419 appctx->appctx->ctx.hlua_apphttp.reason = reason;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004420 lua_pushboolean(L, 1);
4421 return 1;
4422}
4423
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004424
Christopher Fauleta2097962019-07-15 16:25:33 +02004425__LJMP static int hlua_applet_http_send_response(lua_State *L)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004426{
4427 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4428 struct stream_interface *si = appctx->appctx->owner;
4429 struct channel *res = si_ic(si);
4430 struct htx *htx;
4431 struct htx_sl *sl;
4432 struct h1m h1m;
4433 const char *status, *reason;
4434 const char *name, *value;
4435 size_t nlen, vlen;
4436 unsigned int flags;
4437
4438 /* Send the message at once. */
4439 htx = htx_from_buf(&res->buf);
4440 h1m_init_res(&h1m);
4441
4442 /* Use the same http version than the request. */
4443 status = ultoa_r(appctx->appctx->ctx.hlua_apphttp.status, trash.area, trash.size);
4444 reason = appctx->appctx->ctx.hlua_apphttp.reason;
4445 if (reason == NULL)
4446 reason = http_get_reason(appctx->appctx->ctx.hlua_apphttp.status);
4447 if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) {
4448 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11);
4449 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason));
4450 }
4451 else {
4452 flags = HTX_SL_F_IS_RESP;
4453 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason));
4454 }
4455 if (!sl) {
4456 hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n",
4457 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4458 WILL_LJMP(lua_error(L));
4459 }
4460 sl->info.res.status = appctx->appctx->ctx.hlua_apphttp.status;
4461
4462 /* Get the array associated to the field "response" in the object AppletHTTP. */
4463 lua_pushvalue(L, 0);
4464 if (lua_getfield(L, 1, "response") != LUA_TTABLE) {
4465 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n",
4466 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4467 WILL_LJMP(lua_error(L));
4468 }
4469
4470 /* Browse the list of headers. */
4471 lua_pushnil(L);
4472 while(lua_next(L, -2) != 0) {
4473 /* We expect a string as -2. */
4474 if (lua_type(L, -2) != LUA_TSTRING) {
4475 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n",
4476 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4477 lua_typename(L, lua_type(L, -2)));
4478 WILL_LJMP(lua_error(L));
4479 }
4480 name = lua_tolstring(L, -2, &nlen);
4481
4482 /* We expect an array as -1. */
4483 if (lua_type(L, -1) != LUA_TTABLE) {
4484 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n",
4485 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4486 name,
4487 lua_typename(L, lua_type(L, -1)));
4488 WILL_LJMP(lua_error(L));
4489 }
4490
4491 /* Browse the table who is on the top of the stack. */
4492 lua_pushnil(L);
4493 while(lua_next(L, -2) != 0) {
4494 int id;
4495
4496 /* We expect a number as -2. */
4497 if (lua_type(L, -2) != LUA_TNUMBER) {
4498 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n",
4499 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4500 name,
4501 lua_typename(L, lua_type(L, -2)));
4502 WILL_LJMP(lua_error(L));
4503 }
4504 id = lua_tointeger(L, -2);
4505
4506 /* We expect a string as -2. */
4507 if (lua_type(L, -1) != LUA_TSTRING) {
4508 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n",
4509 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4510 name, id,
4511 lua_typename(L, lua_type(L, -1)));
4512 WILL_LJMP(lua_error(L));
4513 }
4514 value = lua_tolstring(L, -1, &vlen);
4515
4516 /* Simple Protocol checks. */
4517 if (isteqi(ist2(name, nlen), ist("transfer-encoding")))
Christopher Faulet700d9e82020-01-31 12:21:52 +01004518 h1_parse_xfer_enc_header(&h1m, ist2(value, vlen));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004519 else if (isteqi(ist2(name, nlen), ist("content-length"))) {
4520 struct ist v = ist2(value, vlen);
4521 int ret;
4522
4523 ret = h1_parse_cont_len_header(&h1m, &v);
4524 if (ret < 0) {
4525 hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n",
4526 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4527 name);
4528 WILL_LJMP(lua_error(L));
4529 }
4530 else if (ret == 0)
4531 goto next; /* Skip it */
4532 }
4533
4534 /* Add a new header */
4535 if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) {
4536 hlua_pusherror(L, "Lua applet http '%s': Failed to add header '%s' in the response.\n",
4537 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4538 name);
4539 WILL_LJMP(lua_error(L));
4540 }
4541 next:
4542 /* Remove the array from the stack, and get next element with a remaining string. */
4543 lua_pop(L, 1);
4544 }
4545
4546 /* Remove the array from the stack, and get next element with a remaining string. */
4547 lua_pop(L, 1);
4548 }
4549
4550 if (h1m.flags & H1_MF_CHNK)
4551 h1m.flags &= ~H1_MF_CLEN;
4552 if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK))
4553 h1m.flags |= H1_MF_XFER_LEN;
4554
4555 /* Uset HTX start-line flags */
4556 if (h1m.flags & H1_MF_XFER_ENC)
4557 flags |= HTX_SL_F_XFER_ENC;
4558 if (h1m.flags & H1_MF_XFER_LEN) {
4559 flags |= HTX_SL_F_XFER_LEN;
4560 if (h1m.flags & H1_MF_CHNK)
4561 flags |= HTX_SL_F_CHNK;
4562 else if (h1m.flags & H1_MF_CLEN)
4563 flags |= HTX_SL_F_CLEN;
4564 if (h1m.body_len == 0)
4565 flags |= HTX_SL_F_BODYLESS;
4566 }
4567 sl->flags |= flags;
4568
4569 /* If we dont have a content-length set, and the HTTP version is 1.1
4570 * and the status code implies the presence of a message body, we must
4571 * announce a transfer encoding chunked. This is required by haproxy
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004572 * for the keepalive compliance. If the applet announces a transfer-encoding
4573 * chunked itself, don't do anything.
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004574 */
4575 if ((flags & (HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN)) == HTX_SL_F_VER_11 &&
4576 appctx->appctx->ctx.hlua_apphttp.status >= 200 &&
4577 appctx->appctx->ctx.hlua_apphttp.status != 204 &&
4578 appctx->appctx->ctx.hlua_apphttp.status != 304) {
4579 /* Add a new header */
4580 sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN);
4581 if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) {
4582 hlua_pusherror(L, "Lua applet http '%s': Failed to add header 'transfer-encoding' in the response.\n",
4583 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4584 WILL_LJMP(lua_error(L));
4585 }
4586 }
4587
4588 /* Finalize headers. */
4589 if (!htx_add_endof(htx, HTX_BLK_EOH)) {
4590 hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n",
4591 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4592 WILL_LJMP(lua_error(L));
4593 }
4594
4595 if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) {
4596 b_reset(&res->buf);
4597 hlua_pusherror(L, "Lua: 'start_response': response header block too big");
4598 WILL_LJMP(lua_error(L));
4599 }
4600
4601 htx_to_buf(htx, &res->buf);
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01004602 channel_add_input(res, htx->data);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004603
4604 /* Headers sent, set the flag. */
4605 appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT;
4606 return 0;
4607
4608}
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004609/* We will build the status line and the headers of the HTTP response.
4610 * We will try send at once if its not possible, we give back the hand
4611 * waiting for more room.
4612 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004613__LJMP static int hlua_applet_http_start_response_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004614{
4615 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4616 struct stream_interface *si = appctx->appctx->owner;
4617 struct channel *res = si_ic(si);
4618
4619 if (co_data(res)) {
4620 si_rx_room_blk(si);
Christopher Fauleta2097962019-07-15 16:25:33 +02004621 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_start_response_yield, TICK_ETERNITY, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004622 }
Christopher Fauleta2097962019-07-15 16:25:33 +02004623 return MAY_LJMP(hlua_applet_http_send_response(L));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004624}
4625
4626
Christopher Fauleta2097962019-07-15 16:25:33 +02004627__LJMP static int hlua_applet_http_start_response(lua_State *L)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004628{
Christopher Fauleta2097962019-07-15 16:25:33 +02004629 return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004630}
4631
Christopher Fauleta2097962019-07-15 16:25:33 +02004632/*
4633 *
4634 *
4635 * Class HTTP
4636 *
4637 *
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004638 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004639
4640/* Returns a struct hlua_txn if the stack entry "ud" is
4641 * a class stream, otherwise it throws an error.
4642 */
4643__LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004644{
Christopher Fauleta2097962019-07-15 16:25:33 +02004645 return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref));
4646}
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004647
Christopher Fauleta2097962019-07-15 16:25:33 +02004648/* This function creates and push in the stack a HTTP object
4649 * according with a current TXN.
4650 */
4651static int hlua_http_new(lua_State *L, struct hlua_txn *txn)
4652{
4653 struct hlua_txn *htxn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004654
Christopher Fauleta2097962019-07-15 16:25:33 +02004655 /* Check stack size. */
4656 if (!lua_checkstack(L, 3))
4657 return 0;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004658
Christopher Fauleta2097962019-07-15 16:25:33 +02004659 /* Create the object: obj[0] = userdata.
4660 * Note that the base of the Converters object is the
4661 * same than the TXN object.
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004662 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004663 lua_newtable(L);
4664 htxn = lua_newuserdata(L, sizeof(*htxn));
4665 lua_rawseti(L, -2, 0);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004666
4667 htxn->s = txn->s;
4668 htxn->p = txn->p;
Christopher Faulet256b69a2019-05-23 11:14:21 +02004669 htxn->dir = txn->dir;
4670 htxn->flags = txn->flags;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004671
4672 /* Pop a class stream metatable and affect it to the table. */
4673 lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref);
4674 lua_setmetatable(L, -2);
4675
4676 return 1;
4677}
4678
4679/* This function creates ans returns an array of HTTP headers.
4680 * This function does not fails. It is used as wrapper with the
4681 * 2 following functions.
4682 */
Christopher Faulet9d1332b2020-02-24 16:46:16 +01004683__LJMP static int hlua_http_get_headers(lua_State *L, struct http_msg *msg)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004684{
Christopher Fauleta2097962019-07-15 16:25:33 +02004685 struct htx *htx;
4686 int32_t pos;
4687
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004688 /* Create the table. */
4689 lua_newtable(L);
4690
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004691
Christopher Fauleta2097962019-07-15 16:25:33 +02004692 htx = htxbuf(&msg->chn->buf);
4693 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
4694 struct htx_blk *blk = htx_get_blk(htx, pos);
4695 enum htx_blk_type type = htx_get_blk_type(blk);
4696 struct ist n, v;
4697 int len;
Christopher Faulet724a12c2018-12-13 22:12:15 +01004698
Christopher Fauleta2097962019-07-15 16:25:33 +02004699 if (type == HTX_BLK_HDR) {
4700 n = htx_get_blk_name(htx,blk);
4701 v = htx_get_blk_value(htx, blk);
Christopher Faulet724a12c2018-12-13 22:12:15 +01004702 }
Christopher Fauleta2097962019-07-15 16:25:33 +02004703 else if (type == HTX_BLK_EOH)
4704 break;
4705 else
4706 continue;
Christopher Faulet724a12c2018-12-13 22:12:15 +01004707
Christopher Fauleta2097962019-07-15 16:25:33 +02004708 /* Check for existing entry:
4709 * assume that the table is on the top of the stack, and
4710 * push the key in the stack, the function lua_gettable()
4711 * perform the lookup.
4712 */
4713 lua_pushlstring(L, n.ptr, n.len);
4714 lua_gettable(L, -2);
Christopher Faulet724a12c2018-12-13 22:12:15 +01004715
Christopher Fauleta2097962019-07-15 16:25:33 +02004716 switch (lua_type(L, -1)) {
4717 case LUA_TNIL:
4718 /* Table not found, create it. */
4719 lua_pop(L, 1); /* remove the nil value. */
4720 lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */
4721 lua_newtable(L); /* create and push empty table. */
4722 lua_pushlstring(L, v.ptr, v.len); /* push header value. */
4723 lua_rawseti(L, -2, 0); /* index header value (pop it). */
4724 lua_rawset(L, -3); /* index new table with header name (pop the values). */
Christopher Faulet724a12c2018-12-13 22:12:15 +01004725 break;
Christopher Faulet724a12c2018-12-13 22:12:15 +01004726
Christopher Fauleta2097962019-07-15 16:25:33 +02004727 case LUA_TTABLE:
4728 /* Entry found: push the value in the table. */
4729 len = lua_rawlen(L, -1);
4730 lua_pushlstring(L, v.ptr, v.len); /* push header value. */
4731 lua_rawseti(L, -2, len+1); /* index header value (pop it). */
4732 lua_pop(L, 1); /* remove the table (it is stored in the main table). */
4733 break;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004734
Christopher Fauleta2097962019-07-15 16:25:33 +02004735 default:
4736 /* Other cases are errors. */
4737 hlua_pusherror(L, "internal error during the parsing of headers.");
4738 WILL_LJMP(lua_error(L));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004739 }
4740 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004741 return 1;
4742}
4743
4744__LJMP static int hlua_http_req_get_headers(lua_State *L)
4745{
4746 struct hlua_txn *htxn;
4747
4748 MAY_LJMP(check_args(L, 1, "req_get_headers"));
4749 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4750
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004751 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004752 WILL_LJMP(lua_error(L));
4753
Christopher Faulet9d1332b2020-02-24 16:46:16 +01004754 return hlua_http_get_headers(L, &htxn->s->txn->req);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004755}
4756
4757__LJMP static int hlua_http_res_get_headers(lua_State *L)
4758{
4759 struct hlua_txn *htxn;
4760
4761 MAY_LJMP(check_args(L, 1, "res_get_headers"));
4762 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4763
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004764 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004765 WILL_LJMP(lua_error(L));
4766
Christopher Faulet9d1332b2020-02-24 16:46:16 +01004767 return hlua_http_get_headers(L, &htxn->s->txn->rsp);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004768}
4769
4770/* This function replace full header, or just a value in
4771 * the request or in the response. It is a wrapper fir the
4772 * 4 following functions.
4773 */
Christopher Fauletd1914aa2020-02-24 16:52:46 +01004774__LJMP static inline int hlua_http_rep_hdr(lua_State *L, struct http_msg *msg, int full)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004775{
4776 size_t name_len;
4777 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
4778 const char *reg = MAY_LJMP(luaL_checkstring(L, 3));
4779 const char *value = MAY_LJMP(luaL_checkstring(L, 4));
Christopher Fauleta2097962019-07-15 16:25:33 +02004780 struct htx *htx;
Dragan Dosen26743032019-04-30 15:54:36 +02004781 struct my_regex *re;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004782
Dragan Dosen26743032019-04-30 15:54:36 +02004783 if (!(re = regex_comp(reg, 1, 1, NULL)))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004784 WILL_LJMP(luaL_argerror(L, 3, "invalid regex"));
4785
Christopher Fauleta2097962019-07-15 16:25:33 +02004786 htx = htxbuf(&msg->chn->buf);
Christopher Fauletd1914aa2020-02-24 16:52:46 +01004787 http_replace_hdrs(chn_strm(msg->chn), htx, ist2(name, name_len), value, re, full);
Dragan Dosen26743032019-04-30 15:54:36 +02004788 regex_free(re);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004789 return 0;
4790}
4791
4792__LJMP static int hlua_http_req_rep_hdr(lua_State *L)
4793{
4794 struct hlua_txn *htxn;
4795
4796 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
4797 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4798
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004799 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004800 WILL_LJMP(lua_error(L));
4801
Christopher Fauletd1914aa2020-02-24 16:52:46 +01004802 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->req, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004803}
4804
4805__LJMP static int hlua_http_res_rep_hdr(lua_State *L)
4806{
4807 struct hlua_txn *htxn;
4808
4809 MAY_LJMP(check_args(L, 4, "res_rep_hdr"));
4810 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4811
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004812 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004813 WILL_LJMP(lua_error(L));
4814
Christopher Fauletd1914aa2020-02-24 16:52:46 +01004815 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->rsp, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004816}
4817
4818__LJMP static int hlua_http_req_rep_val(lua_State *L)
4819{
4820 struct hlua_txn *htxn;
4821
4822 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
4823 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4824
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004825 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004826 WILL_LJMP(lua_error(L));
4827
Christopher Fauletd1914aa2020-02-24 16:52:46 +01004828 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->req, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004829}
4830
4831__LJMP static int hlua_http_res_rep_val(lua_State *L)
4832{
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004833 struct hlua_txn *htxn;
4834
4835 MAY_LJMP(check_args(L, 4, "res_rep_val"));
4836 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4837
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004838 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004839 WILL_LJMP(lua_error(L));
4840
Christopher Fauletd1914aa2020-02-24 16:52:46 +01004841 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->rsp, 0));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004842}
4843
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004844/* This function deletes all the occurrences of an header.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004845 * It is a wrapper for the 2 following functions.
4846 */
Christopher Fauletd31c7b32020-02-25 09:37:57 +01004847__LJMP static inline int hlua_http_del_hdr(lua_State *L, struct http_msg *msg)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004848{
4849 size_t len;
4850 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Christopher Fauleta2097962019-07-15 16:25:33 +02004851 struct htx *htx = htxbuf(&msg->chn->buf);
4852 struct http_hdr_ctx ctx;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004853
Christopher Fauleta2097962019-07-15 16:25:33 +02004854 ctx.blk = NULL;
4855 while (http_find_header(htx, ist2(name, len), &ctx, 1))
4856 http_remove_header(htx, &ctx);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004857 return 0;
4858}
4859
4860__LJMP static int hlua_http_req_del_hdr(lua_State *L)
4861{
4862 struct hlua_txn *htxn;
4863
4864 MAY_LJMP(check_args(L, 2, "req_del_hdr"));
4865 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4866
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004867 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004868 WILL_LJMP(lua_error(L));
4869
Christopher Fauletd31c7b32020-02-25 09:37:57 +01004870 return hlua_http_del_hdr(L, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004871}
4872
4873__LJMP static int hlua_http_res_del_hdr(lua_State *L)
4874{
4875 struct hlua_txn *htxn;
4876
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004877 MAY_LJMP(check_args(L, 2, "res_del_hdr"));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004878 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4879
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004880 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004881 WILL_LJMP(lua_error(L));
4882
Christopher Fauletd31c7b32020-02-25 09:37:57 +01004883 return hlua_http_del_hdr(L, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004884}
4885
4886/* This function adds an header. It is a wrapper used by
4887 * the 2 following functions.
4888 */
Christopher Fauletd31c7b32020-02-25 09:37:57 +01004889__LJMP static inline int hlua_http_add_hdr(lua_State *L, struct http_msg *msg)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004890{
4891 size_t name_len;
4892 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
4893 size_t value_len;
4894 const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len));
Christopher Fauleta2097962019-07-15 16:25:33 +02004895 struct htx *htx = htxbuf(&msg->chn->buf);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004896
Christopher Fauleta2097962019-07-15 16:25:33 +02004897 lua_pushboolean(L, http_add_header(htx, ist2(name, name_len),
4898 ist2(value, value_len)));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004899 return 0;
4900}
4901
4902__LJMP static int hlua_http_req_add_hdr(lua_State *L)
4903{
4904 struct hlua_txn *htxn;
4905
4906 MAY_LJMP(check_args(L, 3, "req_add_hdr"));
4907 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4908
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004909 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004910 WILL_LJMP(lua_error(L));
4911
Christopher Fauletd31c7b32020-02-25 09:37:57 +01004912 return hlua_http_add_hdr(L, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004913}
4914
4915__LJMP static int hlua_http_res_add_hdr(lua_State *L)
4916{
4917 struct hlua_txn *htxn;
4918
4919 MAY_LJMP(check_args(L, 3, "res_add_hdr"));
4920 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4921
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004922 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004923 WILL_LJMP(lua_error(L));
4924
Christopher Fauletd31c7b32020-02-25 09:37:57 +01004925 return hlua_http_add_hdr(L, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004926}
4927
4928static int hlua_http_req_set_hdr(lua_State *L)
4929{
4930 struct hlua_txn *htxn;
4931
4932 MAY_LJMP(check_args(L, 3, "req_set_hdr"));
4933 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4934
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004935 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004936 WILL_LJMP(lua_error(L));
4937
Christopher Fauletd31c7b32020-02-25 09:37:57 +01004938 hlua_http_del_hdr(L, &htxn->s->txn->req);
4939 return hlua_http_add_hdr(L, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004940}
4941
4942static int hlua_http_res_set_hdr(lua_State *L)
4943{
4944 struct hlua_txn *htxn;
4945
4946 MAY_LJMP(check_args(L, 3, "res_set_hdr"));
4947 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4948
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004949 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004950 WILL_LJMP(lua_error(L));
4951
Christopher Fauletd31c7b32020-02-25 09:37:57 +01004952 hlua_http_del_hdr(L, &htxn->s->txn->rsp);
4953 return hlua_http_add_hdr(L, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004954}
4955
4956/* This function set the method. */
4957static int hlua_http_req_set_meth(lua_State *L)
4958{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02004959 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004960 size_t name_len;
4961 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004962
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004963 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004964 WILL_LJMP(lua_error(L));
4965
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004966 lua_pushboolean(L, http_req_replace_stline(0, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004967 return 1;
4968}
4969
4970/* This function set the method. */
4971static int hlua_http_req_set_path(lua_State *L)
4972{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02004973 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004974 size_t name_len;
4975 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004976
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004977 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004978 WILL_LJMP(lua_error(L));
4979
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004980 lua_pushboolean(L, http_req_replace_stline(1, 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 query-string. */
4985static int hlua_http_req_set_query(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 FOURNIER08504f42015-03-16 14:17:08 +01004990
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004991 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004992 WILL_LJMP(lua_error(L));
4993
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004994 /* Check length. */
4995 if (name_len > trash.size - 1) {
4996 lua_pushboolean(L, 0);
4997 return 1;
4998 }
4999
5000 /* Add the mark question as prefix. */
5001 chunk_reset(&trash);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005002 trash.area[trash.data++] = '?';
5003 memcpy(trash.area + trash.data, name, name_len);
5004 trash.data += name_len;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005005
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005006 lua_pushboolean(L,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005007 http_req_replace_stline(2, trash.area, trash.data, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005008 return 1;
5009}
5010
5011/* This function set the uri. */
5012static int hlua_http_req_set_uri(lua_State *L)
5013{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005014 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005015 size_t name_len;
5016 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005017
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005018 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005019 WILL_LJMP(lua_error(L));
5020
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005021 lua_pushboolean(L, http_req_replace_stline(3, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005022 return 1;
5023}
5024
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005025/* This function set the response code & optionally reason. */
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005026static int hlua_http_res_set_status(lua_State *L)
5027{
5028 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5029 unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2));
Christopher Faulet96bff762019-12-17 13:46:18 +01005030 const char *str = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
5031 const struct ist reason = ist2(str, (str ? strlen(str) : 0));
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005032
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005033 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005034 WILL_LJMP(lua_error(L));
5035
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005036 http_res_set_status(code, reason, htxn->s);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005037 return 0;
5038}
5039
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005040/*
5041 *
5042 *
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005043 * Class TXN
5044 *
5045 *
5046 */
5047
5048/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02005049 * a class stream, otherwise it throws an error.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005050 */
5051__LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud)
5052{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02005053 return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref));
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005054}
5055
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005056__LJMP static int hlua_set_var(lua_State *L)
5057{
5058 struct hlua_txn *htxn;
5059 const char *name;
5060 size_t len;
5061 struct sample smp;
5062
Tim Duesterhus4e172c92020-05-19 13:49:42 +02005063 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
5064 WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments"));
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005065
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);
Tim Duesterhus4e172c92020-05-19 13:49:42 +02005077
5078 if (lua_gettop(L) == 4 && lua_toboolean(L, 4))
5079 lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0);
5080 else
5081 lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0);
5082
Tim Duesterhus84ebc132020-05-19 13:49:41 +02005083 return 1;
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005084}
5085
Christopher Faulet85d79c92016-11-09 16:54:56 +01005086__LJMP static int hlua_unset_var(lua_State *L)
5087{
5088 struct hlua_txn *htxn;
5089 const char *name;
5090 size_t len;
5091 struct sample smp;
5092
5093 MAY_LJMP(check_args(L, 2, "unset_var"));
5094
5095 /* It is useles to retrieve the stream, but this function
5096 * runs only in a stream context.
5097 */
5098 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5099 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5100
5101 /* Unset the variable. */
5102 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Tim Duesterhus84ebc132020-05-19 13:49:41 +02005103 lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0);
5104 return 1;
Christopher Faulet85d79c92016-11-09 16:54:56 +01005105}
5106
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005107__LJMP static int hlua_get_var(lua_State *L)
5108{
5109 struct hlua_txn *htxn;
5110 const char *name;
5111 size_t len;
5112 struct sample smp;
5113
5114 MAY_LJMP(check_args(L, 2, "get_var"));
5115
5116 /* It is useles to retrieve the stream, but this function
5117 * runs only in a stream context.
5118 */
5119 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5120 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5121
Willy Tarreau7560dd42016-03-10 16:28:58 +01005122 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Willy Tarreau6204cd92016-03-10 16:33:04 +01005123 if (!vars_get_by_name(name, len, &smp)) {
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005124 lua_pushnil(L);
5125 return 1;
5126 }
5127
5128 return hlua_smp2lua(L, &smp);
5129}
5130
Willy Tarreau59551662015-03-10 14:23:13 +01005131__LJMP static int hlua_set_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005132{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005133 struct hlua *hlua;
5134
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005135 MAY_LJMP(check_args(L, 2, "set_priv"));
5136
Willy Tarreau87b09662015-04-03 00:22:06 +02005137 /* It is useles to retrieve the stream, but this function
5138 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005139 */
5140 MAY_LJMP(hlua_checktxn(L, 1));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005141 hlua = hlua_gethlua(L);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005142
5143 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02005144 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005145
5146 /* Get and store new value. */
5147 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
5148 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
5149
5150 return 0;
5151}
5152
Willy Tarreau59551662015-03-10 14:23:13 +01005153__LJMP static int hlua_get_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005154{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005155 struct hlua *hlua;
5156
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005157 MAY_LJMP(check_args(L, 1, "get_priv"));
5158
Willy Tarreau87b09662015-04-03 00:22:06 +02005159 /* It is useles to retrieve the stream, but this function
5160 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005161 */
5162 MAY_LJMP(hlua_checktxn(L, 1));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005163 hlua = hlua_gethlua(L);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005164
5165 /* Push configuration index in the stack. */
5166 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
5167
5168 return 1;
5169}
5170
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005171/* Create stack entry containing a class TXN. This function
5172 * return 0 if the stack does not contains free slots,
5173 * otherwise it returns 1.
5174 */
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005175static int hlua_txn_new(lua_State *L, struct stream *s, struct proxy *p, int dir, int flags)
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005176{
Willy Tarreaude491382015-04-06 11:04:28 +02005177 struct hlua_txn *htxn;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005178
5179 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005180 if (!lua_checkstack(L, 3))
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005181 return 0;
5182
5183 /* NOTE: The allocation never fails. The failure
5184 * throw an error, and the function never returns.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08005185 * if the throw is not available, the process is aborted.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005186 */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005187 /* Create the object: obj[0] = userdata. */
5188 lua_newtable(L);
Willy Tarreaude491382015-04-06 11:04:28 +02005189 htxn = lua_newuserdata(L, sizeof(*htxn));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005190 lua_rawseti(L, -2, 0);
5191
Willy Tarreaude491382015-04-06 11:04:28 +02005192 htxn->s = s;
5193 htxn->p = p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01005194 htxn->dir = dir;
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005195 htxn->flags = flags;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005196
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005197 /* Create the "f" field that contains a list of fetches. */
5198 lua_pushstring(L, "f");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005199 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005200 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005201 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005202
5203 /* Create the "sf" field that contains a list of stringsafe fetches. */
5204 lua_pushstring(L, "sf");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005205 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP | HLUA_F_AS_STRING))
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005206 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005207 lua_rawset(L, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005208
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005209 /* Create the "c" field that contains a list of converters. */
5210 lua_pushstring(L, "c");
Willy Tarreaude491382015-04-06 11:04:28 +02005211 if (!hlua_converters_new(L, htxn, 0))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005212 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005213 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005214
5215 /* Create the "sc" field that contains a list of stringsafe converters. */
5216 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01005217 if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING))
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005218 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005219 lua_rawset(L, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005220
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005221 /* Create the "req" field that contains the request channel object. */
5222 lua_pushstring(L, "req");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005223 if (!hlua_channel_new(L, &s->req))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005224 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005225 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005226
5227 /* Create the "res" field that contains the response channel object. */
5228 lua_pushstring(L, "res");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005229 if (!hlua_channel_new(L, &s->res))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005230 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005231 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005232
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005233 /* Creates the HTTP object is the current proxy allows http. */
5234 lua_pushstring(L, "http");
5235 if (p->mode == PR_MODE_HTTP) {
Willy Tarreaude491382015-04-06 11:04:28 +02005236 if (!hlua_http_new(L, htxn))
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005237 return 0;
5238 }
5239 else
5240 lua_pushnil(L);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005241 lua_rawset(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005242
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005243 /* Pop a class sesison metatable and affect it to the userdata. */
5244 lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref);
5245 lua_setmetatable(L, -2);
5246
5247 return 1;
5248}
5249
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005250__LJMP static int hlua_txn_deflog(lua_State *L)
5251{
5252 const char *msg;
5253 struct hlua_txn *htxn;
5254
5255 MAY_LJMP(check_args(L, 2, "deflog"));
5256 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5257 msg = MAY_LJMP(luaL_checkstring(L, 2));
5258
5259 hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg);
5260 return 0;
5261}
5262
5263__LJMP static int hlua_txn_log(lua_State *L)
5264{
5265 int level;
5266 const char *msg;
5267 struct hlua_txn *htxn;
5268
5269 MAY_LJMP(check_args(L, 3, "log"));
5270 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5271 level = MAY_LJMP(luaL_checkinteger(L, 2));
5272 msg = MAY_LJMP(luaL_checkstring(L, 3));
5273
5274 if (level < 0 || level >= NB_LOG_LEVELS)
5275 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
5276
5277 hlua_sendlog(htxn->s->be, level, msg);
5278 return 0;
5279}
5280
5281__LJMP static int hlua_txn_log_debug(lua_State *L)
5282{
5283 const char *msg;
5284 struct hlua_txn *htxn;
5285
5286 MAY_LJMP(check_args(L, 2, "Debug"));
5287 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5288 msg = MAY_LJMP(luaL_checkstring(L, 2));
5289 hlua_sendlog(htxn->s->be, LOG_DEBUG, msg);
5290 return 0;
5291}
5292
5293__LJMP static int hlua_txn_log_info(lua_State *L)
5294{
5295 const char *msg;
5296 struct hlua_txn *htxn;
5297
5298 MAY_LJMP(check_args(L, 2, "Info"));
5299 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5300 msg = MAY_LJMP(luaL_checkstring(L, 2));
5301 hlua_sendlog(htxn->s->be, LOG_INFO, msg);
5302 return 0;
5303}
5304
5305__LJMP static int hlua_txn_log_warning(lua_State *L)
5306{
5307 const char *msg;
5308 struct hlua_txn *htxn;
5309
5310 MAY_LJMP(check_args(L, 2, "Warning"));
5311 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5312 msg = MAY_LJMP(luaL_checkstring(L, 2));
5313 hlua_sendlog(htxn->s->be, LOG_WARNING, msg);
5314 return 0;
5315}
5316
5317__LJMP static int hlua_txn_log_alert(lua_State *L)
5318{
5319 const char *msg;
5320 struct hlua_txn *htxn;
5321
5322 MAY_LJMP(check_args(L, 2, "Alert"));
5323 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5324 msg = MAY_LJMP(luaL_checkstring(L, 2));
5325 hlua_sendlog(htxn->s->be, LOG_ALERT, msg);
5326 return 0;
5327}
5328
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005329__LJMP static int hlua_txn_set_loglevel(lua_State *L)
5330{
5331 struct hlua_txn *htxn;
5332 int ll;
5333
5334 MAY_LJMP(check_args(L, 2, "set_loglevel"));
5335 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5336 ll = MAY_LJMP(luaL_checkinteger(L, 2));
5337
5338 if (ll < 0 || ll > 7)
5339 WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7"));
5340
5341 htxn->s->logs.level = ll;
5342 return 0;
5343}
5344
5345__LJMP static int hlua_txn_set_tos(lua_State *L)
5346{
5347 struct hlua_txn *htxn;
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005348 int tos;
5349
5350 MAY_LJMP(check_args(L, 2, "set_tos"));
5351 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5352 tos = MAY_LJMP(luaL_checkinteger(L, 2));
5353
Willy Tarreau1a18b542018-12-11 16:37:42 +01005354 conn_set_tos(objt_conn(htxn->s->sess->origin), tos);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005355 return 0;
5356}
5357
5358__LJMP static int hlua_txn_set_mark(lua_State *L)
5359{
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005360 struct hlua_txn *htxn;
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005361 int mark;
5362
5363 MAY_LJMP(check_args(L, 2, "set_mark"));
5364 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5365 mark = MAY_LJMP(luaL_checkinteger(L, 2));
5366
Lukas Tribus579e3e32019-08-11 18:03:45 +02005367 conn_set_mark(objt_conn(htxn->s->sess->origin), mark);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005368 return 0;
5369}
5370
Patrick Hemmer268a7072018-05-11 12:52:31 -04005371__LJMP static int hlua_txn_set_priority_class(lua_State *L)
5372{
5373 struct hlua_txn *htxn;
5374
5375 MAY_LJMP(check_args(L, 2, "set_priority_class"));
5376 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5377 htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2)));
5378 return 0;
5379}
5380
5381__LJMP static int hlua_txn_set_priority_offset(lua_State *L)
5382{
5383 struct hlua_txn *htxn;
5384
5385 MAY_LJMP(check_args(L, 2, "set_priority_offset"));
5386 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5387 htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2)));
5388 return 0;
5389}
5390
Christopher Faulet700d9e82020-01-31 12:21:52 +01005391/* Forward the Reply object to the client. This function converts the reply in
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05005392 * HTX an push it to into the response channel. It is response to forward the
Christopher Faulet700d9e82020-01-31 12:21:52 +01005393 * message and terminate the transaction. It returns 1 on success and 0 on
5394 * error. The Reply must be on top of the stack.
5395 */
5396__LJMP static int hlua_txn_forward_reply(lua_State *L, struct stream *s)
5397{
5398 struct htx *htx;
5399 struct htx_sl *sl;
5400 struct h1m h1m;
5401 const char *status, *reason, *body;
5402 size_t status_len, reason_len, body_len;
5403 int ret, code, flags;
5404
5405 code = 200;
5406 status = "200";
5407 status_len = 3;
5408 ret = lua_getfield(L, -1, "status");
5409 if (ret == LUA_TNUMBER) {
5410 code = lua_tointeger(L, -1);
5411 status = lua_tolstring(L, -1, &status_len);
5412 }
5413 lua_pop(L, 1);
5414
5415 reason = http_get_reason(code);
5416 reason_len = strlen(reason);
5417 ret = lua_getfield(L, -1, "reason");
5418 if (ret == LUA_TSTRING)
5419 reason = lua_tolstring(L, -1, &reason_len);
5420 lua_pop(L, 1);
5421
5422 body = NULL;
5423 body_len = 0;
5424 ret = lua_getfield(L, -1, "body");
5425 if (ret == LUA_TSTRING)
5426 body = lua_tolstring(L, -1, &body_len);
5427 lua_pop(L, 1);
5428
5429 /* Prepare the response before inserting the headers */
5430 h1m_init_res(&h1m);
5431 htx = htx_from_buf(&s->res.buf);
5432 channel_htx_truncate(&s->res, htx);
5433 if (s->txn->req.flags & HTTP_MSGF_VER_11) {
5434 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11);
5435 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"),
5436 ist2(status, status_len), ist2(reason, reason_len));
5437 }
5438 else {
5439 flags = HTX_SL_F_IS_RESP;
5440 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"),
5441 ist2(status, status_len), ist2(reason, reason_len));
5442 }
5443 if (!sl)
5444 goto fail;
5445 sl->info.res.status = code;
5446
5447 /* Push in the stack the "headers" entry. */
5448 ret = lua_getfield(L, -1, "headers");
5449 if (ret != LUA_TTABLE)
5450 goto skip_headers;
5451
5452 lua_pushnil(L);
5453 while (lua_next(L, -2) != 0) {
5454 struct ist name, value;
5455 const char *n, *v;
5456 size_t nlen, vlen;
5457
5458 if (!lua_isstring(L, -2) || !lua_istable(L, -1)) {
5459 /* Skip element if the key is not a string or if the value is not a table */
5460 goto next_hdr;
5461 }
5462
5463 n = lua_tolstring(L, -2, &nlen);
5464 name = ist2(n, nlen);
5465 if (isteqi(name, ist("content-length"))) {
5466 /* Always skip content-length header. It will be added
5467 * later with the correct len
5468 */
5469 goto next_hdr;
5470 }
5471
5472 /* Loop on header's values */
5473 lua_pushnil(L);
5474 while (lua_next(L, -2)) {
5475 if (!lua_isstring(L, -1)) {
5476 /* Skip the value if it is not a string */
5477 goto next_value;
5478 }
5479
5480 v = lua_tolstring(L, -1, &vlen);
5481 value = ist2(v, vlen);
5482
5483 if (isteqi(name, ist("transfer-encoding")))
5484 h1_parse_xfer_enc_header(&h1m, value);
5485 if (!htx_add_header(htx, ist2(n, nlen), ist2(v, vlen)))
5486 goto fail;
5487
5488 next_value:
5489 lua_pop(L, 1);
5490 }
5491
5492 next_hdr:
5493 lua_pop(L, 1);
5494 }
5495 skip_headers:
5496 lua_pop(L, 1);
5497
5498 /* Update h1m flags: CLEN is set if CHNK is not present */
5499 if (!(h1m.flags & H1_MF_CHNK)) {
5500 const char *clen = ultoa(body_len);
5501
5502 h1m.flags |= H1_MF_CLEN;
5503 if (!htx_add_header(htx, ist("content-length"), ist(clen)))
5504 goto fail;
5505 }
5506 if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK))
5507 h1m.flags |= H1_MF_XFER_LEN;
5508
5509 /* Update HTX start-line flags */
5510 if (h1m.flags & H1_MF_XFER_ENC)
5511 flags |= HTX_SL_F_XFER_ENC;
5512 if (h1m.flags & H1_MF_XFER_LEN) {
5513 flags |= HTX_SL_F_XFER_LEN;
5514 if (h1m.flags & H1_MF_CHNK)
5515 flags |= HTX_SL_F_CHNK;
5516 else if (h1m.flags & H1_MF_CLEN)
5517 flags |= HTX_SL_F_CLEN;
5518 if (h1m.body_len == 0)
5519 flags |= HTX_SL_F_BODYLESS;
5520 }
5521 sl->flags |= flags;
5522
5523
5524 if (!htx_add_endof(htx, HTX_BLK_EOH) ||
5525 (body_len && !htx_add_data_atonce(htx, ist2(body, body_len))) ||
5526 !htx_add_endof(htx, HTX_BLK_EOM))
5527 goto fail;
5528
5529 /* Now, forward the response and terminate the transaction */
5530 s->txn->status = code;
5531 htx_to_buf(htx, &s->res.buf);
5532 if (!http_forward_proxy_resp(s, 1))
5533 goto fail;
5534
5535 return 1;
5536
5537 fail:
5538 channel_htx_truncate(&s->res, htx);
5539 return 0;
5540}
5541
5542/* Terminate a transaction if called from a lua action. For TCP streams,
5543 * processing is just aborted. Nothing is returned to the client and all
5544 * arguments are ignored. For HTTP streams, if a reply is passed as argument, it
5545 * is forwarded to the client before terminating the transaction. On success,
5546 * the function exits with ACT_RET_DONE code. If an error occurred, it exits
5547 * with ACT_RET_ERR code. If this function is not called from a lua action, it
5548 * just exits without any processing.
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005549 */
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005550__LJMP static int hlua_txn_done(lua_State *L)
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005551{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005552 struct hlua_txn *htxn;
Christopher Faulet700d9e82020-01-31 12:21:52 +01005553 struct stream *s;
5554 int finst;
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005555
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005556 htxn = MAY_LJMP(hlua_checktxn(L, 1));
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005557
Christopher Faulet700d9e82020-01-31 12:21:52 +01005558 /* If the flags NOTERM is set, we cannot terminate the session, so we
5559 * just end the execution of the current lua code. */
5560 if (htxn->flags & HLUA_TXN_NOTERM)
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005561 WILL_LJMP(hlua_done(L));
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005562
Christopher Faulet700d9e82020-01-31 12:21:52 +01005563 s = htxn->s;
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005564 if (!IS_HTX_STRM(htxn->s)) {
Christopher Faulet700d9e82020-01-31 12:21:52 +01005565 struct channel *req = &s->req;
5566 struct channel *res = &s->res;
Willy Tarreau81389672015-03-10 12:03:52 +01005567
Christopher Faulet700d9e82020-01-31 12:21:52 +01005568 channel_auto_read(req);
5569 channel_abort(req);
5570 channel_auto_close(req);
5571 channel_erase(req);
5572
5573 res->wex = tick_add_ifset(now_ms, res->wto);
5574 channel_auto_read(res);
5575 channel_auto_close(res);
5576 channel_shutr_now(res);
5577
5578 finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_D);
5579 goto done;
Christopher Fauletfe6a71b2019-07-26 16:40:24 +02005580 }
Thierry FOURNIER10ec2142015-08-24 17:23:45 +02005581
Christopher Faulet700d9e82020-01-31 12:21:52 +01005582 if (lua_gettop(L) == 1 || !lua_istable(L, 2)) {
5583 /* No reply or invalid reply */
5584 s->txn->status = 0;
5585 http_reply_and_close(s, 0, NULL);
5586 }
5587 else {
5588 /* Remove extra args to have the reply on top of the stack */
5589 if (lua_gettop(L) > 2)
5590 lua_pop(L, lua_gettop(L) - 2);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005591
Christopher Faulet700d9e82020-01-31 12:21:52 +01005592 if (!hlua_txn_forward_reply(L, s)) {
5593 if (!(s->flags & SF_ERR_MASK))
5594 s->flags |= SF_ERR_PRXCOND;
5595 lua_pushinteger(L, ACT_RET_ERR);
5596 WILL_LJMP(hlua_done(L));
5597 return 0; /* Never reached */
5598 }
Christopher Faulet4d0e2632019-07-16 10:52:40 +02005599 }
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005600
Christopher Faulet700d9e82020-01-31 12:21:52 +01005601 finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_H);
5602 if (htxn->dir == SMP_OPT_DIR_REQ) {
5603 /* let's log the request time */
5604 s->logs.tv_request = now;
5605 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
5606 _HA_ATOMIC_ADD(&s->sess->fe->fe_counters.intercepted_req, 1);
5607 }
Christopher Fauletfe6a71b2019-07-26 16:40:24 +02005608
Christopher Faulet700d9e82020-01-31 12:21:52 +01005609 done:
5610 if (!(s->flags & SF_ERR_MASK))
5611 s->flags |= SF_ERR_LOCAL;
5612 if (!(s->flags & SF_FINST_MASK))
5613 s->flags |= finst;
Christopher Fauletfe6a71b2019-07-26 16:40:24 +02005614
Christopher Faulet4ad73102020-03-05 11:07:31 +01005615 lua_pushinteger(L, ACT_RET_ABRT);
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005616 WILL_LJMP(hlua_done(L));
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005617 return 0;
5618}
5619
Christopher Faulet700d9e82020-01-31 12:21:52 +01005620/*
5621 *
5622 *
5623 * Class REPLY
5624 *
5625 *
5626 */
5627
5628/* Pushes the TXN reply onto the top of the stack. If the stask does not have a
5629 * free slots, the function fails and returns 0;
5630 */
5631static int hlua_txn_reply_new(lua_State *L)
5632{
5633 struct hlua_txn *htxn;
5634 const char *reason, *body = NULL;
5635 int ret, status;
5636
5637 htxn = MAY_LJMP(hlua_checktxn(L, 1));
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005638 if (!IS_HTX_STRM(htxn->s)) {
Christopher Faulet700d9e82020-01-31 12:21:52 +01005639 hlua_pusherror(L, "txn object is not an HTTP transaction.");
5640 WILL_LJMP(lua_error(L));
5641 }
5642
5643 /* Default value */
5644 status = 200;
5645 reason = http_get_reason(status);
5646
5647 if (lua_istable(L, 2)) {
5648 /* load status and reason from the table argument at index 2 */
5649 ret = lua_getfield(L, 2, "status");
5650 if (ret == LUA_TNIL)
5651 goto reason;
5652 else if (ret != LUA_TNUMBER) {
5653 /* invalid status: ignore the reason */
5654 goto body;
5655 }
5656 status = lua_tointeger(L, -1);
5657
5658 reason:
5659 lua_pop(L, 1); /* restore the stack: remove status */
5660 ret = lua_getfield(L, 2, "reason");
5661 if (ret == LUA_TSTRING)
5662 reason = lua_tostring(L, -1);
5663
5664 body:
5665 lua_pop(L, 1); /* restore the stack: remove invalid status or reason */
5666 ret = lua_getfield(L, 2, "body");
5667 if (ret == LUA_TSTRING)
5668 body = lua_tostring(L, -1);
5669 lua_pop(L, 1); /* restore the stack: remove body */
5670 }
5671
5672 /* Create the Reply table */
5673 lua_newtable(L);
5674
5675 /* Add status element */
5676 lua_pushstring(L, "status");
5677 lua_pushinteger(L, status);
5678 lua_settable(L, -3);
5679
5680 /* Add reason element */
5681 reason = http_get_reason(status);
5682 lua_pushstring(L, "reason");
5683 lua_pushstring(L, reason);
5684 lua_settable(L, -3);
5685
5686 /* Add body element, nil if undefined */
5687 lua_pushstring(L, "body");
5688 if (body)
5689 lua_pushstring(L, body);
5690 else
5691 lua_pushnil(L);
5692 lua_settable(L, -3);
5693
5694 /* Add headers element */
5695 lua_pushstring(L, "headers");
5696 lua_newtable(L);
5697
5698 /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */
5699 if (lua_istable(L, 2)) {
5700 /* load headers from the table argument at index 2. If it is a table, copy it. */
5701 ret = lua_getfield(L, 2, "headers");
5702 if (ret == LUA_TTABLE) {
5703 /* stack: [ ... <headers:table>, <table> ] */
5704 lua_pushnil(L);
5705 while (lua_next(L, -2) != 0) {
5706 /* stack: [ ... <headers:table>, <table>, k, v] */
5707 if (!lua_isstring(L, -1) && !lua_istable(L, -1)) {
5708 /* invalid value type, skip it */
5709 lua_pop(L, 1);
5710 continue;
5711 }
5712
5713
5714 /* Duplicate the key and swap it with the value. */
5715 lua_pushvalue(L, -2);
5716 lua_insert(L, -2);
5717 /* stack: [ ... <headers:table>, <table>, k, k, v ] */
5718
5719 lua_newtable(L);
5720 lua_insert(L, -2);
5721 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, v ] */
5722
5723 if (lua_isstring(L, -1)) {
5724 /* push the value in the inner table */
5725 lua_rawseti(L, -2, 1);
5726 }
5727 else { /* table */
5728 lua_pushnil(L);
5729 while (lua_next(L, -2) != 0) {
5730 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2, v2 ] */
5731 if (!lua_isstring(L, -1)) {
5732 /* invalid value type, skip it*/
5733 lua_pop(L, 1);
5734 continue;
5735 }
5736 /* push the value in the inner table */
5737 lua_rawseti(L, -4, lua_rawlen(L, -4) + 1);
5738 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2 ] */
5739 }
5740 lua_pop(L, 1);
5741 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table> ] */
5742 }
5743
5744 /* push (k,v) on the stack in the headers table:
5745 * stack: [ ... <headers:table>, <table>, k, k, v ]
5746 */
5747 lua_settable(L, -5);
5748 /* stack: [ ... <headers:table>, <table>, k ] */
5749 }
5750 }
5751 lua_pop(L, 1);
5752 }
5753 /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */
5754 lua_settable(L, -3);
5755 /* stack: [ txn, <Arg:table>, <Reply:table> ] */
5756
5757 /* Pop a class sesison metatable and affect it to the userdata. */
5758 lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_reply_ref);
5759 lua_setmetatable(L, -2);
5760 return 1;
5761}
5762
5763/* Set the reply status code, and optionally the reason. If no reason is
5764 * provided, the default one corresponding to the status code is used.
5765 */
5766__LJMP static int hlua_txn_reply_set_status(lua_State *L)
5767{
5768 int status = MAY_LJMP(luaL_checkinteger(L, 2));
5769 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
5770
5771 /* First argument (self) must be a table */
5772 luaL_checktype(L, 1, LUA_TTABLE);
5773
5774 if (status < 100 || status > 599) {
5775 lua_pushboolean(L, 0);
5776 return 1;
5777 }
5778 if (!reason)
5779 reason = http_get_reason(status);
5780
5781 lua_pushinteger(L, status);
5782 lua_setfield(L, 1, "status");
5783
5784 lua_pushstring(L, reason);
5785 lua_setfield(L, 1, "reason");
5786
5787 lua_pushboolean(L, 1);
5788 return 1;
5789}
5790
5791/* Add a header into the reply object. Each header name is associated to an
5792 * array of values in the "headers" table. If the header name is not found, a
5793 * new entry is created.
5794 */
5795__LJMP static int hlua_txn_reply_add_header(lua_State *L)
5796{
5797 const char *name = MAY_LJMP(luaL_checkstring(L, 2));
5798 const char *value = MAY_LJMP(luaL_checkstring(L, 3));
5799 int ret;
5800
5801 /* First argument (self) must be a table */
5802 luaL_checktype(L, 1, LUA_TTABLE);
5803
5804 /* Push in the stack the "headers" entry. */
5805 ret = lua_getfield(L, 1, "headers");
5806 if (ret != LUA_TTABLE) {
5807 hlua_pusherror(L, "Reply['headers'] is expected to a an array. %s found", lua_typename(L, ret));
5808 WILL_LJMP(lua_error(L));
5809 }
5810
5811 /* check if the header is already registered. If not, register it. */
5812 ret = lua_getfield(L, -1, name);
5813 if (ret == LUA_TNIL) {
5814 /* Entry not found. */
5815 lua_pop(L, 1); /* remove the nil. The "headers" table is the top of the stack. */
5816
5817 /* Insert the new header name in the array in the top of the stack.
5818 * It left the new array in the top of the stack.
5819 */
5820 lua_newtable(L);
5821 lua_pushstring(L, name);
5822 lua_pushvalue(L, -2);
5823 lua_settable(L, -4);
5824 }
5825 else if (ret != LUA_TTABLE) {
5826 hlua_pusherror(L, "Reply['headers']['%s'] is expected to be an array. %s found", name, lua_typename(L, ret));
5827 WILL_LJMP(lua_error(L));
5828 }
5829
5830 /* Now the top od thestack is an array of values. We push
5831 * the header value as new entry.
5832 */
5833 lua_pushstring(L, value);
5834 ret = lua_rawlen(L, -2);
5835 lua_rawseti(L, -2, ret + 1);
5836
5837 lua_pushboolean(L, 1);
5838 return 1;
5839}
5840
5841/* Remove all occurrences of a given header name. */
5842__LJMP static int hlua_txn_reply_del_header(lua_State *L)
5843{
5844 const char *name = MAY_LJMP(luaL_checkstring(L, 2));
5845 int ret;
5846
5847 /* First argument (self) must be a table */
5848 luaL_checktype(L, 1, LUA_TTABLE);
5849
5850 /* Push in the stack the "headers" entry. */
5851 ret = lua_getfield(L, 1, "headers");
5852 if (ret != LUA_TTABLE) {
5853 hlua_pusherror(L, "Reply['headers'] is expected to be an array. %s found", lua_typename(L, ret));
5854 WILL_LJMP(lua_error(L));
5855 }
5856
5857 lua_pushstring(L, name);
5858 lua_pushnil(L);
5859 lua_settable(L, -3);
5860
5861 lua_pushboolean(L, 1);
5862 return 1;
5863}
5864
5865/* Set the reply's body. Overwrite any existing entry. */
5866__LJMP static int hlua_txn_reply_set_body(lua_State *L)
5867{
5868 const char *payload = MAY_LJMP(luaL_checkstring(L, 2));
5869
5870 /* First argument (self) must be a table */
5871 luaL_checktype(L, 1, LUA_TTABLE);
5872
5873 lua_pushstring(L, payload);
5874 lua_setfield(L, 1, "body");
5875
5876 lua_pushboolean(L, 1);
5877 return 1;
5878}
5879
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005880__LJMP static int hlua_log(lua_State *L)
5881{
5882 int level;
5883 const char *msg;
5884
5885 MAY_LJMP(check_args(L, 2, "log"));
5886 level = MAY_LJMP(luaL_checkinteger(L, 1));
5887 msg = MAY_LJMP(luaL_checkstring(L, 2));
5888
5889 if (level < 0 || level >= NB_LOG_LEVELS)
5890 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
5891
5892 hlua_sendlog(NULL, level, msg);
5893 return 0;
5894}
5895
5896__LJMP static int hlua_log_debug(lua_State *L)
5897{
5898 const char *msg;
5899
5900 MAY_LJMP(check_args(L, 1, "debug"));
5901 msg = MAY_LJMP(luaL_checkstring(L, 1));
5902 hlua_sendlog(NULL, LOG_DEBUG, msg);
5903 return 0;
5904}
5905
5906__LJMP static int hlua_log_info(lua_State *L)
5907{
5908 const char *msg;
5909
5910 MAY_LJMP(check_args(L, 1, "info"));
5911 msg = MAY_LJMP(luaL_checkstring(L, 1));
5912 hlua_sendlog(NULL, LOG_INFO, msg);
5913 return 0;
5914}
5915
5916__LJMP static int hlua_log_warning(lua_State *L)
5917{
5918 const char *msg;
5919
5920 MAY_LJMP(check_args(L, 1, "warning"));
5921 msg = MAY_LJMP(luaL_checkstring(L, 1));
5922 hlua_sendlog(NULL, LOG_WARNING, msg);
5923 return 0;
5924}
5925
5926__LJMP static int hlua_log_alert(lua_State *L)
5927{
5928 const char *msg;
5929
5930 MAY_LJMP(check_args(L, 1, "alert"));
5931 msg = MAY_LJMP(luaL_checkstring(L, 1));
5932 hlua_sendlog(NULL, LOG_ALERT, msg);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005933 return 0;
5934}
5935
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005936__LJMP static int hlua_sleep_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005937{
5938 int wakeup_ms = lua_tointeger(L, -1);
5939 if (now_ms < wakeup_ms)
Willy Tarreau9635e032018-10-16 17:52:55 +02005940 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005941 return 0;
5942}
5943
5944__LJMP static int hlua_sleep(lua_State *L)
5945{
5946 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005947 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005948
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005949 MAY_LJMP(check_args(L, 1, "sleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005950
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005951 delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005952 wakeup_ms = tick_add(now_ms, delay);
5953 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005954
Willy Tarreau9635e032018-10-16 17:52:55 +02005955 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005956 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005957}
5958
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005959__LJMP static int hlua_msleep(lua_State *L)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005960{
5961 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005962 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005963
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005964 MAY_LJMP(check_args(L, 1, "msleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005965
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005966 delay = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005967 wakeup_ms = tick_add(now_ms, delay);
5968 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005969
Willy Tarreau9635e032018-10-16 17:52:55 +02005970 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005971 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005972}
5973
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01005974/* This functionis an LUA binding. it permits to give back
5975 * the hand at the HAProxy scheduler. It is used when the
5976 * LUA processing consumes a lot of time.
5977 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005978__LJMP static int hlua_yield_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005979{
5980 return 0;
5981}
5982
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01005983__LJMP static int hlua_yield(lua_State *L)
5984{
Willy Tarreau9635e032018-10-16 17:52:55 +02005985 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_yield_yield, TICK_ETERNITY, HLUA_CTRLYIELD));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005986 return 0;
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01005987}
5988
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005989/* This function change the nice of the currently executed
5990 * task. It is used set low or high priority at the current
5991 * task.
5992 */
Willy Tarreau59551662015-03-10 14:23:13 +01005993__LJMP static int hlua_set_nice(lua_State *L)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005994{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005995 struct hlua *hlua;
5996 int nice;
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005997
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005998 MAY_LJMP(check_args(L, 1, "set_nice"));
5999 hlua = hlua_gethlua(L);
6000 nice = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006001
6002 /* If he task is not set, I'm in a start mode. */
6003 if (!hlua || !hlua->task)
6004 return 0;
6005
6006 if (nice < -1024)
6007 nice = -1024;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006008 else if (nice > 1024)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006009 nice = 1024;
6010
6011 hlua->task->nice = nice;
6012 return 0;
6013}
6014
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006015/* This function is used as a callback of a task. It is called by the
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006016 * HAProxy task subsystem when the task is awaked. The LUA runtime can
6017 * return an E_AGAIN signal, the emmiter of this signal must set a
6018 * signal to wake the task.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006019 *
6020 * Task wrapper are longjmp safe because the only one Lua code
6021 * executed is the safe hlua_ctx_resume();
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006022 */
Willy Tarreau60409db2019-08-21 14:14:50 +02006023struct task *hlua_process_task(struct task *task, void *context, unsigned short state)
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006024{
Olivier Houchard9f6af332018-05-25 14:04:04 +02006025 struct hlua *hlua = context;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006026 enum hlua_exec status;
6027
Christopher Faulet5bc99722018-04-25 10:34:45 +02006028 if (task->thread_mask == MAX_THREADS_MASK)
6029 task_set_affinity(task, tid_bit);
6030
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006031 /* If it is the first call to the task, we must initialize the
6032 * execution timeouts.
6033 */
6034 if (!HLUA_IS_RUNNING(hlua))
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02006035 hlua->max_time = hlua_timeout_task;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006036
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006037 /* Execute the Lua code. */
6038 status = hlua_ctx_resume(hlua, 1);
6039
6040 switch (status) {
6041 /* finished or yield */
6042 case HLUA_E_OK:
6043 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006044 task_destroy(task);
Tim Duesterhuscd235c62018-04-24 13:56:01 +02006045 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006046 break;
6047
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006048 case HLUA_E_AGAIN: /* co process or timeout wake me later. */
Thierry FOURNIERcb146882017-12-10 17:10:57 +01006049 notification_gc(&hlua->com);
PiBa-NLfe971b32018-05-02 22:27:14 +02006050 task->expire = hlua->wake_time;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006051 break;
6052
6053 /* finished with error. */
6054 case HLUA_E_ERRMSG:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006055 SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006056 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006057 task_destroy(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02006058 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006059 break;
6060
6061 case HLUA_E_ERR:
6062 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006063 SEND_ERR(NULL, "Lua task: unknown error.\n");
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006064 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006065 task_destroy(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02006066 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006067 break;
6068 }
Emeric Brun253e53e2017-10-17 18:58:40 +02006069 return task;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006070}
6071
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006072/* This function is an LUA binding that register LUA function to be
6073 * executed after the HAProxy configuration parsing and before the
6074 * HAProxy scheduler starts. This function expect only one LUA
6075 * argument that is a function. This function returns nothing, but
6076 * throws if an error is encountered.
6077 */
6078__LJMP static int hlua_register_init(lua_State *L)
6079{
6080 struct hlua_init_function *init;
6081 int ref;
6082
6083 MAY_LJMP(check_args(L, 1, "register_init"));
6084
6085 ref = MAY_LJMP(hlua_checkfunction(L, 1));
6086
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006087 init = calloc(1, sizeof(*init));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006088 if (!init)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006089 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006090
6091 init->function_ref = ref;
6092 LIST_ADDQ(&hlua_init_functions, &init->l);
6093 return 0;
6094}
6095
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006096/* This functio is an LUA binding. It permits to register a task
6097 * executed in parallel of the main HAroxy activity. The task is
6098 * created and it is set in the HAProxy scheduler. It can be called
6099 * from the "init" section, "post init" or during the runtime.
6100 *
6101 * Lua prototype:
6102 *
6103 * <none> core.register_task(<function>)
6104 */
6105static int hlua_register_task(lua_State *L)
6106{
6107 struct hlua *hlua;
6108 struct task *task;
6109 int ref;
6110
6111 MAY_LJMP(check_args(L, 1, "register_task"));
6112
6113 ref = MAY_LJMP(hlua_checkfunction(L, 1));
6114
Willy Tarreaubafbe012017-11-24 17:34:44 +01006115 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006116 if (!hlua)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006117 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006118
Emeric Brunc60def82017-09-27 14:59:38 +02006119 task = task_new(MAX_THREADS_MASK);
Willy Tarreaue09101e2018-10-16 17:37:12 +02006120 if (!task)
6121 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
6122
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006123 task->context = hlua;
6124 task->process = hlua_process_task;
6125
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006126 if (!hlua_ctx_init(hlua, task, 1))
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006127 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006128
6129 /* Restore the function in the stack. */
6130 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref);
6131 hlua->nargs = 0;
6132
6133 /* Schedule task. */
6134 task_schedule(task, now_ms);
6135
6136 return 0;
6137}
6138
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006139/* Wrapper called by HAProxy to execute an LUA converter. This wrapper
6140 * doesn't allow "yield" functions because the HAProxy engine cannot
6141 * resume converters.
6142 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006143static int hlua_sample_conv_wrapper(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006144{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006145 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006146 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006147 const char *error;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006148
Willy Tarreaube508f12016-03-10 11:47:01 +01006149 if (!stream)
6150 return 0;
6151
Willy Tarreau87b09662015-04-03 00:22:06 +02006152 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006153 * Lua context can be not initialized. This behavior
6154 * permits to save performances because a systematic
6155 * Lua initialization cause 5% performances loss.
6156 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006157 if (!stream->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006158 stream->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006159 if (!stream->hlua) {
6160 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
6161 return 0;
6162 }
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006163 if (!hlua_ctx_init(stream->hlua, stream->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006164 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
6165 return 0;
6166 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006167 }
6168
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006169 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006170 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006171
6172 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006173 if (!SET_SAFE_LJMP(stream->hlua->T)) {
6174 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
6175 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006176 else
6177 error = "critical error";
6178 SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006179 return 0;
6180 }
6181
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006182 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006183 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006184 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006185 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006186 return 0;
6187 }
6188
6189 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006190 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006191
6192 /* convert input sample and pust-it in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006193 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006194 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006195 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006196 return 0;
6197 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006198 hlua_smp2lua(stream->hlua->T, smp);
6199 stream->hlua->nargs = 1;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006200
6201 /* push keywords in the stack. */
6202 if (arg_p) {
6203 for (; arg_p->type != ARGT_STOP; arg_p++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006204 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006205 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006206 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006207 return 0;
6208 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006209 hlua_arg2lua(stream->hlua->T, arg_p);
6210 stream->hlua->nargs++;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006211 }
6212 }
6213
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006214 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006215 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006216
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006217 /* At this point the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006218 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006219 }
6220
6221 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006222 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006223 /* finished. */
6224 case HLUA_E_OK:
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02006225 /* If the stack is empty, the function fails. */
6226 if (lua_gettop(stream->hlua->T) <= 0)
6227 return 0;
6228
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006229 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006230 hlua_lua2smp(stream->hlua->T, -1, smp);
6231 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006232 return 1;
6233
6234 /* yield. */
6235 case HLUA_E_AGAIN:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006236 SEND_ERR(stream->be, "Lua converter '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006237 return 0;
6238
6239 /* finished with error. */
6240 case HLUA_E_ERRMSG:
6241 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006242 SEND_ERR(stream->be, "Lua converter '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006243 fcn->name, lua_tostring(stream->hlua->T, -1));
6244 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006245 return 0;
6246
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006247 case HLUA_E_ETMOUT:
6248 SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name);
6249 return 0;
6250
6251 case HLUA_E_NOMEM:
6252 SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name);
6253 return 0;
6254
6255 case HLUA_E_YIELD:
6256 SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name);
6257 return 0;
6258
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006259 case HLUA_E_ERR:
6260 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006261 SEND_ERR(stream->be, "Lua converter '%s' returns an unknown error.\n", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006262
6263 default:
6264 return 0;
6265 }
6266}
6267
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006268/* Wrapper called by HAProxy to execute a sample-fetch. this wrapper
6269 * doesn't allow "yield" functions because the HAProxy engine cannot
Willy Tarreaube508f12016-03-10 11:47:01 +01006270 * resume sample-fetches. This function will be called by the sample
6271 * fetch engine to call lua-based fetch operations.
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006272 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +02006273static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp,
6274 const char *kw, void *private)
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006275{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006276 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006277 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006278 const char *error;
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006279 unsigned int hflags = HLUA_TXN_NOTERM;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006280
Willy Tarreaube508f12016-03-10 11:47:01 +01006281 if (!stream)
6282 return 0;
Christopher Fauletafd8f102018-11-08 11:34:21 +01006283
Willy Tarreau87b09662015-04-03 00:22:06 +02006284 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006285 * Lua context can be not initialized. This behavior
6286 * permits to save performances because a systematic
6287 * Lua initialization cause 5% performances loss.
6288 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006289 if (!stream->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006290 stream->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006291 if (!stream->hlua) {
6292 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
6293 return 0;
6294 }
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006295 if (!hlua_ctx_init(stream->hlua, stream->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006296 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
6297 return 0;
6298 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006299 }
6300
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006301 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006302 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006303
6304 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006305 if (!SET_SAFE_LJMP(stream->hlua->T)) {
6306 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
6307 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006308 else
6309 error = "critical error";
6310 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006311 return 0;
6312 }
6313
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006314 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006315 if (!lua_checkstack(stream->hlua->T, 2)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006316 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006317 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006318 return 0;
6319 }
6320
6321 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006322 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006323
6324 /* push arguments in the stack. */
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006325 if (!hlua_txn_new(stream->hlua->T, stream, smp->px, smp->opt & SMP_OPT_DIR, hflags)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006326 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006327 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006328 return 0;
6329 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006330 stream->hlua->nargs = 1;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006331
6332 /* push keywords in the stack. */
6333 for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) {
6334 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006335 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006336 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006337 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006338 return 0;
6339 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006340 hlua_arg2lua(stream->hlua->T, arg_p);
6341 stream->hlua->nargs++;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006342 }
6343
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006344 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006345 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006346
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006347 /* At this point the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006348 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006349 }
6350
6351 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006352 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006353 /* finished. */
6354 case HLUA_E_OK:
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02006355 /* If the stack is empty, the function fails. */
6356 if (lua_gettop(stream->hlua->T) <= 0)
6357 return 0;
6358
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006359 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006360 hlua_lua2smp(stream->hlua->T, -1, smp);
6361 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006362
6363 /* Set the end of execution flag. */
6364 smp->flags &= ~SMP_F_MAY_CHANGE;
6365 return 1;
6366
6367 /* yield. */
6368 case HLUA_E_AGAIN:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006369 SEND_ERR(smp->px, "Lua sample-fetch '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006370 return 0;
6371
6372 /* finished with error. */
6373 case HLUA_E_ERRMSG:
6374 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006375 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006376 fcn->name, lua_tostring(stream->hlua->T, -1));
6377 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006378 return 0;
6379
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006380 case HLUA_E_ETMOUT:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006381 SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name);
6382 return 0;
6383
6384 case HLUA_E_NOMEM:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006385 SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name);
6386 return 0;
6387
6388 case HLUA_E_YIELD:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006389 SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name);
6390 return 0;
6391
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006392 case HLUA_E_ERR:
6393 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006394 SEND_ERR(smp->px, "Lua sample-fetch '%s' returns an unknown error.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006395
6396 default:
6397 return 0;
6398 }
6399}
6400
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006401/* This function is an LUA binding used for registering
6402 * "sample-conv" functions. It expects a converter name used
6403 * in the haproxy configuration file, and an LUA function.
6404 */
6405__LJMP static int hlua_register_converters(lua_State *L)
6406{
6407 struct sample_conv_kw_list *sck;
6408 const char *name;
6409 int ref;
6410 int len;
6411 struct hlua_function *fcn;
6412
6413 MAY_LJMP(check_args(L, 2, "register_converters"));
6414
6415 /* First argument : converter name. */
6416 name = MAY_LJMP(luaL_checkstring(L, 1));
6417
6418 /* Second argument : lua function. */
6419 ref = MAY_LJMP(hlua_checkfunction(L, 2));
6420
6421 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006422 sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006423 if (!sck)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006424 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006425 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006426 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006427 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006428
6429 /* Fill fcn. */
6430 fcn->name = strdup(name);
6431 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006432 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006433 fcn->function_ref = ref;
6434
6435 /* List head */
6436 sck->list.n = sck->list.p = NULL;
6437
6438 /* converter keyword. */
6439 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006440 sck->kw[0].kw = calloc(1, len);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006441 if (!sck->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006442 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006443
6444 snprintf((char *)sck->kw[0].kw, len, "lua.%s", name);
6445 sck->kw[0].process = hlua_sample_conv_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01006446 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 +01006447 sck->kw[0].val_args = NULL;
6448 sck->kw[0].in_type = SMP_T_STR;
6449 sck->kw[0].out_type = SMP_T_STR;
6450 sck->kw[0].private = fcn;
6451
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006452 /* Register this new converter */
6453 sample_register_convs(sck);
6454
6455 return 0;
6456}
6457
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006458/* This function is an LUA binding used for registering
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006459 * "sample-fetch" functions. It expects a converter name used
6460 * in the haproxy configuration file, and an LUA function.
6461 */
6462__LJMP static int hlua_register_fetches(lua_State *L)
6463{
6464 const char *name;
6465 int ref;
6466 int len;
6467 struct sample_fetch_kw_list *sfk;
6468 struct hlua_function *fcn;
6469
6470 MAY_LJMP(check_args(L, 2, "register_fetches"));
6471
6472 /* First argument : sample-fetch name. */
6473 name = MAY_LJMP(luaL_checkstring(L, 1));
6474
6475 /* Second argument : lua function. */
6476 ref = MAY_LJMP(hlua_checkfunction(L, 2));
6477
6478 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006479 sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006480 if (!sfk)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006481 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006482 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006483 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006484 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006485
6486 /* Fill fcn. */
6487 fcn->name = strdup(name);
6488 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006489 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006490 fcn->function_ref = ref;
6491
6492 /* List head */
6493 sfk->list.n = sfk->list.p = NULL;
6494
6495 /* sample-fetch keyword. */
6496 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006497 sfk->kw[0].kw = calloc(1, len);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006498 if (!sfk->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006499 return luaL_error(L, "Lua out of memory error.");
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006500
6501 snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name);
6502 sfk->kw[0].process = hlua_sample_fetch_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01006503 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 +01006504 sfk->kw[0].val_args = NULL;
6505 sfk->kw[0].out_type = SMP_T_STR;
6506 sfk->kw[0].use = SMP_USE_HTTP_ANY;
6507 sfk->kw[0].val = 0;
6508 sfk->kw[0].private = fcn;
6509
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006510 /* Register this new fetch. */
6511 sample_register_fetches(sfk);
6512
6513 return 0;
6514}
6515
Christopher Faulet501465d2020-02-26 14:54:16 +01006516/* This function is a lua binding to set the wake_time.
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006517 */
Christopher Faulet501465d2020-02-26 14:54:16 +01006518__LJMP static int hlua_set_wake_time(lua_State *L)
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006519{
6520 struct hlua *hlua = hlua_gethlua(L);
6521 unsigned int delay;
6522 unsigned int wakeup_ms;
6523
6524 MAY_LJMP(check_args(L, 1, "wake_time"));
6525
6526 delay = MAY_LJMP(luaL_checkinteger(L, 1));
6527 wakeup_ms = tick_add(now_ms, delay);
6528 hlua->wake_time = wakeup_ms;
6529 return 0;
6530}
6531
6532
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006533/* This function is a wrapper to execute each LUA function declared as an action
6534 * wrapper during the initialisation period. This function may return any
6535 * ACT_RET_* value. On error ACT_RET_CONT is returned and the action is
6536 * ignored. If the lua action yields, ACT_RET_YIELD is returned. On success, the
6537 * return value is the first element on the stack.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006538 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006539static enum act_return hlua_action(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +02006540 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006541{
6542 char **arg;
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006543 unsigned int hflags = 0;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006544 int dir, act_ret = ACT_RET_CONT;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006545 const char *error;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006546
6547 switch (rule->from) {
Christopher Fauletd8f0e072020-02-25 09:45:51 +01006548 case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break;
6549 case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break;
6550 case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break;
6551 case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006552 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006553 SEND_ERR(px, "Lua: internal error while execute action.\n");
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006554 goto end;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006555 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006556
Willy Tarreau87b09662015-04-03 00:22:06 +02006557 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006558 * Lua context can be not initialized. This behavior
6559 * permits to save performances because a systematic
6560 * Lua initialization cause 5% performances loss.
6561 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006562 if (!s->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006563 s->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006564 if (!s->hlua) {
6565 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
6566 rule->arg.hlua_rule->fcn.name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006567 goto end;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006568 }
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006569 if (!hlua_ctx_init(s->hlua, s->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006570 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
6571 rule->arg.hlua_rule->fcn.name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006572 goto end;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006573 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006574 }
6575
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006576 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006577 if (!HLUA_IS_RUNNING(s->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006578
6579 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006580 if (!SET_SAFE_LJMP(s->hlua->T)) {
6581 if (lua_type(s->hlua->T, -1) == LUA_TSTRING)
6582 error = lua_tostring(s->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006583 else
6584 error = "critical error";
6585 SEND_ERR(px, "Lua function '%s': %s.\n",
6586 rule->arg.hlua_rule->fcn.name, error);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006587 goto end;
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006588 }
6589
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006590 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006591 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006592 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006593 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006594 RESET_SAFE_LJMP(s->hlua->T);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006595 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006596 }
6597
6598 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006599 lua_rawgeti(s->hlua->T, LUA_REGISTRYINDEX, rule->arg.hlua_rule->fcn.function_ref);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006600
Willy Tarreau87b09662015-04-03 00:22:06 +02006601 /* Create and and push object stream in the stack. */
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006602 if (!hlua_txn_new(s->hlua->T, s, px, dir, hflags)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006603 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006604 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006605 RESET_SAFE_LJMP(s->hlua->T);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006606 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006607 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006608 s->hlua->nargs = 1;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006609
6610 /* push keywords in the stack. */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006611 for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006612 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006613 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006614 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006615 RESET_SAFE_LJMP(s->hlua->T);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006616 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006617 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006618 lua_pushstring(s->hlua->T, *arg);
6619 s->hlua->nargs++;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006620 }
6621
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006622 /* Now the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006623 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006624
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006625 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006626 s->hlua->max_time = hlua_timeout_session;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006627 }
6628
Christopher Faulet23308eb2020-06-02 18:46:07 +02006629 /* Always reset the analyse expiration timeout for the corresponding
6630 * channel in case the lua script yield, to be sure to not keep an
6631 * expired timeout.
6632 */
6633 if (dir == SMP_OPT_DIR_REQ)
6634 s->req.analyse_exp = TICK_ETERNITY;
6635 else
6636 s->res.analyse_exp = TICK_ETERNITY;
6637
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006638 /* Execute the function. */
Christopher Faulet105ba6c2019-12-18 14:41:51 +01006639 switch (hlua_ctx_resume(s->hlua, !(flags & ACT_OPT_FINAL))) {
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006640 /* finished. */
6641 case HLUA_E_OK:
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006642 /* Catch the return value */
6643 if (lua_gettop(s->hlua->T) > 0)
6644 act_ret = lua_tointeger(s->hlua->T, -1);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006645
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006646 /* Set timeout in the required channel. */
6647 if (act_ret == ACT_RET_YIELD && s->hlua->wake_time != TICK_ETERNITY) {
6648 if (dir == SMP_OPT_DIR_REQ)
6649 s->req.analyse_exp = s->hlua->wake_time;
6650 else
6651 s->res.analyse_exp = s->hlua->wake_time;
6652 }
6653 goto end;
6654
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006655 /* yield. */
6656 case HLUA_E_AGAIN:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006657 /* Set timeout in the required channel. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006658 if (s->hlua->wake_time != TICK_ETERNITY) {
Christopher Faulet81921b12019-08-14 23:19:45 +02006659 if (dir == SMP_OPT_DIR_REQ)
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006660 s->req.analyse_exp = s->hlua->wake_time;
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006661 else
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006662 s->res.analyse_exp = s->hlua->wake_time;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006663 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006664 /* Some actions can be wake up when a "write" event
6665 * is detected on a response channel. This is useful
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006666 * only for actions targeted on the requests.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006667 */
Christopher Faulet51fa3582019-07-26 14:54:52 +02006668 if (HLUA_IS_WAKERESWR(s->hlua))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006669 s->res.flags |= CF_WAKE_WRITE;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006670 if (HLUA_IS_WAKEREQWR(s->hlua))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006671 s->req.flags |= CF_WAKE_WRITE;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006672 act_ret = ACT_RET_YIELD;
6673 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006674
6675 /* finished with error. */
6676 case HLUA_E_ERRMSG:
6677 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006678 SEND_ERR(px, "Lua function '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006679 rule->arg.hlua_rule->fcn.name, lua_tostring(s->hlua->T, -1));
6680 lua_pop(s->hlua->T, 1);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006681 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006682
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006683 case HLUA_E_ETMOUT:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006684 SEND_ERR(px, "Lua function '%s': execution timeout.\n", rule->arg.hlua_rule->fcn.name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006685 goto end;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006686
6687 case HLUA_E_NOMEM:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006688 SEND_ERR(px, "Lua function '%s': out of memory error.\n", rule->arg.hlua_rule->fcn.name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006689 goto end;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006690
6691 case HLUA_E_YIELD:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006692 SEND_ERR(px, "Lua function '%s': aborting Lua processing on expired timeout.\n",
6693 rule->arg.hlua_rule->fcn.name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006694 goto end;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006695
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006696 case HLUA_E_ERR:
6697 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006698 SEND_ERR(px, "Lua function '%s' return an unknown error.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006699 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006700
6701 default:
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006702 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006703 }
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006704
6705 end:
6706 return act_ret;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006707}
6708
Olivier Houchard9f6af332018-05-25 14:04:04 +02006709struct task *hlua_applet_wakeup(struct task *t, void *context, unsigned short state)
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006710{
Olivier Houchard9f6af332018-05-25 14:04:04 +02006711 struct appctx *ctx = context;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006712
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006713 appctx_wakeup(ctx);
Willy Tarreaud9587412017-08-23 16:07:33 +02006714 t->expire = TICK_ETERNITY;
Willy Tarreaud1aa41f2017-07-21 16:41:56 +02006715 return t;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006716}
6717
6718static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
6719{
6720 struct stream_interface *si = ctx->owner;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006721 struct hlua *hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006722 struct task *task;
6723 char **arg;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006724 const char *error;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006725
Willy Tarreaubafbe012017-11-24 17:34:44 +01006726 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006727 if (!hlua) {
6728 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
6729 ctx->rule->arg.hlua_rule->fcn.name);
6730 return 0;
6731 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006732 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006733 ctx->ctx.hlua_apptcp.hlua = hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006734 ctx->ctx.hlua_apptcp.flags = 0;
6735
6736 /* Create task used by signal to wakeup applets. */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01006737 task = task_new(tid_bit);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006738 if (!task) {
6739 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
6740 ctx->rule->arg.hlua_rule->fcn.name);
6741 return 0;
6742 }
6743 task->nice = 0;
6744 task->context = ctx;
6745 task->process = hlua_applet_wakeup;
6746 ctx->ctx.hlua_apptcp.task = task;
6747
6748 /* In the execution wrappers linked with a stream, the
6749 * Lua context can be not initialized. This behavior
6750 * permits to save performances because a systematic
6751 * Lua initialization cause 5% performances loss.
6752 */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006753 if (!hlua_ctx_init(hlua, task, 0)) {
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006754 SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n",
6755 ctx->rule->arg.hlua_rule->fcn.name);
6756 return 0;
6757 }
6758
6759 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02006760 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006761
6762 /* The following Lua calls can fail. */
6763 if (!SET_SAFE_LJMP(hlua->T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01006764 if (lua_type(hlua->T, -1) == LUA_TSTRING)
6765 error = lua_tostring(hlua->T, -1);
6766 else
6767 error = "critical error";
6768 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
6769 ctx->rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006770 return 0;
6771 }
6772
6773 /* Check stack available size. */
6774 if (!lua_checkstack(hlua->T, 1)) {
6775 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
6776 ctx->rule->arg.hlua_rule->fcn.name);
6777 RESET_SAFE_LJMP(hlua->T);
6778 return 0;
6779 }
6780
6781 /* Restore the function in the stack. */
6782 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref);
6783
6784 /* Create and and push object stream in the stack. */
6785 if (!hlua_applet_tcp_new(hlua->T, ctx)) {
6786 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
6787 ctx->rule->arg.hlua_rule->fcn.name);
6788 RESET_SAFE_LJMP(hlua->T);
6789 return 0;
6790 }
6791 hlua->nargs = 1;
6792
6793 /* push keywords in the stack. */
6794 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
6795 if (!lua_checkstack(hlua->T, 1)) {
6796 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
6797 ctx->rule->arg.hlua_rule->fcn.name);
6798 RESET_SAFE_LJMP(hlua->T);
6799 return 0;
6800 }
6801 lua_pushstring(hlua->T, *arg);
6802 hlua->nargs++;
6803 }
6804
6805 RESET_SAFE_LJMP(hlua->T);
6806
6807 /* Wakeup the applet ASAP. */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01006808 si_cant_get(si);
Willy Tarreau3367d412018-11-15 10:57:41 +01006809 si_rx_endp_more(si);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006810
6811 return 1;
6812}
6813
Willy Tarreau60409db2019-08-21 14:14:50 +02006814void hlua_applet_tcp_fct(struct appctx *ctx)
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006815{
6816 struct stream_interface *si = ctx->owner;
6817 struct stream *strm = si_strm(si);
6818 struct channel *res = si_ic(si);
6819 struct act_rule *rule = ctx->rule;
6820 struct proxy *px = strm->be;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006821 struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006822
6823 /* The applet execution is already done. */
Olivier Houchard594c8c52018-08-28 14:41:31 +02006824 if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) {
6825 /* eat the whole request */
6826 co_skip(si_oc(si), co_data(si_oc(si)));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006827 return;
Olivier Houchard594c8c52018-08-28 14:41:31 +02006828 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006829
6830 /* If the stream is disconnect or closed, ldo nothing. */
6831 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
6832 return;
6833
6834 /* Execute the function. */
6835 switch (hlua_ctx_resume(hlua, 1)) {
6836 /* finished. */
6837 case HLUA_E_OK:
6838 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
6839
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006840 /* eat the whole request */
Willy Tarreaua79021a2018-06-15 18:07:57 +02006841 co_skip(si_oc(si), co_data(si_oc(si)));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006842 res->flags |= CF_READ_NULL;
6843 si_shutr(si);
6844 return;
6845
6846 /* yield. */
6847 case HLUA_E_AGAIN:
Thierry Fournier0164f202016-02-20 17:47:43 +01006848 if (hlua->wake_time != TICK_ETERNITY)
6849 task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006850 return;
6851
6852 /* finished with error. */
6853 case HLUA_E_ERRMSG:
6854 /* Display log. */
6855 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
6856 rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1));
6857 lua_pop(hlua->T, 1);
6858 goto error;
6859
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006860 case HLUA_E_ETMOUT:
6861 SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n",
6862 rule->arg.hlua_rule->fcn.name);
6863 goto error;
6864
6865 case HLUA_E_NOMEM:
6866 SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n",
6867 rule->arg.hlua_rule->fcn.name);
6868 goto error;
6869
6870 case HLUA_E_YIELD: /* unexpected */
6871 SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n",
6872 rule->arg.hlua_rule->fcn.name);
6873 goto error;
6874
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006875 case HLUA_E_ERR:
6876 /* Display log. */
6877 SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n",
6878 rule->arg.hlua_rule->fcn.name);
6879 goto error;
6880
6881 default:
6882 goto error;
6883 }
6884
6885error:
6886
6887 /* For all other cases, just close the stream. */
6888 si_shutw(si);
6889 si_shutr(si);
6890 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
6891}
6892
6893static void hlua_applet_tcp_release(struct appctx *ctx)
6894{
Olivier Houchard3f795f72019-04-17 22:51:06 +02006895 task_destroy(ctx->ctx.hlua_apptcp.task);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006896 ctx->ctx.hlua_apptcp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006897 hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006898 ctx->ctx.hlua_apptcp.hlua = NULL;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006899}
6900
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006901/* The function returns 1 if the initialisation is complete, 0 if
6902 * an errors occurs and -1 if more data are required for initializing
6903 * the applet.
6904 */
6905static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
6906{
6907 struct stream_interface *si = ctx->owner;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006908 struct http_txn *txn;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006909 struct hlua *hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006910 char **arg;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006911 struct task *task;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006912 const char *error;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006913
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006914 txn = strm->txn;
Willy Tarreaubafbe012017-11-24 17:34:44 +01006915 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006916 if (!hlua) {
6917 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
6918 ctx->rule->arg.hlua_rule->fcn.name);
6919 return 0;
6920 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006921 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006922 ctx->ctx.hlua_apphttp.hlua = hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006923 ctx->ctx.hlua_apphttp.left_bytes = -1;
6924 ctx->ctx.hlua_apphttp.flags = 0;
6925
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +01006926 if (txn->req.flags & HTTP_MSGF_VER_11)
6927 ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11;
6928
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006929 /* Create task used by signal to wakeup applets. */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01006930 task = task_new(tid_bit);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006931 if (!task) {
6932 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
6933 ctx->rule->arg.hlua_rule->fcn.name);
6934 return 0;
6935 }
6936 task->nice = 0;
6937 task->context = ctx;
6938 task->process = hlua_applet_wakeup;
6939 ctx->ctx.hlua_apphttp.task = task;
6940
6941 /* In the execution wrappers linked with a stream, the
6942 * Lua context can be not initialized. This behavior
6943 * permits to save performances because a systematic
6944 * Lua initialization cause 5% performances loss.
6945 */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006946 if (!hlua_ctx_init(hlua, task, 0)) {
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006947 SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n",
6948 ctx->rule->arg.hlua_rule->fcn.name);
6949 return 0;
6950 }
6951
6952 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02006953 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006954
6955 /* The following Lua calls can fail. */
6956 if (!SET_SAFE_LJMP(hlua->T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01006957 if (lua_type(hlua->T, -1) == LUA_TSTRING)
6958 error = lua_tostring(hlua->T, -1);
6959 else
6960 error = "critical error";
6961 SEND_ERR(px, "Lua applet http '%s': %s.\n",
6962 ctx->rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006963 return 0;
6964 }
6965
6966 /* Check stack available size. */
6967 if (!lua_checkstack(hlua->T, 1)) {
6968 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
6969 ctx->rule->arg.hlua_rule->fcn.name);
6970 RESET_SAFE_LJMP(hlua->T);
6971 return 0;
6972 }
6973
6974 /* Restore the function in the stack. */
6975 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref);
6976
6977 /* Create and and push object stream in the stack. */
6978 if (!hlua_applet_http_new(hlua->T, ctx)) {
6979 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
6980 ctx->rule->arg.hlua_rule->fcn.name);
6981 RESET_SAFE_LJMP(hlua->T);
6982 return 0;
6983 }
6984 hlua->nargs = 1;
6985
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006986 /* push keywords in the stack. */
6987 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
6988 if (!lua_checkstack(hlua->T, 1)) {
6989 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
6990 ctx->rule->arg.hlua_rule->fcn.name);
6991 RESET_SAFE_LJMP(hlua->T);
6992 return 0;
6993 }
6994 lua_pushstring(hlua->T, *arg);
6995 hlua->nargs++;
6996 }
6997
6998 RESET_SAFE_LJMP(hlua->T);
6999
7000 /* Wakeup the applet when data is ready for read. */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01007001 si_cant_get(si);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007002
7003 return 1;
7004}
7005
Willy Tarreau60409db2019-08-21 14:14:50 +02007006void hlua_applet_http_fct(struct appctx *ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007007{
7008 struct stream_interface *si = ctx->owner;
7009 struct stream *strm = si_strm(si);
7010 struct channel *req = si_oc(si);
7011 struct channel *res = si_ic(si);
7012 struct act_rule *rule = ctx->rule;
7013 struct proxy *px = strm->be;
7014 struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua;
7015 struct htx *req_htx, *res_htx;
7016
7017 res_htx = htx_from_buf(&res->buf);
7018
7019 /* If the stream is disconnect or closed, ldo nothing. */
7020 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
7021 goto out;
7022
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007023 /* Check if the input buffer is available. */
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007024 if (!b_size(&res->buf)) {
7025 si_rx_room_blk(si);
7026 goto out;
7027 }
7028 /* check that the output is not closed */
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007029 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007030 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7031
7032 /* Set the currently running flag. */
7033 if (!HLUA_IS_RUNNING(hlua) &&
7034 !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
7035 struct htx_blk *blk;
7036 size_t count = co_data(req);
7037
7038 if (!count) {
7039 si_cant_get(si);
7040 goto out;
7041 }
7042
7043 /* We need to flush the request header. This left the body for
7044 * the Lua.
7045 */
7046 req_htx = htx_from_buf(&req->buf);
Christopher Fauleta3f15502019-05-13 15:27:23 +02007047 blk = htx_get_first_blk(req_htx);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007048 while (count && blk) {
7049 enum htx_blk_type type = htx_get_blk_type(blk);
7050 uint32_t sz = htx_get_blksz(blk);
7051
7052 if (sz > count) {
7053 si_cant_get(si);
Christopher Faulet0ae79d02019-02-27 21:36:59 +01007054 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007055 goto out;
7056 }
7057
7058 count -= sz;
7059 co_set_data(req, co_data(req) - sz);
7060 blk = htx_remove_blk(req_htx, blk);
7061
7062 if (type == HTX_BLK_EOH)
7063 break;
7064 }
Christopher Faulet0ae79d02019-02-27 21:36:59 +01007065 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007066 }
7067
7068 /* Executes The applet if it is not done. */
7069 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
7070
7071 /* Execute the function. */
7072 switch (hlua_ctx_resume(hlua, 1)) {
7073 /* finished. */
7074 case HLUA_E_OK:
7075 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7076 break;
7077
7078 /* yield. */
7079 case HLUA_E_AGAIN:
7080 if (hlua->wake_time != TICK_ETERNITY)
7081 task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time);
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007082 goto out;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007083
7084 /* finished with error. */
7085 case HLUA_E_ERRMSG:
7086 /* Display log. */
7087 SEND_ERR(px, "Lua applet http '%s': %s.\n",
7088 rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1));
7089 lua_pop(hlua->T, 1);
7090 goto error;
7091
7092 case HLUA_E_ETMOUT:
7093 SEND_ERR(px, "Lua applet http '%s': execution timeout.\n",
7094 rule->arg.hlua_rule->fcn.name);
7095 goto error;
7096
7097 case HLUA_E_NOMEM:
7098 SEND_ERR(px, "Lua applet http '%s': out of memory error.\n",
7099 rule->arg.hlua_rule->fcn.name);
7100 goto error;
7101
7102 case HLUA_E_YIELD: /* unexpected */
7103 SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n",
7104 rule->arg.hlua_rule->fcn.name);
7105 goto error;
7106
7107 case HLUA_E_ERR:
7108 /* Display log. */
7109 SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n",
7110 rule->arg.hlua_rule->fcn.name);
7111 goto error;
7112
7113 default:
7114 goto error;
7115 }
7116 }
7117
7118 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007119 if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT)
7120 goto done;
7121
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007122 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT))
7123 goto error;
7124
Christopher Faulet54b5e212019-06-04 10:08:28 +02007125 /* Don't add TLR because mux-h1 will take care of it */
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007126 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
7127 si_rx_room_blk(si);
7128 goto out;
7129 }
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01007130 channel_add_input(res, 1);
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007131 strm->txn->status = ctx->ctx.hlua_apphttp.status;
7132 ctx->ctx.hlua_apphttp.flags |= APPLET_RSP_SENT;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007133 }
7134
7135 done:
7136 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007137 if (!(res->flags & CF_SHUTR)) {
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007138 res->flags |= CF_READ_NULL;
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007139 si_shutr(si);
7140 }
7141
7142 /* eat the whole request */
7143 if (co_data(req)) {
7144 req_htx = htx_from_buf(&req->buf);
7145 co_htx_skip(req, req_htx, co_data(req));
7146 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007147 }
7148 }
7149
7150 out:
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007151 htx_to_buf(res_htx, &res->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007152 return;
7153
7154 error:
7155
7156 /* If we are in HTTP mode, and we are not send any
7157 * data, return a 500 server error in best effort:
7158 * if there is no room available in the buffer,
7159 * just close the connection.
7160 */
7161 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
Christopher Fauletf7346382019-07-17 22:02:08 +02007162 struct buffer *err = &http_err_chunks[HTTP_ERR_500];
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007163
7164 channel_erase(res);
7165 res->buf.data = b_data(err);
7166 memcpy(res->buf.area, b_head(err), b_data(err));
7167 res_htx = htx_from_buf(&res->buf);
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01007168 channel_add_input(res, res_htx->data);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007169 }
7170 if (!(strm->flags & SF_ERR_MASK))
7171 strm->flags |= SF_ERR_RESOURCE;
7172 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7173 goto done;
7174}
7175
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007176static void hlua_applet_http_release(struct appctx *ctx)
7177{
Olivier Houchard3f795f72019-04-17 22:51:06 +02007178 task_destroy(ctx->ctx.hlua_apphttp.task);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007179 ctx->ctx.hlua_apphttp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007180 hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007181 ctx->ctx.hlua_apphttp.hlua = NULL;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007182}
7183
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007184/* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007185 * success case, else return ACT_RET_PRS_ERR.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007186 *
7187 * This function can fail with an abort() due to an Lua critical error.
7188 * We are in the configuration parsing process of HAProxy, this abort() is
7189 * tolerated.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007190 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007191static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px,
7192 struct act_rule *rule, char **err)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007193{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007194 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007195 int i;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007196
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007197 /* Memory for the rule. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007198 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007199 if (!rule->arg.hlua_rule) {
7200 memprintf(err, "out of memory error");
Thierry FOURNIERafa80492015-08-19 09:04:15 +02007201 return ACT_RET_PRS_ERR;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007202 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007203
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007204 /* Memory for arguments. */
7205 rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, sizeof(char *));
7206 if (!rule->arg.hlua_rule->args) {
7207 memprintf(err, "out of memory error");
7208 return ACT_RET_PRS_ERR;
7209 }
7210
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007211 /* Reference the Lua function and store the reference. */
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007212 rule->arg.hlua_rule->fcn = *fcn;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007213
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007214 /* Expect some arguments */
7215 for (i = 0; i < fcn->nargs; i++) {
Thierry FOURNIER1725c2e2019-01-06 19:38:49 +01007216 if (*args[*cur_arg] == '\0') {
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007217 memprintf(err, "expect %d arguments", fcn->nargs);
7218 return ACT_RET_PRS_ERR;
7219 }
Thierry FOURNIER1725c2e2019-01-06 19:38:49 +01007220 rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]);
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007221 if (!rule->arg.hlua_rule->args[i]) {
7222 memprintf(err, "out of memory error");
7223 return ACT_RET_PRS_ERR;
7224 }
7225 (*cur_arg)++;
7226 }
7227 rule->arg.hlua_rule->args[i] = NULL;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007228
Thierry FOURNIER42148732015-09-02 17:17:33 +02007229 rule->action = ACT_CUSTOM;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007230 rule->action_ptr = hlua_action;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02007231 return ACT_RET_PRS_OK;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007232}
7233
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007234static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px,
7235 struct act_rule *rule, char **err)
7236{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007237 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007238
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01007239 /* HTTP applets are forbidden in tcp-request rules.
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007240 * HTTP applet request requires everything initialized by
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01007241 * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER).
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007242 * The applet will be immediately initialized, but its before
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01007243 * the call of this analyzer.
7244 */
7245 if (rule->from != ACT_F_HTTP_REQ) {
7246 memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets");
7247 return ACT_RET_PRS_ERR;
7248 }
7249
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007250 /* Memory for the rule. */
7251 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
7252 if (!rule->arg.hlua_rule) {
7253 memprintf(err, "out of memory error");
7254 return ACT_RET_PRS_ERR;
7255 }
7256
7257 /* Reference the Lua function and store the reference. */
7258 rule->arg.hlua_rule->fcn = *fcn;
7259
7260 /* TODO: later accept arguments. */
7261 rule->arg.hlua_rule->args = NULL;
7262
7263 /* Add applet pointer in the rule. */
7264 rule->applet.obj_type = OBJ_TYPE_APPLET;
7265 rule->applet.name = fcn->name;
7266 rule->applet.init = hlua_applet_http_init;
7267 rule->applet.fct = hlua_applet_http_fct;
7268 rule->applet.release = hlua_applet_http_release;
7269 rule->applet.timeout = hlua_timeout_applet;
7270
7271 return ACT_RET_PRS_OK;
7272}
7273
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007274/* This function is an LUA binding used for registering
7275 * "sample-conv" functions. It expects a converter name used
7276 * in the haproxy configuration file, and an LUA function.
7277 */
7278__LJMP static int hlua_register_action(lua_State *L)
7279{
7280 struct action_kw_list *akl;
7281 const char *name;
7282 int ref;
7283 int len;
7284 struct hlua_function *fcn;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007285 int nargs;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007286
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007287 /* Initialise the number of expected arguments at 0. */
7288 nargs = 0;
7289
7290 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
7291 WILL_LJMP(luaL_error(L, "'register_action' needs between 3 and 4 arguments"));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007292
7293 /* First argument : converter name. */
7294 name = MAY_LJMP(luaL_checkstring(L, 1));
7295
7296 /* Second argument : environment. */
7297 if (lua_type(L, 2) != LUA_TTABLE)
7298 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
7299
7300 /* Third argument : lua function. */
7301 ref = MAY_LJMP(hlua_checkfunction(L, 3));
7302
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007303 /* Fourth argument : number of mandatory arguments expected on the configuration line. */
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007304 if (lua_gettop(L) >= 4)
7305 nargs = MAY_LJMP(luaL_checkinteger(L, 4));
7306
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007307 /* browse the second argument as an array. */
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007308 lua_pushnil(L);
7309 while (lua_next(L, 2) != 0) {
7310 if (lua_type(L, -1) != LUA_TSTRING)
7311 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
7312
7313 /* Check required environment. Only accepted "http" or "tcp". */
7314 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007315 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007316 if (!akl)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007317 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007318 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007319 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007320 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007321
7322 /* Fill fcn. */
7323 fcn->name = strdup(name);
7324 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007325 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007326 fcn->function_ref = ref;
7327
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007328 /* Set the expected number od arguments. */
7329 fcn->nargs = nargs;
7330
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007331 /* List head */
7332 akl->list.n = akl->list.p = NULL;
7333
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007334 /* action keyword. */
7335 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007336 akl->kw[0].kw = calloc(1, len);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007337 if (!akl->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007338 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007339
7340 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
7341
7342 akl->kw[0].match_pfx = 0;
7343 akl->kw[0].private = fcn;
7344 akl->kw[0].parse = action_register_lua;
7345
7346 /* select the action registering point. */
7347 if (strcmp(lua_tostring(L, -1), "tcp-req") == 0)
7348 tcp_req_cont_keywords_register(akl);
7349 else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0)
7350 tcp_res_cont_keywords_register(akl);
7351 else if (strcmp(lua_tostring(L, -1), "http-req") == 0)
7352 http_req_keywords_register(akl);
7353 else if (strcmp(lua_tostring(L, -1), "http-res") == 0)
7354 http_res_keywords_register(akl);
7355 else
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007356 WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. "
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007357 "'tcp-req', 'tcp-res', 'http-req' or 'http-res' "
7358 "are expected.", lua_tostring(L, -1)));
7359
7360 /* pop the environment string. */
7361 lua_pop(L, 1);
7362 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007363 return ACT_RET_PRS_OK;
7364}
7365
7366static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px,
7367 struct act_rule *rule, char **err)
7368{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007369 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007370
Christopher Faulet280f85b2019-07-15 15:02:04 +02007371 if (px->mode == PR_MODE_HTTP) {
7372 memprintf(err, "Lua TCP services cannot be used on HTTP proxies");
Christopher Fauletafd8f102018-11-08 11:34:21 +01007373 return ACT_RET_PRS_ERR;
7374 }
7375
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007376 /* Memory for the rule. */
7377 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
7378 if (!rule->arg.hlua_rule) {
7379 memprintf(err, "out of memory error");
7380 return ACT_RET_PRS_ERR;
7381 }
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007382
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007383 /* Reference the Lua function and store the reference. */
7384 rule->arg.hlua_rule->fcn = *fcn;
7385
7386 /* TODO: later accept arguments. */
7387 rule->arg.hlua_rule->args = NULL;
7388
7389 /* Add applet pointer in the rule. */
7390 rule->applet.obj_type = OBJ_TYPE_APPLET;
7391 rule->applet.name = fcn->name;
7392 rule->applet.init = hlua_applet_tcp_init;
7393 rule->applet.fct = hlua_applet_tcp_fct;
7394 rule->applet.release = hlua_applet_tcp_release;
7395 rule->applet.timeout = hlua_timeout_applet;
7396
7397 return 0;
7398}
7399
7400/* This function is an LUA binding used for registering
7401 * "sample-conv" functions. It expects a converter name used
7402 * in the haproxy configuration file, and an LUA function.
7403 */
7404__LJMP static int hlua_register_service(lua_State *L)
7405{
7406 struct action_kw_list *akl;
7407 const char *name;
7408 const char *env;
7409 int ref;
7410 int len;
7411 struct hlua_function *fcn;
7412
7413 MAY_LJMP(check_args(L, 3, "register_service"));
7414
7415 /* First argument : converter name. */
7416 name = MAY_LJMP(luaL_checkstring(L, 1));
7417
7418 /* Second argument : environment. */
7419 env = MAY_LJMP(luaL_checkstring(L, 2));
7420
7421 /* Third argument : lua function. */
7422 ref = MAY_LJMP(hlua_checkfunction(L, 3));
7423
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007424 /* Allocate and fill the sample fetch keyword struct. */
7425 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
7426 if (!akl)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007427 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007428 fcn = calloc(1, sizeof(*fcn));
7429 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007430 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007431
7432 /* Fill fcn. */
7433 len = strlen("<lua.>") + strlen(name) + 1;
7434 fcn->name = calloc(1, len);
7435 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007436 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007437 snprintf((char *)fcn->name, len, "<lua.%s>", name);
7438 fcn->function_ref = ref;
7439
7440 /* List head */
7441 akl->list.n = akl->list.p = NULL;
7442
7443 /* converter keyword. */
7444 len = strlen("lua.") + strlen(name) + 1;
7445 akl->kw[0].kw = calloc(1, len);
7446 if (!akl->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007447 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007448
7449 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
7450
Thierry FOURNIER / OZON.IO02564fd2016-11-12 11:07:05 +01007451 /* Check required environment. Only accepted "http" or "tcp". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007452 if (strcmp(env, "tcp") == 0)
7453 akl->kw[0].parse = action_register_service_tcp;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007454 else if (strcmp(env, "http") == 0)
7455 akl->kw[0].parse = action_register_service_http;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007456 else
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007457 WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. "
Eric Salamafe7456f2017-12-21 14:30:07 +01007458 "'tcp' or 'http' are expected.", env));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007459
7460 akl->kw[0].match_pfx = 0;
7461 akl->kw[0].private = fcn;
7462
7463 /* End of array. */
7464 memset(&akl->kw[1], 0, sizeof(*akl->kw));
7465
7466 /* Register this new converter */
7467 service_keywords_register(akl);
7468
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007469 return 0;
7470}
7471
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007472/* This function initialises Lua cli handler. It copies the
7473 * arguments in the Lua stack and create channel IO objects.
7474 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02007475static int hlua_cli_parse_fct(char **args, char *payload, struct appctx *appctx, void *private)
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007476{
7477 struct hlua *hlua;
7478 struct hlua_function *fcn;
7479 int i;
7480 const char *error;
7481
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007482 fcn = private;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007483 appctx->ctx.hlua_cli.fcn = private;
7484
Willy Tarreaubafbe012017-11-24 17:34:44 +01007485 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007486 if (!hlua) {
7487 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007488 return 1;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007489 }
7490 HLUA_INIT(hlua);
7491 appctx->ctx.hlua_cli.hlua = hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007492
7493 /* Create task used by signal to wakeup applets.
Ilya Shipitsind4259502020-04-08 01:07:56 +05007494 * We use the same wakeup function than the Lua applet_tcp and
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007495 * applet_http. It is absolutely compatible.
7496 */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01007497 appctx->ctx.hlua_cli.task = task_new(tid_bit);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007498 if (!appctx->ctx.hlua_cli.task) {
Thierry FOURNIERffbf5692016-12-16 11:14:06 +01007499 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007500 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007501 }
7502 appctx->ctx.hlua_cli.task->nice = 0;
7503 appctx->ctx.hlua_cli.task->context = appctx;
7504 appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup;
7505
7506 /* Initialises the Lua context */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01007507 if (!hlua_ctx_init(hlua, appctx->ctx.hlua_cli.task, 0)) {
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007508 SEND_ERR(NULL, "Lua cli '%s': can't initialize Lua context.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007509 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007510 }
7511
7512 /* The following Lua calls can fail. */
7513 if (!SET_SAFE_LJMP(hlua->T)) {
7514 if (lua_type(hlua->T, -1) == LUA_TSTRING)
7515 error = lua_tostring(hlua->T, -1);
7516 else
7517 error = "critical error";
7518 SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error);
7519 goto error;
7520 }
7521
7522 /* Check stack available size. */
7523 if (!lua_checkstack(hlua->T, 2)) {
7524 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7525 goto error;
7526 }
7527
7528 /* Restore the function in the stack. */
7529 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
7530
7531 /* Once the arguments parsed, the CLI is like an AppletTCP,
7532 * so push AppletTCP in the stack.
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007533 */
7534 if (!hlua_applet_tcp_new(hlua->T, appctx)) {
7535 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7536 goto error;
7537 }
7538 hlua->nargs = 1;
7539
7540 /* push keywords in the stack. */
7541 for (i = 0; *args[i]; i++) {
7542 /* Check stack available size. */
7543 if (!lua_checkstack(hlua->T, 1)) {
7544 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7545 goto error;
7546 }
7547 lua_pushstring(hlua->T, args[i]);
7548 hlua->nargs++;
7549 }
7550
7551 /* We must initialize the execution timeouts. */
7552 hlua->max_time = hlua_timeout_session;
7553
7554 /* At this point the execution is safe. */
7555 RESET_SAFE_LJMP(hlua->T);
7556
7557 /* It's ok */
7558 return 0;
7559
7560 /* It's not ok. */
7561error:
7562 RESET_SAFE_LJMP(hlua->T);
7563 hlua_ctx_destroy(hlua);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007564 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007565 return 1;
7566}
7567
7568static int hlua_cli_io_handler_fct(struct appctx *appctx)
7569{
7570 struct hlua *hlua;
7571 struct stream_interface *si;
7572 struct hlua_function *fcn;
7573
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007574 hlua = appctx->ctx.hlua_cli.hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007575 si = appctx->owner;
Willy Tarreau8ae4f752016-12-14 15:41:45 +01007576 fcn = appctx->ctx.hlua_cli.fcn;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007577
7578 /* If the stream is disconnect or closed, ldo nothing. */
7579 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
7580 return 1;
7581
7582 /* Execute the function. */
7583 switch (hlua_ctx_resume(hlua, 1)) {
7584
7585 /* finished. */
7586 case HLUA_E_OK:
7587 return 1;
7588
7589 /* yield. */
7590 case HLUA_E_AGAIN:
7591 /* We want write. */
7592 if (HLUA_IS_WAKERESWR(hlua))
Willy Tarreaudb398432018-11-15 11:08:52 +01007593 si_rx_room_blk(si);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007594 /* Set the timeout. */
7595 if (hlua->wake_time != TICK_ETERNITY)
7596 task_schedule(hlua->task, hlua->wake_time);
7597 return 0;
7598
7599 /* finished with error. */
7600 case HLUA_E_ERRMSG:
7601 /* Display log. */
7602 SEND_ERR(NULL, "Lua cli '%s': %s.\n",
7603 fcn->name, lua_tostring(hlua->T, -1));
7604 lua_pop(hlua->T, 1);
7605 return 1;
7606
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007607 case HLUA_E_ETMOUT:
7608 SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n",
7609 fcn->name);
7610 return 1;
7611
7612 case HLUA_E_NOMEM:
7613 SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n",
7614 fcn->name);
7615 return 1;
7616
7617 case HLUA_E_YIELD: /* unexpected */
7618 SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n",
7619 fcn->name);
7620 return 1;
7621
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007622 case HLUA_E_ERR:
7623 /* Display log. */
7624 SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n",
7625 fcn->name);
7626 return 1;
7627
7628 default:
7629 return 1;
7630 }
7631
7632 return 1;
7633}
7634
7635static void hlua_cli_io_release_fct(struct appctx *appctx)
7636{
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007637 hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007638 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007639}
7640
7641/* This function is an LUA binding used for registering
7642 * new keywords in the cli. It expects a list of keywords
7643 * which are the "path". It is limited to 5 keywords. A
7644 * description of the command, a function to be executed
7645 * for the parsing and a function for io handlers.
7646 */
7647__LJMP static int hlua_register_cli(lua_State *L)
7648{
7649 struct cli_kw_list *cli_kws;
7650 const char *message;
7651 int ref_io;
7652 int len;
7653 struct hlua_function *fcn;
7654 int index;
7655 int i;
7656
7657 MAY_LJMP(check_args(L, 3, "register_cli"));
7658
7659 /* First argument : an array of maximum 5 keywords. */
7660 if (!lua_istable(L, 1))
7661 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table"));
7662
7663 /* Second argument : string with contextual message. */
7664 message = MAY_LJMP(luaL_checkstring(L, 2));
7665
7666 /* Third and fourth argument : lua function. */
7667 ref_io = MAY_LJMP(hlua_checkfunction(L, 3));
7668
7669 /* Allocate and fill the sample fetch keyword struct. */
7670 cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2);
7671 if (!cli_kws)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007672 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007673 fcn = calloc(1, sizeof(*fcn));
7674 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007675 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007676
7677 /* Fill path. */
7678 index = 0;
7679 lua_pushnil(L);
7680 while(lua_next(L, 1) != 0) {
7681 if (index >= 5)
7682 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries"));
7683 if (lua_type(L, -1) != LUA_TSTRING)
7684 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings"));
7685 cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1));
7686 if (!cli_kws->kw[0].str_kw[index])
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007687 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007688 index++;
7689 lua_pop(L, 1);
7690 }
7691
7692 /* Copy help message. */
7693 cli_kws->kw[0].usage = strdup(message);
7694 if (!cli_kws->kw[0].usage)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007695 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007696
7697 /* Fill fcn io handler. */
7698 len = strlen("<lua.cli>") + 1;
7699 for (i = 0; i < index; i++)
7700 len += strlen(cli_kws->kw[0].str_kw[i]) + 1;
7701 fcn->name = calloc(1, len);
7702 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007703 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007704 strncat((char *)fcn->name, "<lua.cli", len);
7705 for (i = 0; i < index; i++) {
7706 strncat((char *)fcn->name, ".", len);
7707 strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len);
7708 }
7709 strncat((char *)fcn->name, ">", len);
7710 fcn->function_ref = ref_io;
7711
7712 /* Fill last entries. */
7713 cli_kws->kw[0].private = fcn;
7714 cli_kws->kw[0].parse = hlua_cli_parse_fct;
7715 cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct;
7716 cli_kws->kw[0].io_release = hlua_cli_io_release_fct;
7717
7718 /* Register this new converter */
7719 cli_register_kw(cli_kws);
7720
7721 return 0;
7722}
7723
Thierry FOURNIERbd413492015-03-03 16:52:26 +01007724static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx,
7725 struct proxy *defpx, const char *file, int line,
7726 char **err, unsigned int *timeout)
7727{
7728 const char *error;
7729
7730 error = parse_time_err(args[1], timeout, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02007731 if (error == PARSE_TIME_OVER) {
7732 memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)",
7733 args[1], args[0]);
7734 return -1;
7735 }
7736 else if (error == PARSE_TIME_UNDER) {
7737 memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)",
7738 args[1], args[0]);
7739 return -1;
7740 }
7741 else if (error) {
Thierry FOURNIERbd413492015-03-03 16:52:26 +01007742 memprintf(err, "%s: invalid timeout", args[0]);
7743 return -1;
7744 }
7745 return 0;
7746}
7747
7748static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx,
7749 struct proxy *defpx, const char *file, int line,
7750 char **err)
7751{
7752 return hlua_read_timeout(args, section_type, curpx, defpx,
7753 file, line, err, &hlua_timeout_session);
7754}
7755
7756static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx,
7757 struct proxy *defpx, const char *file, int line,
7758 char **err)
7759{
7760 return hlua_read_timeout(args, section_type, curpx, defpx,
7761 file, line, err, &hlua_timeout_task);
7762}
7763
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007764static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx,
7765 struct proxy *defpx, const char *file, int line,
7766 char **err)
7767{
7768 return hlua_read_timeout(args, section_type, curpx, defpx,
7769 file, line, err, &hlua_timeout_applet);
7770}
7771
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01007772static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx,
7773 struct proxy *defpx, const char *file, int line,
7774 char **err)
7775{
7776 char *error;
7777
7778 hlua_nb_instruction = strtoll(args[1], &error, 10);
7779 if (*error != '\0') {
7780 memprintf(err, "%s: invalid number", args[0]);
7781 return -1;
7782 }
7783 return 0;
7784}
7785
Willy Tarreau32f61e22015-03-18 17:54:59 +01007786static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx,
7787 struct proxy *defpx, const char *file, int line,
7788 char **err)
7789{
7790 char *error;
7791
7792 if (*(args[1]) == 0) {
7793 memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]);
7794 return -1;
7795 }
7796 hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L;
7797 if (*error != '\0') {
7798 memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error);
7799 return -1;
7800 }
7801 return 0;
7802}
7803
7804
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007805/* This function is called by the main configuration key "lua-load". It loads and
7806 * execute an lua file during the parsing of the HAProxy configuration file. It is
7807 * the main lua entry point.
7808 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007809 * This function runs with the HAProxy keywords API. It returns -1 if an error
7810 * occurs, otherwise it returns 0.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007811 *
7812 * In some error case, LUA set an error message in top of the stack. This function
7813 * returns this error message in the HAProxy logs and pop it from the stack.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007814 *
7815 * This function can fail with an abort() due to an Lua critical error.
7816 * We are in the configuration parsing process of HAProxy, this abort() is
7817 * tolerated.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007818 */
7819static int hlua_load(char **args, int section_type, struct proxy *curpx,
7820 struct proxy *defpx, const char *file, int line,
7821 char **err)
7822{
7823 int error;
7824
7825 /* Just load and compile the file. */
7826 error = luaL_loadfile(gL.T, args[1]);
7827 if (error) {
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007828 memprintf(err, "error in Lua file '%s': %s", args[1], lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007829 lua_pop(gL.T, 1);
7830 return -1;
7831 }
7832
7833 /* If no syntax error where detected, execute the code. */
7834 error = lua_pcall(gL.T, 0, LUA_MULTRET, 0);
7835 switch (error) {
7836 case LUA_OK:
7837 break;
7838 case LUA_ERRRUN:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007839 memprintf(err, "Lua runtime error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007840 lua_pop(gL.T, 1);
7841 return -1;
7842 case LUA_ERRMEM:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007843 memprintf(err, "Lua out of memory error.n");
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007844 return -1;
7845 case LUA_ERRERR:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007846 memprintf(err, "Lua message handler error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007847 lua_pop(gL.T, 1);
7848 return -1;
7849 case LUA_ERRGCMM:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007850 memprintf(err, "Lua garbage collector error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007851 lua_pop(gL.T, 1);
7852 return -1;
7853 default:
Ilya Shipitsind4259502020-04-08 01:07:56 +05007854 memprintf(err, "Lua unknown error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007855 lua_pop(gL.T, 1);
7856 return -1;
7857 }
7858
7859 return 0;
7860}
7861
Tim Duesterhusc9fc9f22020-01-12 13:55:39 +01007862/* Prepend the given <path> followed by a semicolon to the `package.<type>` variable
7863 * in the given <ctx>.
7864 */
7865static int hlua_prepend_path(struct hlua ctx, char *type, char *path)
7866{
7867 lua_getglobal(ctx.T, "package"); /* push package variable */
7868 lua_pushstring(ctx.T, path); /* push given path */
7869 lua_pushstring(ctx.T, ";"); /* push semicolon */
7870 lua_getfield(ctx.T, -3, type); /* push old path */
7871 lua_concat(ctx.T, 3); /* concatenate to new path */
7872 lua_setfield(ctx.T, -2, type); /* store new path */
7873 lua_pop(ctx.T, 1); /* pop package variable */
7874
7875 return 0;
7876}
7877
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01007878static int hlua_config_prepend_path(char **args, int section_type, struct proxy *curpx,
7879 struct proxy *defpx, const char *file, int line,
7880 char **err)
7881{
7882 char *path;
7883 char *type = "path";
7884 if (too_many_args(2, args, err, NULL)) {
7885 return -1;
7886 }
7887
7888 if (!(*args[1])) {
7889 memprintf(err, "'%s' expects to receive a <path> as argument", args[0]);
7890 return -1;
7891 }
7892 path = args[1];
7893
7894 if (*args[2]) {
7895 if (strcmp(args[2], "path") != 0 && strcmp(args[2], "cpath") != 0) {
7896 memprintf(err, "'%s' expects <type> to either be 'path' or 'cpath'", args[0]);
7897 return -1;
7898 }
7899 type = args[2];
7900 }
7901
7902 return hlua_prepend_path(gL, type, path);
7903}
7904
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007905/* configuration keywords declaration */
7906static struct cfg_kw_list cfg_kws = {{ },{
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01007907 { CFG_GLOBAL, "lua-prepend-path", hlua_config_prepend_path },
Thierry FOURNIERbd413492015-03-03 16:52:26 +01007908 { CFG_GLOBAL, "lua-load", hlua_load },
7909 { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout },
7910 { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout },
Thierry FOURNIER56da1012015-10-01 08:42:31 +02007911 { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout },
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01007912 { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield },
Willy Tarreau32f61e22015-03-18 17:54:59 +01007913 { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem },
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007914 { 0, NULL, NULL },
7915}};
7916
Willy Tarreau0108d902018-11-25 19:14:37 +01007917INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
7918
Christopher Fauletafd8f102018-11-08 11:34:21 +01007919
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007920/* This function can fail with an abort() due to an Lua critical error.
7921 * We are in the initialisation process of HAProxy, this abort() is
7922 * tolerated.
7923 */
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007924int hlua_post_init()
7925{
7926 struct hlua_init_function *init;
7927 const char *msg;
7928 enum hlua_exec ret;
Thierry Fournierfd107a22016-02-19 19:57:23 +01007929 const char *error;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007930
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007931 /* Call post initialisation function in safe environment. */
Thierry Fournier3d4a6752016-02-19 20:53:30 +01007932 if (!SET_SAFE_LJMP(gL.T)) {
7933 if (lua_type(gL.T, -1) == LUA_TSTRING)
7934 error = lua_tostring(gL.T, -1);
7935 else
7936 error = "critical error";
7937 fprintf(stderr, "Lua post-init: %s.\n", error);
7938 exit(1);
7939 }
Frédéric Lécaille54f2bcf2018-08-29 13:46:24 +02007940
7941#if USE_OPENSSL
7942 /* Initialize SSL server. */
7943 if (socket_ssl.xprt->prepare_srv) {
7944 int saved_used_backed = global.ssl_used_backend;
7945 // don't affect maxconn automatic computation
7946 socket_ssl.xprt->prepare_srv(&socket_ssl);
7947 global.ssl_used_backend = saved_used_backed;
7948 }
7949#endif
7950
Thierry Fournier3d4a6752016-02-19 20:53:30 +01007951 hlua_fcn_post_init(gL.T);
7952 RESET_SAFE_LJMP(gL.T);
7953
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007954 list_for_each_entry(init, &hlua_init_functions, l) {
7955 lua_rawgeti(gL.T, LUA_REGISTRYINDEX, init->function_ref);
7956 ret = hlua_ctx_resume(&gL, 0);
7957 switch (ret) {
7958 case HLUA_E_OK:
7959 lua_pop(gL.T, -1);
7960 return 1;
7961 case HLUA_E_AGAIN:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007962 ha_alert("Lua init: yield not allowed.\n");
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007963 return 0;
7964 case HLUA_E_ERRMSG:
7965 msg = lua_tostring(gL.T, -1);
Christopher Faulet767a84b2017-11-24 16:50:31 +01007966 ha_alert("lua init: %s.\n", msg);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007967 return 0;
7968 case HLUA_E_ERR:
7969 default:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007970 ha_alert("Lua init: unknown runtime error.\n");
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007971 return 0;
7972 }
7973 }
7974 return 1;
7975}
7976
Willy Tarreau32f61e22015-03-18 17:54:59 +01007977/* The memory allocator used by the Lua stack. <ud> is a pointer to the
7978 * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize>
7979 * is the previously allocated size or the kind of object in case of a new
7980 * allocation. <nsize> is the requested new size.
7981 */
7982static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
7983{
7984 struct hlua_mem_allocator *zone = ud;
7985
7986 if (nsize == 0) {
7987 /* it's a free */
7988 if (ptr)
7989 zone->allocated -= osize;
7990 free(ptr);
7991 return NULL;
7992 }
7993
7994 if (!ptr) {
7995 /* it's a new allocation */
7996 if (zone->limit && zone->allocated + nsize > zone->limit)
7997 return NULL;
7998
7999 ptr = malloc(nsize);
8000 if (ptr)
8001 zone->allocated += nsize;
8002 return ptr;
8003 }
8004
8005 /* it's a realloc */
8006 if (zone->limit && zone->allocated + nsize - osize > zone->limit)
8007 return NULL;
8008
8009 ptr = realloc(ptr, nsize);
8010 if (ptr)
8011 zone->allocated += nsize - osize;
8012 return ptr;
8013}
8014
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008015/* Ithis function can fail with an abort() due to an Lua critical error.
8016 * We are in the initialisation process of HAProxy, this abort() is
8017 * tolerated.
8018 */
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01008019void hlua_init(void)
8020{
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008021 int i;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008022 int idx;
8023 struct sample_fetch *sf;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008024 struct sample_conv *sc;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008025 char *p;
Thierry Fournierfd107a22016-02-19 19:57:23 +01008026 const char *error_msg;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008027#ifdef USE_OPENSSL
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008028 struct srv_kw *kw;
8029 int tmp_error;
8030 char *error;
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008031 char *args[] = { /* SSL client configuration. */
8032 "ssl",
8033 "verify",
8034 "none",
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008035 NULL
8036 };
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008037#endif
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008038
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008039 /* Init main lua stack. */
8040 gL.Mref = LUA_REFNIL;
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01008041 gL.flags = 0;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01008042 LIST_INIT(&gL.com);
Willy Tarreau42ef75f2017-04-12 21:40:29 +02008043 gL.T = lua_newstate(hlua_alloc, &hlua_global_allocator);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008044 hlua_sethlua(&gL);
8045 gL.Tref = LUA_REFNIL;
8046 gL.task = NULL;
8047
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08008048 /* From this point, until the end of the initialisation function,
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008049 * the Lua function can fail with an abort. We are in the initialisation
8050 * process of HAProxy, this abort() is tolerated.
8051 */
8052
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008053 /* Initialise lua. */
8054 luaL_openlibs(gL.T);
Tim Duesterhus541fe1e2020-01-12 13:55:41 +01008055#define HLUA_PREPEND_PATH_TOSTRING1(x) #x
8056#define HLUA_PREPEND_PATH_TOSTRING(x) HLUA_PREPEND_PATH_TOSTRING1(x)
8057#ifdef HLUA_PREPEND_PATH
8058 hlua_prepend_path(gL, "path", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_PATH));
8059#endif
8060#ifdef HLUA_PREPEND_CPATH
8061 hlua_prepend_path(gL, "cpath", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_CPATH));
8062#endif
8063#undef HLUA_PREPEND_PATH_TOSTRING
8064#undef HLUA_PREPEND_PATH_TOSTRING1
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008065
Thierry Fournier75933d42016-01-21 09:30:18 +01008066 /* Set safe environment for the initialisation. */
8067 if (!SET_SAFE_LJMP(gL.T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01008068 if (lua_type(gL.T, -1) == LUA_TSTRING)
8069 error_msg = lua_tostring(gL.T, -1);
8070 else
8071 error_msg = "critical error";
8072 fprintf(stderr, "Lua init: %s.\n", error_msg);
Thierry Fournier75933d42016-01-21 09:30:18 +01008073 exit(1);
8074 }
8075
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008076 /*
8077 *
8078 * Create "core" object.
8079 *
8080 */
8081
Thierry FOURNIERa2d8c652015-03-11 17:29:39 +01008082 /* This table entry is the object "core" base. */
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008083 lua_newtable(gL.T);
8084
8085 /* Push the loglevel constants. */
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008086 for (i = 0; i < NB_LOG_LEVELS; i++)
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008087 hlua_class_const_int(gL.T, log_levels[i], i);
8088
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008089 /* Register special functions. */
8090 hlua_class_function(gL.T, "register_init", hlua_register_init);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01008091 hlua_class_function(gL.T, "register_task", hlua_register_task);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01008092 hlua_class_function(gL.T, "register_fetches", hlua_register_fetches);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01008093 hlua_class_function(gL.T, "register_converters", hlua_register_converters);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02008094 hlua_class_function(gL.T, "register_action", hlua_register_action);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008095 hlua_class_function(gL.T, "register_service", hlua_register_service);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008096 hlua_class_function(gL.T, "register_cli", hlua_register_cli);
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01008097 hlua_class_function(gL.T, "yield", hlua_yield);
Willy Tarreau59551662015-03-10 14:23:13 +01008098 hlua_class_function(gL.T, "set_nice", hlua_set_nice);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01008099 hlua_class_function(gL.T, "sleep", hlua_sleep);
8100 hlua_class_function(gL.T, "msleep", hlua_msleep);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01008101 hlua_class_function(gL.T, "add_acl", hlua_add_acl);
8102 hlua_class_function(gL.T, "del_acl", hlua_del_acl);
8103 hlua_class_function(gL.T, "set_map", hlua_set_map);
8104 hlua_class_function(gL.T, "del_map", hlua_del_map);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008105 hlua_class_function(gL.T, "tcp", hlua_socket_new);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01008106 hlua_class_function(gL.T, "log", hlua_log);
8107 hlua_class_function(gL.T, "Debug", hlua_log_debug);
8108 hlua_class_function(gL.T, "Info", hlua_log_info);
8109 hlua_class_function(gL.T, "Warning", hlua_log_warning);
8110 hlua_class_function(gL.T, "Alert", hlua_log_alert);
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02008111 hlua_class_function(gL.T, "done", hlua_done);
Thierry Fournierfb0b5462016-01-21 09:28:58 +01008112 hlua_fcn_reg_core_fcn(gL.T);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008113
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008114 lua_setglobal(gL.T, "core");
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008115
8116 /*
8117 *
Christopher Faulet0f3c8902020-01-31 18:57:12 +01008118 * Create "act" object.
8119 *
8120 */
8121
8122 /* This table entry is the object "act" base. */
8123 lua_newtable(gL.T);
8124
8125 /* push action return constants */
8126 hlua_class_const_int(gL.T, "CONTINUE", ACT_RET_CONT);
8127 hlua_class_const_int(gL.T, "STOP", ACT_RET_STOP);
8128 hlua_class_const_int(gL.T, "YIELD", ACT_RET_YIELD);
8129 hlua_class_const_int(gL.T, "ERROR", ACT_RET_ERR);
8130 hlua_class_const_int(gL.T, "DONE", ACT_RET_DONE);
8131 hlua_class_const_int(gL.T, "DENY", ACT_RET_DENY);
8132 hlua_class_const_int(gL.T, "ABORT", ACT_RET_ABRT);
8133 hlua_class_const_int(gL.T, "INVALID", ACT_RET_INV);
8134
Christopher Faulet501465d2020-02-26 14:54:16 +01008135 hlua_class_function(gL.T, "wake_time", hlua_set_wake_time);
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01008136
Christopher Faulet0f3c8902020-01-31 18:57:12 +01008137 lua_setglobal(gL.T, "act");
8138
8139 /*
8140 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008141 * Register class Map
8142 *
8143 */
8144
8145 /* This table entry is the object "Map" base. */
8146 lua_newtable(gL.T);
8147
8148 /* register pattern types. */
8149 for (i=0; i<PAT_MATCH_NUM; i++)
8150 hlua_class_const_int(gL.T, pat_match_names[i], i);
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01008151 for (i=0; i<PAT_MATCH_NUM; i++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008152 snprintf(trash.area, trash.size, "_%s", pat_match_names[i]);
8153 hlua_class_const_int(gL.T, trash.area, i);
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01008154 }
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008155
8156 /* register constructor. */
8157 hlua_class_function(gL.T, "new", hlua_map_new);
8158
8159 /* Create and fill the metatable. */
8160 lua_newtable(gL.T);
8161
Ilya Shipitsind4259502020-04-08 01:07:56 +05008162 /* Create and fill the __index entry. */
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008163 lua_pushstring(gL.T, "__index");
8164 lua_newtable(gL.T);
8165
8166 /* Register . */
8167 hlua_class_function(gL.T, "lookup", hlua_map_lookup);
8168 hlua_class_function(gL.T, "slookup", hlua_map_slookup);
8169
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008170 lua_rawset(gL.T, -3);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008171
Thierry Fournier45e78d72016-02-19 18:34:46 +01008172 /* Register previous table in the registry with reference and named entry.
8173 * The function hlua_register_metatable() pops the stack, so we
8174 * previously create a copy of the table.
8175 */
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008176 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008177 class_map_ref = hlua_register_metatable(gL.T, CLASS_MAP);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008178
8179 /* Assign the metatable to the mai Map object. */
8180 lua_setmetatable(gL.T, -2);
8181
8182 /* Set a name to the table. */
8183 lua_setglobal(gL.T, "Map");
8184
8185 /*
8186 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008187 * Register class Channel
8188 *
8189 */
8190
8191 /* Create and fill the metatable. */
8192 lua_newtable(gL.T);
8193
Ilya Shipitsind4259502020-04-08 01:07:56 +05008194 /* Create and fill the __index entry. */
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008195 lua_pushstring(gL.T, "__index");
8196 lua_newtable(gL.T);
8197
8198 /* Register . */
8199 hlua_class_function(gL.T, "get", hlua_channel_get);
8200 hlua_class_function(gL.T, "dup", hlua_channel_dup);
8201 hlua_class_function(gL.T, "getline", hlua_channel_getline);
8202 hlua_class_function(gL.T, "set", hlua_channel_set);
8203 hlua_class_function(gL.T, "append", hlua_channel_append);
8204 hlua_class_function(gL.T, "send", hlua_channel_send);
8205 hlua_class_function(gL.T, "forward", hlua_channel_forward);
8206 hlua_class_function(gL.T, "get_in_len", hlua_channel_get_in_len);
8207 hlua_class_function(gL.T, "get_out_len", hlua_channel_get_out_len);
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01008208 hlua_class_function(gL.T, "is_full", hlua_channel_is_full);
Christopher Faulet2ac9ba22020-02-25 10:15:50 +01008209 hlua_class_function(gL.T, "is_resp", hlua_channel_is_resp);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008210
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008211 lua_rawset(gL.T, -3);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008212
8213 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008214 class_channel_ref = hlua_register_metatable(gL.T, CLASS_CHANNEL);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008215
8216 /*
8217 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008218 * Register class Fetches
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008219 *
8220 */
8221
8222 /* Create and fill the metatable. */
8223 lua_newtable(gL.T);
8224
Ilya Shipitsind4259502020-04-08 01:07:56 +05008225 /* Create and fill the __index entry. */
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008226 lua_pushstring(gL.T, "__index");
8227 lua_newtable(gL.T);
8228
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008229 /* Browse existing fetches and create the associated
8230 * object method.
8231 */
8232 sf = NULL;
8233 while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) {
8234
8235 /* Dont register the keywork if the arguments check function are
8236 * not safe during the runtime.
8237 */
8238 if ((sf->val_args != NULL) &&
8239 (sf->val_args != val_payload_lv) &&
8240 (sf->val_args != val_hdr))
8241 continue;
8242
8243 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
8244 * by an underscore.
8245 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008246 strncpy(trash.area, sf->kw, trash.size);
8247 trash.area[trash.size - 1] = '\0';
8248 for (p = trash.area; *p; p++)
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008249 if (*p == '.' || *p == '-' || *p == '+')
8250 *p = '_';
8251
8252 /* Register the function. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008253 lua_pushstring(gL.T, trash.area);
Willy Tarreau2ec22742015-03-10 14:27:20 +01008254 lua_pushlightuserdata(gL.T, sf);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008255 lua_pushcclosure(gL.T, hlua_run_sample_fetch, 1);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008256 lua_rawset(gL.T, -3);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008257 }
8258
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008259 lua_rawset(gL.T, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008260
8261 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008262 class_fetches_ref = hlua_register_metatable(gL.T, CLASS_FETCHES);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008263
8264 /*
8265 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008266 * Register class Converters
8267 *
8268 */
8269
8270 /* Create and fill the metatable. */
8271 lua_newtable(gL.T);
8272
8273 /* Create and fill the __index entry. */
8274 lua_pushstring(gL.T, "__index");
8275 lua_newtable(gL.T);
8276
8277 /* Browse existing converters and create the associated
8278 * object method.
8279 */
8280 sc = NULL;
8281 while ((sc = sample_conv_getnext(sc, &idx)) != NULL) {
8282 /* Dont register the keywork if the arguments check function are
8283 * not safe during the runtime.
8284 */
8285 if (sc->val_args != NULL)
8286 continue;
8287
8288 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
8289 * by an underscore.
8290 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008291 strncpy(trash.area, sc->kw, trash.size);
8292 trash.area[trash.size - 1] = '\0';
8293 for (p = trash.area; *p; p++)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008294 if (*p == '.' || *p == '-' || *p == '+')
8295 *p = '_';
8296
8297 /* Register the function. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008298 lua_pushstring(gL.T, trash.area);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008299 lua_pushlightuserdata(gL.T, sc);
8300 lua_pushcclosure(gL.T, hlua_run_sample_conv, 1);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008301 lua_rawset(gL.T, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008302 }
8303
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008304 lua_rawset(gL.T, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008305
8306 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008307 class_converters_ref = hlua_register_metatable(gL.T, CLASS_CONVERTERS);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008308
8309 /*
8310 *
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008311 * Register class HTTP
8312 *
8313 */
8314
8315 /* Create and fill the metatable. */
8316 lua_newtable(gL.T);
8317
Ilya Shipitsind4259502020-04-08 01:07:56 +05008318 /* Create and fill the __index entry. */
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008319 lua_pushstring(gL.T, "__index");
8320 lua_newtable(gL.T);
8321
8322 /* Register Lua functions. */
8323 hlua_class_function(gL.T, "req_get_headers",hlua_http_req_get_headers);
8324 hlua_class_function(gL.T, "req_del_header", hlua_http_req_del_hdr);
8325 hlua_class_function(gL.T, "req_rep_header", hlua_http_req_rep_hdr);
8326 hlua_class_function(gL.T, "req_rep_value", hlua_http_req_rep_val);
8327 hlua_class_function(gL.T, "req_add_header", hlua_http_req_add_hdr);
8328 hlua_class_function(gL.T, "req_set_header", hlua_http_req_set_hdr);
8329 hlua_class_function(gL.T, "req_set_method", hlua_http_req_set_meth);
8330 hlua_class_function(gL.T, "req_set_path", hlua_http_req_set_path);
8331 hlua_class_function(gL.T, "req_set_query", hlua_http_req_set_query);
8332 hlua_class_function(gL.T, "req_set_uri", hlua_http_req_set_uri);
8333
8334 hlua_class_function(gL.T, "res_get_headers",hlua_http_res_get_headers);
8335 hlua_class_function(gL.T, "res_del_header", hlua_http_res_del_hdr);
8336 hlua_class_function(gL.T, "res_rep_header", hlua_http_res_rep_hdr);
8337 hlua_class_function(gL.T, "res_rep_value", hlua_http_res_rep_val);
8338 hlua_class_function(gL.T, "res_add_header", hlua_http_res_add_hdr);
8339 hlua_class_function(gL.T, "res_set_header", hlua_http_res_set_hdr);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02008340 hlua_class_function(gL.T, "res_set_status", hlua_http_res_set_status);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008341
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008342 lua_rawset(gL.T, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008343
8344 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008345 class_http_ref = hlua_register_metatable(gL.T, CLASS_HTTP);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008346
8347 /*
8348 *
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008349 * Register class AppletTCP
8350 *
8351 */
8352
8353 /* Create and fill the metatable. */
8354 lua_newtable(gL.T);
8355
Ilya Shipitsind4259502020-04-08 01:07:56 +05008356 /* Create and fill the __index entry. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008357 lua_pushstring(gL.T, "__index");
8358 lua_newtable(gL.T);
8359
8360 /* Register Lua functions. */
Thierry FOURNIER / OZON.IO3e1d7912016-12-12 12:29:34 +01008361 hlua_class_function(gL.T, "getline", hlua_applet_tcp_getline);
8362 hlua_class_function(gL.T, "receive", hlua_applet_tcp_recv);
8363 hlua_class_function(gL.T, "send", hlua_applet_tcp_send);
8364 hlua_class_function(gL.T, "set_priv", hlua_applet_tcp_set_priv);
8365 hlua_class_function(gL.T, "get_priv", hlua_applet_tcp_get_priv);
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01008366 hlua_class_function(gL.T, "set_var", hlua_applet_tcp_set_var);
8367 hlua_class_function(gL.T, "unset_var", hlua_applet_tcp_unset_var);
8368 hlua_class_function(gL.T, "get_var", hlua_applet_tcp_get_var);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008369
8370 lua_settable(gL.T, -3);
8371
8372 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008373 class_applet_tcp_ref = hlua_register_metatable(gL.T, CLASS_APPLET_TCP);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008374
8375 /*
8376 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02008377 * Register class AppletHTTP
8378 *
8379 */
8380
8381 /* Create and fill the metatable. */
8382 lua_newtable(gL.T);
8383
Ilya Shipitsind4259502020-04-08 01:07:56 +05008384 /* Create and fill the __index entry. */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02008385 lua_pushstring(gL.T, "__index");
8386 lua_newtable(gL.T);
8387
8388 /* Register Lua functions. */
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01008389 hlua_class_function(gL.T, "set_priv", hlua_applet_http_set_priv);
8390 hlua_class_function(gL.T, "get_priv", hlua_applet_http_get_priv);
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01008391 hlua_class_function(gL.T, "set_var", hlua_applet_http_set_var);
8392 hlua_class_function(gL.T, "unset_var", hlua_applet_http_unset_var);
8393 hlua_class_function(gL.T, "get_var", hlua_applet_http_get_var);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02008394 hlua_class_function(gL.T, "getline", hlua_applet_http_getline);
8395 hlua_class_function(gL.T, "receive", hlua_applet_http_recv);
8396 hlua_class_function(gL.T, "send", hlua_applet_http_send);
8397 hlua_class_function(gL.T, "add_header", hlua_applet_http_addheader);
8398 hlua_class_function(gL.T, "set_status", hlua_applet_http_status);
8399 hlua_class_function(gL.T, "start_response", hlua_applet_http_start_response);
8400
8401 lua_settable(gL.T, -3);
8402
8403 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008404 class_applet_http_ref = hlua_register_metatable(gL.T, CLASS_APPLET_HTTP);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02008405
8406 /*
8407 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008408 * Register class TXN
8409 *
8410 */
8411
8412 /* Create and fill the metatable. */
8413 lua_newtable(gL.T);
8414
Ilya Shipitsind4259502020-04-08 01:07:56 +05008415 /* Create and fill the __index entry. */
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008416 lua_pushstring(gL.T, "__index");
8417 lua_newtable(gL.T);
8418
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01008419 /* Register Lua functions. */
Patrick Hemmer268a7072018-05-11 12:52:31 -04008420 hlua_class_function(gL.T, "set_priv", hlua_set_priv);
8421 hlua_class_function(gL.T, "get_priv", hlua_get_priv);
8422 hlua_class_function(gL.T, "set_var", hlua_set_var);
8423 hlua_class_function(gL.T, "unset_var", hlua_unset_var);
8424 hlua_class_function(gL.T, "get_var", hlua_get_var);
8425 hlua_class_function(gL.T, "done", hlua_txn_done);
Christopher Faulet700d9e82020-01-31 12:21:52 +01008426 hlua_class_function(gL.T, "reply", hlua_txn_reply_new);
Patrick Hemmer268a7072018-05-11 12:52:31 -04008427 hlua_class_function(gL.T, "set_loglevel", hlua_txn_set_loglevel);
8428 hlua_class_function(gL.T, "set_tos", hlua_txn_set_tos);
8429 hlua_class_function(gL.T, "set_mark", hlua_txn_set_mark);
8430 hlua_class_function(gL.T, "set_priority_class", hlua_txn_set_priority_class);
8431 hlua_class_function(gL.T, "set_priority_offset", hlua_txn_set_priority_offset);
8432 hlua_class_function(gL.T, "deflog", hlua_txn_deflog);
8433 hlua_class_function(gL.T, "log", hlua_txn_log);
8434 hlua_class_function(gL.T, "Debug", hlua_txn_log_debug);
8435 hlua_class_function(gL.T, "Info", hlua_txn_log_info);
8436 hlua_class_function(gL.T, "Warning", hlua_txn_log_warning);
8437 hlua_class_function(gL.T, "Alert", hlua_txn_log_alert);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01008438
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008439 lua_rawset(gL.T, -3);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008440
8441 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008442 class_txn_ref = hlua_register_metatable(gL.T, CLASS_TXN);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008443
8444 /*
8445 *
Christopher Faulet700d9e82020-01-31 12:21:52 +01008446 * Register class reply
8447 *
8448 */
8449 lua_newtable(gL.T);
8450 lua_pushstring(gL.T, "__index");
8451 lua_newtable(gL.T);
8452 hlua_class_function(gL.T, "set_status", hlua_txn_reply_set_status);
8453 hlua_class_function(gL.T, "add_header", hlua_txn_reply_add_header);
8454 hlua_class_function(gL.T, "del_header", hlua_txn_reply_del_header);
8455 hlua_class_function(gL.T, "set_body", hlua_txn_reply_set_body);
8456 lua_settable(gL.T, -3); /* Sets the __index entry. */
8457 class_txn_reply_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
8458
8459
8460 /*
8461 *
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008462 * Register class Socket
8463 *
8464 */
8465
8466 /* Create and fill the metatable. */
8467 lua_newtable(gL.T);
8468
Ilya Shipitsind4259502020-04-08 01:07:56 +05008469 /* Create and fill the __index entry. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008470 lua_pushstring(gL.T, "__index");
8471 lua_newtable(gL.T);
8472
Baptiste Assmann84bb4932015-03-02 21:40:06 +01008473#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008474 hlua_class_function(gL.T, "connect_ssl", hlua_socket_connect_ssl);
Baptiste Assmann84bb4932015-03-02 21:40:06 +01008475#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008476 hlua_class_function(gL.T, "connect", hlua_socket_connect);
8477 hlua_class_function(gL.T, "send", hlua_socket_send);
8478 hlua_class_function(gL.T, "receive", hlua_socket_receive);
8479 hlua_class_function(gL.T, "close", hlua_socket_close);
8480 hlua_class_function(gL.T, "getpeername", hlua_socket_getpeername);
8481 hlua_class_function(gL.T, "getsockname", hlua_socket_getsockname);
8482 hlua_class_function(gL.T, "setoption", hlua_socket_setoption);
8483 hlua_class_function(gL.T, "settimeout", hlua_socket_settimeout);
8484
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008485 lua_rawset(gL.T, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008486
8487 /* Register the garbage collector entry. */
8488 lua_pushstring(gL.T, "__gc");
8489 lua_pushcclosure(gL.T, hlua_socket_gc, 0);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008490 lua_rawset(gL.T, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008491
8492 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008493 class_socket_ref = hlua_register_metatable(gL.T, CLASS_SOCKET);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008494
8495 /* Proxy and server configuration initialisation. */
8496 memset(&socket_proxy, 0, sizeof(socket_proxy));
8497 init_new_proxy(&socket_proxy);
8498 socket_proxy.parent = NULL;
8499 socket_proxy.last_change = now.tv_sec;
8500 socket_proxy.id = "LUA-SOCKET";
8501 socket_proxy.cap = PR_CAP_FE | PR_CAP_BE;
8502 socket_proxy.maxconn = 0;
8503 socket_proxy.accept = NULL;
8504 socket_proxy.options2 |= PR_O2_INDEPSTR;
8505 socket_proxy.srv = NULL;
8506 socket_proxy.conn_retries = 0;
8507 socket_proxy.timeout.connect = 5000; /* By default the timeout connection is 5s. */
8508
8509 /* Init TCP server: unchanged parameters */
8510 memset(&socket_tcp, 0, sizeof(socket_tcp));
8511 socket_tcp.next = NULL;
8512 socket_tcp.proxy = &socket_proxy;
8513 socket_tcp.obj_type = OBJ_TYPE_SERVER;
8514 LIST_INIT(&socket_tcp.actconns);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04008515 socket_tcp.pendconns = EB_ROOT;
Christopher Faulet40a007c2017-07-03 15:41:01 +02008516 socket_tcp.idle_conns = NULL;
8517 socket_tcp.safe_conns = NULL;
Emeric Brun52a91d32017-08-31 14:41:55 +02008518 socket_tcp.next_state = SRV_ST_RUNNING; /* early server setup */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008519 socket_tcp.last_change = 0;
8520 socket_tcp.id = "LUA-TCP-CONN";
8521 socket_tcp.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8522 socket_tcp.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8523 socket_tcp.pp_opts = 0; /* Remove proxy protocol. */
8524
8525 /* XXX: Copy default parameter from default server,
8526 * but the default server is not initialized.
8527 */
8528 socket_tcp.maxqueue = socket_proxy.defsrv.maxqueue;
8529 socket_tcp.minconn = socket_proxy.defsrv.minconn;
8530 socket_tcp.maxconn = socket_proxy.defsrv.maxconn;
8531 socket_tcp.slowstart = socket_proxy.defsrv.slowstart;
8532 socket_tcp.onerror = socket_proxy.defsrv.onerror;
8533 socket_tcp.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
8534 socket_tcp.onmarkedup = socket_proxy.defsrv.onmarkedup;
8535 socket_tcp.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
8536 socket_tcp.uweight = socket_proxy.defsrv.iweight;
8537 socket_tcp.iweight = socket_proxy.defsrv.iweight;
8538
8539 socket_tcp.check.status = HCHK_STATUS_INI;
8540 socket_tcp.check.rise = socket_proxy.defsrv.check.rise;
8541 socket_tcp.check.fall = socket_proxy.defsrv.check.fall;
8542 socket_tcp.check.health = socket_tcp.check.rise; /* socket, but will fall down at first failure */
8543 socket_tcp.check.server = &socket_tcp;
8544
8545 socket_tcp.agent.status = HCHK_STATUS_INI;
8546 socket_tcp.agent.rise = socket_proxy.defsrv.agent.rise;
8547 socket_tcp.agent.fall = socket_proxy.defsrv.agent.fall;
8548 socket_tcp.agent.health = socket_tcp.agent.rise; /* socket, but will fall down at first failure */
8549 socket_tcp.agent.server = &socket_tcp;
8550
Willy Tarreaua261e9b2016-12-22 20:44:00 +01008551 socket_tcp.xprt = xprt_get(XPRT_RAW);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008552
8553#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008554 /* Init TCP server: unchanged parameters */
8555 memset(&socket_ssl, 0, sizeof(socket_ssl));
8556 socket_ssl.next = NULL;
8557 socket_ssl.proxy = &socket_proxy;
8558 socket_ssl.obj_type = OBJ_TYPE_SERVER;
8559 LIST_INIT(&socket_ssl.actconns);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04008560 socket_ssl.pendconns = EB_ROOT;
Willy Tarreaub784b352019-02-07 14:48:24 +01008561 socket_ssl.idle_conns = NULL;
8562 socket_ssl.safe_conns = NULL;
Emeric Brun52a91d32017-08-31 14:41:55 +02008563 socket_ssl.next_state = SRV_ST_RUNNING; /* early server setup */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008564 socket_ssl.last_change = 0;
8565 socket_ssl.id = "LUA-SSL-CONN";
8566 socket_ssl.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8567 socket_ssl.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8568 socket_ssl.pp_opts = 0; /* Remove proxy protocol. */
8569
8570 /* XXX: Copy default parameter from default server,
8571 * but the default server is not initialized.
8572 */
8573 socket_ssl.maxqueue = socket_proxy.defsrv.maxqueue;
8574 socket_ssl.minconn = socket_proxy.defsrv.minconn;
8575 socket_ssl.maxconn = socket_proxy.defsrv.maxconn;
8576 socket_ssl.slowstart = socket_proxy.defsrv.slowstart;
8577 socket_ssl.onerror = socket_proxy.defsrv.onerror;
8578 socket_ssl.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
8579 socket_ssl.onmarkedup = socket_proxy.defsrv.onmarkedup;
8580 socket_ssl.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
8581 socket_ssl.uweight = socket_proxy.defsrv.iweight;
8582 socket_ssl.iweight = socket_proxy.defsrv.iweight;
8583
8584 socket_ssl.check.status = HCHK_STATUS_INI;
8585 socket_ssl.check.rise = socket_proxy.defsrv.check.rise;
8586 socket_ssl.check.fall = socket_proxy.defsrv.check.fall;
8587 socket_ssl.check.health = socket_ssl.check.rise; /* socket, but will fall down at first failure */
8588 socket_ssl.check.server = &socket_ssl;
8589
8590 socket_ssl.agent.status = HCHK_STATUS_INI;
8591 socket_ssl.agent.rise = socket_proxy.defsrv.agent.rise;
8592 socket_ssl.agent.fall = socket_proxy.defsrv.agent.fall;
8593 socket_ssl.agent.health = socket_ssl.agent.rise; /* socket, but will fall down at first failure */
8594 socket_ssl.agent.server = &socket_ssl;
8595
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008596 socket_ssl.use_ssl = 1;
Willy Tarreaua261e9b2016-12-22 20:44:00 +01008597 socket_ssl.xprt = xprt_get(XPRT_SSL);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008598
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008599 for (idx = 0; args[idx] != NULL; idx++) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008600 if ((kw = srv_find_kw(args[idx])) != NULL) { /* Maybe it's registered server keyword */
8601 /*
8602 *
8603 * If the keyword is not known, we can search in the registered
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08008604 * server keywords. This is useful to configure special SSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008605 * features like client certificates and ssl_verify.
8606 *
8607 */
8608 tmp_error = kw->parse(args, &idx, &socket_proxy, &socket_ssl, &error);
8609 if (tmp_error != 0) {
8610 fprintf(stderr, "INTERNAL ERROR: %s\n", error);
8611 abort(); /* This must be never arrives because the command line
8612 not editable by the user. */
8613 }
8614 idx += kw->skip;
8615 }
8616 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008617#endif
Thierry Fournier75933d42016-01-21 09:30:18 +01008618
8619 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01008620}
Willy Tarreaubb57d942016-12-21 19:04:56 +01008621
Willy Tarreau80713382018-11-26 10:19:54 +01008622static void hlua_register_build_options(void)
8623{
Willy Tarreaubb57d942016-12-21 19:04:56 +01008624 char *ptr = NULL;
Willy Tarreau80713382018-11-26 10:19:54 +01008625
Willy Tarreaubb57d942016-12-21 19:04:56 +01008626 memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE);
8627 hap_register_build_opts(ptr, 1);
8628}
Willy Tarreau80713382018-11-26 10:19:54 +01008629
8630INITCALL0(STG_REGISTER, hlua_register_build_options);