blob: f2d471de696ce1f369dec5b3438142ca59bcf6f6 [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 Tarreau3f0f82e2020-06-04 19:42:41 +020024#include <haproxy/applet.h>
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020025#include <haproxy/api.h>
Willy Tarreau8d2b7772020-05-27 10:58:19 +020026#include <import/ebpttree.h>
Thierry FOURNIER380d0932015-01-23 14:27:52 +010027
Willy Tarreaudfd3de82020-06-04 23:46:14 +020028#include <haproxy/arg.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020029#include <haproxy/cfgparse.h>
Willy Tarreauf1d32c42020-06-04 21:07:02 +020030#include <haproxy/channel.h>
Willy Tarreau83487a82020-06-04 20:19:54 +020031#include <haproxy/cli.h>
Willy Tarreau7ea393d2020-06-04 18:02:10 +020032#include <haproxy/connection.h>
Willy Tarreau87735332020-06-04 09:08:41 +020033#include <haproxy/http_htx.h>
Willy Tarreau3f567e42020-05-28 15:29:19 +020034#include <haproxy/thread.h>
Willy Tarreau7cd8b6e2020-06-02 17:32:26 +020035#include <haproxy/regex.h>
Willy Tarreau374b4422020-06-02 17:46:16 +020036#include <haproxy/xref.h>
Willy Tarreau5413a872020-06-02 19:33:08 +020037#include <haproxy/h1.h>
Willy Tarreau86416052020-06-04 09:20:54 +020038#include <haproxy/hlua.h>
Willy Tarreau8c794002020-06-04 10:05:25 +020039#include <haproxy/hlua_fcn.h>
Willy Tarreauc2b1ff02020-06-04 21:21:03 +020040#include <haproxy/http_ana.h>
Willy Tarreau126ba3a2020-06-04 18:26:43 +020041#include <haproxy/http_fetch.h>
Willy Tarreauc761f842020-06-04 11:40:28 +020042#include <haproxy/http_rules.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020043#include <haproxy/log.h>
Willy Tarreau2cd58092020-06-04 15:10:43 +020044#include <haproxy/map.h>
Willy Tarreau8efbdfb2020-06-04 11:29:21 +020045#include <haproxy/obj_type.h>
Willy Tarreau225a90a2020-06-04 15:06:28 +020046#include <haproxy/pattern.h>
Willy Tarreau469509b2020-06-04 15:13:30 +020047#include <haproxy/payload.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020048#include <haproxy/proxy-t.h>
Willy Tarreaue6ce10b2020-06-04 15:33:47 +020049#include <haproxy/sample.h>
Willy Tarreau1e56f922020-06-04 23:20:13 +020050#include <haproxy/server-t.h>
Willy Tarreau48d25b32020-06-04 18:58:52 +020051#include <haproxy/session.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020052#include <haproxy/stats-t.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020053#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020054#include <haproxy/stream_interface.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +020055#include <haproxy/task.h>
Willy Tarreau8b550af2020-06-04 17:42:48 +020056#include <haproxy/tcp_rules.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020057#include <haproxy/tools.h>
Willy Tarreaua1718922020-06-04 16:25:31 +020058#include <haproxy/vars.h>
Thierry FOURNIER380d0932015-01-23 14:27:52 +010059
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +010060/* Lua uses longjmp to perform yield or throwing errors. This
61 * macro is used only for identifying the function that can
62 * not return because a longjmp is executed.
63 * __LJMP marks a prototype of hlua file that can use longjmp.
64 * WILL_LJMP() marks an lua function that will use longjmp.
65 * MAY_LJMP() marks an lua function that may use longjmp.
66 */
67#define __LJMP
Willy Tarreau4e7cc332018-10-20 17:45:48 +020068#define WILL_LJMP(func) do { func; my_unreachable(); } while(0)
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +010069#define MAY_LJMP(func) func
70
Thierry FOURNIERbabae282015-09-17 11:36:37 +020071/* This couple of function executes securely some Lua calls outside of
72 * the lua runtime environment. Each Lua call can return a longjmp
73 * if it encounter a memory error.
74 *
75 * Lua documentation extract:
76 *
77 * If an error happens outside any protected environment, Lua calls
78 * a panic function (see lua_atpanic) and then calls abort, thus
79 * exiting the host application. Your panic function can avoid this
80 * exit by never returning (e.g., doing a long jump to your own
81 * recovery point outside Lua).
82 *
83 * The panic function runs as if it were a message handler (see
84 * §2.3); in particular, the error message is at the top of the
85 * stack. However, there is no guarantee about stack space. To push
86 * anything on the stack, the panic function must first check the
87 * available space (see §4.2).
88 *
89 * We must check all the Lua entry point. This includes:
90 * - The include/proto/hlua.h exported functions
91 * - the task wrapper function
92 * - The action wrapper function
93 * - The converters wrapper function
94 * - The sample-fetch wrapper functions
95 *
96 * It is tolerated that the initilisation function returns an abort.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -080097 * Before each Lua abort, an error message is written on stderr.
Thierry FOURNIERbabae282015-09-17 11:36:37 +020098 *
99 * The macro SET_SAFE_LJMP initialise the longjmp. The Macro
100 * RESET_SAFE_LJMP reset the longjmp. These function must be macro
101 * because they must be exists in the program stack when the longjmp
102 * is called.
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +0200103 *
104 * Note that the Lua processing is not really thread safe. It provides
105 * heavy system which consists to add our own lock function in the Lua
106 * code and recompile the library. This system will probably not accepted
107 * by maintainers of various distribs.
108 *
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500109 * Our main execution point of the Lua is the function lua_resume(). A
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +0200110 * quick looking on the Lua sources displays a lua_lock() a the start
111 * of function and a lua_unlock() at the end of the function. So I
112 * conclude that the Lua thread safe mode just perform a mutex around
113 * all execution. So I prefer to do this in the HAProxy code, it will be
114 * easier for distro maintainers.
115 *
116 * Note that the HAProxy lua functions rounded by the macro SET_SAFE_LJMP
117 * and RESET_SAFE_LJMP manipulates the Lua stack, so it will be careful
118 * to set mutex around these functions.
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200119 */
Willy Tarreau86abe442018-11-25 20:12:18 +0100120__decl_spinlock(hlua_global_lock);
Thierry FOURNIERffbad792017-07-12 11:39:04 +0200121THREAD_LOCAL jmp_buf safe_ljmp_env;
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200122static int hlua_panic_safe(lua_State *L) { return 0; }
Willy Tarreau9d6bb5a2020-02-06 15:55:41 +0100123static int hlua_panic_ljmp(lua_State *L) { WILL_LJMP(longjmp(safe_ljmp_env, 1)); }
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200124
125#define SET_SAFE_LJMP(__L) \
126 ({ \
127 int ret; \
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100128 HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200129 if (setjmp(safe_ljmp_env) != 0) { \
130 lua_atpanic(__L, hlua_panic_safe); \
131 ret = 0; \
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100132 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200133 } else { \
134 lua_atpanic(__L, hlua_panic_ljmp); \
135 ret = 1; \
136 } \
137 ret; \
138 })
139
140/* If we are the last function catching Lua errors, we
141 * must reset the panic function.
142 */
143#define RESET_SAFE_LJMP(__L) \
144 do { \
145 lua_atpanic(__L, hlua_panic_safe); \
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100146 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200147 } while(0)
148
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200149/* Applet status flags */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200150#define APPLET_DONE 0x01 /* applet processing is done. */
Christopher Faulet18c2e8d2019-03-01 12:02:08 +0100151/* unused: 0x02 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200152#define APPLET_HDR_SENT 0x04 /* Response header sent. */
Christopher Fauleta2097962019-07-15 16:25:33 +0200153/* unused: 0x08, 0x10 */
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +0100154#define APPLET_HTTP11 0x20 /* Last chunk sent. */
Christopher Faulet56a3d6e2019-02-27 22:06:23 +0100155#define APPLET_RSP_SENT 0x40 /* The response was fully sent */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200156
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100157/* The main Lua execution context. */
158struct hlua gL;
159
Thierry FOURNIERebed6e92016-12-16 11:54:07 +0100160/* This is the memory pool containing struct lua for applets
161 * (including cli).
162 */
Willy Tarreau8ceae722018-11-26 11:58:30 +0100163DECLARE_STATIC_POOL(pool_head_hlua, "hlua", sizeof(struct hlua));
Thierry FOURNIERebed6e92016-12-16 11:54:07 +0100164
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100165/* Used for Socket connection. */
166static struct proxy socket_proxy;
167static struct server socket_tcp;
168#ifdef USE_OPENSSL
169static struct server socket_ssl;
170#endif
171
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +0100172/* List head of the function called at the initialisation time. */
173struct list hlua_init_functions = LIST_HEAD_INIT(hlua_init_functions);
174
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +0100175/* The following variables contains the reference of the different
176 * Lua classes. These references are useful for identify metadata
177 * associated with an object.
178 */
Thierry FOURNIER65f34c62015-02-16 20:11:43 +0100179static int class_txn_ref;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100180static int class_socket_ref;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +0100181static int class_channel_ref;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +0100182static int class_fetches_ref;
Thierry FOURNIER594afe72015-03-10 23:58:30 +0100183static int class_converters_ref;
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100184static int class_http_ref;
Thierry FOURNIER3def3932015-04-07 11:27:54 +0200185static int class_map_ref;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200186static int class_applet_tcp_ref;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200187static int class_applet_http_ref;
Christopher Faulet700d9e82020-01-31 12:21:52 +0100188static int class_txn_reply_ref;
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +0100189
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100190/* Global Lua execution timeout. By default Lua, execution linked
Willy Tarreau87b09662015-04-03 00:22:06 +0200191 * with stream (actions, sample-fetches and converters) have a
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100192 * short timeout. Lua linked with tasks doesn't have a timeout
193 * because a task may remain alive during all the haproxy execution.
194 */
195static unsigned int hlua_timeout_session = 4000; /* session timeout. */
196static unsigned int hlua_timeout_task = TICK_ETERNITY; /* task timeout. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200197static unsigned int hlua_timeout_applet = 4000; /* applet timeout. */
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100198
Thierry FOURNIERee9f8022015-03-03 17:37:37 +0100199/* Interrupts the Lua processing each "hlua_nb_instruction" instructions.
200 * it is used for preventing infinite loops.
201 *
202 * I test the scheer with an infinite loop containing one incrementation
203 * and one test. I run this loop between 10 seconds, I raise a ceil of
204 * 710M loops from one interrupt each 9000 instructions, so I fix the value
205 * to one interrupt each 10 000 instructions.
206 *
207 * configured | Number of
208 * instructions | loops executed
209 * between two | in milions
210 * forced yields |
211 * ---------------+---------------
212 * 10 | 160
213 * 500 | 670
214 * 1000 | 680
215 * 5000 | 700
216 * 7000 | 700
217 * 8000 | 700
218 * 9000 | 710 <- ceil
219 * 10000 | 710
220 * 100000 | 710
221 * 1000000 | 710
222 *
223 */
224static unsigned int hlua_nb_instruction = 10000;
225
Willy Tarreau32f61e22015-03-18 17:54:59 +0100226/* Descriptor for the memory allocation state. If limit is not null, it will
227 * be enforced on any memory allocation.
228 */
229struct hlua_mem_allocator {
230 size_t allocated;
231 size_t limit;
232};
233
234static struct hlua_mem_allocator hlua_global_allocator;
235
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100236/* These functions converts types between HAProxy internal args or
237 * sample and LUA types. Another function permits to check if the
238 * LUA stack contains arguments according with an required ARG_T
239 * format.
240 */
241static int hlua_arg2lua(lua_State *L, const struct arg *arg);
242static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100243__LJMP static int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp,
David Carlier0c437f42016-04-27 16:21:56 +0100244 uint64_t mask, struct proxy *p);
Willy Tarreau5eadada2015-03-10 17:28:54 +0100245static int hlua_smp2lua(lua_State *L, struct sample *smp);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100246static int hlua_smp2lua_str(lua_State *L, struct sample *smp);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100247static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp);
248
Christopher Faulet9d1332b2020-02-24 16:46:16 +0100249__LJMP static int hlua_http_get_headers(lua_State *L, struct http_msg *msg);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200250
Thierry FOURNIER23bc3752015-09-11 19:15:43 +0200251#define SEND_ERR(__be, __fmt, __args...) \
252 do { \
253 send_log(__be, LOG_ERR, __fmt, ## __args); \
254 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) \
Christopher Faulet767a84b2017-11-24 16:50:31 +0100255 ha_alert(__fmt, ## __args); \
Thierry FOURNIER23bc3752015-09-11 19:15:43 +0200256 } while (0)
257
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100258/* Used to check an Lua function type in the stack. It creates and
259 * returns a reference of the function. This function throws an
260 * error if the rgument is not a "function".
261 */
262__LJMP unsigned int hlua_checkfunction(lua_State *L, int argno)
263{
264 if (!lua_isfunction(L, argno)) {
Thierry FOURNIERfd1e9552018-02-23 18:41:18 +0100265 const char *msg = lua_pushfstring(L, "function expected, got %s", luaL_typename(L, argno));
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100266 WILL_LJMP(luaL_argerror(L, argno, msg));
267 }
268 lua_pushvalue(L, argno);
269 return luaL_ref(L, LUA_REGISTRYINDEX);
270}
271
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200272/* Return the string that is of the top of the stack. */
273const char *hlua_get_top_error_string(lua_State *L)
274{
275 if (lua_gettop(L) < 1)
276 return "unknown error";
277 if (lua_type(L, -1) != LUA_TSTRING)
278 return "unknown error";
279 return lua_tostring(L, -1);
280}
281
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200282__LJMP static const char *hlua_traceback(lua_State *L)
283{
284 lua_Debug ar;
285 int level = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +0200286 struct buffer *msg = get_trash_chunk();
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200287 int filled = 0;
288
289 while (lua_getstack(L, level++, &ar)) {
290
291 /* Add separator */
292 if (filled)
293 chunk_appendf(msg, ", ");
294 filled = 1;
295
296 /* Fill fields:
297 * 'S': fills in the fields source, short_src, linedefined, lastlinedefined, and what;
298 * 'l': fills in the field currentline;
299 * 'n': fills in the field name and namewhat;
300 * 't': fills in the field istailcall;
301 */
302 lua_getinfo(L, "Slnt", &ar);
303
304 /* Append code localisation */
305 if (ar.currentline > 0)
306 chunk_appendf(msg, "%s:%d ", ar.short_src, ar.currentline);
307 else
308 chunk_appendf(msg, "%s ", ar.short_src);
309
310 /*
311 * Get function name
312 *
313 * if namewhat is no empty, name is defined.
314 * what contains "Lua" for Lua function, "C" for C function,
315 * or "main" for main code.
316 */
317 if (*ar.namewhat != '\0' && ar.name != NULL) /* is there a name from code? */
318 chunk_appendf(msg, "%s '%s'", ar.namewhat, ar.name); /* use it */
319
320 else if (*ar.what == 'm') /* "main", the code is not executed in a function */
321 chunk_appendf(msg, "main chunk");
322
323 else if (*ar.what != 'C') /* for Lua functions, use <file:line> */
324 chunk_appendf(msg, "C function line %d", ar.linedefined);
325
326 else /* nothing left... */
327 chunk_appendf(msg, "?");
328
329
330 /* Display tailed call */
331 if (ar.istailcall)
332 chunk_appendf(msg, " ...");
333 }
334
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200335 return msg->area;
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200336}
337
338
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100339/* This function check the number of arguments available in the
340 * stack. If the number of arguments available is not the same
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500341 * then <nb> an error is thrown.
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100342 */
343__LJMP static inline void check_args(lua_State *L, int nb, char *fcn)
344{
345 if (lua_gettop(L) == nb)
346 return;
347 WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb));
348}
349
Mark Lakes22154b42018-01-29 14:38:40 -0800350/* This function pushes an error string prefixed by the file name
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100351 * and the line number where the error is encountered.
352 */
353static int hlua_pusherror(lua_State *L, const char *fmt, ...)
354{
355 va_list argp;
356 va_start(argp, fmt);
357 luaL_where(L, 1);
358 lua_pushvfstring(L, fmt, argp);
359 va_end(argp);
360 lua_concat(L, 2);
361 return 1;
362}
363
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100364/* This functions is used with sample fetch and converters. It
365 * converts the HAProxy configuration argument in a lua stack
366 * values.
367 *
368 * It takes an array of "arg", and each entry of the array is
369 * converted and pushed in the LUA stack.
370 */
371static int hlua_arg2lua(lua_State *L, const struct arg *arg)
372{
373 switch (arg->type) {
374 case ARGT_SINT:
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100375 case ARGT_TIME:
376 case ARGT_SIZE:
Thierry FOURNIERf90838b2015-03-06 13:48:32 +0100377 lua_pushinteger(L, arg->data.sint);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100378 break;
379
380 case ARGT_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200381 lua_pushlstring(L, arg->data.str.area, arg->data.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100382 break;
383
384 case ARGT_IPV4:
385 case ARGT_IPV6:
386 case ARGT_MSK4:
387 case ARGT_MSK6:
388 case ARGT_FE:
389 case ARGT_BE:
390 case ARGT_TAB:
391 case ARGT_SRV:
392 case ARGT_USR:
393 case ARGT_MAP:
394 default:
395 lua_pushnil(L);
396 break;
397 }
398 return 1;
399}
400
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500401/* This function take one entry in an LUA stack at the index "ud",
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100402 * and try to convert it in an HAProxy argument entry. This is useful
Ilya Shipitsind4259502020-04-08 01:07:56 +0500403 * with sample fetch wrappers. The input arguments are given to the
404 * lua wrapper and converted as arg list by the function.
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100405 */
406static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg)
407{
408 switch (lua_type(L, ud)) {
409
410 case LUA_TNUMBER:
411 case LUA_TBOOLEAN:
412 arg->type = ARGT_SINT;
413 arg->data.sint = lua_tointeger(L, ud);
414 break;
415
416 case LUA_TSTRING:
417 arg->type = ARGT_STR;
Tim Duesterhus2e89dec2019-09-29 23:03:08 +0200418 arg->data.str.area = (char *)lua_tolstring(L, ud, &arg->data.str.data);
Tim Duesterhus29d2e8a2019-09-29 23:03:07 +0200419 /* We don't know the actual size of the underlying allocation, so be conservative. */
420 arg->data.str.size = arg->data.str.data;
421 arg->data.str.head = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100422 break;
423
424 case LUA_TUSERDATA:
425 case LUA_TNIL:
426 case LUA_TTABLE:
427 case LUA_TFUNCTION:
428 case LUA_TTHREAD:
429 case LUA_TLIGHTUSERDATA:
430 arg->type = ARGT_SINT;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +0200431 arg->data.sint = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100432 break;
433 }
434 return 1;
435}
436
437/* the following functions are used to convert a struct sample
438 * in Lua type. This useful to convert the return of the
Ilya Shipitsind4259502020-04-08 01:07:56 +0500439 * fetches or converters.
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100440 */
Willy Tarreau5eadada2015-03-10 17:28:54 +0100441static int hlua_smp2lua(lua_State *L, struct sample *smp)
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100442{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200443 switch (smp->data.type) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100444 case SMP_T_SINT:
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100445 case SMP_T_BOOL:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200446 lua_pushinteger(L, smp->data.u.sint);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100447 break;
448
449 case SMP_T_BIN:
450 case SMP_T_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200451 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100452 break;
453
454 case SMP_T_METH:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200455 switch (smp->data.u.meth.meth) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100456 case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break;
457 case HTTP_METH_GET: lua_pushstring(L, "GET"); break;
458 case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break;
459 case HTTP_METH_POST: lua_pushstring(L, "POST"); break;
460 case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break;
461 case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break;
462 case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break;
463 case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break;
464 case HTTP_METH_OTHER:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200465 lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100466 break;
467 default:
468 lua_pushnil(L);
469 break;
470 }
471 break;
472
473 case SMP_T_IPV4:
474 case SMP_T_IPV6:
475 case SMP_T_ADDR: /* This type is never used to qualify a sample. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200476 if (sample_casts[smp->data.type][SMP_T_STR] &&
477 sample_casts[smp->data.type][SMP_T_STR](smp))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200478 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Willy Tarreau5eadada2015-03-10 17:28:54 +0100479 else
480 lua_pushnil(L);
481 break;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100482 default:
483 lua_pushnil(L);
484 break;
485 }
486 return 1;
487}
488
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100489/* the following functions are used to convert a struct sample
490 * in Lua strings. This is useful to convert the return of the
Ilya Shipitsind4259502020-04-08 01:07:56 +0500491 * fetches or converters.
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100492 */
493static int hlua_smp2lua_str(lua_State *L, struct sample *smp)
494{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200495 switch (smp->data.type) {
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100496
497 case SMP_T_BIN:
498 case SMP_T_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200499 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100500 break;
501
502 case SMP_T_METH:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200503 switch (smp->data.u.meth.meth) {
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100504 case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break;
505 case HTTP_METH_GET: lua_pushstring(L, "GET"); break;
506 case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break;
507 case HTTP_METH_POST: lua_pushstring(L, "POST"); break;
508 case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break;
509 case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break;
510 case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break;
511 case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break;
512 case HTTP_METH_OTHER:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200513 lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100514 break;
515 default:
516 lua_pushstring(L, "");
517 break;
518 }
519 break;
520
521 case SMP_T_SINT:
522 case SMP_T_BOOL:
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100523 case SMP_T_IPV4:
524 case SMP_T_IPV6:
525 case SMP_T_ADDR: /* This type is never used to qualify a sample. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200526 if (sample_casts[smp->data.type][SMP_T_STR] &&
527 sample_casts[smp->data.type][SMP_T_STR](smp))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200528 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100529 else
530 lua_pushstring(L, "");
531 break;
532 default:
533 lua_pushstring(L, "");
534 break;
535 }
536 return 1;
537}
538
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100539/* the following functions are used to convert an Lua type in a
540 * struct sample. This is useful to provide data from a converter
541 * to the LUA code.
542 */
543static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp)
544{
545 switch (lua_type(L, ud)) {
546
547 case LUA_TNUMBER:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200548 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200549 smp->data.u.sint = lua_tointeger(L, ud);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100550 break;
551
552
553 case LUA_TBOOLEAN:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200554 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200555 smp->data.u.sint = lua_toboolean(L, ud);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100556 break;
557
558 case LUA_TSTRING:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200559 smp->data.type = SMP_T_STR;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100560 smp->flags |= SMP_F_CONST;
Tim Duesterhus2e89dec2019-09-29 23:03:08 +0200561 smp->data.u.str.area = (char *)lua_tolstring(L, ud, &smp->data.u.str.data);
Tim Duesterhus29d2e8a2019-09-29 23:03:07 +0200562 /* We don't know the actual size of the underlying allocation, so be conservative. */
563 smp->data.u.str.size = smp->data.u.str.data;
564 smp->data.u.str.head = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100565 break;
566
567 case LUA_TUSERDATA:
568 case LUA_TNIL:
569 case LUA_TTABLE:
570 case LUA_TFUNCTION:
571 case LUA_TTHREAD:
572 case LUA_TLIGHTUSERDATA:
Thierry FOURNIER93405e12015-08-26 14:19:03 +0200573 case LUA_TNONE:
574 default:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200575 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200576 smp->data.u.sint = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100577 break;
578 }
579 return 1;
580}
581
Ilya Shipitsind4259502020-04-08 01:07:56 +0500582/* This function check the "argp" built by another conversion function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800583 * is in accord with the expected argp defined by the "mask". The function
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100584 * returns true or false. It can be adjust the types if there compatibles.
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100585 *
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500586 * This function assumes that the argp argument contains ARGM_NBARGS + 1
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100587 * entries.
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100588 */
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100589__LJMP int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp,
David Carlier0c437f42016-04-27 16:21:56 +0100590 uint64_t mask, struct proxy *p)
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100591{
592 int min_arg;
593 int idx;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100594 struct proxy *px;
595 char *sname, *pname;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100596
597 idx = 0;
598 min_arg = ARGM(mask);
599 mask >>= ARGM_BITS;
600
601 while (1) {
602
603 /* Check oversize. */
604 if (idx >= ARGM_NBARGS && argp[idx].type != ARGT_STOP) {
Cyril Bonté577a36a2015-03-02 00:08:38 +0100605 WILL_LJMP(luaL_argerror(L, first + idx, "Malformed argument mask"));
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100606 }
607
608 /* Check for mandatory arguments. */
609 if (argp[idx].type == ARGT_STOP) {
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100610 if (idx < min_arg) {
611
612 /* If miss other argument than the first one, we return an error. */
613 if (idx > 0)
614 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
615
616 /* If first argument have a certain type, some default values
617 * may be used. See the function smp_resolve_args().
618 */
619 switch (mask & ARGT_MASK) {
620
621 case ARGT_FE:
622 if (!(p->cap & PR_CAP_FE))
623 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
624 argp[idx].data.prx = p;
625 argp[idx].type = ARGT_FE;
626 argp[idx+1].type = ARGT_STOP;
627 break;
628
629 case ARGT_BE:
630 if (!(p->cap & PR_CAP_BE))
631 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
632 argp[idx].data.prx = p;
633 argp[idx].type = ARGT_BE;
634 argp[idx+1].type = ARGT_STOP;
635 break;
636
637 case ARGT_TAB:
638 argp[idx].data.prx = p;
639 argp[idx].type = ARGT_TAB;
640 argp[idx+1].type = ARGT_STOP;
641 break;
642
643 default:
644 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
645 break;
646 }
647 }
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100648 return 0;
649 }
650
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500651 /* Check for exceed the number of required argument. */
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100652 if ((mask & ARGT_MASK) == ARGT_STOP &&
653 argp[idx].type != ARGT_STOP) {
654 WILL_LJMP(luaL_argerror(L, first + idx, "Last argument expected"));
655 }
656
657 if ((mask & ARGT_MASK) == ARGT_STOP &&
658 argp[idx].type == ARGT_STOP) {
659 return 0;
660 }
661
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100662 /* Convert some argument types. */
663 switch (mask & ARGT_MASK) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100664 case ARGT_SINT:
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100665 if (argp[idx].type != ARGT_SINT)
666 WILL_LJMP(luaL_argerror(L, first + idx, "integer expected"));
667 argp[idx].type = ARGT_SINT;
668 break;
669
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100670 case ARGT_TIME:
671 if (argp[idx].type != ARGT_SINT)
672 WILL_LJMP(luaL_argerror(L, first + idx, "integer expected"));
Thierry FOURNIER29176f32015-07-07 00:41:29 +0200673 argp[idx].type = ARGT_TIME;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100674 break;
675
676 case ARGT_SIZE:
677 if (argp[idx].type != ARGT_SINT)
678 WILL_LJMP(luaL_argerror(L, first + idx, "integer expected"));
Thierry FOURNIER29176f32015-07-07 00:41:29 +0200679 argp[idx].type = ARGT_SIZE;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100680 break;
681
682 case ARGT_FE:
683 if (argp[idx].type != ARGT_STR)
684 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200685 memcpy(trash.area, argp[idx].data.str.area,
686 argp[idx].data.str.data);
687 trash.area[argp[idx].data.str.data] = 0;
688 argp[idx].data.prx = proxy_fe_by_name(trash.area);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100689 if (!argp[idx].data.prx)
690 WILL_LJMP(luaL_argerror(L, first + idx, "frontend doesn't exist"));
691 argp[idx].type = ARGT_FE;
692 break;
693
694 case ARGT_BE:
695 if (argp[idx].type != ARGT_STR)
696 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200697 memcpy(trash.area, argp[idx].data.str.area,
698 argp[idx].data.str.data);
699 trash.area[argp[idx].data.str.data] = 0;
700 argp[idx].data.prx = proxy_be_by_name(trash.area);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100701 if (!argp[idx].data.prx)
702 WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist"));
703 argp[idx].type = ARGT_BE;
704 break;
705
706 case ARGT_TAB:
707 if (argp[idx].type != ARGT_STR)
708 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200709 memcpy(trash.area, argp[idx].data.str.area,
710 argp[idx].data.str.data);
711 trash.area[argp[idx].data.str.data] = 0;
Tim Duesterhus9fe7c632019-09-29 23:03:09 +0200712 argp[idx].data.t = stktable_find_by_name(trash.area);
713 if (!argp[idx].data.t)
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100714 WILL_LJMP(luaL_argerror(L, first + idx, "table doesn't exist"));
715 argp[idx].type = ARGT_TAB;
716 break;
717
718 case ARGT_SRV:
719 if (argp[idx].type != ARGT_STR)
720 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200721 memcpy(trash.area, argp[idx].data.str.area,
722 argp[idx].data.str.data);
723 trash.area[argp[idx].data.str.data] = 0;
724 sname = strrchr(trash.area, '/');
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100725 if (sname) {
726 *sname++ = '\0';
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200727 pname = trash.area;
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200728 px = proxy_be_by_name(pname);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100729 if (!px)
730 WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist"));
731 }
732 else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200733 sname = trash.area;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100734 px = p;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100735 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100736 argp[idx].data.srv = findserver(px, sname);
737 if (!argp[idx].data.srv)
738 WILL_LJMP(luaL_argerror(L, first + idx, "server doesn't exist"));
739 argp[idx].type = ARGT_SRV;
740 break;
741
742 case ARGT_IPV4:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200743 memcpy(trash.area, argp[idx].data.str.area,
744 argp[idx].data.str.data);
745 trash.area[argp[idx].data.str.data] = 0;
746 if (inet_pton(AF_INET, trash.area, &argp[idx].data.ipv4))
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100747 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 address"));
748 argp[idx].type = ARGT_IPV4;
749 break;
750
751 case ARGT_MSK4:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200752 memcpy(trash.area, argp[idx].data.str.area,
753 argp[idx].data.str.data);
754 trash.area[argp[idx].data.str.data] = 0;
755 if (!str2mask(trash.area, &argp[idx].data.ipv4))
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100756 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 mask"));
757 argp[idx].type = ARGT_MSK4;
758 break;
759
760 case ARGT_IPV6:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200761 memcpy(trash.area, argp[idx].data.str.area,
762 argp[idx].data.str.data);
763 trash.area[argp[idx].data.str.data] = 0;
764 if (inet_pton(AF_INET6, trash.area, &argp[idx].data.ipv6))
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100765 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 address"));
766 argp[idx].type = ARGT_IPV6;
767 break;
768
769 case ARGT_MSK6:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200770 memcpy(trash.area, argp[idx].data.str.area,
771 argp[idx].data.str.data);
772 trash.area[argp[idx].data.str.data] = 0;
773 if (!str2mask6(trash.area, &argp[idx].data.ipv6))
Tim Duesterhusb814da62018-01-25 16:24:50 +0100774 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 mask"));
775 argp[idx].type = ARGT_MSK6;
776 break;
777
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100778 case ARGT_MAP:
779 case ARGT_REG:
780 case ARGT_USR:
781 WILL_LJMP(luaL_argerror(L, first + idx, "type not yet supported"));
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100782 break;
783 }
784
785 /* Check for type of argument. */
786 if ((mask & ARGT_MASK) != argp[idx].type) {
787 const char *msg = lua_pushfstring(L, "'%s' expected, got '%s'",
788 arg_type_names[(mask & ARGT_MASK)],
789 arg_type_names[argp[idx].type & ARGT_MASK]);
790 WILL_LJMP(luaL_argerror(L, first + idx, msg));
791 }
792
793 /* Next argument. */
794 mask >>= ARGT_BITS;
795 idx++;
796 }
797}
798
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100799/*
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500800 * The following functions are used to make correspondence between the the
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100801 * executed lua pointer and the "struct hlua *" that contain the context.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100802 *
803 * - hlua_gethlua : return the hlua context associated with an lua_State.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100804 * - hlua_sethlua : create the association between hlua context and lua_state.
805 */
806static inline struct hlua *hlua_gethlua(lua_State *L)
807{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +0100808 struct hlua **hlua = lua_getextraspace(L);
809 return *hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100810}
811static inline void hlua_sethlua(struct hlua *hlua)
812{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +0100813 struct hlua **hlua_store = lua_getextraspace(hlua->T);
814 *hlua_store = hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100815}
816
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100817/* This function is used to send logs. It try to send on screen (stderr)
818 * and on the default syslog server.
819 */
820static inline void hlua_sendlog(struct proxy *px, int level, const char *msg)
821{
822 struct tm tm;
823 char *p;
824
825 /* Cleanup the log message. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200826 p = trash.area;
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100827 for (; *msg != '\0'; msg++, p++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200828 if (p >= trash.area + trash.size - 1) {
Thierry FOURNIERccf00632015-09-16 12:47:03 +0200829 /* Break the message if exceed the buffer size. */
830 *(p-4) = ' ';
831 *(p-3) = '.';
832 *(p-2) = '.';
833 *(p-1) = '.';
834 break;
835 }
Willy Tarreau90807112020-02-25 08:16:33 +0100836 if (isprint((unsigned char)*msg))
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100837 *p = *msg;
838 else
839 *p = '.';
840 }
841 *p = '\0';
842
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200843 send_log(px, level, "%s\n", trash.area);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100844 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
Willy Tarreaua678b432015-08-28 10:14:59 +0200845 get_localtime(date.tv_sec, &tm);
846 fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n",
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100847 log_levels[level], tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec,
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200848 (int)getpid(), trash.area);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100849 fflush(stderr);
850 }
851}
852
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100853/* This function just ensure that the yield will be always
854 * returned with a timeout and permit to set some flags
855 */
856__LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx,
Thierry FOURNIERf90838b2015-03-06 13:48:32 +0100857 lua_KFunction k, int timeout, unsigned int flags)
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100858{
859 struct hlua *hlua = hlua_gethlua(L);
860
861 /* Set the wake timeout. If timeout is required, we set
862 * the expiration time.
863 */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +0200864 hlua->wake_time = timeout;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100865
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +0100866 hlua->flags |= flags;
867
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100868 /* Process the yield. */
Willy Tarreau9635e032018-10-16 17:52:55 +0200869 MAY_LJMP(lua_yieldk(L, nresults, ctx, k));
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100870}
871
Willy Tarreau87b09662015-04-03 00:22:06 +0200872/* This function initialises the Lua environment stored in the stream.
873 * It must be called at the start of the stream. This function creates
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100874 * an LUA coroutine. It can not be use to crete the main LUA context.
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200875 *
876 * This function is particular. it initialises a new Lua thread. If the
877 * initialisation fails (example: out of memory error), the lua function
878 * throws an error (longjmp).
879 *
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100880 * In some case (at least one), this function can be called from safe
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500881 * environment, so we must not initialise it. While the support of
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100882 * threads appear, the safe environment set a lock to ensure only one
883 * Lua execution at a time. If we initialize safe environment in another
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500884 * safe environment, we have a dead lock.
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100885 *
886 * set "already_safe" true if the context is initialized form safe
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500887 * Lua function.
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100888 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800889 * This function manipulates two Lua stacks: the main and the thread. Only
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200890 * the main stack can fail. The thread is not manipulated. This function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800891 * MUST NOT manipulate the created thread stack state, because it is not
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500892 * protected against errors thrown by the thread stack.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100893 */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100894int hlua_ctx_init(struct hlua *lua, struct task *task, int already_safe)
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100895{
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100896 if (!already_safe) {
897 if (!SET_SAFE_LJMP(gL.T)) {
898 lua->Tref = LUA_REFNIL;
899 return 0;
900 }
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200901 }
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100902 lua->Mref = LUA_REFNIL;
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +0100903 lua->flags = 0;
Willy Tarreauf31af932020-01-14 09:59:38 +0100904 lua->gc_count = 0;
Christopher Fauletbc275a92020-02-26 14:55:16 +0100905 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100906 LIST_INIT(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100907 lua->T = lua_newthread(gL.T);
908 if (!lua->T) {
909 lua->Tref = LUA_REFNIL;
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100910 if (!already_safe)
911 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100912 return 0;
913 }
914 hlua_sethlua(lua);
915 lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
916 lua->task = task;
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100917 if (!already_safe)
918 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100919 return 1;
920}
921
Willy Tarreau87b09662015-04-03 00:22:06 +0200922/* Used to destroy the Lua coroutine when the attached stream or task
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100923 * is destroyed. The destroy also the memory context. The struct "lua"
924 * is not freed.
925 */
926void hlua_ctx_destroy(struct hlua *lua)
927{
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100928 if (!lua)
Thierry FOURNIERa718b292015-03-04 16:48:34 +0100929 return;
930
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100931 if (!lua->T)
932 goto end;
933
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100934 /* Purge all the pending signals. */
Thierry FOURNIERd6975962017-07-12 14:31:10 +0200935 notification_purge(&lua->com);
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100936
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200937 if (!SET_SAFE_LJMP(lua->T))
938 return;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100939 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200940 RESET_SAFE_LJMP(lua->T);
Thierry FOURNIER5a50a852015-09-23 16:59:28 +0200941
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200942 if (!SET_SAFE_LJMP(gL.T))
943 return;
944 luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref);
945 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER5a50a852015-09-23 16:59:28 +0200946 /* Forces a garbage collecting process. If the Lua program is finished
947 * without error, we run the GC on the thread pointer. Its freed all
948 * the unused memory.
949 * If the thread is finnish with an error or is currently yielded,
950 * it seems that the GC applied on the thread doesn't clean anything,
951 * so e run the GC on the main thread.
952 * NOTE: maybe this action locks all the Lua threads untiml the en of
953 * the garbage collection.
954 */
Willy Tarreauf31af932020-01-14 09:59:38 +0100955 if (lua->gc_count) {
Thierry FOURNIER7bd10d52017-07-17 00:44:40 +0200956 if (!SET_SAFE_LJMP(gL.T))
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200957 return;
Thierry FOURNIER7bd10d52017-07-17 00:44:40 +0200958 lua_gc(gL.T, LUA_GCCOLLECT, 0);
959 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +0200960 }
Thierry FOURNIER5a50a852015-09-23 16:59:28 +0200961
Thierry FOURNIERa7b536b2015-09-21 22:50:24 +0200962 lua->T = NULL;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100963
964end:
Willy Tarreaubafbe012017-11-24 17:34:44 +0100965 pool_free(pool_head_hlua, lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100966}
967
968/* This function is used to restore the Lua context when a coroutine
969 * fails. This function copy the common memory between old coroutine
970 * and the new coroutine. The old coroutine is destroyed, and its
971 * replaced by the new coroutine.
972 * If the flag "keep_msg" is set, the last entry of the old is assumed
973 * as string error message and it is copied in the new stack.
974 */
975static int hlua_ctx_renew(struct hlua *lua, int keep_msg)
976{
977 lua_State *T;
978 int new_ref;
979
980 /* Renew the main LUA stack doesn't have sense. */
981 if (lua == &gL)
982 return 0;
983
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100984 /* New Lua coroutine. */
985 T = lua_newthread(gL.T);
986 if (!T)
987 return 0;
988
989 /* Copy last error message. */
990 if (keep_msg)
991 lua_xmove(lua->T, T, 1);
992
993 /* Copy data between the coroutines. */
994 lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref);
995 lua_xmove(lua->T, T, 1);
996 new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Valur poped. */
997
998 /* Destroy old data. */
999 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
1000
1001 /* The thread is garbage collected by Lua. */
1002 luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref);
1003
1004 /* Fill the struct with the new coroutine values. */
1005 lua->Mref = new_ref;
1006 lua->T = T;
1007 lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
1008
1009 /* Set context. */
1010 hlua_sethlua(lua);
1011
1012 return 1;
1013}
1014
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001015void hlua_hook(lua_State *L, lua_Debug *ar)
1016{
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001017 struct hlua *hlua = hlua_gethlua(L);
1018
1019 /* Lua cannot yield when its returning from a function,
1020 * so, we can fix the interrupt hook to 1 instruction,
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001021 * expecting that the function is finished.
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001022 */
1023 if (lua_gethookmask(L) & LUA_MASKRET) {
1024 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1);
1025 return;
1026 }
1027
1028 /* restore the interrupt condition. */
1029 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
1030
1031 /* If we interrupt the Lua processing in yieldable state, we yield.
1032 * If the state is not yieldable, trying yield causes an error.
1033 */
1034 if (lua_isyieldable(L))
Willy Tarreau9635e032018-10-16 17:52:55 +02001035 MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD));
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001036
Thierry FOURNIERa85cfb12015-03-13 14:50:06 +01001037 /* If we cannot yield, update the clock and check the timeout. */
1038 tv_update_date(0, 1);
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001039 hlua->run_time += now_ms - hlua->start_time;
1040 if (hlua->max_time && hlua->run_time >= hlua->max_time) {
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001041 lua_pushfstring(L, "execution timeout");
1042 WILL_LJMP(lua_error(L));
1043 }
1044
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001045 /* Update the start time. */
1046 hlua->start_time = now_ms;
1047
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001048 /* Try to interrupt the process at the end of the current
1049 * unyieldable function.
1050 */
1051 lua_sethook(hlua->T, hlua_hook, LUA_MASKRET|LUA_MASKCOUNT, hlua_nb_instruction);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001052}
1053
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001054/* This function start or resumes the Lua stack execution. If the flag
1055 * "yield_allowed" if no set and the LUA stack execution returns a yield
1056 * The function return an error.
1057 *
1058 * The function can returns 4 values:
1059 * - HLUA_E_OK : The execution is terminated without any errors.
1060 * - HLUA_E_AGAIN : The execution must continue at the next associated
1061 * task wakeup.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001062 * - HLUA_E_ERRMSG : An error has occurred, an error message is set in
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001063 * the top of the stack.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001064 * - HLUA_E_ERR : An error has occurred without error message.
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001065 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001066 * If an error occurred, the stack is renewed and it is ready to run new
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001067 * LUA code.
1068 */
1069static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed)
1070{
1071 int ret;
1072 const char *msg;
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001073 const char *trace;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001074
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001075 /* Initialise run time counter. */
1076 if (!HLUA_IS_RUNNING(lua))
1077 lua->run_time = 0;
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001078
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001079 /* Lock the whole Lua execution. This lock must be before the
1080 * label "resume_execution".
1081 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001082 HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock);
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001083
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001084resume_execution:
1085
1086 /* This hook interrupts the Lua processing each 'hlua_nb_instruction'
1087 * instructions. it is used for preventing infinite loops.
1088 */
1089 lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
1090
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +01001091 /* Remove all flags except the running flags. */
Thierry FOURNIER2f3867f2015-09-28 01:02:01 +02001092 HLUA_SET_RUN(lua);
1093 HLUA_CLR_CTRLYIELD(lua);
1094 HLUA_CLR_WAKERESWR(lua);
1095 HLUA_CLR_WAKEREQWR(lua);
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +01001096
Christopher Fauletbc275a92020-02-26 14:55:16 +01001097 /* Update the start time and reset wake_time. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001098 lua->start_time = now_ms;
Christopher Fauletbc275a92020-02-26 14:55:16 +01001099 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001100
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001101 /* Call the function. */
1102 ret = lua_resume(lua->T, gL.T, lua->nargs);
1103 switch (ret) {
1104
1105 case LUA_OK:
1106 ret = HLUA_E_OK;
1107 break;
1108
1109 case LUA_YIELD:
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001110 /* Check if the execution timeout is expired. It it is the case, we
1111 * break the Lua execution.
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001112 */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001113 tv_update_date(0, 1);
1114 lua->run_time += now_ms - lua->start_time;
1115 if (lua->max_time && lua->run_time > lua->max_time) {
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001116 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001117 ret = HLUA_E_ETMOUT;
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001118 break;
1119 }
1120 /* Process the forced yield. if the general yield is not allowed or
1121 * if no task were associated this the current Lua execution
1122 * coroutine, we resume the execution. Else we want to return in the
1123 * scheduler and we want to be waked up again, to continue the
1124 * current Lua execution. So we schedule our own task.
1125 */
1126 if (HLUA_IS_CTRLYIELDING(lua)) {
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001127 if (!yield_allowed || !lua->task)
1128 goto resume_execution;
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001129 task_wakeup(lua->task, TASK_WOKEN_MSG);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001130 }
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001131 if (!yield_allowed) {
1132 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001133 ret = HLUA_E_YIELD;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001134 break;
1135 }
1136 ret = HLUA_E_AGAIN;
1137 break;
1138
1139 case LUA_ERRRUN:
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001140
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001141 /* Special exit case. The traditional exit is returned as an error
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001142 * because the errors ares the only one mean to return immediately
1143 * from and lua execution.
1144 */
1145 if (lua->flags & HLUA_EXIT) {
1146 ret = HLUA_E_OK;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01001147 hlua_ctx_renew(lua, 1);
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001148 break;
1149 }
1150
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001151 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001152 if (!lua_checkstack(lua->T, 1)) {
1153 ret = HLUA_E_ERR;
1154 break;
1155 }
1156 msg = lua_tostring(lua->T, -1);
1157 lua_settop(lua->T, 0); /* Empty the stack. */
1158 lua_pop(lua->T, 1);
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001159 trace = hlua_traceback(lua->T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001160 if (msg)
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001161 lua_pushfstring(lua->T, "runtime error: %s from %s", msg, trace);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001162 else
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001163 lua_pushfstring(lua->T, "unknown runtime error from %s", trace);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001164 ret = HLUA_E_ERRMSG;
1165 break;
1166
1167 case LUA_ERRMEM:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001168 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001169 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001170 ret = HLUA_E_NOMEM;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001171 break;
1172
1173 case LUA_ERRERR:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001174 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001175 if (!lua_checkstack(lua->T, 1)) {
1176 ret = HLUA_E_ERR;
1177 break;
1178 }
1179 msg = lua_tostring(lua->T, -1);
1180 lua_settop(lua->T, 0); /* Empty the stack. */
1181 lua_pop(lua->T, 1);
1182 if (msg)
1183 lua_pushfstring(lua->T, "message handler error: %s", msg);
1184 else
1185 lua_pushfstring(lua->T, "message handler error");
1186 ret = HLUA_E_ERRMSG;
1187 break;
1188
1189 default:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001190 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001191 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001192 ret = HLUA_E_ERR;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001193 break;
1194 }
1195
1196 switch (ret) {
1197 case HLUA_E_AGAIN:
1198 break;
1199
1200 case HLUA_E_ERRMSG:
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001201 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001202 hlua_ctx_renew(lua, 1);
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001203 HLUA_CLR_RUN(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001204 break;
1205
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001206 case HLUA_E_ETMOUT:
1207 case HLUA_E_NOMEM:
1208 case HLUA_E_YIELD:
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001209 case HLUA_E_ERR:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001210 HLUA_CLR_RUN(lua);
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001211 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001212 hlua_ctx_renew(lua, 0);
1213 break;
1214
1215 case HLUA_E_OK:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001216 HLUA_CLR_RUN(lua);
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001217 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001218 break;
1219 }
1220
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001221 /* This is the main exit point, remove the Lua lock. */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001222 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock);
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001223
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001224 return ret;
1225}
1226
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001227/* This function exit the current code. */
1228__LJMP static int hlua_done(lua_State *L)
1229{
1230 struct hlua *hlua = hlua_gethlua(L);
1231
1232 hlua->flags |= HLUA_EXIT;
1233 WILL_LJMP(lua_error(L));
1234
1235 return 0;
1236}
1237
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001238/* This function is an LUA binding. It provides a function
1239 * for deleting ACL from a referenced ACL file.
1240 */
1241__LJMP static int hlua_del_acl(lua_State *L)
1242{
1243 const char *name;
1244 const char *key;
1245 struct pat_ref *ref;
1246
1247 MAY_LJMP(check_args(L, 2, "del_acl"));
1248
1249 name = MAY_LJMP(luaL_checkstring(L, 1));
1250 key = MAY_LJMP(luaL_checkstring(L, 2));
1251
1252 ref = pat_ref_lookup(name);
1253 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001254 WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001255
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001256 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001257 pat_ref_delete(ref, key);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001258 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001259 return 0;
1260}
1261
1262/* This function is an LUA binding. It provides a function
1263 * for deleting map entry from a referenced map file.
1264 */
1265static int hlua_del_map(lua_State *L)
1266{
1267 const char *name;
1268 const char *key;
1269 struct pat_ref *ref;
1270
1271 MAY_LJMP(check_args(L, 2, "del_map"));
1272
1273 name = MAY_LJMP(luaL_checkstring(L, 1));
1274 key = MAY_LJMP(luaL_checkstring(L, 2));
1275
1276 ref = pat_ref_lookup(name);
1277 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001278 WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001279
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001280 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001281 pat_ref_delete(ref, key);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001282 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001283 return 0;
1284}
1285
1286/* This function is an LUA binding. It provides a function
1287 * for adding ACL pattern from a referenced ACL file.
1288 */
1289static int hlua_add_acl(lua_State *L)
1290{
1291 const char *name;
1292 const char *key;
1293 struct pat_ref *ref;
1294
1295 MAY_LJMP(check_args(L, 2, "add_acl"));
1296
1297 name = MAY_LJMP(luaL_checkstring(L, 1));
1298 key = MAY_LJMP(luaL_checkstring(L, 2));
1299
1300 ref = pat_ref_lookup(name);
1301 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001302 WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001303
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001304 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001305 if (pat_ref_find_elt(ref, key) == NULL)
1306 pat_ref_add(ref, key, NULL, NULL);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001307 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001308 return 0;
1309}
1310
1311/* This function is an LUA binding. It provides a function
1312 * for setting map pattern and sample from a referenced map
1313 * file.
1314 */
1315static int hlua_set_map(lua_State *L)
1316{
1317 const char *name;
1318 const char *key;
1319 const char *value;
1320 struct pat_ref *ref;
1321
1322 MAY_LJMP(check_args(L, 3, "set_map"));
1323
1324 name = MAY_LJMP(luaL_checkstring(L, 1));
1325 key = MAY_LJMP(luaL_checkstring(L, 2));
1326 value = MAY_LJMP(luaL_checkstring(L, 3));
1327
1328 ref = pat_ref_lookup(name);
1329 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001330 WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001331
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001332 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001333 if (pat_ref_find_elt(ref, key) != NULL)
1334 pat_ref_set(ref, key, value, NULL);
1335 else
1336 pat_ref_add(ref, key, value, NULL);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001337 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001338 return 0;
1339}
1340
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001341/* A class is a lot of memory that contain data. This data can be a table,
1342 * an integer or user data. This data is associated with a metatable. This
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001343 * metatable have an original version registered in the global context with
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001344 * the name of the object (_G[<name>] = <metable> ).
1345 *
1346 * A metable is a table that modify the standard behavior of a standard
1347 * access to the associated data. The entries of this new metatable are
1348 * defined as is:
1349 *
1350 * http://lua-users.org/wiki/MetatableEvents
1351 *
1352 * __index
1353 *
1354 * we access an absent field in a table, the result is nil. This is
1355 * true, but it is not the whole truth. Actually, such access triggers
1356 * the interpreter to look for an __index metamethod: If there is no
1357 * such method, as usually happens, then the access results in nil;
1358 * otherwise, the metamethod will provide the result.
1359 *
1360 * Control 'prototype' inheritance. When accessing "myTable[key]" and
1361 * the key does not appear in the table, but the metatable has an __index
1362 * property:
1363 *
1364 * - if the value is a function, the function is called, passing in the
1365 * table and the key; the return value of that function is returned as
1366 * the result.
1367 *
1368 * - if the value is another table, the value of the key in that table is
1369 * asked for and returned (and if it doesn't exist in that table, but that
1370 * table's metatable has an __index property, then it continues on up)
1371 *
1372 * - Use "rawget(myTable,key)" to skip this metamethod.
1373 *
1374 * http://www.lua.org/pil/13.4.1.html
1375 *
1376 * __newindex
1377 *
1378 * Like __index, but control property assignment.
1379 *
1380 * __mode - Control weak references. A string value with one or both
1381 * of the characters 'k' and 'v' which specifies that the the
1382 * keys and/or values in the table are weak references.
1383 *
1384 * __call - Treat a table like a function. When a table is followed by
1385 * parenthesis such as "myTable( 'foo' )" and the metatable has
1386 * a __call key pointing to a function, that function is invoked
1387 * (passing any specified arguments) and the return value is
1388 * returned.
1389 *
1390 * __metatable - Hide the metatable. When "getmetatable( myTable )" is
1391 * called, if the metatable for myTable has a __metatable
1392 * key, the value of that key is returned instead of the
1393 * actual metatable.
1394 *
1395 * __tostring - Control string representation. When the builtin
1396 * "tostring( myTable )" function is called, if the metatable
1397 * for myTable has a __tostring property set to a function,
1398 * that function is invoked (passing myTable to it) and the
1399 * return value is used as the string representation.
1400 *
1401 * __len - Control table length. When the table length is requested using
1402 * the length operator ( '#' ), if the metatable for myTable has
1403 * a __len key pointing to a function, that function is invoked
1404 * (passing myTable to it) and the return value used as the value
1405 * of "#myTable".
1406 *
1407 * __gc - Userdata finalizer code. When userdata is set to be garbage
1408 * collected, if the metatable has a __gc field pointing to a
1409 * function, that function is first invoked, passing the userdata
1410 * to it. The __gc metamethod is not called for tables.
1411 * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html)
1412 *
1413 * Special metamethods for redefining standard operators:
1414 * http://www.lua.org/pil/13.1.html
1415 *
1416 * __add "+"
1417 * __sub "-"
1418 * __mul "*"
1419 * __div "/"
1420 * __unm "!"
1421 * __pow "^"
1422 * __concat ".."
1423 *
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001424 * Special methods for redefining standard relations
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001425 * http://www.lua.org/pil/13.2.html
1426 *
1427 * __eq "=="
1428 * __lt "<"
1429 * __le "<="
1430 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001431
1432/*
1433 *
1434 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001435 * Class Map
1436 *
1437 *
1438 */
1439
1440/* Returns a struct hlua_map if the stack entry "ud" is
1441 * a class session, otherwise it throws an error.
1442 */
1443__LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud)
1444{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001445 return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001446}
1447
1448/* This function is the map constructor. It don't need
1449 * the class Map object. It creates and return a new Map
1450 * object. It must be called only during "body" or "init"
1451 * context because it process some filesystem accesses.
1452 */
1453__LJMP static int hlua_map_new(struct lua_State *L)
1454{
1455 const char *fn;
1456 int match = PAT_MATCH_STR;
1457 struct sample_conv conv;
1458 const char *file = "";
1459 int line = 0;
1460 lua_Debug ar;
1461 char *err = NULL;
1462 struct arg args[2];
1463
1464 if (lua_gettop(L) < 1 || lua_gettop(L) > 2)
1465 WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument."));
1466
1467 fn = MAY_LJMP(luaL_checkstring(L, 1));
1468
1469 if (lua_gettop(L) >= 2) {
1470 match = MAY_LJMP(luaL_checkinteger(L, 2));
1471 if (match < 0 || match >= PAT_MATCH_NUM)
1472 WILL_LJMP(luaL_error(L, "'new' needs a valid match method."));
1473 }
1474
1475 /* Get Lua filename and line number. */
1476 if (lua_getstack(L, 1, &ar)) { /* check function at level */
1477 lua_getinfo(L, "Sl", &ar); /* get info about it */
1478 if (ar.currentline > 0) { /* is there info? */
1479 file = ar.short_src;
1480 line = ar.currentline;
1481 }
1482 }
1483
1484 /* fill fake sample_conv struct. */
1485 conv.kw = ""; /* unused. */
1486 conv.process = NULL; /* unused. */
1487 conv.arg_mask = 0; /* unused. */
1488 conv.val_args = NULL; /* unused. */
1489 conv.out_type = SMP_T_STR;
1490 conv.private = (void *)(long)match;
1491 switch (match) {
1492 case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break;
1493 case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break;
1494 case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break;
1495 case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break;
1496 case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break;
1497 case PAT_MATCH_END: conv.in_type = SMP_T_STR; break;
1498 case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001499 case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001500 case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break;
1501 default:
1502 WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode."));
1503 }
1504
1505 /* fill fake args. */
1506 args[0].type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001507 args[0].data.str.area = (char *)fn;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001508 args[1].type = ARGT_STOP;
1509
1510 /* load the map. */
1511 if (!sample_load_map(args, &conv, file, line, &err)) {
1512 /* error case: we cant use luaL_error because we must
1513 * free the err variable.
1514 */
1515 luaL_where(L, 1);
1516 lua_pushfstring(L, "'new': %s.", err);
1517 lua_concat(L, 2);
1518 free(err);
1519 WILL_LJMP(lua_error(L));
1520 }
1521
1522 /* create the lua object. */
1523 lua_newtable(L);
1524 lua_pushlightuserdata(L, args[0].data.map);
1525 lua_rawseti(L, -2, 0);
1526
1527 /* Pop a class Map metatable and affect it to the userdata. */
1528 lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref);
1529 lua_setmetatable(L, -2);
1530
1531
1532 return 1;
1533}
1534
1535__LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str)
1536{
1537 struct map_descriptor *desc;
1538 struct pattern *pat;
1539 struct sample smp;
1540
1541 MAY_LJMP(check_args(L, 2, "lookup"));
1542 desc = MAY_LJMP(hlua_checkmap(L, 1));
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001543 if (desc->pat.expect_type == SMP_T_SINT) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001544 smp.data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001545 smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001546 }
1547 else {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001548 smp.data.type = SMP_T_STR;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001549 smp.flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001550 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 +02001551 }
1552
1553 pat = pattern_exec_match(&desc->pat, &smp, 1);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001554 if (!pat || !pat->data) {
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001555 if (str)
1556 lua_pushstring(L, "");
1557 else
1558 lua_pushnil(L);
1559 return 1;
1560 }
1561
1562 /* The Lua pattern must return a string, so we can't check the returned type */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001563 lua_pushlstring(L, pat->data->u.str.area, pat->data->u.str.data);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001564 return 1;
1565}
1566
1567__LJMP static int hlua_map_lookup(struct lua_State *L)
1568{
1569 return _hlua_map_lookup(L, 0);
1570}
1571
1572__LJMP static int hlua_map_slookup(struct lua_State *L)
1573{
1574 return _hlua_map_lookup(L, 1);
1575}
1576
1577/*
1578 *
1579 *
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001580 * Class Socket
1581 *
1582 *
1583 */
1584
1585__LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud)
1586{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001587 return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001588}
1589
1590/* This function is the handler called for each I/O on the established
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001591 * connection. It is used for notify space available to send or data
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001592 * received.
1593 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001594static void hlua_socket_handler(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001595{
Willy Tarreau00a37f02015-04-13 12:05:19 +02001596 struct stream_interface *si = appctx->owner;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001597
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001598 if (appctx->ctx.hlua_cosocket.die) {
1599 si_shutw(si);
1600 si_shutr(si);
1601 si_ic(si)->flags |= CF_READ_NULL;
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001602 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1603 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001604 stream_shutdown(si_strm(si), SF_ERR_KILLED);
1605 }
1606
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001607 /* If we cant write, wakeup the pending write signals. */
1608 if (channel_output_closed(si_ic(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001609 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001610
1611 /* If we cant read, wakeup the pending read signals. */
1612 if (channel_input_closed(si_oc(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001613 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001614
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02001615 /* if the connection is not established, inform the stream that we want
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001616 * to be notified whenever the connection completes.
1617 */
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02001618 if (si_opposite(si)->state < SI_ST_EST) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01001619 si_cant_get(si);
Willy Tarreau12c24232018-12-06 15:29:50 +01001620 si_rx_conn_blk(si);
Willy Tarreau3367d412018-11-15 10:57:41 +01001621 si_rx_endp_more(si);
Willy Tarreaud4da1962015-04-20 01:31:23 +02001622 return;
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001623 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001624
1625 /* This function is called after the connect. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01001626 appctx->ctx.hlua_cosocket.connected = 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001627
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001628 /* Wake the tasks which wants to write if the buffer have available space. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001629 if (channel_may_recv(si_ic(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001630 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001631
1632 /* Wake the tasks which wants to read if the buffer contains data. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001633 if (!channel_is_empty(si_oc(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001634 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001635
1636 /* Some data were injected in the buffer, notify the stream
1637 * interface.
1638 */
1639 if (!channel_is_empty(si_ic(si)))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01001640 si_update(si);
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001641
1642 /* If write notifications are registered, we considers we want
Willy Tarreau3367d412018-11-15 10:57:41 +01001643 * to write, so we clear the blocking flag.
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001644 */
1645 if (notification_registered(&appctx->ctx.hlua_cosocket.wake_on_write))
Willy Tarreau3367d412018-11-15 10:57:41 +01001646 si_rx_endp_more(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001647}
1648
Willy Tarreau87b09662015-04-03 00:22:06 +02001649/* This function is called when the "struct stream" is destroyed.
1650 * Remove the link from the object to this stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001651 * Wake all the pending signals.
1652 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001653static void hlua_socket_release(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001654{
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001655 struct xref *peer;
1656
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001657 /* Remove my link in the original object. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001658 peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref);
1659 if (peer)
1660 xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001661
1662 /* Wake all the task waiting for me. */
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001663 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1664 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001665}
1666
1667/* If the garbage collectio of the object is launch, nobody
Willy Tarreau87b09662015-04-03 00:22:06 +02001668 * uses this object. If the stream does not exists, just quit.
1669 * Send the shutdown signal to the stream. In some cases,
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001670 * pending signal can rest in the read and write lists. destroy
1671 * it.
1672 */
1673__LJMP static int hlua_socket_gc(lua_State *L)
1674{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001675 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001676 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001677 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001678
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001679 MAY_LJMP(check_args(L, 1, "__gc"));
1680
1681 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001682 peer = xref_get_peer_and_lock(&socket->xref);
1683 if (!peer)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001684 return 0;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001685 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001686
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001687 /* Set the flag which destroy the session. */
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001688 appctx->ctx.hlua_cosocket.die = 1;
1689 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001690
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001691 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001692 xref_disconnect(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001693 return 0;
1694}
1695
1696/* The close function send shutdown signal and break the
Willy Tarreau87b09662015-04-03 00:22:06 +02001697 * links between the stream and the object.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001698 */
sada05ed3302018-05-11 11:48:18 -07001699__LJMP static int hlua_socket_close_helper(lua_State *L)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001700{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001701 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001702 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001703 struct xref *peer;
Willy Tarreauf31af932020-01-14 09:59:38 +01001704 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001705
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001706 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001707
1708 /* Check if we run on the same thread than the xreator thread.
1709 * We cannot access to the socket if the thread is different.
1710 */
1711 if (socket->tid != tid)
1712 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1713
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001714 peer = xref_get_peer_and_lock(&socket->xref);
1715 if (!peer)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001716 return 0;
Willy Tarreauf31af932020-01-14 09:59:38 +01001717
1718 hlua->gc_count--;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001719 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001720
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001721 /* Set the flag which destroy the session. */
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001722 appctx->ctx.hlua_cosocket.die = 1;
1723 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001724
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001725 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001726 xref_disconnect(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001727 return 0;
1728}
1729
sada05ed3302018-05-11 11:48:18 -07001730/* The close function calls close_helper.
1731 */
1732__LJMP static int hlua_socket_close(lua_State *L)
1733{
1734 MAY_LJMP(check_args(L, 1, "close"));
1735 return hlua_socket_close_helper(L);
1736}
1737
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001738/* This Lua function assumes that the stack contain three parameters.
1739 * 1 - USERDATA containing a struct socket
1740 * 2 - INTEGER with values of the macro defined below
1741 * If the integer is -1, we must read at most one line.
1742 * If the integer is -2, we ust read all the data until the
1743 * end of the stream.
1744 * If the integer is positive value, we must read a number of
1745 * bytes corresponding to this value.
1746 */
1747#define HLSR_READ_LINE (-1)
1748#define HLSR_READ_ALL (-2)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001749__LJMP static int hlua_socket_receive_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001750{
1751 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
1752 int wanted = lua_tointeger(L, 2);
1753 struct hlua *hlua = hlua_gethlua(L);
1754 struct appctx *appctx;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001755 size_t len;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001756 int nblk;
Willy Tarreau206ba832018-06-14 15:27:31 +02001757 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001758 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02001759 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001760 size_t len2;
Thierry FOURNIER00543922015-03-09 18:35:06 +01001761 int skip_at_end = 0;
Willy Tarreau81389672015-03-10 12:03:52 +01001762 struct channel *oc;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001763 struct stream_interface *si;
1764 struct stream *s;
1765 struct xref *peer;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001766 int missing_bytes;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001767
1768 /* Check if this lua stack is schedulable. */
1769 if (!hlua || !hlua->task)
1770 WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in "
1771 "'frontend', 'backend' or 'task'"));
1772
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001773 /* Check if we run on the same thread than the xreator thread.
1774 * We cannot access to the socket if the thread is different.
1775 */
1776 if (socket->tid != tid)
1777 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1778
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001779 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001780 peer = xref_get_peer_and_lock(&socket->xref);
1781 if (!peer)
1782 goto no_peer;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001783 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
1784 si = appctx->owner;
1785 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001786
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001787 oc = &s->res;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001788 if (wanted == HLSR_READ_LINE) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001789 /* Read line. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001790 nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001791 if (nblk < 0) /* Connection close. */
1792 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001793 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001794 goto connection_empty;
Thierry FOURNIER00543922015-03-09 18:35:06 +01001795
1796 /* remove final \r\n. */
1797 if (nblk == 1) {
1798 if (blk1[len1-1] == '\n') {
1799 len1--;
1800 skip_at_end++;
1801 if (blk1[len1-1] == '\r') {
1802 len1--;
1803 skip_at_end++;
1804 }
1805 }
1806 }
1807 else {
1808 if (blk2[len2-1] == '\n') {
1809 len2--;
1810 skip_at_end++;
1811 if (blk2[len2-1] == '\r') {
1812 len2--;
1813 skip_at_end++;
1814 }
1815 }
1816 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001817 }
1818
1819 else if (wanted == HLSR_READ_ALL) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001820 /* Read all the available data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001821 nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001822 if (nblk < 0) /* Connection close. */
1823 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001824 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001825 goto connection_empty;
1826 }
1827
1828 else {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001829 /* Read a block of data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001830 nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001831 if (nblk < 0) /* Connection close. */
1832 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001833 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001834 goto connection_empty;
1835
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001836 missing_bytes = wanted - socket->b.n;
1837 if (len1 > missing_bytes) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001838 nblk = 1;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001839 len1 = missing_bytes;
1840 } if (nblk == 2 && len1 + len2 > missing_bytes)
1841 len2 = missing_bytes - len1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001842 }
1843
1844 len = len1;
1845
1846 luaL_addlstring(&socket->b, blk1, len1);
1847 if (nblk == 2) {
1848 len += len2;
1849 luaL_addlstring(&socket->b, blk2, len2);
1850 }
1851
1852 /* Consume data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001853 co_skip(oc, len + skip_at_end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001854
1855 /* Don't wait anything. */
Thierry FOURNIER7e4ee472018-05-25 15:03:50 +02001856 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001857
1858 /* If the pattern reclaim to read all the data
1859 * in the connection, got out.
1860 */
1861 if (wanted == HLSR_READ_ALL)
1862 goto connection_empty;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001863 else if (wanted >= 0 && socket->b.n < wanted)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001864 goto connection_empty;
1865
1866 /* Return result. */
1867 luaL_pushresult(&socket->b);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001868 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001869 return 1;
1870
1871connection_closed:
1872
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001873 xref_unlock(&socket->xref, peer);
1874
1875no_peer:
1876
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001877 /* If the buffer containds data. */
1878 if (socket->b.n > 0) {
1879 luaL_pushresult(&socket->b);
1880 return 1;
1881 }
1882 lua_pushnil(L);
1883 lua_pushstring(L, "connection closed.");
1884 return 2;
1885
1886connection_empty:
1887
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001888 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) {
1889 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001890 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001891 }
1892 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02001893 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_receive_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001894 return 0;
1895}
1896
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001897/* This Lua function gets two parameters. The first one can be string
1898 * or a number. If the string is "*l", the user requires one line. If
1899 * the string is "*a", the user requires all the contents of the stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001900 * If the value is a number, the user require a number of bytes equal
1901 * to the value. The default value is "*l" (a line).
1902 *
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001903 * This parameter with a variable type is converted in integer. This
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001904 * integer takes this values:
1905 * -1 : read a line
1906 * -2 : read all the stream
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001907 * >0 : amount of bytes.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001908 *
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001909 * The second parameter is optional. It contains a string that must be
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001910 * concatenated with the read data.
1911 */
1912__LJMP static int hlua_socket_receive(struct lua_State *L)
1913{
1914 int wanted = HLSR_READ_LINE;
1915 const char *pattern;
1916 int type;
1917 char *error;
1918 size_t len;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001919 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001920
1921 if (lua_gettop(L) < 1 || lua_gettop(L) > 3)
1922 WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments."));
1923
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001924 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001925
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001926 /* Check if we run on the same thread than the xreator thread.
1927 * We cannot access to the socket if the thread is different.
1928 */
1929 if (socket->tid != tid)
1930 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1931
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001932 /* check for pattern. */
1933 if (lua_gettop(L) >= 2) {
1934 type = lua_type(L, 2);
1935 if (type == LUA_TSTRING) {
1936 pattern = lua_tostring(L, 2);
1937 if (strcmp(pattern, "*a") == 0)
1938 wanted = HLSR_READ_ALL;
1939 else if (strcmp(pattern, "*l") == 0)
1940 wanted = HLSR_READ_LINE;
1941 else {
1942 wanted = strtoll(pattern, &error, 10);
1943 if (*error != '\0')
1944 WILL_LJMP(luaL_error(L, "Unsupported pattern."));
1945 }
1946 }
1947 else if (type == LUA_TNUMBER) {
1948 wanted = lua_tointeger(L, 2);
1949 if (wanted < 0)
1950 WILL_LJMP(luaL_error(L, "Unsupported size."));
1951 }
1952 }
1953
1954 /* Set pattern. */
1955 lua_pushinteger(L, wanted);
Tim Duesterhusc6e377e2018-01-04 19:32:13 +01001956
1957 /* Check if we would replace the top by itself. */
1958 if (lua_gettop(L) != 2)
1959 lua_replace(L, 2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001960
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001961 /* init buffer, and fill it with prefix. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001962 luaL_buffinit(L, &socket->b);
1963
1964 /* Check prefix. */
1965 if (lua_gettop(L) >= 3) {
1966 if (lua_type(L, 3) != LUA_TSTRING)
1967 WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix"));
1968 pattern = lua_tolstring(L, 3, &len);
1969 luaL_addlstring(&socket->b, pattern, len);
1970 }
1971
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001972 return __LJMP(hlua_socket_receive_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001973}
1974
1975/* Write the Lua input string in the output buffer.
Mark Lakes22154b42018-01-29 14:38:40 -08001976 * This function returns a yield if no space is available.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001977 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001978static int hlua_socket_write_yield(struct lua_State *L,int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001979{
1980 struct hlua_socket *socket;
1981 struct hlua *hlua = hlua_gethlua(L);
1982 struct appctx *appctx;
1983 size_t buf_len;
1984 const char *buf;
1985 int len;
1986 int send_len;
1987 int sent;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001988 struct xref *peer;
1989 struct stream_interface *si;
1990 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001991
1992 /* Check if this lua stack is schedulable. */
1993 if (!hlua || !hlua->task)
1994 WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in "
1995 "'frontend', 'backend' or 'task'"));
1996
1997 /* Get object */
1998 socket = MAY_LJMP(hlua_checksocket(L, 1));
1999 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002000 sent = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002001
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002002 /* Check if we run on the same thread than the xreator thread.
2003 * We cannot access to the socket if the thread is different.
2004 */
2005 if (socket->tid != tid)
2006 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2007
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002008 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002009 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002010 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002011 lua_pushinteger(L, -1);
2012 return 1;
2013 }
2014 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2015 si = appctx->owner;
2016 s = si_strm(si);
2017
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002018 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002019 if (channel_output_closed(&s->req)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002020 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002021 lua_pushinteger(L, -1);
2022 return 1;
2023 }
2024
2025 /* Update the input buffer data. */
2026 buf += sent;
2027 send_len = buf_len - sent;
2028
2029 /* All the data are sent. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002030 if (sent >= buf_len) {
2031 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002032 return 1; /* Implicitly return the length sent. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002033 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002034
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002035 /* Check if the buffer is available because HAProxy doesn't allocate
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01002036 * the request buffer if its not required.
2037 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002038 if (s->req.buf.size == 0) {
Willy Tarreau581abd32018-10-25 10:21:41 +02002039 if (!si_alloc_ibuf(si, &appctx->buffer_wait))
Christopher Faulet33834b12016-12-19 09:29:06 +01002040 goto hlua_socket_write_yield_return;
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01002041 }
2042
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002043 /* Check for available space. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002044 len = b_room(&s->req.buf);
Christopher Faulet33834b12016-12-19 09:29:06 +01002045 if (len <= 0) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002046 goto hlua_socket_write_yield_return;
Christopher Faulet33834b12016-12-19 09:29:06 +01002047 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002048
2049 /* send data */
2050 if (len < send_len)
2051 send_len = len;
Thierry FOURNIER66b89192018-05-27 01:14:47 +02002052 len = ci_putblk(&s->req, buf, send_len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002053
2054 /* "Not enough space" (-1), "Buffer too little to contain
2055 * the data" (-2) are not expected because the available length
2056 * is tested.
2057 * Other unknown error are also not expected.
2058 */
2059 if (len <= 0) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01002060 if (len == -1)
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002061 s->req.flags |= CF_WAKE_WRITE;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002062
sada05ed3302018-05-11 11:48:18 -07002063 MAY_LJMP(hlua_socket_close_helper(L));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002064 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002065 lua_pushinteger(L, -1);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002066 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002067 return 1;
2068 }
2069
2070 /* update buffers. */
Thierry FOURNIER101b9762018-05-27 01:27:40 +02002071 appctx_wakeup(appctx);
Willy Tarreaude70fa12015-09-26 11:25:05 +02002072
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002073 s->req.rex = TICK_ETERNITY;
2074 s->res.wex = TICK_ETERNITY;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002075
2076 /* Update length sent. */
2077 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002078 lua_pushinteger(L, sent + len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002079
2080 /* All the data buffer is sent ? */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002081 if (sent + len >= buf_len) {
2082 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002083 return 1;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002084 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002085
2086hlua_socket_write_yield_return:
Thierry FOURNIERba42fcd2018-05-27 00:59:48 +02002087 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2088 xref_unlock(&socket->xref, peer);
2089 WILL_LJMP(luaL_error(L, "out of memory"));
2090 }
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002091 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002092 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_write_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002093 return 0;
2094}
2095
2096/* This function initiate the send of data. It just check the input
2097 * parameters and push an integer in the Lua stack that contain the
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002098 * amount of data written to the buffer. This is used by the function
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002099 * "hlua_socket_write_yield" that can yield.
2100 *
2101 * The Lua function gets between 3 and 4 parameters. The first one is
2102 * the associated object. The second is a string buffer. The third is
2103 * a facultative integer that represents where is the buffer position
2104 * of the start of the data that can send. The first byte is the
2105 * position "1". The default value is "1". The fourth argument is a
2106 * facultative integer that represents where is the buffer position
2107 * of the end of the data that can send. The default is the last byte.
2108 */
2109static int hlua_socket_send(struct lua_State *L)
2110{
2111 int i;
2112 int j;
2113 const char *buf;
2114 size_t buf_len;
2115
2116 /* Check number of arguments. */
2117 if (lua_gettop(L) < 2 || lua_gettop(L) > 4)
2118 WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments"));
2119
2120 /* Get the string. */
2121 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
2122
2123 /* Get and check j. */
2124 if (lua_gettop(L) == 4) {
2125 j = MAY_LJMP(luaL_checkinteger(L, 4));
2126 if (j < 0)
2127 j = buf_len + j + 1;
2128 if (j > buf_len)
2129 j = buf_len + 1;
2130 lua_pop(L, 1);
2131 }
2132 else
2133 j = buf_len;
2134
2135 /* Get and check i. */
2136 if (lua_gettop(L) == 3) {
2137 i = MAY_LJMP(luaL_checkinteger(L, 3));
2138 if (i < 0)
2139 i = buf_len + i + 1;
2140 if (i > buf_len)
2141 i = buf_len + 1;
2142 lua_pop(L, 1);
2143 } else
2144 i = 1;
2145
2146 /* Check bth i and j. */
2147 if (i > j) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002148 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002149 return 1;
2150 }
2151 if (i == 0 && j == 0) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002152 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002153 return 1;
2154 }
2155 if (i == 0)
2156 i = 1;
2157 if (j == 0)
2158 j = 1;
2159
2160 /* Pop the string. */
2161 lua_pop(L, 1);
2162
2163 /* Update the buffer length. */
2164 buf += i - 1;
2165 buf_len = j - i + 1;
2166 lua_pushlstring(L, buf, buf_len);
2167
2168 /* This unsigned is used to remember the amount of sent data. */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002169 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002170
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002171 return MAY_LJMP(hlua_socket_write_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002172}
2173
Willy Tarreau22b0a682015-06-17 19:43:49 +02002174#define SOCKET_INFO_MAX_LEN sizeof("[0000:0000:0000:0000:0000:0000:0000:0000]:12345")
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002175__LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr)
2176{
2177 static char buffer[SOCKET_INFO_MAX_LEN];
2178 int ret;
2179 int len;
2180 char *p;
2181
2182 ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1);
2183 if (ret <= 0) {
2184 lua_pushnil(L);
2185 return 1;
2186 }
2187
2188 if (ret == AF_UNIX) {
2189 lua_pushstring(L, buffer+1);
2190 return 1;
2191 }
2192 else if (ret == AF_INET6) {
2193 buffer[0] = '[';
2194 len = strlen(buffer);
2195 buffer[len] = ']';
2196 len++;
2197 buffer[len] = ':';
2198 len++;
2199 p = buffer;
2200 }
2201 else if (ret == AF_INET) {
2202 p = buffer + 1;
2203 len = strlen(p);
2204 p[len] = ':';
2205 len++;
2206 }
2207 else {
2208 lua_pushnil(L);
2209 return 1;
2210 }
2211
2212 if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) {
2213 lua_pushnil(L);
2214 return 1;
2215 }
2216
2217 lua_pushstring(L, p);
2218 return 1;
2219}
2220
2221/* Returns information about the peer of the connection. */
2222__LJMP static int hlua_socket_getpeername(struct lua_State *L)
2223{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002224 struct hlua_socket *socket;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002225 struct xref *peer;
2226 struct appctx *appctx;
2227 struct stream_interface *si;
2228 struct stream *s;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002229 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002230
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002231 MAY_LJMP(check_args(L, 1, "getpeername"));
2232
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002233 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002234
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002235 /* Check if we run on the same thread than the xreator thread.
2236 * We cannot access to the socket if the thread is different.
2237 */
2238 if (socket->tid != tid)
2239 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2240
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002241 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002242 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002243 if (!peer) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002244 lua_pushnil(L);
2245 return 1;
2246 }
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002247 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2248 si = appctx->owner;
2249 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002250
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002251 if (!s->target_addr) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002252 xref_unlock(&socket->xref, peer);
Willy Tarreaua71f6422016-11-16 17:00:14 +01002253 lua_pushnil(L);
2254 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002255 }
2256
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002257 ret = MAY_LJMP(hlua_socket_info(L, s->target_addr));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002258 xref_unlock(&socket->xref, peer);
2259 return ret;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002260}
2261
2262/* Returns information about my connection side. */
2263static int hlua_socket_getsockname(struct lua_State *L)
2264{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002265 struct hlua_socket *socket;
2266 struct connection *conn;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002267 struct appctx *appctx;
2268 struct xref *peer;
2269 struct stream_interface *si;
2270 struct stream *s;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002271 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002272
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002273 MAY_LJMP(check_args(L, 1, "getsockname"));
2274
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002275 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002276
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002277 /* Check if we run on the same thread than the xreator thread.
2278 * We cannot access to the socket if the thread is different.
2279 */
2280 if (socket->tid != tid)
2281 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2282
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002283 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002284 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002285 if (!peer) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002286 lua_pushnil(L);
2287 return 1;
2288 }
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002289 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2290 si = appctx->owner;
2291 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002292
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002293 conn = cs_conn(objt_cs(s->si[1].end));
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002294 if (!conn || !conn_get_src(conn)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002295 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002296 lua_pushnil(L);
2297 return 1;
2298 }
2299
Willy Tarreau9da9a6f2019-07-17 14:49:44 +02002300 ret = hlua_socket_info(L, conn->src);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002301 xref_unlock(&socket->xref, peer);
2302 return ret;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002303}
2304
2305/* This struct define the applet. */
Willy Tarreau30576452015-04-13 13:50:30 +02002306static struct applet update_applet = {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002307 .obj_type = OBJ_TYPE_APPLET,
2308 .name = "<LUA_TCP>",
2309 .fct = hlua_socket_handler,
2310 .release = hlua_socket_release,
2311};
2312
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002313__LJMP static int hlua_socket_connect_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002314{
2315 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
2316 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002317 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002318 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002319 struct stream_interface *si;
2320 struct stream *s;
2321
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002322 /* Check if we run on the same thread than the xreator thread.
2323 * We cannot access to the socket if the thread is different.
2324 */
2325 if (socket->tid != tid)
2326 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2327
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002328 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002329 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002330 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002331 lua_pushnil(L);
2332 lua_pushstring(L, "Can't connect");
2333 return 2;
2334 }
2335 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2336 si = appctx->owner;
2337 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002338
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002339 /* Check if we run on the same thread than the xreator thread.
2340 * We cannot access to the socket if the thread is different.
2341 */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002342 if (socket->tid != tid) {
2343 xref_unlock(&socket->xref, peer);
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002344 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002345 }
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002346
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002347 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002348 if (!hlua || channel_output_closed(&s->req)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002349 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002350 lua_pushnil(L);
2351 lua_pushstring(L, "Can't connect");
2352 return 2;
2353 }
2354
Willy Tarreaue09101e2018-10-16 17:37:12 +02002355 appctx = __objt_appctx(s->si[0].end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002356
2357 /* Check for connection established. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002358 if (appctx->ctx.hlua_cosocket.connected) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002359 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002360 lua_pushinteger(L, 1);
2361 return 1;
2362 }
2363
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002364 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2365 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002366 WILL_LJMP(luaL_error(L, "out of memory error"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002367 }
2368 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002369 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002370 return 0;
2371}
2372
2373/* This function fail or initite the connection. */
2374__LJMP static int hlua_socket_connect(struct lua_State *L)
2375{
2376 struct hlua_socket *socket;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002377 int port = -1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002378 const char *ip;
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002379 struct hlua *hlua;
2380 struct appctx *appctx;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002381 int low, high;
2382 struct sockaddr_storage *addr;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002383 struct xref *peer;
2384 struct stream_interface *si;
2385 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002386
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002387 if (lua_gettop(L) < 2)
2388 WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments"));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002389
2390 /* Get args. */
2391 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002392
2393 /* Check if we run on the same thread than the xreator thread.
2394 * We cannot access to the socket if the thread is different.
2395 */
2396 if (socket->tid != tid)
2397 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2398
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002399 ip = MAY_LJMP(luaL_checkstring(L, 2));
Tim Duesterhus6edab862018-01-06 19:04:45 +01002400 if (lua_gettop(L) >= 3) {
2401 luaL_Buffer b;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002402 port = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002403
Tim Duesterhus6edab862018-01-06 19:04:45 +01002404 /* Force the ip to end with a colon, to support IPv6 addresses
2405 * that are not enclosed within square brackets.
2406 */
2407 if (port > 0) {
2408 luaL_buffinit(L, &b);
2409 luaL_addstring(&b, ip);
2410 luaL_addchar(&b, ':');
2411 luaL_pushresult(&b);
2412 ip = lua_tolstring(L, lua_gettop(L), NULL);
2413 }
2414 }
2415
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002416 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002417 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002418 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002419 lua_pushnil(L);
2420 return 1;
2421 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002422
2423 /* Parse ip address. */
Willy Tarreau48ef4c92017-01-06 18:32:38 +01002424 addr = str2sa_range(ip, NULL, &low, &high, NULL, NULL, NULL, 0);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002425 if (!addr) {
2426 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002427 WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002428 }
2429 if (low != high) {
2430 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002431 WILL_LJMP(luaL_error(L, "connect: port ranges not supported : address '%s'", ip));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002432 }
Willy Tarreau9da9a6f2019-07-17 14:49:44 +02002433
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002434 /* Set port. */
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002435 if (low == 0) {
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002436 if (addr->ss_family == AF_INET) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002437 if (port == -1) {
2438 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002439 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002440 }
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002441 ((struct sockaddr_in *)addr)->sin_port = htons(port);
2442 } else if (addr->ss_family == AF_INET6) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002443 if (port == -1) {
2444 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002445 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002446 }
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002447 ((struct sockaddr_in6 *)addr)->sin6_port = htons(port);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002448 }
2449 }
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002450
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002451 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2452 si = appctx->owner;
2453 s = si_strm(si);
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002454
2455 if (!sockaddr_alloc(&s->target_addr)) {
2456 xref_unlock(&socket->xref, peer);
2457 WILL_LJMP(luaL_error(L, "connect: internal error"));
2458 }
2459 *s->target_addr = *addr;
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002460 s->flags |= SF_ADDR_SET;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002461
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002462 hlua = hlua_gethlua(L);
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002463
2464 /* inform the stream that we want to be notified whenever the
2465 * connection completes.
2466 */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01002467 si_cant_get(&s->si[0]);
Willy Tarreau3367d412018-11-15 10:57:41 +01002468 si_rx_endp_more(&s->si[0]);
Thierry FOURNIER8c8fbbe2015-09-26 17:02:35 +02002469 appctx_wakeup(appctx);
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002470
Willy Tarreauf31af932020-01-14 09:59:38 +01002471 hlua->gc_count++;
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +02002472
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002473 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2474 xref_unlock(&socket->xref, peer);
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002475 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002476 }
2477 xref_unlock(&socket->xref, peer);
PiBa-NL706d5ee2018-05-05 23:51:42 +02002478
2479 task_wakeup(s->task, TASK_WOKEN_INIT);
2480 /* Return yield waiting for connection. */
2481
Willy Tarreau9635e032018-10-16 17:52:55 +02002482 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002483
2484 return 0;
2485}
2486
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002487#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002488__LJMP static int hlua_socket_connect_ssl(struct lua_State *L)
2489{
2490 struct hlua_socket *socket;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002491 struct xref *peer;
2492 struct appctx *appctx;
2493 struct stream_interface *si;
2494 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002495
2496 MAY_LJMP(check_args(L, 3, "connect_ssl"));
2497 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002498
2499 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002500 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002501 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002502 lua_pushnil(L);
2503 return 1;
2504 }
2505 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2506 si = appctx->owner;
2507 s = si_strm(si);
2508
2509 s->target = &socket_ssl.obj_type;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002510 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002511 return MAY_LJMP(hlua_socket_connect(L));
2512}
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002513#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002514
2515__LJMP static int hlua_socket_setoption(struct lua_State *L)
2516{
2517 return 0;
2518}
2519
2520__LJMP static int hlua_socket_settimeout(struct lua_State *L)
2521{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002522 struct hlua_socket *socket;
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002523 int tmout;
Mark Lakes56cc1252018-03-27 09:48:06 +02002524 double dtmout;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002525 struct xref *peer;
2526 struct appctx *appctx;
2527 struct stream_interface *si;
2528 struct stream *s;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002529
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002530 MAY_LJMP(check_args(L, 2, "settimeout"));
2531
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002532 socket = MAY_LJMP(hlua_checksocket(L, 1));
Mark Lakes56cc1252018-03-27 09:48:06 +02002533
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002534 /* convert the timeout to millis */
2535 dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002536
Thierry Fournier17a921b2018-03-08 09:59:02 +01002537 /* Check for negative values */
Mark Lakes56cc1252018-03-27 09:48:06 +02002538 if (dtmout < 0)
Thierry Fournier17a921b2018-03-08 09:59:02 +01002539 WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values"));
2540
Mark Lakes56cc1252018-03-27 09:48:06 +02002541 if (dtmout > INT_MAX) /* overflow check */
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002542 WILL_LJMP(luaL_error(L, "settimeout: cannot set values larger than %d ms", INT_MAX));
Mark Lakes56cc1252018-03-27 09:48:06 +02002543
2544 tmout = MS_TO_TICKS((int)dtmout);
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002545 if (tmout == 0)
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05002546 tmout++; /* very small timeouts are adjusted to a minimum of 1ms */
Mark Lakes56cc1252018-03-27 09:48:06 +02002547
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002548 /* Check if we run on the same thread than the xreator thread.
2549 * We cannot access to the socket if the thread is different.
2550 */
2551 if (socket->tid != tid)
2552 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2553
Mark Lakes56cc1252018-03-27 09:48:06 +02002554 /* check for connection break. If some data were read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002555 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002556 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002557 hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts.");
2558 WILL_LJMP(lua_error(L));
2559 return 0;
2560 }
2561 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2562 si = appctx->owner;
2563 s = si_strm(si);
2564
Cyril Bonté7bb63452018-08-17 23:51:02 +02002565 s->sess->fe->timeout.connect = tmout;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002566 s->req.rto = tmout;
2567 s->req.wto = tmout;
2568 s->res.rto = tmout;
2569 s->res.wto = tmout;
Cyril Bonté7bb63452018-08-17 23:51:02 +02002570 s->req.rex = tick_add_ifset(now_ms, tmout);
2571 s->req.wex = tick_add_ifset(now_ms, tmout);
2572 s->res.rex = tick_add_ifset(now_ms, tmout);
2573 s->res.wex = tick_add_ifset(now_ms, tmout);
2574
2575 s->task->expire = tick_add_ifset(now_ms, tmout);
2576 task_queue(s->task);
2577
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002578 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002579
Thierry Fourniere9636f12018-03-08 09:54:32 +01002580 lua_pushinteger(L, 1);
Tim Duesterhus119a5f12018-01-06 19:16:25 +01002581 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002582}
2583
2584__LJMP static int hlua_socket_new(lua_State *L)
2585{
2586 struct hlua_socket *socket;
2587 struct appctx *appctx;
Willy Tarreau15b5e142015-04-04 14:38:25 +02002588 struct session *sess;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002589 struct stream *strm;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002590
2591 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002592 if (!lua_checkstack(L, 3)) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002593 hlua_pusherror(L, "socket: full stack");
2594 goto out_fail_conf;
2595 }
2596
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002597 /* Create the object: obj[0] = userdata. */
2598 lua_newtable(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002599 socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket)));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002600 lua_rawseti(L, -2, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002601 memset(socket, 0, sizeof(*socket));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002602 socket->tid = tid;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002603
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05002604 /* Check if the various memory pools are initialized. */
Willy Tarreaubafbe012017-11-24 17:34:44 +01002605 if (!pool_head_stream || !pool_head_buffer) {
Thierry FOURNIER4a6170c2015-03-09 17:07:10 +01002606 hlua_pusherror(L, "socket: uninitialized pools.");
2607 goto out_fail_conf;
2608 }
2609
Willy Tarreau87b09662015-04-03 00:22:06 +02002610 /* Pop a class stream metatable and affect it to the userdata. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002611 lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref);
2612 lua_setmetatable(L, -2);
2613
Willy Tarreaud420a972015-04-06 00:39:18 +02002614 /* Create the applet context */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01002615 appctx = appctx_new(&update_applet, tid_bit);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002616 if (!appctx) {
2617 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaufeb76402015-04-03 14:10:06 +02002618 goto out_fail_conf;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002619 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002620
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002621 appctx->ctx.hlua_cosocket.connected = 0;
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02002622 appctx->ctx.hlua_cosocket.die = 0;
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002623 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write);
2624 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read);
Willy Tarreaub2bf8332015-04-04 15:58:58 +02002625
Willy Tarreaud420a972015-04-06 00:39:18 +02002626 /* Now create a session, task and stream for this applet */
2627 sess = session_new(&socket_proxy, NULL, &appctx->obj_type);
2628 if (!sess) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002629 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002630 goto out_fail_sess;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002631 }
2632
Willy Tarreau87787ac2017-08-28 16:22:54 +02002633 strm = stream_new(sess, &appctx->obj_type);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002634 if (!strm) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002635 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002636 goto out_fail_stream;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002637 }
2638
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002639 /* Initialise cross reference between stream and Lua socket object. */
2640 xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002641
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002642 /* Configure "right" stream interface. this "si" is used to connect
2643 * and retrieve data from the server. The connection is initialized
2644 * with the "struct server".
2645 */
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002646 si_set_state(&strm->si[1], SI_ST_ASS);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002647
2648 /* Force destination server. */
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002649 strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_BE_ASSIGNED;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002650 strm->target = &socket_tcp.obj_type;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002651
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002652 return 1;
2653
Willy Tarreaud420a972015-04-06 00:39:18 +02002654 out_fail_stream:
Willy Tarreau11c36242015-04-04 15:54:03 +02002655 session_free(sess);
Willy Tarreaud420a972015-04-06 00:39:18 +02002656 out_fail_sess:
2657 appctx_free(appctx);
2658 out_fail_conf:
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002659 WILL_LJMP(lua_error(L));
2660 return 0;
2661}
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01002662
2663/*
2664 *
2665 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002666 * Class Channel
2667 *
2668 *
2669 */
2670
2671/* Returns the struct hlua_channel join to the class channel in the
2672 * stack entry "ud" or throws an argument error.
2673 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002674__LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002675{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02002676 return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002677}
2678
Willy Tarreau47860ed2015-03-10 14:07:50 +01002679/* Pushes the channel onto the top of the stack. If the stask does not have a
2680 * free slots, the function fails and returns 0;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002681 */
Willy Tarreau2a71af42015-03-10 13:51:50 +01002682static int hlua_channel_new(lua_State *L, struct channel *channel)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002683{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002684 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002685 if (!lua_checkstack(L, 3))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002686 return 0;
2687
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002688 lua_newtable(L);
Willy Tarreau47860ed2015-03-10 14:07:50 +01002689 lua_pushlightuserdata(L, channel);
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002690 lua_rawseti(L, -2, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002691
2692 /* Pop a class sesison metatable and affect it to the userdata. */
2693 lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref);
2694 lua_setmetatable(L, -2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002695 return 1;
2696}
2697
2698/* Duplicate all the data present in the input channel and put it
2699 * in a string LUA variables. Returns -1 and push a nil value in
2700 * the stack if the channel is closed and all the data are consumed,
2701 * returns 0 if no data are available, otherwise it returns the length
Ilya Shipitsind4259502020-04-08 01:07:56 +05002702 * of the built string.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002703 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002704static inline int _hlua_channel_dup(struct channel *chn, lua_State *L)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002705{
2706 char *blk1;
2707 char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02002708 size_t len1;
2709 size_t len2;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002710 int ret;
2711 luaL_Buffer b;
2712
Willy Tarreau06d80a92017-10-19 14:32:15 +02002713 ret = ci_getblk_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002714 if (unlikely(ret == 0))
2715 return 0;
2716
2717 if (unlikely(ret < 0)) {
2718 lua_pushnil(L);
2719 return -1;
2720 }
2721
2722 luaL_buffinit(L, &b);
2723 luaL_addlstring(&b, blk1, len1);
2724 if (unlikely(ret == 2))
2725 luaL_addlstring(&b, blk2, len2);
2726 luaL_pushresult(&b);
2727
2728 if (unlikely(ret == 2))
2729 return len1 + len2;
2730 return len1;
2731}
2732
2733/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2734 * a yield. This function keep the data in the buffer.
2735 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002736__LJMP static int hlua_channel_dup_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002737{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002738 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002739
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002740 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2741
Christopher Faulet3f829a42018-12-13 21:56:45 +01002742 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2743 WILL_LJMP(lua_error(L));
2744
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002745 if (_hlua_channel_dup(chn, L) == 0)
Willy Tarreau9635e032018-10-16 17:52:55 +02002746 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_dup_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002747 return 1;
2748}
2749
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002750/* Check arguments for the function "hlua_channel_dup_yield". */
2751__LJMP static int hlua_channel_dup(lua_State *L)
2752{
2753 MAY_LJMP(check_args(L, 1, "dup"));
2754 MAY_LJMP(hlua_checkchannel(L, 1));
2755 return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0));
2756}
2757
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002758/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2759 * a yield. This function consumes the data in the buffer. It returns
2760 * a string containing the data or a nil pointer if no data are available
2761 * and the channel is closed.
2762 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002763__LJMP static int hlua_channel_get_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002764{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002765 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002766 int ret;
2767
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002768 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002769
Christopher Faulet3f829a42018-12-13 21:56:45 +01002770 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2771 WILL_LJMP(lua_error(L));
2772
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002773 ret = _hlua_channel_dup(chn, L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002774 if (unlikely(ret == 0))
Willy Tarreau9635e032018-10-16 17:52:55 +02002775 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002776
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002777 if (unlikely(ret == -1))
2778 return 1;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002779
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002780 b_sub(&chn->buf, ret);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002781 return 1;
2782}
2783
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002784/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002785__LJMP static int hlua_channel_get(lua_State *L)
2786{
2787 MAY_LJMP(check_args(L, 1, "get"));
2788 MAY_LJMP(hlua_checkchannel(L, 1));
2789 return MAY_LJMP(hlua_channel_get_yield(L, 0, 0));
2790}
2791
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002792/* This functions consumes and returns one line. If the channel is closed,
2793 * and the last data does not contains a final '\n', the data are returned
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002794 * without the final '\n'. When no more data are available, it returns nil
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002795 * value.
2796 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002797__LJMP static int hlua_channel_getline_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002798{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002799 char *blk1;
2800 char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02002801 size_t len1;
2802 size_t len2;
2803 size_t len;
Willy Tarreau47860ed2015-03-10 14:07:50 +01002804 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002805 int ret;
2806 luaL_Buffer b;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002807
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002808 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2809
Christopher Faulet3f829a42018-12-13 21:56:45 +01002810 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2811 WILL_LJMP(lua_error(L));
2812
Willy Tarreau06d80a92017-10-19 14:32:15 +02002813 ret = ci_getline_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002814 if (ret == 0)
Willy Tarreau9635e032018-10-16 17:52:55 +02002815 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_getline_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002816
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002817 if (ret == -1) {
2818 lua_pushnil(L);
2819 return 1;
2820 }
2821
2822 luaL_buffinit(L, &b);
2823 luaL_addlstring(&b, blk1, len1);
2824 len = len1;
2825 if (unlikely(ret == 2)) {
2826 luaL_addlstring(&b, blk2, len2);
2827 len += len2;
2828 }
2829 luaL_pushresult(&b);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002830 b_rep_blk(&chn->buf, ci_head(chn), ci_head(chn) + len, NULL, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002831 return 1;
2832}
2833
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002834/* Check arguments for the function "hlua_channel_getline_yield". */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002835__LJMP static int hlua_channel_getline(lua_State *L)
2836{
2837 MAY_LJMP(check_args(L, 1, "getline"));
2838 MAY_LJMP(hlua_checkchannel(L, 1));
2839 return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0));
2840}
2841
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002842/* This function takes a string as input, and append it at the
2843 * input side of channel. If the data is too big, but a space
2844 * is probably available after sending some data, the function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002845 * yields. If the data is bigger than the buffer, or if the
2846 * channel is closed, it returns -1. Otherwise, it returns the
2847 * amount of data written.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002848 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002849__LJMP static int hlua_channel_append_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002850{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002851 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002852 size_t len;
2853 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
2854 int l = MAY_LJMP(luaL_checkinteger(L, 3));
2855 int ret;
2856 int max;
2857
Christopher Faulet3f829a42018-12-13 21:56:45 +01002858 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2859 WILL_LJMP(lua_error(L));
2860
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002861 /* Check if the buffer is available because HAProxy doesn't allocate
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002862 * the request buffer if its not required.
2863 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002864 if (chn->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +01002865 si_rx_buff_blk(chn_prod(chn));
Willy Tarreau9635e032018-10-16 17:52:55 +02002866 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002867 }
2868
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002869 max = channel_recv_limit(chn) - b_data(&chn->buf);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002870 if (max > len - l)
2871 max = len - l;
2872
Willy Tarreau06d80a92017-10-19 14:32:15 +02002873 ret = ci_putblk(chn, str + l, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002874 if (ret == -2 || ret == -3) {
2875 lua_pushinteger(L, -1);
2876 return 1;
2877 }
Willy Tarreaubc18da12015-03-13 14:00:47 +01002878 if (ret == -1) {
2879 chn->flags |= CF_WAKE_WRITE;
Willy Tarreau9635e032018-10-16 17:52:55 +02002880 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Willy Tarreaubc18da12015-03-13 14:00:47 +01002881 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002882 l += ret;
2883 lua_pop(L, 1);
2884 lua_pushinteger(L, l);
2885
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002886 max = channel_recv_limit(chn) - b_data(&chn->buf);
Willy Tarreaua79021a2018-06-15 18:07:57 +02002887 if (max == 0 && co_data(chn) == 0) {
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002888 /* There are no space available, and the output buffer is empty.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002889 * in this case, we cannot add more data, so we cannot yield,
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05002890 * we return the amount of copied data.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002891 */
2892 return 1;
2893 }
2894 if (l < len)
Willy Tarreau9635e032018-10-16 17:52:55 +02002895 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002896 return 1;
2897}
2898
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002899/* Just a wrapper of "hlua_channel_append_yield". It returns the length
2900 * of the written string, or -1 if the channel is closed or if the
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002901 * buffer size is too little for the data.
2902 */
2903__LJMP static int hlua_channel_append(lua_State *L)
2904{
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002905 size_t len;
2906
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002907 MAY_LJMP(check_args(L, 2, "append"));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002908 MAY_LJMP(hlua_checkchannel(L, 1));
2909 MAY_LJMP(luaL_checklstring(L, 2, &len));
2910 MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002911 lua_pushinteger(L, 0);
2912
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002913 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002914}
2915
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002916/* Just a wrapper of "hlua_channel_append_yield". This wrapper starts
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002917 * his process by cleaning the buffer. The result is a replacement
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002918 * of the current data. It returns the length of the written string,
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002919 * or -1 if the channel is closed or if the buffer size is too
2920 * little for the data.
2921 */
2922__LJMP static int hlua_channel_set(lua_State *L)
2923{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002924 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002925
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002926 MAY_LJMP(check_args(L, 2, "set"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002927 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002928 lua_pushinteger(L, 0);
2929
Christopher Faulet3f829a42018-12-13 21:56:45 +01002930 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2931 WILL_LJMP(lua_error(L));
2932
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002933 b_set_data(&chn->buf, co_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002934
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002935 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002936}
2937
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002938/* Append data in the output side of the buffer. This data is immediately
2939 * sent. The function returns the amount of data written. If the buffer
2940 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002941 * if the channel is closed.
2942 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002943__LJMP static int hlua_channel_send_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002944{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002945 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002946 size_t len;
2947 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
2948 int l = MAY_LJMP(luaL_checkinteger(L, 3));
2949 int max;
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01002950 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002951
Christopher Faulet3f829a42018-12-13 21:56:45 +01002952 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2953 WILL_LJMP(lua_error(L));
2954
Willy Tarreau47860ed2015-03-10 14:07:50 +01002955 if (unlikely(channel_output_closed(chn))) {
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002956 lua_pushinteger(L, -1);
2957 return 1;
2958 }
2959
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002960 /* Check if the buffer is available because HAProxy doesn't allocate
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01002961 * the request buffer if its not required.
2962 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002963 if (chn->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +01002964 si_rx_buff_blk(chn_prod(chn));
Willy Tarreau9635e032018-10-16 17:52:55 +02002965 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01002966 }
2967
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002968 /* The written data will be immediately sent, so we can check
2969 * the available space without taking in account the reserve.
2970 * The reserve is guaranteed for the processing of incoming
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002971 * data, because the buffer will be flushed.
2972 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002973 max = b_room(&chn->buf);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002974
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002975 /* If there is no space available, and the output buffer is empty.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002976 * in this case, we cannot add more data, so we cannot yield,
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05002977 * we return the amount of copied data.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002978 */
Willy Tarreaua79021a2018-06-15 18:07:57 +02002979 if (max == 0 && co_data(chn) == 0)
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002980 return 1;
2981
2982 /* Adjust the real required length. */
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002983 if (max > len - l)
2984 max = len - l;
2985
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002986 /* The buffer available size may be not contiguous. This test
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002987 * detects a non contiguous buffer and realign it.
2988 */
Willy Tarreau3f679992018-06-15 15:06:42 +02002989 if (ci_space_for_replace(chn) < max)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002990 channel_slow_realign(chn, trash.area);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002991
2992 /* Copy input data in the buffer. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002993 max = b_rep_blk(&chn->buf, ci_head(chn), ci_head(chn), str + l, max);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002994
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002995 /* buffer replace considers that the input part is filled.
2996 * so, I must forward these new data in the output part.
2997 */
Willy Tarreaubcbd3932018-06-06 07:13:22 +02002998 c_adv(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002999
3000 l += max;
3001 lua_pop(L, 1);
3002 lua_pushinteger(L, l);
3003
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003004 /* If there is no space available, and the output buffer is empty.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003005 * in this case, we cannot add more data, so we cannot yield,
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003006 * we return the amount of copied data.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003007 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003008 max = b_room(&chn->buf);
Willy Tarreaua79021a2018-06-15 18:07:57 +02003009 if (max == 0 && co_data(chn) == 0)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003010 return 1;
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003011
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003012 if (l < len) {
3013 /* If we are waiting for space in the response buffer, we
3014 * must set the flag WAKERESWR. This flag required the task
3015 * wake up if any activity is detected on the response buffer.
3016 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003017 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003018 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01003019 else
3020 HLUA_SET_WAKEREQWR(hlua);
Willy Tarreau9635e032018-10-16 17:52:55 +02003021 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003022 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003023
3024 return 1;
3025}
3026
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003027/* Just a wrapper of "_hlua_channel_send". This wrapper permits
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003028 * yield the LUA process, and resume it without checking the
3029 * input arguments.
3030 */
3031__LJMP static int hlua_channel_send(lua_State *L)
3032{
3033 MAY_LJMP(check_args(L, 2, "send"));
3034 lua_pushinteger(L, 0);
3035
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003036 return MAY_LJMP(hlua_channel_send_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003037}
3038
3039/* This function forward and amount of butes. The data pass from
3040 * the input side of the buffer to the output side, and can be
3041 * forwarded. This function never fails.
3042 *
3043 * The Lua function takes an amount of bytes to be forwarded in
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003044 * input. It returns the number of bytes forwarded.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003045 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003046__LJMP static int hlua_channel_forward_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003047{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003048 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003049 int len;
3050 int l;
3051 int max;
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003052 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003053
3054 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Faulet3f829a42018-12-13 21:56:45 +01003055
3056 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
3057 WILL_LJMP(lua_error(L));
3058
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003059 len = MAY_LJMP(luaL_checkinteger(L, 2));
3060 l = MAY_LJMP(luaL_checkinteger(L, -1));
3061
3062 max = len - l;
Willy Tarreaua79021a2018-06-15 18:07:57 +02003063 if (max > ci_data(chn))
3064 max = ci_data(chn);
Willy Tarreau47860ed2015-03-10 14:07:50 +01003065 channel_forward(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003066 l += max;
3067
3068 lua_pop(L, 1);
3069 lua_pushinteger(L, l);
3070
3071 /* Check if it miss bytes to forward. */
3072 if (l < len) {
3073 /* The the input channel or the output channel are closed, we
3074 * must return the amount of data forwarded.
3075 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003076 if (channel_input_closed(chn) || channel_output_closed(chn))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003077 return 1;
3078
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003079 /* If we are waiting for space data in the response buffer, we
3080 * must set the flag WAKERESWR. This flag required the task
3081 * wake up if any activity is detected on the response buffer.
3082 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003083 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003084 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01003085 else
3086 HLUA_SET_WAKEREQWR(hlua);
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003087
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003088 /* Otherwise, we can yield waiting for new data in the inpout side. */
Willy Tarreau9635e032018-10-16 17:52:55 +02003089 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_forward_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003090 }
3091
3092 return 1;
3093}
3094
3095/* Just check the input and prepare the stack for the previous
3096 * function "hlua_channel_forward_yield"
3097 */
3098__LJMP static int hlua_channel_forward(lua_State *L)
3099{
3100 MAY_LJMP(check_args(L, 2, "forward"));
3101 MAY_LJMP(hlua_checkchannel(L, 1));
3102 MAY_LJMP(luaL_checkinteger(L, 2));
3103
3104 lua_pushinteger(L, 0);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003105 return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003106}
3107
3108/* Just returns the number of bytes available in the input
3109 * side of the buffer. This function never fails.
3110 */
3111__LJMP static int hlua_channel_get_in_len(lua_State *L)
3112{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003113 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003114
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003115 MAY_LJMP(check_args(L, 1, "get_in_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003116 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Fauleta3ceac12018-12-14 13:39:09 +01003117 if (IS_HTX_STRM(chn_strm(chn))) {
3118 struct htx *htx = htxbuf(&chn->buf);
3119 lua_pushinteger(L, htx->data - co_data(chn));
3120 }
3121 else
3122 lua_pushinteger(L, ci_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003123 return 1;
3124}
3125
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003126/* Returns true if the channel is full. */
3127__LJMP static int hlua_channel_is_full(lua_State *L)
3128{
3129 struct channel *chn;
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003130
3131 MAY_LJMP(check_args(L, 1, "is_full"));
3132 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Faulet0ec740e2020-02-26 11:59:19 +01003133 /* ignore the reserve, we are not on a producer side (ie in an
3134 * applet).
3135 */
3136 lua_pushboolean(L, channel_full(chn, 0));
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003137 return 1;
3138}
3139
Christopher Faulet2ac9ba22020-02-25 10:15:50 +01003140/* Returns true if the channel is the response channel. */
3141__LJMP static int hlua_channel_is_resp(lua_State *L)
3142{
3143 struct channel *chn;
3144
3145 MAY_LJMP(check_args(L, 1, "is_resp"));
3146 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3147
3148 lua_pushboolean(L, !!(chn->flags & CF_ISRESP));
3149 return 1;
3150}
3151
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003152/* Just returns the number of bytes available in the output
3153 * side of the buffer. This function never fails.
3154 */
3155__LJMP static int hlua_channel_get_out_len(lua_State *L)
3156{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003157 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003158
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003159 MAY_LJMP(check_args(L, 1, "get_out_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003160 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Willy Tarreaua79021a2018-06-15 18:07:57 +02003161 lua_pushinteger(L, co_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003162 return 1;
3163}
3164
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003165/*
3166 *
3167 *
3168 * Class Fetches
3169 *
3170 *
3171 */
3172
3173/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003174 * a class stream, otherwise it throws an error.
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003175 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003176__LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003177{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003178 return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003179}
3180
3181/* This function creates and push in the stack a fetch object according
3182 * with a current TXN.
3183 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003184static int hlua_fetches_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003185{
Willy Tarreau7073c472015-04-06 11:15:40 +02003186 struct hlua_smp *hsmp;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003187
3188 /* Check stack size. */
3189 if (!lua_checkstack(L, 3))
3190 return 0;
3191
3192 /* Create the object: obj[0] = userdata.
3193 * Note that the base of the Fetches object is the
3194 * transaction object.
3195 */
3196 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003197 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003198 lua_rawseti(L, -2, 0);
3199
Willy Tarreau7073c472015-04-06 11:15:40 +02003200 hsmp->s = txn->s;
3201 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003202 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003203 hsmp->flags = flags;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003204
3205 /* Pop a class sesison metatable and affect it to the userdata. */
3206 lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref);
3207 lua_setmetatable(L, -2);
3208
3209 return 1;
3210}
3211
3212/* This function is an LUA binding. It is called with each sample-fetch.
3213 * It uses closure argument to store the associated sample-fetch. It
3214 * returns only one argument or throws an error. An error is thrown
3215 * only if an error is encountered during the argument parsing. If
3216 * the "sample-fetch" function fails, nil is returned.
3217 */
3218__LJMP static int hlua_run_sample_fetch(lua_State *L)
3219{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003220 struct hlua_smp *hsmp;
Willy Tarreau2ec22742015-03-10 14:27:20 +01003221 struct sample_fetch *f;
Frédéric Lécaillef874a832018-06-15 13:56:04 +02003222 struct arg args[ARGM_NBARGS + 1] = {{0}};
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003223 int i;
3224 struct sample smp;
3225
3226 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003227 f = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003228
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003229 /* Get traditional arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003230 hsmp = MAY_LJMP(hlua_checkfetches(L, 1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003231
Thierry FOURNIERca988662015-12-20 18:43:03 +01003232 /* Check execution authorization. */
3233 if (f->use & SMP_USE_HTTP_ANY &&
3234 !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) {
3235 lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which "
3236 "is not available in Lua services", f->kw);
3237 WILL_LJMP(lua_error(L));
3238 }
3239
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003240 /* Get extra arguments. */
3241 for (i = 0; i < lua_gettop(L) - 1; i++) {
3242 if (i >= ARGM_NBARGS)
3243 break;
3244 hlua_lua2arg(L, i + 2, &args[i]);
3245 }
3246 args[i].type = ARGT_STOP;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003247 args[i].data.str.area = NULL;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003248
3249 /* Check arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003250 MAY_LJMP(hlua_lua2arg_check(L, 2, args, f->arg_mask, hsmp->p));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003251
3252 /* Run the special args checker. */
Willy Tarreau2ec22742015-03-10 14:27:20 +01003253 if (f->val_args && !f->val_args(args, NULL)) {
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003254 lua_pushfstring(L, "error in arguments");
3255 WILL_LJMP(lua_error(L));
3256 }
3257
3258 /* Initialise the sample. */
3259 memset(&smp, 0, sizeof(smp));
3260
3261 /* Run the sample fetch process. */
Willy Tarreau1777ea62016-03-10 16:15:46 +01003262 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003263 if (!f->process(args, &smp, f->kw, f->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003264 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003265 lua_pushstring(L, "");
3266 else
3267 lua_pushnil(L);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003268 return 1;
3269 }
3270
3271 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003272 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003273 hlua_smp2lua_str(L, &smp);
3274 else
3275 hlua_smp2lua(L, &smp);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003276 return 1;
3277}
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003278
3279/*
3280 *
3281 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003282 * Class Converters
3283 *
3284 *
3285 */
3286
3287/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003288 * a class stream, otherwise it throws an error.
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003289 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003290__LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003291{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003292 return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003293}
3294
3295/* This function creates and push in the stack a Converters object
3296 * according with a current TXN.
3297 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003298static int hlua_converters_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003299{
Willy Tarreau7073c472015-04-06 11:15:40 +02003300 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003301
3302 /* Check stack size. */
3303 if (!lua_checkstack(L, 3))
3304 return 0;
3305
3306 /* Create the object: obj[0] = userdata.
3307 * Note that the base of the Converters object is the
3308 * same than the TXN object.
3309 */
3310 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003311 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003312 lua_rawseti(L, -2, 0);
3313
Willy Tarreau7073c472015-04-06 11:15:40 +02003314 hsmp->s = txn->s;
3315 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003316 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003317 hsmp->flags = flags;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003318
Willy Tarreau87b09662015-04-03 00:22:06 +02003319 /* Pop a class stream metatable and affect it to the table. */
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003320 lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref);
3321 lua_setmetatable(L, -2);
3322
3323 return 1;
3324}
3325
3326/* This function is an LUA binding. It is called with each converter.
3327 * It uses closure argument to store the associated converter. It
3328 * returns only one argument or throws an error. An error is thrown
3329 * only if an error is encountered during the argument parsing. If
3330 * the converter function function fails, nil is returned.
3331 */
3332__LJMP static int hlua_run_sample_conv(lua_State *L)
3333{
Willy Tarreauda5f1082015-04-06 11:17:13 +02003334 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003335 struct sample_conv *conv;
Frédéric Lécaillef874a832018-06-15 13:56:04 +02003336 struct arg args[ARGM_NBARGS + 1] = {{0}};
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003337 int i;
3338 struct sample smp;
3339
3340 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003341 conv = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003342
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003343 /* Get traditional arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003344 hsmp = MAY_LJMP(hlua_checkconverters(L, 1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003345
3346 /* Get extra arguments. */
3347 for (i = 0; i < lua_gettop(L) - 2; i++) {
3348 if (i >= ARGM_NBARGS)
3349 break;
3350 hlua_lua2arg(L, i + 3, &args[i]);
3351 }
3352 args[i].type = ARGT_STOP;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003353 args[i].data.str.area = NULL;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003354
3355 /* Check arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003356 MAY_LJMP(hlua_lua2arg_check(L, 3, args, conv->arg_mask, hsmp->p));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003357
3358 /* Run the special args checker. */
3359 if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) {
3360 hlua_pusherror(L, "error in arguments");
3361 WILL_LJMP(lua_error(L));
3362 }
3363
3364 /* Initialise the sample. */
3365 if (!hlua_lua2smp(L, 2, &smp)) {
3366 hlua_pusherror(L, "error in the input argument");
3367 WILL_LJMP(lua_error(L));
3368 }
3369
Willy Tarreau1777ea62016-03-10 16:15:46 +01003370 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
3371
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003372 /* Apply expected cast. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003373 if (!sample_casts[smp.data.type][conv->in_type]) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003374 hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'",
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003375 smp_to_type[smp.data.type], smp_to_type[conv->in_type]);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003376 WILL_LJMP(lua_error(L));
3377 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003378 if (sample_casts[smp.data.type][conv->in_type] != c_none &&
3379 !sample_casts[smp.data.type][conv->in_type](&smp)) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003380 hlua_pusherror(L, "error during the input argument casting");
3381 WILL_LJMP(lua_error(L));
3382 }
3383
3384 /* Run the sample conversion process. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003385 if (!conv->process(args, &smp, conv->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003386 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003387 lua_pushstring(L, "");
3388 else
Willy Tarreaua678b432015-08-28 10:14:59 +02003389 lua_pushnil(L);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003390 return 1;
3391 }
3392
3393 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003394 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003395 hlua_smp2lua_str(L, &smp);
3396 else
3397 hlua_smp2lua(L, &smp);
Willy Tarreaua678b432015-08-28 10:14:59 +02003398 return 1;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003399}
3400
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003401/*
3402 *
3403 *
3404 * Class AppletTCP
3405 *
3406 *
3407 */
3408
3409/* Returns a struct hlua_txn if the stack entry "ud" is
3410 * a class stream, otherwise it throws an error.
3411 */
3412__LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud)
3413{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003414 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003415}
3416
3417/* This function creates and push in the stack an Applet object
3418 * according with a current TXN.
3419 */
3420static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx)
3421{
3422 struct hlua_appctx *appctx;
3423 struct stream_interface *si = ctx->owner;
3424 struct stream *s = si_strm(si);
3425 struct proxy *p = s->be;
3426
3427 /* Check stack size. */
3428 if (!lua_checkstack(L, 3))
3429 return 0;
3430
3431 /* Create the object: obj[0] = userdata.
3432 * Note that the base of the Converters object is the
3433 * same than the TXN object.
3434 */
3435 lua_newtable(L);
3436 appctx = lua_newuserdata(L, sizeof(*appctx));
3437 lua_rawseti(L, -2, 0);
3438 appctx->appctx = ctx;
3439 appctx->htxn.s = s;
3440 appctx->htxn.p = p;
3441
3442 /* Create the "f" field that contains a list of fetches. */
3443 lua_pushstring(L, "f");
3444 if (!hlua_fetches_new(L, &appctx->htxn, 0))
3445 return 0;
3446 lua_settable(L, -3);
3447
3448 /* Create the "sf" field that contains a list of stringsafe fetches. */
3449 lua_pushstring(L, "sf");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003450 if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003451 return 0;
3452 lua_settable(L, -3);
3453
3454 /* Create the "c" field that contains a list of converters. */
3455 lua_pushstring(L, "c");
3456 if (!hlua_converters_new(L, &appctx->htxn, 0))
3457 return 0;
3458 lua_settable(L, -3);
3459
3460 /* Create the "sc" field that contains a list of stringsafe converters. */
3461 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003462 if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003463 return 0;
3464 lua_settable(L, -3);
3465
3466 /* Pop a class stream metatable and affect it to the table. */
3467 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref);
3468 lua_setmetatable(L, -2);
3469
3470 return 1;
3471}
3472
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003473__LJMP static int hlua_applet_tcp_set_var(lua_State *L)
3474{
3475 struct hlua_appctx *appctx;
3476 struct stream *s;
3477 const char *name;
3478 size_t len;
3479 struct sample smp;
3480
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003481 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
3482 WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments"));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003483
3484 /* It is useles to retrieve the stream, but this function
3485 * runs only in a stream context.
3486 */
3487 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3488 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3489 s = appctx->htxn.s;
3490
3491 /* Converts the third argument in a sample. */
3492 hlua_lua2smp(L, 3, &smp);
3493
3494 /* Store the sample in a variable. */
3495 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003496
3497 if (lua_gettop(L) == 4 && lua_toboolean(L, 4))
3498 lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0);
3499 else
3500 lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0);
3501
Tim Duesterhus84ebc132020-05-19 13:49:41 +02003502 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003503}
3504
3505__LJMP static int hlua_applet_tcp_unset_var(lua_State *L)
3506{
3507 struct hlua_appctx *appctx;
3508 struct stream *s;
3509 const char *name;
3510 size_t len;
3511 struct sample smp;
3512
3513 MAY_LJMP(check_args(L, 2, "unset_var"));
3514
3515 /* It is useles to retrieve the stream, but this function
3516 * runs only in a stream context.
3517 */
3518 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3519 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3520 s = appctx->htxn.s;
3521
3522 /* Unset the variable. */
3523 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus84ebc132020-05-19 13:49:41 +02003524 lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0);
3525 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003526}
3527
3528__LJMP static int hlua_applet_tcp_get_var(lua_State *L)
3529{
3530 struct hlua_appctx *appctx;
3531 struct stream *s;
3532 const char *name;
3533 size_t len;
3534 struct sample smp;
3535
3536 MAY_LJMP(check_args(L, 2, "get_var"));
3537
3538 /* It is useles to retrieve the stream, but this function
3539 * runs only in a stream context.
3540 */
3541 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3542 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3543 s = appctx->htxn.s;
3544
3545 smp_set_owner(&smp, s->be, s->sess, s, 0);
3546 if (!vars_get_by_name(name, len, &smp)) {
3547 lua_pushnil(L);
3548 return 1;
3549 }
3550
3551 return hlua_smp2lua(L, &smp);
3552}
3553
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003554__LJMP static int hlua_applet_tcp_set_priv(lua_State *L)
3555{
3556 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3557 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003558 struct hlua *hlua;
3559
3560 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003561 if (!s->hlua)
3562 return 0;
3563 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003564
3565 MAY_LJMP(check_args(L, 2, "set_priv"));
3566
3567 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02003568 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003569
3570 /* Get and store new value. */
3571 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
3572 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
3573
3574 return 0;
3575}
3576
3577__LJMP static int hlua_applet_tcp_get_priv(lua_State *L)
3578{
3579 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3580 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003581 struct hlua *hlua;
3582
3583 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003584 if (!s->hlua) {
3585 lua_pushnil(L);
3586 return 1;
3587 }
3588 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003589
3590 /* Push configuration index in the stack. */
3591 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
3592
3593 return 1;
3594}
3595
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003596/* If expected data not yet available, it returns a yield. This function
3597 * consumes the data in the buffer. It returns a string containing the
3598 * data. This string can be empty.
3599 */
3600__LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx)
3601{
3602 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3603 struct stream_interface *si = appctx->appctx->owner;
3604 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02003605 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003606 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02003607 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003608 size_t len2;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003609
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003610 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003611 ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003612
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003613 /* Data not yet available. return yield. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003614 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003615 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003616 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_getline_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003617 }
3618
3619 /* End of data: commit the total strings and return. */
3620 if (ret < 0) {
3621 luaL_pushresult(&appctx->b);
3622 return 1;
3623 }
3624
3625 /* Ensure that the block 2 length is usable. */
3626 if (ret == 1)
3627 len2 = 0;
3628
3629 /* dont check the max length read and dont check. */
3630 luaL_addlstring(&appctx->b, blk1, len1);
3631 luaL_addlstring(&appctx->b, blk2, len2);
3632
3633 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003634 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003635 luaL_pushresult(&appctx->b);
3636 return 1;
3637}
3638
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003639/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003640__LJMP static int hlua_applet_tcp_getline(lua_State *L)
3641{
3642 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3643
3644 /* Initialise the string catenation. */
3645 luaL_buffinit(L, &appctx->b);
3646
3647 return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0));
3648}
3649
3650/* If expected data not yet available, it returns a yield. This function
3651 * consumes the data in the buffer. It returns a string containing the
3652 * data. This string can be empty.
3653 */
3654__LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx)
3655{
3656 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3657 struct stream_interface *si = appctx->appctx->owner;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003658 size_t len = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003659 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02003660 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003661 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02003662 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003663 size_t len2;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003664
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003665 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003666 ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003667
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003668 /* Data not yet available. return yield. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003669 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003670 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003671 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003672 }
3673
3674 /* End of data: commit the total strings and return. */
3675 if (ret < 0) {
3676 luaL_pushresult(&appctx->b);
3677 return 1;
3678 }
3679
3680 /* Ensure that the block 2 length is usable. */
3681 if (ret == 1)
3682 len2 = 0;
3683
3684 if (len == -1) {
3685
3686 /* If len == -1, catenate all the data avalaile and
3687 * yield because we want to get all the data until
3688 * the end of data stream.
3689 */
3690 luaL_addlstring(&appctx->b, blk1, len1);
3691 luaL_addlstring(&appctx->b, blk2, len2);
Willy Tarreau06d80a92017-10-19 14:32:15 +02003692 co_skip(si_oc(si), len1 + len2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003693 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003694 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003695
3696 } else {
3697
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003698 /* Copy the first block caping to the length required. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003699 if (len1 > len)
3700 len1 = len;
3701 luaL_addlstring(&appctx->b, blk1, len1);
3702 len -= len1;
3703
3704 /* Copy the second block. */
3705 if (len2 > len)
3706 len2 = len;
3707 luaL_addlstring(&appctx->b, blk2, len2);
3708 len -= len2;
3709
3710 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003711 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003712
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003713 /* If there is no other data available, yield waiting for new data. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003714 if (len > 0) {
3715 lua_pushinteger(L, len);
3716 lua_replace(L, 2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003717 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003718 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003719 }
3720
3721 /* return the result. */
3722 luaL_pushresult(&appctx->b);
3723 return 1;
3724 }
3725
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003726 /* we never execute this */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003727 hlua_pusherror(L, "Lua: internal error");
3728 WILL_LJMP(lua_error(L));
3729 return 0;
3730}
3731
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003732/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003733__LJMP static int hlua_applet_tcp_recv(lua_State *L)
3734{
3735 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3736 int len = -1;
3737
3738 if (lua_gettop(L) > 2)
3739 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
3740 if (lua_gettop(L) >= 2) {
3741 len = MAY_LJMP(luaL_checkinteger(L, 2));
3742 lua_pop(L, 1);
3743 }
3744
3745 /* Confirm or set the required length */
3746 lua_pushinteger(L, len);
3747
3748 /* Initialise the string catenation. */
3749 luaL_buffinit(L, &appctx->b);
3750
3751 return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0));
3752}
3753
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003754/* Append data in the output side of the buffer. This data is immediately
3755 * sent. The function returns the amount of data written. If the buffer
3756 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003757 * if the channel is closed.
3758 */
3759__LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx)
3760{
3761 size_t len;
3762 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3763 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
3764 int l = MAY_LJMP(luaL_checkinteger(L, 3));
3765 struct stream_interface *si = appctx->appctx->owner;
3766 struct channel *chn = si_ic(si);
3767 int max;
3768
3769 /* Get the max amount of data which can write as input in the channel. */
3770 max = channel_recv_max(chn);
3771 if (max > (len - l))
3772 max = len - l;
3773
3774 /* Copy data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003775 ci_putblk(chn, str + l, max);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003776
3777 /* update counters. */
3778 l += max;
3779 lua_pop(L, 1);
3780 lua_pushinteger(L, l);
3781
3782 /* If some data is not send, declares the situation to the
3783 * applet, and returns a yield.
3784 */
3785 if (l < len) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003786 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003787 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003788 }
3789
3790 return 1;
3791}
3792
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003793/* Just a wrapper of "hlua_applet_tcp_send_yield". This wrapper permits
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003794 * yield the LUA process, and resume it without checking the
3795 * input arguments.
3796 */
3797__LJMP static int hlua_applet_tcp_send(lua_State *L)
3798{
3799 MAY_LJMP(check_args(L, 2, "send"));
3800 lua_pushinteger(L, 0);
3801
3802 return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0));
3803}
3804
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003805/*
3806 *
3807 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003808 * Class AppletHTTP
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003809 *
3810 *
3811 */
3812
3813/* Returns a struct hlua_txn if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003814 * a class stream, otherwise it throws an error.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003815 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003816__LJMP static struct hlua_appctx *hlua_checkapplet_http(lua_State *L, int ud)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003817{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003818 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003819}
3820
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003821/* This function creates and push in the stack an Applet object
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003822 * according with a current TXN.
3823 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003824static int hlua_applet_http_new(lua_State *L, struct appctx *ctx)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003825{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003826 struct hlua_appctx *appctx;
Thierry FOURNIER841475e2015-12-11 17:10:09 +01003827 struct hlua_txn htxn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003828 struct stream_interface *si = ctx->owner;
3829 struct stream *s = si_strm(si);
3830 struct proxy *px = s->be;
Christopher Fauleta2097962019-07-15 16:25:33 +02003831 struct htx *htx;
3832 struct htx_blk *blk;
3833 struct htx_sl *sl;
3834 struct ist path;
3835 unsigned long long len = 0;
3836 int32_t pos;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003837
3838 /* Check stack size. */
3839 if (!lua_checkstack(L, 3))
3840 return 0;
3841
3842 /* Create the object: obj[0] = userdata.
3843 * Note that the base of the Converters object is the
3844 * same than the TXN object.
3845 */
3846 lua_newtable(L);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003847 appctx = lua_newuserdata(L, sizeof(*appctx));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003848 lua_rawseti(L, -2, 0);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003849 appctx->appctx = ctx;
3850 appctx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */
Robin H. Johnson52f5db22017-01-01 13:10:52 -08003851 appctx->appctx->ctx.hlua_apphttp.reason = NULL; /* Use default reason based on status */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003852 appctx->htxn.s = s;
3853 appctx->htxn.p = px;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003854
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003855 /* Create the "f" field that contains a list of fetches. */
3856 lua_pushstring(L, "f");
3857 if (!hlua_fetches_new(L, &appctx->htxn, 0))
3858 return 0;
3859 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003860
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003861 /* Create the "sf" field that contains a list of stringsafe fetches. */
3862 lua_pushstring(L, "sf");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003863 if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003864 return 0;
3865 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003866
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003867 /* Create the "c" field that contains a list of converters. */
3868 lua_pushstring(L, "c");
3869 if (!hlua_converters_new(L, &appctx->htxn, 0))
3870 return 0;
3871 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003872
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003873 /* Create the "sc" field that contains a list of stringsafe converters. */
3874 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003875 if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003876 return 0;
3877 lua_settable(L, -3);
Willy Tarreaueee5b512015-04-03 23:46:31 +02003878
Christopher Fauleta2097962019-07-15 16:25:33 +02003879 htx = htxbuf(&s->req.buf);
3880 blk = htx_get_first_blk(htx);
Christopher Fauletea009732019-11-18 15:50:25 +01003881 BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL);
Christopher Fauleta2097962019-07-15 16:25:33 +02003882 sl = htx_get_blk_ptr(htx, blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003883
Christopher Fauleta2097962019-07-15 16:25:33 +02003884 /* Stores the request method. */
3885 lua_pushstring(L, "method");
3886 lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl));
3887 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003888
Christopher Fauleta2097962019-07-15 16:25:33 +02003889 /* Stores the http version. */
3890 lua_pushstring(L, "version");
3891 lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl));
3892 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003893
Christopher Fauleta2097962019-07-15 16:25:33 +02003894 /* creates an array of headers. hlua_http_get_headers() crates and push
3895 * the array on the top of the stack.
3896 */
3897 lua_pushstring(L, "headers");
3898 htxn.s = s;
3899 htxn.p = px;
3900 htxn.dir = SMP_OPT_DIR_REQ;
Christopher Faulet9d1332b2020-02-24 16:46:16 +01003901 if (!hlua_http_get_headers(L, &htxn.s->txn->req))
Christopher Fauleta2097962019-07-15 16:25:33 +02003902 return 0;
3903 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003904
Christopher Fauleta2097962019-07-15 16:25:33 +02003905 path = http_get_path(htx_sl_req_uri(sl));
Tim Duesterhused526372020-03-05 17:56:33 +01003906 if (isttest(path)) {
Christopher Fauleta2097962019-07-15 16:25:33 +02003907 char *p, *q, *end;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003908
Christopher Fauleta2097962019-07-15 16:25:33 +02003909 p = path.ptr;
3910 end = path.ptr + path.len;
3911 q = p;
3912 while (q < end && *q != '?')
3913 q++;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003914
Thierry FOURNIER7d388632017-02-22 02:06:16 +01003915 /* Stores the request path. */
Christopher Fauleta2097962019-07-15 16:25:33 +02003916 lua_pushstring(L, "path");
3917 lua_pushlstring(L, p, q - p);
Thierry FOURNIER7d388632017-02-22 02:06:16 +01003918 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003919
Christopher Fauleta2097962019-07-15 16:25:33 +02003920 /* Stores the query string. */
3921 lua_pushstring(L, "qs");
3922 if (*q == '?')
3923 q++;
3924 lua_pushlstring(L, q, end - q);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003925 lua_settable(L, -3);
Christopher Fauleta2097962019-07-15 16:25:33 +02003926 }
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003927
Christopher Fauleta2097962019-07-15 16:25:33 +02003928 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
3929 struct htx_blk *blk = htx_get_blk(htx, pos);
3930 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003931
Christopher Fauleta2097962019-07-15 16:25:33 +02003932 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
3933 break;
3934 if (type == HTX_BLK_DATA)
3935 len += htx_get_blksz(blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003936 }
Christopher Fauleta2097962019-07-15 16:25:33 +02003937 if (htx->extra != ULLONG_MAX)
3938 len += htx->extra;
3939
3940 /* Stores the request path. */
3941 lua_pushstring(L, "length");
3942 lua_pushinteger(L, len);
3943 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003944
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003945 /* Create an empty array of HTTP request headers. */
3946 lua_pushstring(L, "response");
3947 lua_newtable(L);
3948 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003949
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003950 /* Pop a class stream metatable and affect it to the table. */
3951 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref);
3952 lua_setmetatable(L, -2);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003953
3954 return 1;
3955}
3956
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003957__LJMP static int hlua_applet_http_set_var(lua_State *L)
3958{
3959 struct hlua_appctx *appctx;
3960 struct stream *s;
3961 const char *name;
3962 size_t len;
3963 struct sample smp;
3964
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003965 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
3966 WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments"));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003967
3968 /* It is useles to retrieve the stream, but this function
3969 * runs only in a stream context.
3970 */
3971 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
3972 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3973 s = appctx->htxn.s;
3974
3975 /* Converts the third argument in a sample. */
3976 hlua_lua2smp(L, 3, &smp);
3977
3978 /* Store the sample in a variable. */
3979 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003980
3981 if (lua_gettop(L) == 4 && lua_toboolean(L, 4))
3982 lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0);
3983 else
3984 lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0);
3985
Tim Duesterhus84ebc132020-05-19 13:49:41 +02003986 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003987}
3988
3989__LJMP static int hlua_applet_http_unset_var(lua_State *L)
3990{
3991 struct hlua_appctx *appctx;
3992 struct stream *s;
3993 const char *name;
3994 size_t len;
3995 struct sample smp;
3996
3997 MAY_LJMP(check_args(L, 2, "unset_var"));
3998
3999 /* It is useles to retrieve the stream, but this function
4000 * runs only in a stream context.
4001 */
4002 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4003 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4004 s = appctx->htxn.s;
4005
4006 /* Unset the variable. */
4007 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus84ebc132020-05-19 13:49:41 +02004008 lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0);
4009 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004010}
4011
4012__LJMP static int hlua_applet_http_get_var(lua_State *L)
4013{
4014 struct hlua_appctx *appctx;
4015 struct stream *s;
4016 const char *name;
4017 size_t len;
4018 struct sample smp;
4019
4020 MAY_LJMP(check_args(L, 2, "get_var"));
4021
4022 /* It is useles to retrieve the stream, but this function
4023 * runs only in a stream context.
4024 */
4025 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4026 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4027 s = appctx->htxn.s;
4028
4029 smp_set_owner(&smp, s->be, s->sess, s, 0);
4030 if (!vars_get_by_name(name, len, &smp)) {
4031 lua_pushnil(L);
4032 return 1;
4033 }
4034
4035 return hlua_smp2lua(L, &smp);
4036}
4037
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004038__LJMP static int hlua_applet_http_set_priv(lua_State *L)
4039{
4040 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4041 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004042 struct hlua *hlua;
4043
4044 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004045 if (!s->hlua)
4046 return 0;
4047 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004048
4049 MAY_LJMP(check_args(L, 2, "set_priv"));
4050
4051 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02004052 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004053
4054 /* Get and store new value. */
4055 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
4056 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
4057
4058 return 0;
4059}
4060
4061__LJMP static int hlua_applet_http_get_priv(lua_State *L)
4062{
4063 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4064 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004065 struct hlua *hlua;
4066
4067 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004068 if (!s->hlua) {
4069 lua_pushnil(L);
4070 return 1;
4071 }
4072 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004073
4074 /* Push configuration index in the stack. */
4075 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
4076
4077 return 1;
4078}
4079
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004080/* If expected data not yet available, it returns a yield. This function
4081 * consumes the data in the buffer. It returns a string containing the
4082 * data. This string can be empty.
4083 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004084__LJMP static int hlua_applet_http_getline_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004085{
4086 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4087 struct stream_interface *si = appctx->appctx->owner;
4088 struct channel *req = si_oc(si);
4089 struct htx *htx;
4090 struct htx_blk *blk;
4091 size_t count;
4092 int stop = 0;
4093
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004094 htx = htx_from_buf(&req->buf);
4095 count = co_data(req);
Christopher Fauleta3f15502019-05-13 15:27:23 +02004096 blk = htx_get_first_blk(htx);
Christopher Fauletcc26b132018-12-18 21:20:57 +01004097
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004098 while (count && !stop && blk) {
4099 enum htx_blk_type type = htx_get_blk_type(blk);
4100 uint32_t sz = htx_get_blksz(blk);
4101 struct ist v;
4102 uint32_t vlen;
4103 char *nl;
4104
Christopher Faulete461e342018-12-18 16:43:35 +01004105 if (type == HTX_BLK_EOM) {
4106 stop = 1;
4107 break;
4108 }
4109
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004110 vlen = sz;
4111 if (vlen > count) {
4112 if (type != HTX_BLK_DATA)
4113 break;
4114 vlen = count;
4115 }
4116
4117 switch (type) {
4118 case HTX_BLK_UNUSED:
4119 break;
4120
4121 case HTX_BLK_DATA:
4122 v = htx_get_blk_value(htx, blk);
4123 v.len = vlen;
4124 nl = istchr(v, '\n');
4125 if (nl != NULL) {
4126 stop = 1;
4127 vlen = nl - v.ptr + 1;
4128 }
4129 luaL_addlstring(&appctx->b, v.ptr, vlen);
4130 break;
4131
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004132 case HTX_BLK_TLR:
4133 case HTX_BLK_EOM:
4134 stop = 1;
4135 break;
4136
4137 default:
4138 break;
4139 }
4140
4141 co_set_data(req, co_data(req) - vlen);
4142 count -= vlen;
4143 if (sz == vlen)
4144 blk = htx_remove_blk(htx, blk);
4145 else {
4146 htx_cut_data_blk(htx, blk, vlen);
4147 break;
4148 }
4149 }
4150
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004151 htx_to_buf(htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004152 if (!stop) {
4153 si_cant_get(si);
Christopher Fauleta2097962019-07-15 16:25:33 +02004154 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_getline_yield, TICK_ETERNITY, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004155 }
4156
4157 /* return the result. */
4158 luaL_pushresult(&appctx->b);
4159 return 1;
4160}
4161
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004162
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004163/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004164__LJMP static int hlua_applet_http_getline(lua_State *L)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004165{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004166 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004167
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004168 /* Initialise the string catenation. */
4169 luaL_buffinit(L, &appctx->b);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004170
Christopher Fauleta2097962019-07-15 16:25:33 +02004171 return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004172}
4173
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004174/* If expected data not yet available, it returns a yield. This function
4175 * consumes the data in the buffer. It returns a string containing the
4176 * data. This string can be empty.
4177 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004178__LJMP static int hlua_applet_http_recv_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004179{
4180 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4181 struct stream_interface *si = appctx->appctx->owner;
4182 struct channel *req = si_oc(si);
4183 struct htx *htx;
4184 struct htx_blk *blk;
4185 size_t count;
4186 int len;
4187
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004188 htx = htx_from_buf(&req->buf);
4189 len = MAY_LJMP(luaL_checkinteger(L, 2));
4190 count = co_data(req);
Christopher Faulet29f17582019-05-23 11:03:26 +02004191 blk = htx_get_head_blk(htx);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004192 while (count && len && blk) {
4193 enum htx_blk_type type = htx_get_blk_type(blk);
4194 uint32_t sz = htx_get_blksz(blk);
4195 struct ist v;
4196 uint32_t vlen;
4197
Christopher Faulete461e342018-12-18 16:43:35 +01004198 if (type == HTX_BLK_EOM) {
4199 len = 0;
4200 break;
4201 }
4202
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004203 vlen = sz;
4204 if (len > 0 && vlen > len)
4205 vlen = len;
4206 if (vlen > count) {
4207 if (type != HTX_BLK_DATA)
4208 break;
4209 vlen = count;
4210 }
4211
4212 switch (type) {
4213 case HTX_BLK_UNUSED:
4214 break;
4215
4216 case HTX_BLK_DATA:
4217 v = htx_get_blk_value(htx, blk);
4218 luaL_addlstring(&appctx->b, v.ptr, vlen);
4219 break;
4220
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004221 case HTX_BLK_TLR:
4222 case HTX_BLK_EOM:
4223 len = 0;
4224 break;
4225
4226 default:
4227 break;
4228 }
4229
4230 co_set_data(req, co_data(req) - vlen);
4231 count -= vlen;
4232 if (len > 0)
4233 len -= vlen;
4234 if (sz == vlen)
4235 blk = htx_remove_blk(htx, blk);
4236 else {
4237 htx_cut_data_blk(htx, blk, vlen);
4238 break;
4239 }
4240 }
4241
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004242 htx_to_buf(htx, &req->buf);
4243
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004244 /* If we are no other data available, yield waiting for new data. */
4245 if (len) {
4246 if (len > 0) {
4247 lua_pushinteger(L, len);
4248 lua_replace(L, 2);
4249 }
4250 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004251 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004252 }
4253
4254 /* return the result. */
4255 luaL_pushresult(&appctx->b);
4256 return 1;
4257}
4258
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004259/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004260__LJMP static int hlua_applet_http_recv(lua_State *L)
4261{
4262 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4263 int len = -1;
4264
4265 /* Check arguments. */
4266 if (lua_gettop(L) > 2)
4267 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
4268 if (lua_gettop(L) >= 2) {
4269 len = MAY_LJMP(luaL_checkinteger(L, 2));
4270 lua_pop(L, 1);
4271 }
4272
Christopher Fauleta2097962019-07-15 16:25:33 +02004273 lua_pushinteger(L, len);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004274
Christopher Fauleta2097962019-07-15 16:25:33 +02004275 /* Initialise the string catenation. */
4276 luaL_buffinit(L, &appctx->b);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004277
Christopher Fauleta2097962019-07-15 16:25:33 +02004278 return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004279}
4280
4281/* Append data in the output side of the buffer. This data is immediately
4282 * sent. The function returns the amount of data written. If the buffer
4283 * cannot contain the data, the function yields. The function returns -1
4284 * if the channel is closed.
4285 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004286__LJMP static int hlua_applet_http_send_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004287{
4288 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4289 struct stream_interface *si = appctx->appctx->owner;
4290 struct channel *res = si_ic(si);
4291 struct htx *htx = htx_from_buf(&res->buf);
4292 const char *data;
4293 size_t len;
4294 int l = MAY_LJMP(luaL_checkinteger(L, 3));
4295 int max;
4296
Christopher Faulet9060fc02019-07-03 11:39:30 +02004297 max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx));
Christopher Faulet4b0e9b22019-01-09 12:16:58 +01004298 if (!max)
4299 goto snd_yield;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004300
4301 data = MAY_LJMP(luaL_checklstring(L, 2, &len));
4302
4303 /* Get the max amount of data which can write as input in the channel. */
4304 if (max > (len - l))
4305 max = len - l;
4306
4307 /* Copy data. */
Willy Tarreau0a7ef022019-05-28 10:30:11 +02004308 max = htx_add_data(htx, ist2(data + l, max));
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01004309 channel_add_input(res, max);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004310
4311 /* update counters. */
4312 l += max;
4313 lua_pop(L, 1);
4314 lua_pushinteger(L, l);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004315
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004316 /* If some data is not send, declares the situation to the
4317 * applet, and returns a yield.
4318 */
4319 if (l < len) {
Christopher Faulet4b0e9b22019-01-09 12:16:58 +01004320 snd_yield:
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004321 htx_to_buf(htx, &res->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004322 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004323 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004324 }
4325
Christopher Fauleta2097962019-07-15 16:25:33 +02004326 htx_to_buf(htx, &res->buf);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004327 return 1;
4328}
4329
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004330/* Just a wrapper of "hlua_applet_send_yield". This wrapper permits
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004331 * yield the LUA process, and resume it without checking the
4332 * input arguments.
4333 */
4334__LJMP static int hlua_applet_http_send(lua_State *L)
4335{
4336 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004337
4338 /* We want to send some data. Headers must be sent. */
4339 if (!(appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
4340 hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data.");
4341 WILL_LJMP(lua_error(L));
4342 }
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004343
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004344 /* This integer is used for followinf the amount of data sent. */
Christopher Fauleta2097962019-07-15 16:25:33 +02004345 lua_pushinteger(L, 0);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004346
Christopher Fauleta2097962019-07-15 16:25:33 +02004347 return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004348}
4349
4350__LJMP static int hlua_applet_http_addheader(lua_State *L)
4351{
4352 const char *name;
4353 int ret;
4354
4355 MAY_LJMP(hlua_checkapplet_http(L, 1));
4356 name = MAY_LJMP(luaL_checkstring(L, 2));
4357 MAY_LJMP(luaL_checkstring(L, 3));
4358
4359 /* Push in the stack the "response" entry. */
4360 ret = lua_getfield(L, 1, "response");
4361 if (ret != LUA_TTABLE) {
4362 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] "
4363 "is expected as an array. %s found", lua_typename(L, ret));
4364 WILL_LJMP(lua_error(L));
4365 }
4366
4367 /* check if the header is already registered if it is not
4368 * the case, register it.
4369 */
4370 ret = lua_getfield(L, -1, name);
4371 if (ret == LUA_TNIL) {
4372
4373 /* Entry not found. */
4374 lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */
4375
4376 /* Insert the new header name in the array in the top of the stack.
4377 * It left the new array in the top of the stack.
4378 */
4379 lua_newtable(L);
4380 lua_pushvalue(L, 2);
4381 lua_pushvalue(L, -2);
4382 lua_settable(L, -4);
4383
4384 } else if (ret != LUA_TTABLE) {
4385
4386 /* corruption error. */
4387 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] "
4388 "is expected as an array. %s found", name, lua_typename(L, ret));
4389 WILL_LJMP(lua_error(L));
4390 }
4391
4392 /* Now the top od thestack is an array of values. We push
4393 * the header value as new entry.
4394 */
4395 lua_pushvalue(L, 3);
4396 ret = lua_rawlen(L, -2);
4397 lua_rawseti(L, -2, ret + 1);
4398 lua_pushboolean(L, 1);
4399 return 1;
4400}
4401
4402__LJMP static int hlua_applet_http_status(lua_State *L)
4403{
4404 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4405 int status = MAY_LJMP(luaL_checkinteger(L, 2));
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004406 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004407
4408 if (status < 100 || status > 599) {
4409 lua_pushboolean(L, 0);
4410 return 1;
4411 }
4412
4413 appctx->appctx->ctx.hlua_apphttp.status = status;
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004414 appctx->appctx->ctx.hlua_apphttp.reason = reason;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004415 lua_pushboolean(L, 1);
4416 return 1;
4417}
4418
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004419
Christopher Fauleta2097962019-07-15 16:25:33 +02004420__LJMP static int hlua_applet_http_send_response(lua_State *L)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004421{
4422 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4423 struct stream_interface *si = appctx->appctx->owner;
4424 struct channel *res = si_ic(si);
4425 struct htx *htx;
4426 struct htx_sl *sl;
4427 struct h1m h1m;
4428 const char *status, *reason;
4429 const char *name, *value;
4430 size_t nlen, vlen;
4431 unsigned int flags;
4432
4433 /* Send the message at once. */
4434 htx = htx_from_buf(&res->buf);
4435 h1m_init_res(&h1m);
4436
4437 /* Use the same http version than the request. */
4438 status = ultoa_r(appctx->appctx->ctx.hlua_apphttp.status, trash.area, trash.size);
4439 reason = appctx->appctx->ctx.hlua_apphttp.reason;
4440 if (reason == NULL)
4441 reason = http_get_reason(appctx->appctx->ctx.hlua_apphttp.status);
4442 if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) {
4443 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11);
4444 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason));
4445 }
4446 else {
4447 flags = HTX_SL_F_IS_RESP;
4448 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason));
4449 }
4450 if (!sl) {
4451 hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n",
4452 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4453 WILL_LJMP(lua_error(L));
4454 }
4455 sl->info.res.status = appctx->appctx->ctx.hlua_apphttp.status;
4456
4457 /* Get the array associated to the field "response" in the object AppletHTTP. */
4458 lua_pushvalue(L, 0);
4459 if (lua_getfield(L, 1, "response") != LUA_TTABLE) {
4460 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n",
4461 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4462 WILL_LJMP(lua_error(L));
4463 }
4464
4465 /* Browse the list of headers. */
4466 lua_pushnil(L);
4467 while(lua_next(L, -2) != 0) {
4468 /* We expect a string as -2. */
4469 if (lua_type(L, -2) != LUA_TSTRING) {
4470 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n",
4471 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4472 lua_typename(L, lua_type(L, -2)));
4473 WILL_LJMP(lua_error(L));
4474 }
4475 name = lua_tolstring(L, -2, &nlen);
4476
4477 /* We expect an array as -1. */
4478 if (lua_type(L, -1) != LUA_TTABLE) {
4479 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n",
4480 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4481 name,
4482 lua_typename(L, lua_type(L, -1)));
4483 WILL_LJMP(lua_error(L));
4484 }
4485
4486 /* Browse the table who is on the top of the stack. */
4487 lua_pushnil(L);
4488 while(lua_next(L, -2) != 0) {
4489 int id;
4490
4491 /* We expect a number as -2. */
4492 if (lua_type(L, -2) != LUA_TNUMBER) {
4493 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n",
4494 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4495 name,
4496 lua_typename(L, lua_type(L, -2)));
4497 WILL_LJMP(lua_error(L));
4498 }
4499 id = lua_tointeger(L, -2);
4500
4501 /* We expect a string as -2. */
4502 if (lua_type(L, -1) != LUA_TSTRING) {
4503 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n",
4504 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4505 name, id,
4506 lua_typename(L, lua_type(L, -1)));
4507 WILL_LJMP(lua_error(L));
4508 }
4509 value = lua_tolstring(L, -1, &vlen);
4510
4511 /* Simple Protocol checks. */
4512 if (isteqi(ist2(name, nlen), ist("transfer-encoding")))
Christopher Faulet700d9e82020-01-31 12:21:52 +01004513 h1_parse_xfer_enc_header(&h1m, ist2(value, vlen));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004514 else if (isteqi(ist2(name, nlen), ist("content-length"))) {
4515 struct ist v = ist2(value, vlen);
4516 int ret;
4517
4518 ret = h1_parse_cont_len_header(&h1m, &v);
4519 if (ret < 0) {
4520 hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n",
4521 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4522 name);
4523 WILL_LJMP(lua_error(L));
4524 }
4525 else if (ret == 0)
4526 goto next; /* Skip it */
4527 }
4528
4529 /* Add a new header */
4530 if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) {
4531 hlua_pusherror(L, "Lua applet http '%s': Failed to add header '%s' in the response.\n",
4532 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4533 name);
4534 WILL_LJMP(lua_error(L));
4535 }
4536 next:
4537 /* Remove the array from the stack, and get next element with a remaining string. */
4538 lua_pop(L, 1);
4539 }
4540
4541 /* Remove the array from the stack, and get next element with a remaining string. */
4542 lua_pop(L, 1);
4543 }
4544
4545 if (h1m.flags & H1_MF_CHNK)
4546 h1m.flags &= ~H1_MF_CLEN;
4547 if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK))
4548 h1m.flags |= H1_MF_XFER_LEN;
4549
4550 /* Uset HTX start-line flags */
4551 if (h1m.flags & H1_MF_XFER_ENC)
4552 flags |= HTX_SL_F_XFER_ENC;
4553 if (h1m.flags & H1_MF_XFER_LEN) {
4554 flags |= HTX_SL_F_XFER_LEN;
4555 if (h1m.flags & H1_MF_CHNK)
4556 flags |= HTX_SL_F_CHNK;
4557 else if (h1m.flags & H1_MF_CLEN)
4558 flags |= HTX_SL_F_CLEN;
4559 if (h1m.body_len == 0)
4560 flags |= HTX_SL_F_BODYLESS;
4561 }
4562 sl->flags |= flags;
4563
4564 /* If we dont have a content-length set, and the HTTP version is 1.1
4565 * and the status code implies the presence of a message body, we must
4566 * announce a transfer encoding chunked. This is required by haproxy
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004567 * for the keepalive compliance. If the applet announces a transfer-encoding
4568 * chunked itself, don't do anything.
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004569 */
4570 if ((flags & (HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN)) == HTX_SL_F_VER_11 &&
4571 appctx->appctx->ctx.hlua_apphttp.status >= 200 &&
4572 appctx->appctx->ctx.hlua_apphttp.status != 204 &&
4573 appctx->appctx->ctx.hlua_apphttp.status != 304) {
4574 /* Add a new header */
4575 sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN);
4576 if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) {
4577 hlua_pusherror(L, "Lua applet http '%s': Failed to add header 'transfer-encoding' in the response.\n",
4578 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4579 WILL_LJMP(lua_error(L));
4580 }
4581 }
4582
4583 /* Finalize headers. */
4584 if (!htx_add_endof(htx, HTX_BLK_EOH)) {
4585 hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n",
4586 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4587 WILL_LJMP(lua_error(L));
4588 }
4589
4590 if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) {
4591 b_reset(&res->buf);
4592 hlua_pusherror(L, "Lua: 'start_response': response header block too big");
4593 WILL_LJMP(lua_error(L));
4594 }
4595
4596 htx_to_buf(htx, &res->buf);
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01004597 channel_add_input(res, htx->data);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004598
4599 /* Headers sent, set the flag. */
4600 appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT;
4601 return 0;
4602
4603}
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004604/* We will build the status line and the headers of the HTTP response.
4605 * We will try send at once if its not possible, we give back the hand
4606 * waiting for more room.
4607 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004608__LJMP static int hlua_applet_http_start_response_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004609{
4610 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4611 struct stream_interface *si = appctx->appctx->owner;
4612 struct channel *res = si_ic(si);
4613
4614 if (co_data(res)) {
4615 si_rx_room_blk(si);
Christopher Fauleta2097962019-07-15 16:25:33 +02004616 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_start_response_yield, TICK_ETERNITY, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004617 }
Christopher Fauleta2097962019-07-15 16:25:33 +02004618 return MAY_LJMP(hlua_applet_http_send_response(L));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004619}
4620
4621
Christopher Fauleta2097962019-07-15 16:25:33 +02004622__LJMP static int hlua_applet_http_start_response(lua_State *L)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004623{
Christopher Fauleta2097962019-07-15 16:25:33 +02004624 return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004625}
4626
Christopher Fauleta2097962019-07-15 16:25:33 +02004627/*
4628 *
4629 *
4630 * Class HTTP
4631 *
4632 *
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004633 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004634
4635/* Returns a struct hlua_txn if the stack entry "ud" is
4636 * a class stream, otherwise it throws an error.
4637 */
4638__LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004639{
Christopher Fauleta2097962019-07-15 16:25:33 +02004640 return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref));
4641}
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004642
Christopher Fauleta2097962019-07-15 16:25:33 +02004643/* This function creates and push in the stack a HTTP object
4644 * according with a current TXN.
4645 */
4646static int hlua_http_new(lua_State *L, struct hlua_txn *txn)
4647{
4648 struct hlua_txn *htxn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004649
Christopher Fauleta2097962019-07-15 16:25:33 +02004650 /* Check stack size. */
4651 if (!lua_checkstack(L, 3))
4652 return 0;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004653
Christopher Fauleta2097962019-07-15 16:25:33 +02004654 /* Create the object: obj[0] = userdata.
4655 * Note that the base of the Converters object is the
4656 * same than the TXN object.
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004657 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004658 lua_newtable(L);
4659 htxn = lua_newuserdata(L, sizeof(*htxn));
4660 lua_rawseti(L, -2, 0);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004661
4662 htxn->s = txn->s;
4663 htxn->p = txn->p;
Christopher Faulet256b69a2019-05-23 11:14:21 +02004664 htxn->dir = txn->dir;
4665 htxn->flags = txn->flags;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004666
4667 /* Pop a class stream metatable and affect it to the table. */
4668 lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref);
4669 lua_setmetatable(L, -2);
4670
4671 return 1;
4672}
4673
4674/* This function creates ans returns an array of HTTP headers.
4675 * This function does not fails. It is used as wrapper with the
4676 * 2 following functions.
4677 */
Christopher Faulet9d1332b2020-02-24 16:46:16 +01004678__LJMP static int hlua_http_get_headers(lua_State *L, struct http_msg *msg)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004679{
Christopher Fauleta2097962019-07-15 16:25:33 +02004680 struct htx *htx;
4681 int32_t pos;
4682
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004683 /* Create the table. */
4684 lua_newtable(L);
4685
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004686
Christopher Fauleta2097962019-07-15 16:25:33 +02004687 htx = htxbuf(&msg->chn->buf);
4688 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
4689 struct htx_blk *blk = htx_get_blk(htx, pos);
4690 enum htx_blk_type type = htx_get_blk_type(blk);
4691 struct ist n, v;
4692 int len;
Christopher Faulet724a12c2018-12-13 22:12:15 +01004693
Christopher Fauleta2097962019-07-15 16:25:33 +02004694 if (type == HTX_BLK_HDR) {
4695 n = htx_get_blk_name(htx,blk);
4696 v = htx_get_blk_value(htx, blk);
Christopher Faulet724a12c2018-12-13 22:12:15 +01004697 }
Christopher Fauleta2097962019-07-15 16:25:33 +02004698 else if (type == HTX_BLK_EOH)
4699 break;
4700 else
4701 continue;
Christopher Faulet724a12c2018-12-13 22:12:15 +01004702
Christopher Fauleta2097962019-07-15 16:25:33 +02004703 /* Check for existing entry:
4704 * assume that the table is on the top of the stack, and
4705 * push the key in the stack, the function lua_gettable()
4706 * perform the lookup.
4707 */
4708 lua_pushlstring(L, n.ptr, n.len);
4709 lua_gettable(L, -2);
Christopher Faulet724a12c2018-12-13 22:12:15 +01004710
Christopher Fauleta2097962019-07-15 16:25:33 +02004711 switch (lua_type(L, -1)) {
4712 case LUA_TNIL:
4713 /* Table not found, create it. */
4714 lua_pop(L, 1); /* remove the nil value. */
4715 lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */
4716 lua_newtable(L); /* create and push empty table. */
4717 lua_pushlstring(L, v.ptr, v.len); /* push header value. */
4718 lua_rawseti(L, -2, 0); /* index header value (pop it). */
4719 lua_rawset(L, -3); /* index new table with header name (pop the values). */
Christopher Faulet724a12c2018-12-13 22:12:15 +01004720 break;
Christopher Faulet724a12c2018-12-13 22:12:15 +01004721
Christopher Fauleta2097962019-07-15 16:25:33 +02004722 case LUA_TTABLE:
4723 /* Entry found: push the value in the table. */
4724 len = lua_rawlen(L, -1);
4725 lua_pushlstring(L, v.ptr, v.len); /* push header value. */
4726 lua_rawseti(L, -2, len+1); /* index header value (pop it). */
4727 lua_pop(L, 1); /* remove the table (it is stored in the main table). */
4728 break;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004729
Christopher Fauleta2097962019-07-15 16:25:33 +02004730 default:
4731 /* Other cases are errors. */
4732 hlua_pusherror(L, "internal error during the parsing of headers.");
4733 WILL_LJMP(lua_error(L));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004734 }
4735 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004736 return 1;
4737}
4738
4739__LJMP static int hlua_http_req_get_headers(lua_State *L)
4740{
4741 struct hlua_txn *htxn;
4742
4743 MAY_LJMP(check_args(L, 1, "req_get_headers"));
4744 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4745
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004746 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004747 WILL_LJMP(lua_error(L));
4748
Christopher Faulet9d1332b2020-02-24 16:46:16 +01004749 return hlua_http_get_headers(L, &htxn->s->txn->req);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004750}
4751
4752__LJMP static int hlua_http_res_get_headers(lua_State *L)
4753{
4754 struct hlua_txn *htxn;
4755
4756 MAY_LJMP(check_args(L, 1, "res_get_headers"));
4757 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4758
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004759 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004760 WILL_LJMP(lua_error(L));
4761
Christopher Faulet9d1332b2020-02-24 16:46:16 +01004762 return hlua_http_get_headers(L, &htxn->s->txn->rsp);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004763}
4764
4765/* This function replace full header, or just a value in
4766 * the request or in the response. It is a wrapper fir the
4767 * 4 following functions.
4768 */
Christopher Fauletd1914aa2020-02-24 16:52:46 +01004769__LJMP static inline int hlua_http_rep_hdr(lua_State *L, struct http_msg *msg, int full)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004770{
4771 size_t name_len;
4772 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
4773 const char *reg = MAY_LJMP(luaL_checkstring(L, 3));
4774 const char *value = MAY_LJMP(luaL_checkstring(L, 4));
Christopher Fauleta2097962019-07-15 16:25:33 +02004775 struct htx *htx;
Dragan Dosen26743032019-04-30 15:54:36 +02004776 struct my_regex *re;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004777
Dragan Dosen26743032019-04-30 15:54:36 +02004778 if (!(re = regex_comp(reg, 1, 1, NULL)))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004779 WILL_LJMP(luaL_argerror(L, 3, "invalid regex"));
4780
Christopher Fauleta2097962019-07-15 16:25:33 +02004781 htx = htxbuf(&msg->chn->buf);
Christopher Fauletd1914aa2020-02-24 16:52:46 +01004782 http_replace_hdrs(chn_strm(msg->chn), htx, ist2(name, name_len), value, re, full);
Dragan Dosen26743032019-04-30 15:54:36 +02004783 regex_free(re);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004784 return 0;
4785}
4786
4787__LJMP static int hlua_http_req_rep_hdr(lua_State *L)
4788{
4789 struct hlua_txn *htxn;
4790
4791 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
4792 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4793
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004794 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004795 WILL_LJMP(lua_error(L));
4796
Christopher Fauletd1914aa2020-02-24 16:52:46 +01004797 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->req, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004798}
4799
4800__LJMP static int hlua_http_res_rep_hdr(lua_State *L)
4801{
4802 struct hlua_txn *htxn;
4803
4804 MAY_LJMP(check_args(L, 4, "res_rep_hdr"));
4805 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4806
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004807 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004808 WILL_LJMP(lua_error(L));
4809
Christopher Fauletd1914aa2020-02-24 16:52:46 +01004810 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->rsp, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004811}
4812
4813__LJMP static int hlua_http_req_rep_val(lua_State *L)
4814{
4815 struct hlua_txn *htxn;
4816
4817 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
4818 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4819
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004820 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004821 WILL_LJMP(lua_error(L));
4822
Christopher Fauletd1914aa2020-02-24 16:52:46 +01004823 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->req, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004824}
4825
4826__LJMP static int hlua_http_res_rep_val(lua_State *L)
4827{
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004828 struct hlua_txn *htxn;
4829
4830 MAY_LJMP(check_args(L, 4, "res_rep_val"));
4831 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4832
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004833 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004834 WILL_LJMP(lua_error(L));
4835
Christopher Fauletd1914aa2020-02-24 16:52:46 +01004836 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->rsp, 0));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004837}
4838
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004839/* This function deletes all the occurrences of an header.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004840 * It is a wrapper for the 2 following functions.
4841 */
Christopher Fauletd31c7b32020-02-25 09:37:57 +01004842__LJMP static inline int hlua_http_del_hdr(lua_State *L, struct http_msg *msg)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004843{
4844 size_t len;
4845 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Christopher Fauleta2097962019-07-15 16:25:33 +02004846 struct htx *htx = htxbuf(&msg->chn->buf);
4847 struct http_hdr_ctx ctx;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004848
Christopher Fauleta2097962019-07-15 16:25:33 +02004849 ctx.blk = NULL;
4850 while (http_find_header(htx, ist2(name, len), &ctx, 1))
4851 http_remove_header(htx, &ctx);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004852 return 0;
4853}
4854
4855__LJMP static int hlua_http_req_del_hdr(lua_State *L)
4856{
4857 struct hlua_txn *htxn;
4858
4859 MAY_LJMP(check_args(L, 2, "req_del_hdr"));
4860 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4861
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004862 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004863 WILL_LJMP(lua_error(L));
4864
Christopher Fauletd31c7b32020-02-25 09:37:57 +01004865 return hlua_http_del_hdr(L, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004866}
4867
4868__LJMP static int hlua_http_res_del_hdr(lua_State *L)
4869{
4870 struct hlua_txn *htxn;
4871
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004872 MAY_LJMP(check_args(L, 2, "res_del_hdr"));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004873 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4874
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004875 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004876 WILL_LJMP(lua_error(L));
4877
Christopher Fauletd31c7b32020-02-25 09:37:57 +01004878 return hlua_http_del_hdr(L, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004879}
4880
4881/* This function adds an header. It is a wrapper used by
4882 * the 2 following functions.
4883 */
Christopher Fauletd31c7b32020-02-25 09:37:57 +01004884__LJMP static inline int hlua_http_add_hdr(lua_State *L, struct http_msg *msg)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004885{
4886 size_t name_len;
4887 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
4888 size_t value_len;
4889 const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len));
Christopher Fauleta2097962019-07-15 16:25:33 +02004890 struct htx *htx = htxbuf(&msg->chn->buf);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004891
Christopher Fauleta2097962019-07-15 16:25:33 +02004892 lua_pushboolean(L, http_add_header(htx, ist2(name, name_len),
4893 ist2(value, value_len)));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004894 return 0;
4895}
4896
4897__LJMP static int hlua_http_req_add_hdr(lua_State *L)
4898{
4899 struct hlua_txn *htxn;
4900
4901 MAY_LJMP(check_args(L, 3, "req_add_hdr"));
4902 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4903
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004904 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004905 WILL_LJMP(lua_error(L));
4906
Christopher Fauletd31c7b32020-02-25 09:37:57 +01004907 return hlua_http_add_hdr(L, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004908}
4909
4910__LJMP static int hlua_http_res_add_hdr(lua_State *L)
4911{
4912 struct hlua_txn *htxn;
4913
4914 MAY_LJMP(check_args(L, 3, "res_add_hdr"));
4915 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4916
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004917 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004918 WILL_LJMP(lua_error(L));
4919
Christopher Fauletd31c7b32020-02-25 09:37:57 +01004920 return hlua_http_add_hdr(L, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004921}
4922
4923static int hlua_http_req_set_hdr(lua_State *L)
4924{
4925 struct hlua_txn *htxn;
4926
4927 MAY_LJMP(check_args(L, 3, "req_set_hdr"));
4928 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4929
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004930 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004931 WILL_LJMP(lua_error(L));
4932
Christopher Fauletd31c7b32020-02-25 09:37:57 +01004933 hlua_http_del_hdr(L, &htxn->s->txn->req);
4934 return hlua_http_add_hdr(L, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004935}
4936
4937static int hlua_http_res_set_hdr(lua_State *L)
4938{
4939 struct hlua_txn *htxn;
4940
4941 MAY_LJMP(check_args(L, 3, "res_set_hdr"));
4942 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4943
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004944 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004945 WILL_LJMP(lua_error(L));
4946
Christopher Fauletd31c7b32020-02-25 09:37:57 +01004947 hlua_http_del_hdr(L, &htxn->s->txn->rsp);
4948 return hlua_http_add_hdr(L, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004949}
4950
4951/* This function set the method. */
4952static int hlua_http_req_set_meth(lua_State *L)
4953{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02004954 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004955 size_t name_len;
4956 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004957
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004958 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004959 WILL_LJMP(lua_error(L));
4960
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004961 lua_pushboolean(L, http_req_replace_stline(0, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004962 return 1;
4963}
4964
4965/* This function set the method. */
4966static int hlua_http_req_set_path(lua_State *L)
4967{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02004968 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004969 size_t name_len;
4970 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004971
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004972 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004973 WILL_LJMP(lua_error(L));
4974
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004975 lua_pushboolean(L, http_req_replace_stline(1, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004976 return 1;
4977}
4978
4979/* This function set the query-string. */
4980static int hlua_http_req_set_query(lua_State *L)
4981{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02004982 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004983 size_t name_len;
4984 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004985
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004986 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004987 WILL_LJMP(lua_error(L));
4988
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004989 /* Check length. */
4990 if (name_len > trash.size - 1) {
4991 lua_pushboolean(L, 0);
4992 return 1;
4993 }
4994
4995 /* Add the mark question as prefix. */
4996 chunk_reset(&trash);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004997 trash.area[trash.data++] = '?';
4998 memcpy(trash.area + trash.data, name, name_len);
4999 trash.data += name_len;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005000
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005001 lua_pushboolean(L,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005002 http_req_replace_stline(2, trash.area, trash.data, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005003 return 1;
5004}
5005
5006/* This function set the uri. */
5007static int hlua_http_req_set_uri(lua_State *L)
5008{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005009 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005010 size_t name_len;
5011 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005012
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005013 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005014 WILL_LJMP(lua_error(L));
5015
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005016 lua_pushboolean(L, http_req_replace_stline(3, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005017 return 1;
5018}
5019
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005020/* This function set the response code & optionally reason. */
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005021static int hlua_http_res_set_status(lua_State *L)
5022{
5023 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5024 unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2));
Christopher Faulet96bff762019-12-17 13:46:18 +01005025 const char *str = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
5026 const struct ist reason = ist2(str, (str ? strlen(str) : 0));
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005027
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005028 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005029 WILL_LJMP(lua_error(L));
5030
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005031 http_res_set_status(code, reason, htxn->s);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005032 return 0;
5033}
5034
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005035/*
5036 *
5037 *
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005038 * Class TXN
5039 *
5040 *
5041 */
5042
5043/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02005044 * a class stream, otherwise it throws an error.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005045 */
5046__LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud)
5047{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02005048 return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref));
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005049}
5050
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005051__LJMP static int hlua_set_var(lua_State *L)
5052{
5053 struct hlua_txn *htxn;
5054 const char *name;
5055 size_t len;
5056 struct sample smp;
5057
Tim Duesterhus4e172c92020-05-19 13:49:42 +02005058 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
5059 WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments"));
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005060
5061 /* It is useles to retrieve the stream, but this function
5062 * runs only in a stream context.
5063 */
5064 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5065 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5066
5067 /* Converts the third argument in a sample. */
5068 hlua_lua2smp(L, 3, &smp);
5069
5070 /* Store the sample in a variable. */
Willy Tarreau7560dd42016-03-10 16:28:58 +01005071 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Tim Duesterhus4e172c92020-05-19 13:49:42 +02005072
5073 if (lua_gettop(L) == 4 && lua_toboolean(L, 4))
5074 lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0);
5075 else
5076 lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0);
5077
Tim Duesterhus84ebc132020-05-19 13:49:41 +02005078 return 1;
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005079}
5080
Christopher Faulet85d79c92016-11-09 16:54:56 +01005081__LJMP static int hlua_unset_var(lua_State *L)
5082{
5083 struct hlua_txn *htxn;
5084 const char *name;
5085 size_t len;
5086 struct sample smp;
5087
5088 MAY_LJMP(check_args(L, 2, "unset_var"));
5089
5090 /* It is useles to retrieve the stream, but this function
5091 * runs only in a stream context.
5092 */
5093 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5094 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5095
5096 /* Unset the variable. */
5097 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Tim Duesterhus84ebc132020-05-19 13:49:41 +02005098 lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0);
5099 return 1;
Christopher Faulet85d79c92016-11-09 16:54:56 +01005100}
5101
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005102__LJMP static int hlua_get_var(lua_State *L)
5103{
5104 struct hlua_txn *htxn;
5105 const char *name;
5106 size_t len;
5107 struct sample smp;
5108
5109 MAY_LJMP(check_args(L, 2, "get_var"));
5110
5111 /* It is useles to retrieve the stream, but this function
5112 * runs only in a stream context.
5113 */
5114 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5115 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5116
Willy Tarreau7560dd42016-03-10 16:28:58 +01005117 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Willy Tarreau6204cd92016-03-10 16:33:04 +01005118 if (!vars_get_by_name(name, len, &smp)) {
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005119 lua_pushnil(L);
5120 return 1;
5121 }
5122
5123 return hlua_smp2lua(L, &smp);
5124}
5125
Willy Tarreau59551662015-03-10 14:23:13 +01005126__LJMP static int hlua_set_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005127{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005128 struct hlua *hlua;
5129
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005130 MAY_LJMP(check_args(L, 2, "set_priv"));
5131
Willy Tarreau87b09662015-04-03 00:22:06 +02005132 /* It is useles to retrieve the stream, but this function
5133 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005134 */
5135 MAY_LJMP(hlua_checktxn(L, 1));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005136 hlua = hlua_gethlua(L);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005137
5138 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02005139 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005140
5141 /* Get and store new value. */
5142 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
5143 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
5144
5145 return 0;
5146}
5147
Willy Tarreau59551662015-03-10 14:23:13 +01005148__LJMP static int hlua_get_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005149{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005150 struct hlua *hlua;
5151
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005152 MAY_LJMP(check_args(L, 1, "get_priv"));
5153
Willy Tarreau87b09662015-04-03 00:22:06 +02005154 /* It is useles to retrieve the stream, but this function
5155 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005156 */
5157 MAY_LJMP(hlua_checktxn(L, 1));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005158 hlua = hlua_gethlua(L);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005159
5160 /* Push configuration index in the stack. */
5161 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
5162
5163 return 1;
5164}
5165
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005166/* Create stack entry containing a class TXN. This function
5167 * return 0 if the stack does not contains free slots,
5168 * otherwise it returns 1.
5169 */
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005170static int hlua_txn_new(lua_State *L, struct stream *s, struct proxy *p, int dir, int flags)
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005171{
Willy Tarreaude491382015-04-06 11:04:28 +02005172 struct hlua_txn *htxn;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005173
5174 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005175 if (!lua_checkstack(L, 3))
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005176 return 0;
5177
5178 /* NOTE: The allocation never fails. The failure
5179 * throw an error, and the function never returns.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08005180 * if the throw is not available, the process is aborted.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005181 */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005182 /* Create the object: obj[0] = userdata. */
5183 lua_newtable(L);
Willy Tarreaude491382015-04-06 11:04:28 +02005184 htxn = lua_newuserdata(L, sizeof(*htxn));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005185 lua_rawseti(L, -2, 0);
5186
Willy Tarreaude491382015-04-06 11:04:28 +02005187 htxn->s = s;
5188 htxn->p = p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01005189 htxn->dir = dir;
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005190 htxn->flags = flags;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005191
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005192 /* Create the "f" field that contains a list of fetches. */
5193 lua_pushstring(L, "f");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005194 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005195 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005196 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005197
5198 /* Create the "sf" field that contains a list of stringsafe fetches. */
5199 lua_pushstring(L, "sf");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005200 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP | HLUA_F_AS_STRING))
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005201 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005202 lua_rawset(L, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005203
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005204 /* Create the "c" field that contains a list of converters. */
5205 lua_pushstring(L, "c");
Willy Tarreaude491382015-04-06 11:04:28 +02005206 if (!hlua_converters_new(L, htxn, 0))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005207 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005208 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005209
5210 /* Create the "sc" field that contains a list of stringsafe converters. */
5211 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01005212 if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING))
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005213 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005214 lua_rawset(L, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005215
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005216 /* Create the "req" field that contains the request channel object. */
5217 lua_pushstring(L, "req");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005218 if (!hlua_channel_new(L, &s->req))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005219 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005220 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005221
5222 /* Create the "res" field that contains the response channel object. */
5223 lua_pushstring(L, "res");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005224 if (!hlua_channel_new(L, &s->res))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005225 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005226 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005227
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005228 /* Creates the HTTP object is the current proxy allows http. */
5229 lua_pushstring(L, "http");
5230 if (p->mode == PR_MODE_HTTP) {
Willy Tarreaude491382015-04-06 11:04:28 +02005231 if (!hlua_http_new(L, htxn))
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005232 return 0;
5233 }
5234 else
5235 lua_pushnil(L);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005236 lua_rawset(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005237
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005238 /* Pop a class sesison metatable and affect it to the userdata. */
5239 lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref);
5240 lua_setmetatable(L, -2);
5241
5242 return 1;
5243}
5244
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005245__LJMP static int hlua_txn_deflog(lua_State *L)
5246{
5247 const char *msg;
5248 struct hlua_txn *htxn;
5249
5250 MAY_LJMP(check_args(L, 2, "deflog"));
5251 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5252 msg = MAY_LJMP(luaL_checkstring(L, 2));
5253
5254 hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg);
5255 return 0;
5256}
5257
5258__LJMP static int hlua_txn_log(lua_State *L)
5259{
5260 int level;
5261 const char *msg;
5262 struct hlua_txn *htxn;
5263
5264 MAY_LJMP(check_args(L, 3, "log"));
5265 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5266 level = MAY_LJMP(luaL_checkinteger(L, 2));
5267 msg = MAY_LJMP(luaL_checkstring(L, 3));
5268
5269 if (level < 0 || level >= NB_LOG_LEVELS)
5270 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
5271
5272 hlua_sendlog(htxn->s->be, level, msg);
5273 return 0;
5274}
5275
5276__LJMP static int hlua_txn_log_debug(lua_State *L)
5277{
5278 const char *msg;
5279 struct hlua_txn *htxn;
5280
5281 MAY_LJMP(check_args(L, 2, "Debug"));
5282 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5283 msg = MAY_LJMP(luaL_checkstring(L, 2));
5284 hlua_sendlog(htxn->s->be, LOG_DEBUG, msg);
5285 return 0;
5286}
5287
5288__LJMP static int hlua_txn_log_info(lua_State *L)
5289{
5290 const char *msg;
5291 struct hlua_txn *htxn;
5292
5293 MAY_LJMP(check_args(L, 2, "Info"));
5294 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5295 msg = MAY_LJMP(luaL_checkstring(L, 2));
5296 hlua_sendlog(htxn->s->be, LOG_INFO, msg);
5297 return 0;
5298}
5299
5300__LJMP static int hlua_txn_log_warning(lua_State *L)
5301{
5302 const char *msg;
5303 struct hlua_txn *htxn;
5304
5305 MAY_LJMP(check_args(L, 2, "Warning"));
5306 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5307 msg = MAY_LJMP(luaL_checkstring(L, 2));
5308 hlua_sendlog(htxn->s->be, LOG_WARNING, msg);
5309 return 0;
5310}
5311
5312__LJMP static int hlua_txn_log_alert(lua_State *L)
5313{
5314 const char *msg;
5315 struct hlua_txn *htxn;
5316
5317 MAY_LJMP(check_args(L, 2, "Alert"));
5318 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5319 msg = MAY_LJMP(luaL_checkstring(L, 2));
5320 hlua_sendlog(htxn->s->be, LOG_ALERT, msg);
5321 return 0;
5322}
5323
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005324__LJMP static int hlua_txn_set_loglevel(lua_State *L)
5325{
5326 struct hlua_txn *htxn;
5327 int ll;
5328
5329 MAY_LJMP(check_args(L, 2, "set_loglevel"));
5330 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5331 ll = MAY_LJMP(luaL_checkinteger(L, 2));
5332
5333 if (ll < 0 || ll > 7)
5334 WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7"));
5335
5336 htxn->s->logs.level = ll;
5337 return 0;
5338}
5339
5340__LJMP static int hlua_txn_set_tos(lua_State *L)
5341{
5342 struct hlua_txn *htxn;
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005343 int tos;
5344
5345 MAY_LJMP(check_args(L, 2, "set_tos"));
5346 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5347 tos = MAY_LJMP(luaL_checkinteger(L, 2));
5348
Willy Tarreau1a18b542018-12-11 16:37:42 +01005349 conn_set_tos(objt_conn(htxn->s->sess->origin), tos);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005350 return 0;
5351}
5352
5353__LJMP static int hlua_txn_set_mark(lua_State *L)
5354{
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005355 struct hlua_txn *htxn;
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005356 int mark;
5357
5358 MAY_LJMP(check_args(L, 2, "set_mark"));
5359 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5360 mark = MAY_LJMP(luaL_checkinteger(L, 2));
5361
Lukas Tribus579e3e32019-08-11 18:03:45 +02005362 conn_set_mark(objt_conn(htxn->s->sess->origin), mark);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005363 return 0;
5364}
5365
Patrick Hemmer268a7072018-05-11 12:52:31 -04005366__LJMP static int hlua_txn_set_priority_class(lua_State *L)
5367{
5368 struct hlua_txn *htxn;
5369
5370 MAY_LJMP(check_args(L, 2, "set_priority_class"));
5371 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5372 htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2)));
5373 return 0;
5374}
5375
5376__LJMP static int hlua_txn_set_priority_offset(lua_State *L)
5377{
5378 struct hlua_txn *htxn;
5379
5380 MAY_LJMP(check_args(L, 2, "set_priority_offset"));
5381 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5382 htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2)));
5383 return 0;
5384}
5385
Christopher Faulet700d9e82020-01-31 12:21:52 +01005386/* Forward the Reply object to the client. This function converts the reply in
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05005387 * HTX an push it to into the response channel. It is response to forward the
Christopher Faulet700d9e82020-01-31 12:21:52 +01005388 * message and terminate the transaction. It returns 1 on success and 0 on
5389 * error. The Reply must be on top of the stack.
5390 */
5391__LJMP static int hlua_txn_forward_reply(lua_State *L, struct stream *s)
5392{
5393 struct htx *htx;
5394 struct htx_sl *sl;
5395 struct h1m h1m;
5396 const char *status, *reason, *body;
5397 size_t status_len, reason_len, body_len;
5398 int ret, code, flags;
5399
5400 code = 200;
5401 status = "200";
5402 status_len = 3;
5403 ret = lua_getfield(L, -1, "status");
5404 if (ret == LUA_TNUMBER) {
5405 code = lua_tointeger(L, -1);
5406 status = lua_tolstring(L, -1, &status_len);
5407 }
5408 lua_pop(L, 1);
5409
5410 reason = http_get_reason(code);
5411 reason_len = strlen(reason);
5412 ret = lua_getfield(L, -1, "reason");
5413 if (ret == LUA_TSTRING)
5414 reason = lua_tolstring(L, -1, &reason_len);
5415 lua_pop(L, 1);
5416
5417 body = NULL;
5418 body_len = 0;
5419 ret = lua_getfield(L, -1, "body");
5420 if (ret == LUA_TSTRING)
5421 body = lua_tolstring(L, -1, &body_len);
5422 lua_pop(L, 1);
5423
5424 /* Prepare the response before inserting the headers */
5425 h1m_init_res(&h1m);
5426 htx = htx_from_buf(&s->res.buf);
5427 channel_htx_truncate(&s->res, htx);
5428 if (s->txn->req.flags & HTTP_MSGF_VER_11) {
5429 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11);
5430 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"),
5431 ist2(status, status_len), ist2(reason, reason_len));
5432 }
5433 else {
5434 flags = HTX_SL_F_IS_RESP;
5435 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"),
5436 ist2(status, status_len), ist2(reason, reason_len));
5437 }
5438 if (!sl)
5439 goto fail;
5440 sl->info.res.status = code;
5441
5442 /* Push in the stack the "headers" entry. */
5443 ret = lua_getfield(L, -1, "headers");
5444 if (ret != LUA_TTABLE)
5445 goto skip_headers;
5446
5447 lua_pushnil(L);
5448 while (lua_next(L, -2) != 0) {
5449 struct ist name, value;
5450 const char *n, *v;
5451 size_t nlen, vlen;
5452
5453 if (!lua_isstring(L, -2) || !lua_istable(L, -1)) {
5454 /* Skip element if the key is not a string or if the value is not a table */
5455 goto next_hdr;
5456 }
5457
5458 n = lua_tolstring(L, -2, &nlen);
5459 name = ist2(n, nlen);
5460 if (isteqi(name, ist("content-length"))) {
5461 /* Always skip content-length header. It will be added
5462 * later with the correct len
5463 */
5464 goto next_hdr;
5465 }
5466
5467 /* Loop on header's values */
5468 lua_pushnil(L);
5469 while (lua_next(L, -2)) {
5470 if (!lua_isstring(L, -1)) {
5471 /* Skip the value if it is not a string */
5472 goto next_value;
5473 }
5474
5475 v = lua_tolstring(L, -1, &vlen);
5476 value = ist2(v, vlen);
5477
5478 if (isteqi(name, ist("transfer-encoding")))
5479 h1_parse_xfer_enc_header(&h1m, value);
5480 if (!htx_add_header(htx, ist2(n, nlen), ist2(v, vlen)))
5481 goto fail;
5482
5483 next_value:
5484 lua_pop(L, 1);
5485 }
5486
5487 next_hdr:
5488 lua_pop(L, 1);
5489 }
5490 skip_headers:
5491 lua_pop(L, 1);
5492
5493 /* Update h1m flags: CLEN is set if CHNK is not present */
5494 if (!(h1m.flags & H1_MF_CHNK)) {
5495 const char *clen = ultoa(body_len);
5496
5497 h1m.flags |= H1_MF_CLEN;
5498 if (!htx_add_header(htx, ist("content-length"), ist(clen)))
5499 goto fail;
5500 }
5501 if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK))
5502 h1m.flags |= H1_MF_XFER_LEN;
5503
5504 /* Update HTX start-line flags */
5505 if (h1m.flags & H1_MF_XFER_ENC)
5506 flags |= HTX_SL_F_XFER_ENC;
5507 if (h1m.flags & H1_MF_XFER_LEN) {
5508 flags |= HTX_SL_F_XFER_LEN;
5509 if (h1m.flags & H1_MF_CHNK)
5510 flags |= HTX_SL_F_CHNK;
5511 else if (h1m.flags & H1_MF_CLEN)
5512 flags |= HTX_SL_F_CLEN;
5513 if (h1m.body_len == 0)
5514 flags |= HTX_SL_F_BODYLESS;
5515 }
5516 sl->flags |= flags;
5517
5518
5519 if (!htx_add_endof(htx, HTX_BLK_EOH) ||
5520 (body_len && !htx_add_data_atonce(htx, ist2(body, body_len))) ||
5521 !htx_add_endof(htx, HTX_BLK_EOM))
5522 goto fail;
5523
5524 /* Now, forward the response and terminate the transaction */
5525 s->txn->status = code;
5526 htx_to_buf(htx, &s->res.buf);
5527 if (!http_forward_proxy_resp(s, 1))
5528 goto fail;
5529
5530 return 1;
5531
5532 fail:
5533 channel_htx_truncate(&s->res, htx);
5534 return 0;
5535}
5536
5537/* Terminate a transaction if called from a lua action. For TCP streams,
5538 * processing is just aborted. Nothing is returned to the client and all
5539 * arguments are ignored. For HTTP streams, if a reply is passed as argument, it
5540 * is forwarded to the client before terminating the transaction. On success,
5541 * the function exits with ACT_RET_DONE code. If an error occurred, it exits
5542 * with ACT_RET_ERR code. If this function is not called from a lua action, it
5543 * just exits without any processing.
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005544 */
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005545__LJMP static int hlua_txn_done(lua_State *L)
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005546{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005547 struct hlua_txn *htxn;
Christopher Faulet700d9e82020-01-31 12:21:52 +01005548 struct stream *s;
5549 int finst;
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005550
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005551 htxn = MAY_LJMP(hlua_checktxn(L, 1));
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005552
Christopher Faulet700d9e82020-01-31 12:21:52 +01005553 /* If the flags NOTERM is set, we cannot terminate the session, so we
5554 * just end the execution of the current lua code. */
5555 if (htxn->flags & HLUA_TXN_NOTERM)
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005556 WILL_LJMP(hlua_done(L));
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005557
Christopher Faulet700d9e82020-01-31 12:21:52 +01005558 s = htxn->s;
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005559 if (!IS_HTX_STRM(htxn->s)) {
Christopher Faulet700d9e82020-01-31 12:21:52 +01005560 struct channel *req = &s->req;
5561 struct channel *res = &s->res;
Willy Tarreau81389672015-03-10 12:03:52 +01005562
Christopher Faulet700d9e82020-01-31 12:21:52 +01005563 channel_auto_read(req);
5564 channel_abort(req);
5565 channel_auto_close(req);
5566 channel_erase(req);
5567
5568 res->wex = tick_add_ifset(now_ms, res->wto);
5569 channel_auto_read(res);
5570 channel_auto_close(res);
5571 channel_shutr_now(res);
5572
5573 finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_D);
5574 goto done;
Christopher Fauletfe6a71b2019-07-26 16:40:24 +02005575 }
Thierry FOURNIER10ec2142015-08-24 17:23:45 +02005576
Christopher Faulet700d9e82020-01-31 12:21:52 +01005577 if (lua_gettop(L) == 1 || !lua_istable(L, 2)) {
5578 /* No reply or invalid reply */
5579 s->txn->status = 0;
5580 http_reply_and_close(s, 0, NULL);
5581 }
5582 else {
5583 /* Remove extra args to have the reply on top of the stack */
5584 if (lua_gettop(L) > 2)
5585 lua_pop(L, lua_gettop(L) - 2);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005586
Christopher Faulet700d9e82020-01-31 12:21:52 +01005587 if (!hlua_txn_forward_reply(L, s)) {
5588 if (!(s->flags & SF_ERR_MASK))
5589 s->flags |= SF_ERR_PRXCOND;
5590 lua_pushinteger(L, ACT_RET_ERR);
5591 WILL_LJMP(hlua_done(L));
5592 return 0; /* Never reached */
5593 }
Christopher Faulet4d0e2632019-07-16 10:52:40 +02005594 }
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005595
Christopher Faulet700d9e82020-01-31 12:21:52 +01005596 finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_H);
5597 if (htxn->dir == SMP_OPT_DIR_REQ) {
5598 /* let's log the request time */
5599 s->logs.tv_request = now;
5600 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
5601 _HA_ATOMIC_ADD(&s->sess->fe->fe_counters.intercepted_req, 1);
5602 }
Christopher Fauletfe6a71b2019-07-26 16:40:24 +02005603
Christopher Faulet700d9e82020-01-31 12:21:52 +01005604 done:
5605 if (!(s->flags & SF_ERR_MASK))
5606 s->flags |= SF_ERR_LOCAL;
5607 if (!(s->flags & SF_FINST_MASK))
5608 s->flags |= finst;
Christopher Fauletfe6a71b2019-07-26 16:40:24 +02005609
Christopher Faulet4ad73102020-03-05 11:07:31 +01005610 lua_pushinteger(L, ACT_RET_ABRT);
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005611 WILL_LJMP(hlua_done(L));
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005612 return 0;
5613}
5614
Christopher Faulet700d9e82020-01-31 12:21:52 +01005615/*
5616 *
5617 *
5618 * Class REPLY
5619 *
5620 *
5621 */
5622
5623/* Pushes the TXN reply onto the top of the stack. If the stask does not have a
5624 * free slots, the function fails and returns 0;
5625 */
5626static int hlua_txn_reply_new(lua_State *L)
5627{
5628 struct hlua_txn *htxn;
5629 const char *reason, *body = NULL;
5630 int ret, status;
5631
5632 htxn = MAY_LJMP(hlua_checktxn(L, 1));
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005633 if (!IS_HTX_STRM(htxn->s)) {
Christopher Faulet700d9e82020-01-31 12:21:52 +01005634 hlua_pusherror(L, "txn object is not an HTTP transaction.");
5635 WILL_LJMP(lua_error(L));
5636 }
5637
5638 /* Default value */
5639 status = 200;
5640 reason = http_get_reason(status);
5641
5642 if (lua_istable(L, 2)) {
5643 /* load status and reason from the table argument at index 2 */
5644 ret = lua_getfield(L, 2, "status");
5645 if (ret == LUA_TNIL)
5646 goto reason;
5647 else if (ret != LUA_TNUMBER) {
5648 /* invalid status: ignore the reason */
5649 goto body;
5650 }
5651 status = lua_tointeger(L, -1);
5652
5653 reason:
5654 lua_pop(L, 1); /* restore the stack: remove status */
5655 ret = lua_getfield(L, 2, "reason");
5656 if (ret == LUA_TSTRING)
5657 reason = lua_tostring(L, -1);
5658
5659 body:
5660 lua_pop(L, 1); /* restore the stack: remove invalid status or reason */
5661 ret = lua_getfield(L, 2, "body");
5662 if (ret == LUA_TSTRING)
5663 body = lua_tostring(L, -1);
5664 lua_pop(L, 1); /* restore the stack: remove body */
5665 }
5666
5667 /* Create the Reply table */
5668 lua_newtable(L);
5669
5670 /* Add status element */
5671 lua_pushstring(L, "status");
5672 lua_pushinteger(L, status);
5673 lua_settable(L, -3);
5674
5675 /* Add reason element */
5676 reason = http_get_reason(status);
5677 lua_pushstring(L, "reason");
5678 lua_pushstring(L, reason);
5679 lua_settable(L, -3);
5680
5681 /* Add body element, nil if undefined */
5682 lua_pushstring(L, "body");
5683 if (body)
5684 lua_pushstring(L, body);
5685 else
5686 lua_pushnil(L);
5687 lua_settable(L, -3);
5688
5689 /* Add headers element */
5690 lua_pushstring(L, "headers");
5691 lua_newtable(L);
5692
5693 /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */
5694 if (lua_istable(L, 2)) {
5695 /* load headers from the table argument at index 2. If it is a table, copy it. */
5696 ret = lua_getfield(L, 2, "headers");
5697 if (ret == LUA_TTABLE) {
5698 /* stack: [ ... <headers:table>, <table> ] */
5699 lua_pushnil(L);
5700 while (lua_next(L, -2) != 0) {
5701 /* stack: [ ... <headers:table>, <table>, k, v] */
5702 if (!lua_isstring(L, -1) && !lua_istable(L, -1)) {
5703 /* invalid value type, skip it */
5704 lua_pop(L, 1);
5705 continue;
5706 }
5707
5708
5709 /* Duplicate the key and swap it with the value. */
5710 lua_pushvalue(L, -2);
5711 lua_insert(L, -2);
5712 /* stack: [ ... <headers:table>, <table>, k, k, v ] */
5713
5714 lua_newtable(L);
5715 lua_insert(L, -2);
5716 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, v ] */
5717
5718 if (lua_isstring(L, -1)) {
5719 /* push the value in the inner table */
5720 lua_rawseti(L, -2, 1);
5721 }
5722 else { /* table */
5723 lua_pushnil(L);
5724 while (lua_next(L, -2) != 0) {
5725 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2, v2 ] */
5726 if (!lua_isstring(L, -1)) {
5727 /* invalid value type, skip it*/
5728 lua_pop(L, 1);
5729 continue;
5730 }
5731 /* push the value in the inner table */
5732 lua_rawseti(L, -4, lua_rawlen(L, -4) + 1);
5733 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2 ] */
5734 }
5735 lua_pop(L, 1);
5736 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table> ] */
5737 }
5738
5739 /* push (k,v) on the stack in the headers table:
5740 * stack: [ ... <headers:table>, <table>, k, k, v ]
5741 */
5742 lua_settable(L, -5);
5743 /* stack: [ ... <headers:table>, <table>, k ] */
5744 }
5745 }
5746 lua_pop(L, 1);
5747 }
5748 /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */
5749 lua_settable(L, -3);
5750 /* stack: [ txn, <Arg:table>, <Reply:table> ] */
5751
5752 /* Pop a class sesison metatable and affect it to the userdata. */
5753 lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_reply_ref);
5754 lua_setmetatable(L, -2);
5755 return 1;
5756}
5757
5758/* Set the reply status code, and optionally the reason. If no reason is
5759 * provided, the default one corresponding to the status code is used.
5760 */
5761__LJMP static int hlua_txn_reply_set_status(lua_State *L)
5762{
5763 int status = MAY_LJMP(luaL_checkinteger(L, 2));
5764 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
5765
5766 /* First argument (self) must be a table */
5767 luaL_checktype(L, 1, LUA_TTABLE);
5768
5769 if (status < 100 || status > 599) {
5770 lua_pushboolean(L, 0);
5771 return 1;
5772 }
5773 if (!reason)
5774 reason = http_get_reason(status);
5775
5776 lua_pushinteger(L, status);
5777 lua_setfield(L, 1, "status");
5778
5779 lua_pushstring(L, reason);
5780 lua_setfield(L, 1, "reason");
5781
5782 lua_pushboolean(L, 1);
5783 return 1;
5784}
5785
5786/* Add a header into the reply object. Each header name is associated to an
5787 * array of values in the "headers" table. If the header name is not found, a
5788 * new entry is created.
5789 */
5790__LJMP static int hlua_txn_reply_add_header(lua_State *L)
5791{
5792 const char *name = MAY_LJMP(luaL_checkstring(L, 2));
5793 const char *value = MAY_LJMP(luaL_checkstring(L, 3));
5794 int ret;
5795
5796 /* First argument (self) must be a table */
5797 luaL_checktype(L, 1, LUA_TTABLE);
5798
5799 /* Push in the stack the "headers" entry. */
5800 ret = lua_getfield(L, 1, "headers");
5801 if (ret != LUA_TTABLE) {
5802 hlua_pusherror(L, "Reply['headers'] is expected to a an array. %s found", lua_typename(L, ret));
5803 WILL_LJMP(lua_error(L));
5804 }
5805
5806 /* check if the header is already registered. If not, register it. */
5807 ret = lua_getfield(L, -1, name);
5808 if (ret == LUA_TNIL) {
5809 /* Entry not found. */
5810 lua_pop(L, 1); /* remove the nil. The "headers" table is the top of the stack. */
5811
5812 /* Insert the new header name in the array in the top of the stack.
5813 * It left the new array in the top of the stack.
5814 */
5815 lua_newtable(L);
5816 lua_pushstring(L, name);
5817 lua_pushvalue(L, -2);
5818 lua_settable(L, -4);
5819 }
5820 else if (ret != LUA_TTABLE) {
5821 hlua_pusherror(L, "Reply['headers']['%s'] is expected to be an array. %s found", name, lua_typename(L, ret));
5822 WILL_LJMP(lua_error(L));
5823 }
5824
5825 /* Now the top od thestack is an array of values. We push
5826 * the header value as new entry.
5827 */
5828 lua_pushstring(L, value);
5829 ret = lua_rawlen(L, -2);
5830 lua_rawseti(L, -2, ret + 1);
5831
5832 lua_pushboolean(L, 1);
5833 return 1;
5834}
5835
5836/* Remove all occurrences of a given header name. */
5837__LJMP static int hlua_txn_reply_del_header(lua_State *L)
5838{
5839 const char *name = MAY_LJMP(luaL_checkstring(L, 2));
5840 int ret;
5841
5842 /* First argument (self) must be a table */
5843 luaL_checktype(L, 1, LUA_TTABLE);
5844
5845 /* Push in the stack the "headers" entry. */
5846 ret = lua_getfield(L, 1, "headers");
5847 if (ret != LUA_TTABLE) {
5848 hlua_pusherror(L, "Reply['headers'] is expected to be an array. %s found", lua_typename(L, ret));
5849 WILL_LJMP(lua_error(L));
5850 }
5851
5852 lua_pushstring(L, name);
5853 lua_pushnil(L);
5854 lua_settable(L, -3);
5855
5856 lua_pushboolean(L, 1);
5857 return 1;
5858}
5859
5860/* Set the reply's body. Overwrite any existing entry. */
5861__LJMP static int hlua_txn_reply_set_body(lua_State *L)
5862{
5863 const char *payload = MAY_LJMP(luaL_checkstring(L, 2));
5864
5865 /* First argument (self) must be a table */
5866 luaL_checktype(L, 1, LUA_TTABLE);
5867
5868 lua_pushstring(L, payload);
5869 lua_setfield(L, 1, "body");
5870
5871 lua_pushboolean(L, 1);
5872 return 1;
5873}
5874
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005875__LJMP static int hlua_log(lua_State *L)
5876{
5877 int level;
5878 const char *msg;
5879
5880 MAY_LJMP(check_args(L, 2, "log"));
5881 level = MAY_LJMP(luaL_checkinteger(L, 1));
5882 msg = MAY_LJMP(luaL_checkstring(L, 2));
5883
5884 if (level < 0 || level >= NB_LOG_LEVELS)
5885 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
5886
5887 hlua_sendlog(NULL, level, msg);
5888 return 0;
5889}
5890
5891__LJMP static int hlua_log_debug(lua_State *L)
5892{
5893 const char *msg;
5894
5895 MAY_LJMP(check_args(L, 1, "debug"));
5896 msg = MAY_LJMP(luaL_checkstring(L, 1));
5897 hlua_sendlog(NULL, LOG_DEBUG, msg);
5898 return 0;
5899}
5900
5901__LJMP static int hlua_log_info(lua_State *L)
5902{
5903 const char *msg;
5904
5905 MAY_LJMP(check_args(L, 1, "info"));
5906 msg = MAY_LJMP(luaL_checkstring(L, 1));
5907 hlua_sendlog(NULL, LOG_INFO, msg);
5908 return 0;
5909}
5910
5911__LJMP static int hlua_log_warning(lua_State *L)
5912{
5913 const char *msg;
5914
5915 MAY_LJMP(check_args(L, 1, "warning"));
5916 msg = MAY_LJMP(luaL_checkstring(L, 1));
5917 hlua_sendlog(NULL, LOG_WARNING, msg);
5918 return 0;
5919}
5920
5921__LJMP static int hlua_log_alert(lua_State *L)
5922{
5923 const char *msg;
5924
5925 MAY_LJMP(check_args(L, 1, "alert"));
5926 msg = MAY_LJMP(luaL_checkstring(L, 1));
5927 hlua_sendlog(NULL, LOG_ALERT, msg);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005928 return 0;
5929}
5930
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005931__LJMP static int hlua_sleep_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005932{
5933 int wakeup_ms = lua_tointeger(L, -1);
5934 if (now_ms < wakeup_ms)
Willy Tarreau9635e032018-10-16 17:52:55 +02005935 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005936 return 0;
5937}
5938
5939__LJMP static int hlua_sleep(lua_State *L)
5940{
5941 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005942 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005943
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005944 MAY_LJMP(check_args(L, 1, "sleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005945
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005946 delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005947 wakeup_ms = tick_add(now_ms, delay);
5948 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005949
Willy Tarreau9635e032018-10-16 17:52:55 +02005950 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005951 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005952}
5953
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005954__LJMP static int hlua_msleep(lua_State *L)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005955{
5956 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005957 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005958
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005959 MAY_LJMP(check_args(L, 1, "msleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005960
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005961 delay = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005962 wakeup_ms = tick_add(now_ms, delay);
5963 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005964
Willy Tarreau9635e032018-10-16 17:52:55 +02005965 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005966 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005967}
5968
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01005969/* This functionis an LUA binding. it permits to give back
5970 * the hand at the HAProxy scheduler. It is used when the
5971 * LUA processing consumes a lot of time.
5972 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005973__LJMP static int hlua_yield_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005974{
5975 return 0;
5976}
5977
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01005978__LJMP static int hlua_yield(lua_State *L)
5979{
Willy Tarreau9635e032018-10-16 17:52:55 +02005980 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_yield_yield, TICK_ETERNITY, HLUA_CTRLYIELD));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005981 return 0;
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01005982}
5983
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005984/* This function change the nice of the currently executed
5985 * task. It is used set low or high priority at the current
5986 * task.
5987 */
Willy Tarreau59551662015-03-10 14:23:13 +01005988__LJMP static int hlua_set_nice(lua_State *L)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005989{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005990 struct hlua *hlua;
5991 int nice;
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005992
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005993 MAY_LJMP(check_args(L, 1, "set_nice"));
5994 hlua = hlua_gethlua(L);
5995 nice = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005996
5997 /* If he task is not set, I'm in a start mode. */
5998 if (!hlua || !hlua->task)
5999 return 0;
6000
6001 if (nice < -1024)
6002 nice = -1024;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006003 else if (nice > 1024)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006004 nice = 1024;
6005
6006 hlua->task->nice = nice;
6007 return 0;
6008}
6009
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006010/* This function is used as a callback of a task. It is called by the
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006011 * HAProxy task subsystem when the task is awaked. The LUA runtime can
6012 * return an E_AGAIN signal, the emmiter of this signal must set a
6013 * signal to wake the task.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006014 *
6015 * Task wrapper are longjmp safe because the only one Lua code
6016 * executed is the safe hlua_ctx_resume();
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006017 */
Willy Tarreau60409db2019-08-21 14:14:50 +02006018struct task *hlua_process_task(struct task *task, void *context, unsigned short state)
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006019{
Olivier Houchard9f6af332018-05-25 14:04:04 +02006020 struct hlua *hlua = context;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006021 enum hlua_exec status;
6022
Christopher Faulet5bc99722018-04-25 10:34:45 +02006023 if (task->thread_mask == MAX_THREADS_MASK)
6024 task_set_affinity(task, tid_bit);
6025
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006026 /* If it is the first call to the task, we must initialize the
6027 * execution timeouts.
6028 */
6029 if (!HLUA_IS_RUNNING(hlua))
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02006030 hlua->max_time = hlua_timeout_task;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006031
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006032 /* Execute the Lua code. */
6033 status = hlua_ctx_resume(hlua, 1);
6034
6035 switch (status) {
6036 /* finished or yield */
6037 case HLUA_E_OK:
6038 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006039 task_destroy(task);
Tim Duesterhuscd235c62018-04-24 13:56:01 +02006040 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006041 break;
6042
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006043 case HLUA_E_AGAIN: /* co process or timeout wake me later. */
Thierry FOURNIERcb146882017-12-10 17:10:57 +01006044 notification_gc(&hlua->com);
PiBa-NLfe971b32018-05-02 22:27:14 +02006045 task->expire = hlua->wake_time;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006046 break;
6047
6048 /* finished with error. */
6049 case HLUA_E_ERRMSG:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006050 SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006051 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006052 task_destroy(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02006053 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006054 break;
6055
6056 case HLUA_E_ERR:
6057 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006058 SEND_ERR(NULL, "Lua task: unknown error.\n");
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006059 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006060 task_destroy(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02006061 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006062 break;
6063 }
Emeric Brun253e53e2017-10-17 18:58:40 +02006064 return task;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006065}
6066
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006067/* This function is an LUA binding that register LUA function to be
6068 * executed after the HAProxy configuration parsing and before the
6069 * HAProxy scheduler starts. This function expect only one LUA
6070 * argument that is a function. This function returns nothing, but
6071 * throws if an error is encountered.
6072 */
6073__LJMP static int hlua_register_init(lua_State *L)
6074{
6075 struct hlua_init_function *init;
6076 int ref;
6077
6078 MAY_LJMP(check_args(L, 1, "register_init"));
6079
6080 ref = MAY_LJMP(hlua_checkfunction(L, 1));
6081
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006082 init = calloc(1, sizeof(*init));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006083 if (!init)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006084 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006085
6086 init->function_ref = ref;
6087 LIST_ADDQ(&hlua_init_functions, &init->l);
6088 return 0;
6089}
6090
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006091/* This functio is an LUA binding. It permits to register a task
6092 * executed in parallel of the main HAroxy activity. The task is
6093 * created and it is set in the HAProxy scheduler. It can be called
6094 * from the "init" section, "post init" or during the runtime.
6095 *
6096 * Lua prototype:
6097 *
6098 * <none> core.register_task(<function>)
6099 */
6100static int hlua_register_task(lua_State *L)
6101{
6102 struct hlua *hlua;
6103 struct task *task;
6104 int ref;
6105
6106 MAY_LJMP(check_args(L, 1, "register_task"));
6107
6108 ref = MAY_LJMP(hlua_checkfunction(L, 1));
6109
Willy Tarreaubafbe012017-11-24 17:34:44 +01006110 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006111 if (!hlua)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006112 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006113
Emeric Brunc60def82017-09-27 14:59:38 +02006114 task = task_new(MAX_THREADS_MASK);
Willy Tarreaue09101e2018-10-16 17:37:12 +02006115 if (!task)
6116 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
6117
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006118 task->context = hlua;
6119 task->process = hlua_process_task;
6120
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006121 if (!hlua_ctx_init(hlua, task, 1))
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006122 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006123
6124 /* Restore the function in the stack. */
6125 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref);
6126 hlua->nargs = 0;
6127
6128 /* Schedule task. */
6129 task_schedule(task, now_ms);
6130
6131 return 0;
6132}
6133
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006134/* Wrapper called by HAProxy to execute an LUA converter. This wrapper
6135 * doesn't allow "yield" functions because the HAProxy engine cannot
6136 * resume converters.
6137 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006138static int hlua_sample_conv_wrapper(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006139{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006140 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006141 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006142 const char *error;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006143
Willy Tarreaube508f12016-03-10 11:47:01 +01006144 if (!stream)
6145 return 0;
6146
Willy Tarreau87b09662015-04-03 00:22:06 +02006147 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006148 * Lua context can be not initialized. This behavior
6149 * permits to save performances because a systematic
6150 * Lua initialization cause 5% performances loss.
6151 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006152 if (!stream->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006153 stream->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006154 if (!stream->hlua) {
6155 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
6156 return 0;
6157 }
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006158 if (!hlua_ctx_init(stream->hlua, stream->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006159 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
6160 return 0;
6161 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006162 }
6163
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006164 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006165 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006166
6167 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006168 if (!SET_SAFE_LJMP(stream->hlua->T)) {
6169 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
6170 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006171 else
6172 error = "critical error";
6173 SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006174 return 0;
6175 }
6176
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006177 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006178 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006179 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006180 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006181 return 0;
6182 }
6183
6184 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006185 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006186
6187 /* convert input sample and pust-it in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006188 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006189 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006190 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006191 return 0;
6192 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006193 hlua_smp2lua(stream->hlua->T, smp);
6194 stream->hlua->nargs = 1;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006195
6196 /* push keywords in the stack. */
6197 if (arg_p) {
6198 for (; arg_p->type != ARGT_STOP; arg_p++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006199 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006200 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006201 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006202 return 0;
6203 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006204 hlua_arg2lua(stream->hlua->T, arg_p);
6205 stream->hlua->nargs++;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006206 }
6207 }
6208
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006209 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006210 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006211
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006212 /* At this point the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006213 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006214 }
6215
6216 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006217 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006218 /* finished. */
6219 case HLUA_E_OK:
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02006220 /* If the stack is empty, the function fails. */
6221 if (lua_gettop(stream->hlua->T) <= 0)
6222 return 0;
6223
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006224 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006225 hlua_lua2smp(stream->hlua->T, -1, smp);
6226 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006227 return 1;
6228
6229 /* yield. */
6230 case HLUA_E_AGAIN:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006231 SEND_ERR(stream->be, "Lua converter '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006232 return 0;
6233
6234 /* finished with error. */
6235 case HLUA_E_ERRMSG:
6236 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006237 SEND_ERR(stream->be, "Lua converter '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006238 fcn->name, lua_tostring(stream->hlua->T, -1));
6239 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006240 return 0;
6241
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006242 case HLUA_E_ETMOUT:
6243 SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name);
6244 return 0;
6245
6246 case HLUA_E_NOMEM:
6247 SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name);
6248 return 0;
6249
6250 case HLUA_E_YIELD:
6251 SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name);
6252 return 0;
6253
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006254 case HLUA_E_ERR:
6255 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006256 SEND_ERR(stream->be, "Lua converter '%s' returns an unknown error.\n", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006257
6258 default:
6259 return 0;
6260 }
6261}
6262
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006263/* Wrapper called by HAProxy to execute a sample-fetch. this wrapper
6264 * doesn't allow "yield" functions because the HAProxy engine cannot
Willy Tarreaube508f12016-03-10 11:47:01 +01006265 * resume sample-fetches. This function will be called by the sample
6266 * fetch engine to call lua-based fetch operations.
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006267 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +02006268static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp,
6269 const char *kw, void *private)
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006270{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006271 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006272 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006273 const char *error;
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006274 unsigned int hflags = HLUA_TXN_NOTERM;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006275
Willy Tarreaube508f12016-03-10 11:47:01 +01006276 if (!stream)
6277 return 0;
Christopher Fauletafd8f102018-11-08 11:34:21 +01006278
Willy Tarreau87b09662015-04-03 00:22:06 +02006279 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006280 * Lua context can be not initialized. This behavior
6281 * permits to save performances because a systematic
6282 * Lua initialization cause 5% performances loss.
6283 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006284 if (!stream->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006285 stream->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006286 if (!stream->hlua) {
6287 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
6288 return 0;
6289 }
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006290 if (!hlua_ctx_init(stream->hlua, stream->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006291 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
6292 return 0;
6293 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006294 }
6295
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006296 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006297 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006298
6299 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006300 if (!SET_SAFE_LJMP(stream->hlua->T)) {
6301 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
6302 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006303 else
6304 error = "critical error";
6305 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006306 return 0;
6307 }
6308
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006309 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006310 if (!lua_checkstack(stream->hlua->T, 2)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006311 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006312 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006313 return 0;
6314 }
6315
6316 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006317 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006318
6319 /* push arguments in the stack. */
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006320 if (!hlua_txn_new(stream->hlua->T, stream, smp->px, smp->opt & SMP_OPT_DIR, hflags)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006321 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006322 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006323 return 0;
6324 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006325 stream->hlua->nargs = 1;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006326
6327 /* push keywords in the stack. */
6328 for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) {
6329 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006330 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006331 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006332 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006333 return 0;
6334 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006335 hlua_arg2lua(stream->hlua->T, arg_p);
6336 stream->hlua->nargs++;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006337 }
6338
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006339 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006340 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006341
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006342 /* At this point the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006343 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006344 }
6345
6346 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006347 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006348 /* finished. */
6349 case HLUA_E_OK:
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02006350 /* If the stack is empty, the function fails. */
6351 if (lua_gettop(stream->hlua->T) <= 0)
6352 return 0;
6353
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006354 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006355 hlua_lua2smp(stream->hlua->T, -1, smp);
6356 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006357
6358 /* Set the end of execution flag. */
6359 smp->flags &= ~SMP_F_MAY_CHANGE;
6360 return 1;
6361
6362 /* yield. */
6363 case HLUA_E_AGAIN:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006364 SEND_ERR(smp->px, "Lua sample-fetch '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006365 return 0;
6366
6367 /* finished with error. */
6368 case HLUA_E_ERRMSG:
6369 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006370 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006371 fcn->name, lua_tostring(stream->hlua->T, -1));
6372 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006373 return 0;
6374
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006375 case HLUA_E_ETMOUT:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006376 SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name);
6377 return 0;
6378
6379 case HLUA_E_NOMEM:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006380 SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name);
6381 return 0;
6382
6383 case HLUA_E_YIELD:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006384 SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name);
6385 return 0;
6386
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006387 case HLUA_E_ERR:
6388 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006389 SEND_ERR(smp->px, "Lua sample-fetch '%s' returns an unknown error.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006390
6391 default:
6392 return 0;
6393 }
6394}
6395
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006396/* This function is an LUA binding used for registering
6397 * "sample-conv" functions. It expects a converter name used
6398 * in the haproxy configuration file, and an LUA function.
6399 */
6400__LJMP static int hlua_register_converters(lua_State *L)
6401{
6402 struct sample_conv_kw_list *sck;
6403 const char *name;
6404 int ref;
6405 int len;
6406 struct hlua_function *fcn;
6407
6408 MAY_LJMP(check_args(L, 2, "register_converters"));
6409
6410 /* First argument : converter name. */
6411 name = MAY_LJMP(luaL_checkstring(L, 1));
6412
6413 /* Second argument : lua function. */
6414 ref = MAY_LJMP(hlua_checkfunction(L, 2));
6415
6416 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006417 sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006418 if (!sck)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006419 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006420 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006421 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006422 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006423
6424 /* Fill fcn. */
6425 fcn->name = strdup(name);
6426 if (!fcn->name)
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 fcn->function_ref = ref;
6429
6430 /* List head */
6431 sck->list.n = sck->list.p = NULL;
6432
6433 /* converter keyword. */
6434 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006435 sck->kw[0].kw = calloc(1, len);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006436 if (!sck->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006437 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006438
6439 snprintf((char *)sck->kw[0].kw, len, "lua.%s", name);
6440 sck->kw[0].process = hlua_sample_conv_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01006441 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 +01006442 sck->kw[0].val_args = NULL;
6443 sck->kw[0].in_type = SMP_T_STR;
6444 sck->kw[0].out_type = SMP_T_STR;
6445 sck->kw[0].private = fcn;
6446
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006447 /* Register this new converter */
6448 sample_register_convs(sck);
6449
6450 return 0;
6451}
6452
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006453/* This function is an LUA binding used for registering
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006454 * "sample-fetch" functions. It expects a converter name used
6455 * in the haproxy configuration file, and an LUA function.
6456 */
6457__LJMP static int hlua_register_fetches(lua_State *L)
6458{
6459 const char *name;
6460 int ref;
6461 int len;
6462 struct sample_fetch_kw_list *sfk;
6463 struct hlua_function *fcn;
6464
6465 MAY_LJMP(check_args(L, 2, "register_fetches"));
6466
6467 /* First argument : sample-fetch name. */
6468 name = MAY_LJMP(luaL_checkstring(L, 1));
6469
6470 /* Second argument : lua function. */
6471 ref = MAY_LJMP(hlua_checkfunction(L, 2));
6472
6473 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006474 sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006475 if (!sfk)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006476 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006477 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006478 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006479 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006480
6481 /* Fill fcn. */
6482 fcn->name = strdup(name);
6483 if (!fcn->name)
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 fcn->function_ref = ref;
6486
6487 /* List head */
6488 sfk->list.n = sfk->list.p = NULL;
6489
6490 /* sample-fetch keyword. */
6491 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006492 sfk->kw[0].kw = calloc(1, len);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006493 if (!sfk->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006494 return luaL_error(L, "Lua out of memory error.");
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006495
6496 snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name);
6497 sfk->kw[0].process = hlua_sample_fetch_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01006498 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 +01006499 sfk->kw[0].val_args = NULL;
6500 sfk->kw[0].out_type = SMP_T_STR;
6501 sfk->kw[0].use = SMP_USE_HTTP_ANY;
6502 sfk->kw[0].val = 0;
6503 sfk->kw[0].private = fcn;
6504
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006505 /* Register this new fetch. */
6506 sample_register_fetches(sfk);
6507
6508 return 0;
6509}
6510
Christopher Faulet501465d2020-02-26 14:54:16 +01006511/* This function is a lua binding to set the wake_time.
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006512 */
Christopher Faulet501465d2020-02-26 14:54:16 +01006513__LJMP static int hlua_set_wake_time(lua_State *L)
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006514{
6515 struct hlua *hlua = hlua_gethlua(L);
6516 unsigned int delay;
6517 unsigned int wakeup_ms;
6518
6519 MAY_LJMP(check_args(L, 1, "wake_time"));
6520
6521 delay = MAY_LJMP(luaL_checkinteger(L, 1));
6522 wakeup_ms = tick_add(now_ms, delay);
6523 hlua->wake_time = wakeup_ms;
6524 return 0;
6525}
6526
6527
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006528/* This function is a wrapper to execute each LUA function declared as an action
6529 * wrapper during the initialisation period. This function may return any
6530 * ACT_RET_* value. On error ACT_RET_CONT is returned and the action is
6531 * ignored. If the lua action yields, ACT_RET_YIELD is returned. On success, the
6532 * return value is the first element on the stack.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006533 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006534static enum act_return hlua_action(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +02006535 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006536{
6537 char **arg;
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006538 unsigned int hflags = 0;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006539 int dir, act_ret = ACT_RET_CONT;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006540 const char *error;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006541
6542 switch (rule->from) {
Christopher Fauletd8f0e072020-02-25 09:45:51 +01006543 case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break;
6544 case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break;
6545 case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break;
6546 case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006547 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006548 SEND_ERR(px, "Lua: internal error while execute action.\n");
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006549 goto end;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006550 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006551
Willy Tarreau87b09662015-04-03 00:22:06 +02006552 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006553 * Lua context can be not initialized. This behavior
6554 * permits to save performances because a systematic
6555 * Lua initialization cause 5% performances loss.
6556 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006557 if (!s->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006558 s->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006559 if (!s->hlua) {
6560 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
6561 rule->arg.hlua_rule->fcn.name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006562 goto end;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006563 }
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006564 if (!hlua_ctx_init(s->hlua, s->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006565 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 FOURNIER05ac4242015-02-27 18:37:27 +01006569 }
6570
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006571 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006572 if (!HLUA_IS_RUNNING(s->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006573
6574 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006575 if (!SET_SAFE_LJMP(s->hlua->T)) {
6576 if (lua_type(s->hlua->T, -1) == LUA_TSTRING)
6577 error = lua_tostring(s->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006578 else
6579 error = "critical error";
6580 SEND_ERR(px, "Lua function '%s': %s.\n",
6581 rule->arg.hlua_rule->fcn.name, error);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006582 goto end;
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006583 }
6584
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006585 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006586 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006587 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006588 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006589 RESET_SAFE_LJMP(s->hlua->T);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006590 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006591 }
6592
6593 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006594 lua_rawgeti(s->hlua->T, LUA_REGISTRYINDEX, rule->arg.hlua_rule->fcn.function_ref);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006595
Willy Tarreau87b09662015-04-03 00:22:06 +02006596 /* Create and and push object stream in the stack. */
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006597 if (!hlua_txn_new(s->hlua->T, s, px, dir, hflags)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006598 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006599 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006600 RESET_SAFE_LJMP(s->hlua->T);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006601 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006602 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006603 s->hlua->nargs = 1;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006604
6605 /* push keywords in the stack. */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006606 for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006607 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006608 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006609 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006610 RESET_SAFE_LJMP(s->hlua->T);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006611 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006612 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006613 lua_pushstring(s->hlua->T, *arg);
6614 s->hlua->nargs++;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006615 }
6616
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006617 /* Now the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006618 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006619
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006620 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006621 s->hlua->max_time = hlua_timeout_session;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006622 }
6623
Christopher Faulet23308eb2020-06-02 18:46:07 +02006624 /* Always reset the analyse expiration timeout for the corresponding
6625 * channel in case the lua script yield, to be sure to not keep an
6626 * expired timeout.
6627 */
6628 if (dir == SMP_OPT_DIR_REQ)
6629 s->req.analyse_exp = TICK_ETERNITY;
6630 else
6631 s->res.analyse_exp = TICK_ETERNITY;
6632
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006633 /* Execute the function. */
Christopher Faulet105ba6c2019-12-18 14:41:51 +01006634 switch (hlua_ctx_resume(s->hlua, !(flags & ACT_OPT_FINAL))) {
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006635 /* finished. */
6636 case HLUA_E_OK:
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006637 /* Catch the return value */
6638 if (lua_gettop(s->hlua->T) > 0)
6639 act_ret = lua_tointeger(s->hlua->T, -1);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006640
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006641 /* Set timeout in the required channel. */
6642 if (act_ret == ACT_RET_YIELD && s->hlua->wake_time != TICK_ETERNITY) {
6643 if (dir == SMP_OPT_DIR_REQ)
6644 s->req.analyse_exp = s->hlua->wake_time;
6645 else
6646 s->res.analyse_exp = s->hlua->wake_time;
6647 }
6648 goto end;
6649
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006650 /* yield. */
6651 case HLUA_E_AGAIN:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006652 /* Set timeout in the required channel. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006653 if (s->hlua->wake_time != TICK_ETERNITY) {
Christopher Faulet81921b12019-08-14 23:19:45 +02006654 if (dir == SMP_OPT_DIR_REQ)
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006655 s->req.analyse_exp = s->hlua->wake_time;
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006656 else
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006657 s->res.analyse_exp = s->hlua->wake_time;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006658 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006659 /* Some actions can be wake up when a "write" event
6660 * is detected on a response channel. This is useful
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006661 * only for actions targeted on the requests.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006662 */
Christopher Faulet51fa3582019-07-26 14:54:52 +02006663 if (HLUA_IS_WAKERESWR(s->hlua))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006664 s->res.flags |= CF_WAKE_WRITE;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006665 if (HLUA_IS_WAKEREQWR(s->hlua))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006666 s->req.flags |= CF_WAKE_WRITE;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006667 act_ret = ACT_RET_YIELD;
6668 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006669
6670 /* finished with error. */
6671 case HLUA_E_ERRMSG:
6672 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006673 SEND_ERR(px, "Lua function '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006674 rule->arg.hlua_rule->fcn.name, lua_tostring(s->hlua->T, -1));
6675 lua_pop(s->hlua->T, 1);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006676 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006677
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006678 case HLUA_E_ETMOUT:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006679 SEND_ERR(px, "Lua function '%s': execution timeout.\n", rule->arg.hlua_rule->fcn.name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006680 goto end;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006681
6682 case HLUA_E_NOMEM:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006683 SEND_ERR(px, "Lua function '%s': out of memory error.\n", rule->arg.hlua_rule->fcn.name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006684 goto end;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006685
6686 case HLUA_E_YIELD:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006687 SEND_ERR(px, "Lua function '%s': aborting Lua processing on expired timeout.\n",
6688 rule->arg.hlua_rule->fcn.name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006689 goto end;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006690
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006691 case HLUA_E_ERR:
6692 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006693 SEND_ERR(px, "Lua function '%s' return an unknown error.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006694 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006695
6696 default:
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006697 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006698 }
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006699
6700 end:
6701 return act_ret;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006702}
6703
Olivier Houchard9f6af332018-05-25 14:04:04 +02006704struct task *hlua_applet_wakeup(struct task *t, void *context, unsigned short state)
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006705{
Olivier Houchard9f6af332018-05-25 14:04:04 +02006706 struct appctx *ctx = context;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006707
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006708 appctx_wakeup(ctx);
Willy Tarreaud9587412017-08-23 16:07:33 +02006709 t->expire = TICK_ETERNITY;
Willy Tarreaud1aa41f2017-07-21 16:41:56 +02006710 return t;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006711}
6712
6713static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
6714{
6715 struct stream_interface *si = ctx->owner;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006716 struct hlua *hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006717 struct task *task;
6718 char **arg;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006719 const char *error;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006720
Willy Tarreaubafbe012017-11-24 17:34:44 +01006721 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006722 if (!hlua) {
6723 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
6724 ctx->rule->arg.hlua_rule->fcn.name);
6725 return 0;
6726 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006727 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006728 ctx->ctx.hlua_apptcp.hlua = hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006729 ctx->ctx.hlua_apptcp.flags = 0;
6730
6731 /* Create task used by signal to wakeup applets. */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01006732 task = task_new(tid_bit);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006733 if (!task) {
6734 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
6735 ctx->rule->arg.hlua_rule->fcn.name);
6736 return 0;
6737 }
6738 task->nice = 0;
6739 task->context = ctx;
6740 task->process = hlua_applet_wakeup;
6741 ctx->ctx.hlua_apptcp.task = task;
6742
6743 /* In the execution wrappers linked with a stream, the
6744 * Lua context can be not initialized. This behavior
6745 * permits to save performances because a systematic
6746 * Lua initialization cause 5% performances loss.
6747 */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006748 if (!hlua_ctx_init(hlua, task, 0)) {
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006749 SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n",
6750 ctx->rule->arg.hlua_rule->fcn.name);
6751 return 0;
6752 }
6753
6754 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02006755 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006756
6757 /* The following Lua calls can fail. */
6758 if (!SET_SAFE_LJMP(hlua->T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01006759 if (lua_type(hlua->T, -1) == LUA_TSTRING)
6760 error = lua_tostring(hlua->T, -1);
6761 else
6762 error = "critical error";
6763 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
6764 ctx->rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006765 return 0;
6766 }
6767
6768 /* Check stack available size. */
6769 if (!lua_checkstack(hlua->T, 1)) {
6770 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
6771 ctx->rule->arg.hlua_rule->fcn.name);
6772 RESET_SAFE_LJMP(hlua->T);
6773 return 0;
6774 }
6775
6776 /* Restore the function in the stack. */
6777 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref);
6778
6779 /* Create and and push object stream in the stack. */
6780 if (!hlua_applet_tcp_new(hlua->T, ctx)) {
6781 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
6782 ctx->rule->arg.hlua_rule->fcn.name);
6783 RESET_SAFE_LJMP(hlua->T);
6784 return 0;
6785 }
6786 hlua->nargs = 1;
6787
6788 /* push keywords in the stack. */
6789 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
6790 if (!lua_checkstack(hlua->T, 1)) {
6791 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
6792 ctx->rule->arg.hlua_rule->fcn.name);
6793 RESET_SAFE_LJMP(hlua->T);
6794 return 0;
6795 }
6796 lua_pushstring(hlua->T, *arg);
6797 hlua->nargs++;
6798 }
6799
6800 RESET_SAFE_LJMP(hlua->T);
6801
6802 /* Wakeup the applet ASAP. */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01006803 si_cant_get(si);
Willy Tarreau3367d412018-11-15 10:57:41 +01006804 si_rx_endp_more(si);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006805
6806 return 1;
6807}
6808
Willy Tarreau60409db2019-08-21 14:14:50 +02006809void hlua_applet_tcp_fct(struct appctx *ctx)
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006810{
6811 struct stream_interface *si = ctx->owner;
6812 struct stream *strm = si_strm(si);
6813 struct channel *res = si_ic(si);
6814 struct act_rule *rule = ctx->rule;
6815 struct proxy *px = strm->be;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006816 struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006817
6818 /* The applet execution is already done. */
Olivier Houchard594c8c52018-08-28 14:41:31 +02006819 if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) {
6820 /* eat the whole request */
6821 co_skip(si_oc(si), co_data(si_oc(si)));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006822 return;
Olivier Houchard594c8c52018-08-28 14:41:31 +02006823 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006824
6825 /* If the stream is disconnect or closed, ldo nothing. */
6826 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
6827 return;
6828
6829 /* Execute the function. */
6830 switch (hlua_ctx_resume(hlua, 1)) {
6831 /* finished. */
6832 case HLUA_E_OK:
6833 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
6834
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006835 /* eat the whole request */
Willy Tarreaua79021a2018-06-15 18:07:57 +02006836 co_skip(si_oc(si), co_data(si_oc(si)));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006837 res->flags |= CF_READ_NULL;
6838 si_shutr(si);
6839 return;
6840
6841 /* yield. */
6842 case HLUA_E_AGAIN:
Thierry Fournier0164f202016-02-20 17:47:43 +01006843 if (hlua->wake_time != TICK_ETERNITY)
6844 task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006845 return;
6846
6847 /* finished with error. */
6848 case HLUA_E_ERRMSG:
6849 /* Display log. */
6850 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
6851 rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1));
6852 lua_pop(hlua->T, 1);
6853 goto error;
6854
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006855 case HLUA_E_ETMOUT:
6856 SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n",
6857 rule->arg.hlua_rule->fcn.name);
6858 goto error;
6859
6860 case HLUA_E_NOMEM:
6861 SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n",
6862 rule->arg.hlua_rule->fcn.name);
6863 goto error;
6864
6865 case HLUA_E_YIELD: /* unexpected */
6866 SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n",
6867 rule->arg.hlua_rule->fcn.name);
6868 goto error;
6869
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006870 case HLUA_E_ERR:
6871 /* Display log. */
6872 SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n",
6873 rule->arg.hlua_rule->fcn.name);
6874 goto error;
6875
6876 default:
6877 goto error;
6878 }
6879
6880error:
6881
6882 /* For all other cases, just close the stream. */
6883 si_shutw(si);
6884 si_shutr(si);
6885 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
6886}
6887
6888static void hlua_applet_tcp_release(struct appctx *ctx)
6889{
Olivier Houchard3f795f72019-04-17 22:51:06 +02006890 task_destroy(ctx->ctx.hlua_apptcp.task);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006891 ctx->ctx.hlua_apptcp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006892 hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006893 ctx->ctx.hlua_apptcp.hlua = NULL;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006894}
6895
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006896/* The function returns 1 if the initialisation is complete, 0 if
6897 * an errors occurs and -1 if more data are required for initializing
6898 * the applet.
6899 */
6900static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
6901{
6902 struct stream_interface *si = ctx->owner;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006903 struct http_txn *txn;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006904 struct hlua *hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006905 char **arg;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006906 struct task *task;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006907 const char *error;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006908
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006909 txn = strm->txn;
Willy Tarreaubafbe012017-11-24 17:34:44 +01006910 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006911 if (!hlua) {
6912 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
6913 ctx->rule->arg.hlua_rule->fcn.name);
6914 return 0;
6915 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006916 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006917 ctx->ctx.hlua_apphttp.hlua = hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006918 ctx->ctx.hlua_apphttp.left_bytes = -1;
6919 ctx->ctx.hlua_apphttp.flags = 0;
6920
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +01006921 if (txn->req.flags & HTTP_MSGF_VER_11)
6922 ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11;
6923
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006924 /* Create task used by signal to wakeup applets. */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01006925 task = task_new(tid_bit);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006926 if (!task) {
6927 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
6928 ctx->rule->arg.hlua_rule->fcn.name);
6929 return 0;
6930 }
6931 task->nice = 0;
6932 task->context = ctx;
6933 task->process = hlua_applet_wakeup;
6934 ctx->ctx.hlua_apphttp.task = task;
6935
6936 /* In the execution wrappers linked with a stream, the
6937 * Lua context can be not initialized. This behavior
6938 * permits to save performances because a systematic
6939 * Lua initialization cause 5% performances loss.
6940 */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006941 if (!hlua_ctx_init(hlua, task, 0)) {
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006942 SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n",
6943 ctx->rule->arg.hlua_rule->fcn.name);
6944 return 0;
6945 }
6946
6947 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02006948 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006949
6950 /* The following Lua calls can fail. */
6951 if (!SET_SAFE_LJMP(hlua->T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01006952 if (lua_type(hlua->T, -1) == LUA_TSTRING)
6953 error = lua_tostring(hlua->T, -1);
6954 else
6955 error = "critical error";
6956 SEND_ERR(px, "Lua applet http '%s': %s.\n",
6957 ctx->rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006958 return 0;
6959 }
6960
6961 /* Check stack available size. */
6962 if (!lua_checkstack(hlua->T, 1)) {
6963 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
6964 ctx->rule->arg.hlua_rule->fcn.name);
6965 RESET_SAFE_LJMP(hlua->T);
6966 return 0;
6967 }
6968
6969 /* Restore the function in the stack. */
6970 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref);
6971
6972 /* Create and and push object stream in the stack. */
6973 if (!hlua_applet_http_new(hlua->T, ctx)) {
6974 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
6975 ctx->rule->arg.hlua_rule->fcn.name);
6976 RESET_SAFE_LJMP(hlua->T);
6977 return 0;
6978 }
6979 hlua->nargs = 1;
6980
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006981 /* push keywords in the stack. */
6982 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
6983 if (!lua_checkstack(hlua->T, 1)) {
6984 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
6985 ctx->rule->arg.hlua_rule->fcn.name);
6986 RESET_SAFE_LJMP(hlua->T);
6987 return 0;
6988 }
6989 lua_pushstring(hlua->T, *arg);
6990 hlua->nargs++;
6991 }
6992
6993 RESET_SAFE_LJMP(hlua->T);
6994
6995 /* Wakeup the applet when data is ready for read. */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01006996 si_cant_get(si);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006997
6998 return 1;
6999}
7000
Willy Tarreau60409db2019-08-21 14:14:50 +02007001void hlua_applet_http_fct(struct appctx *ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007002{
7003 struct stream_interface *si = ctx->owner;
7004 struct stream *strm = si_strm(si);
7005 struct channel *req = si_oc(si);
7006 struct channel *res = si_ic(si);
7007 struct act_rule *rule = ctx->rule;
7008 struct proxy *px = strm->be;
7009 struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua;
7010 struct htx *req_htx, *res_htx;
7011
7012 res_htx = htx_from_buf(&res->buf);
7013
7014 /* If the stream is disconnect or closed, ldo nothing. */
7015 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
7016 goto out;
7017
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007018 /* Check if the input buffer is available. */
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007019 if (!b_size(&res->buf)) {
7020 si_rx_room_blk(si);
7021 goto out;
7022 }
7023 /* check that the output is not closed */
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007024 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007025 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7026
7027 /* Set the currently running flag. */
7028 if (!HLUA_IS_RUNNING(hlua) &&
7029 !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
7030 struct htx_blk *blk;
7031 size_t count = co_data(req);
7032
7033 if (!count) {
7034 si_cant_get(si);
7035 goto out;
7036 }
7037
7038 /* We need to flush the request header. This left the body for
7039 * the Lua.
7040 */
7041 req_htx = htx_from_buf(&req->buf);
Christopher Fauleta3f15502019-05-13 15:27:23 +02007042 blk = htx_get_first_blk(req_htx);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007043 while (count && blk) {
7044 enum htx_blk_type type = htx_get_blk_type(blk);
7045 uint32_t sz = htx_get_blksz(blk);
7046
7047 if (sz > count) {
7048 si_cant_get(si);
Christopher Faulet0ae79d02019-02-27 21:36:59 +01007049 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007050 goto out;
7051 }
7052
7053 count -= sz;
7054 co_set_data(req, co_data(req) - sz);
7055 blk = htx_remove_blk(req_htx, blk);
7056
7057 if (type == HTX_BLK_EOH)
7058 break;
7059 }
Christopher Faulet0ae79d02019-02-27 21:36:59 +01007060 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007061 }
7062
7063 /* Executes The applet if it is not done. */
7064 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
7065
7066 /* Execute the function. */
7067 switch (hlua_ctx_resume(hlua, 1)) {
7068 /* finished. */
7069 case HLUA_E_OK:
7070 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7071 break;
7072
7073 /* yield. */
7074 case HLUA_E_AGAIN:
7075 if (hlua->wake_time != TICK_ETERNITY)
7076 task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time);
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007077 goto out;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007078
7079 /* finished with error. */
7080 case HLUA_E_ERRMSG:
7081 /* Display log. */
7082 SEND_ERR(px, "Lua applet http '%s': %s.\n",
7083 rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1));
7084 lua_pop(hlua->T, 1);
7085 goto error;
7086
7087 case HLUA_E_ETMOUT:
7088 SEND_ERR(px, "Lua applet http '%s': execution timeout.\n",
7089 rule->arg.hlua_rule->fcn.name);
7090 goto error;
7091
7092 case HLUA_E_NOMEM:
7093 SEND_ERR(px, "Lua applet http '%s': out of memory error.\n",
7094 rule->arg.hlua_rule->fcn.name);
7095 goto error;
7096
7097 case HLUA_E_YIELD: /* unexpected */
7098 SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n",
7099 rule->arg.hlua_rule->fcn.name);
7100 goto error;
7101
7102 case HLUA_E_ERR:
7103 /* Display log. */
7104 SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n",
7105 rule->arg.hlua_rule->fcn.name);
7106 goto error;
7107
7108 default:
7109 goto error;
7110 }
7111 }
7112
7113 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007114 if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT)
7115 goto done;
7116
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007117 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT))
7118 goto error;
7119
Christopher Faulet54b5e212019-06-04 10:08:28 +02007120 /* Don't add TLR because mux-h1 will take care of it */
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007121 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
7122 si_rx_room_blk(si);
7123 goto out;
7124 }
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01007125 channel_add_input(res, 1);
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007126 strm->txn->status = ctx->ctx.hlua_apphttp.status;
7127 ctx->ctx.hlua_apphttp.flags |= APPLET_RSP_SENT;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007128 }
7129
7130 done:
7131 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007132 if (!(res->flags & CF_SHUTR)) {
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007133 res->flags |= CF_READ_NULL;
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007134 si_shutr(si);
7135 }
7136
7137 /* eat the whole request */
7138 if (co_data(req)) {
7139 req_htx = htx_from_buf(&req->buf);
7140 co_htx_skip(req, req_htx, co_data(req));
7141 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007142 }
7143 }
7144
7145 out:
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007146 htx_to_buf(res_htx, &res->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007147 return;
7148
7149 error:
7150
7151 /* If we are in HTTP mode, and we are not send any
7152 * data, return a 500 server error in best effort:
7153 * if there is no room available in the buffer,
7154 * just close the connection.
7155 */
7156 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
Christopher Fauletf7346382019-07-17 22:02:08 +02007157 struct buffer *err = &http_err_chunks[HTTP_ERR_500];
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007158
7159 channel_erase(res);
7160 res->buf.data = b_data(err);
7161 memcpy(res->buf.area, b_head(err), b_data(err));
7162 res_htx = htx_from_buf(&res->buf);
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01007163 channel_add_input(res, res_htx->data);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007164 }
7165 if (!(strm->flags & SF_ERR_MASK))
7166 strm->flags |= SF_ERR_RESOURCE;
7167 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7168 goto done;
7169}
7170
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007171static void hlua_applet_http_release(struct appctx *ctx)
7172{
Olivier Houchard3f795f72019-04-17 22:51:06 +02007173 task_destroy(ctx->ctx.hlua_apphttp.task);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007174 ctx->ctx.hlua_apphttp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007175 hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007176 ctx->ctx.hlua_apphttp.hlua = NULL;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007177}
7178
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007179/* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007180 * success case, else return ACT_RET_PRS_ERR.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007181 *
7182 * This function can fail with an abort() due to an Lua critical error.
7183 * We are in the configuration parsing process of HAProxy, this abort() is
7184 * tolerated.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007185 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007186static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px,
7187 struct act_rule *rule, char **err)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007188{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007189 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007190 int i;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007191
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007192 /* Memory for the rule. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007193 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007194 if (!rule->arg.hlua_rule) {
7195 memprintf(err, "out of memory error");
Thierry FOURNIERafa80492015-08-19 09:04:15 +02007196 return ACT_RET_PRS_ERR;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007197 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007198
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007199 /* Memory for arguments. */
7200 rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, sizeof(char *));
7201 if (!rule->arg.hlua_rule->args) {
7202 memprintf(err, "out of memory error");
7203 return ACT_RET_PRS_ERR;
7204 }
7205
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007206 /* Reference the Lua function and store the reference. */
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007207 rule->arg.hlua_rule->fcn = *fcn;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007208
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007209 /* Expect some arguments */
7210 for (i = 0; i < fcn->nargs; i++) {
Thierry FOURNIER1725c2e2019-01-06 19:38:49 +01007211 if (*args[*cur_arg] == '\0') {
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007212 memprintf(err, "expect %d arguments", fcn->nargs);
7213 return ACT_RET_PRS_ERR;
7214 }
Thierry FOURNIER1725c2e2019-01-06 19:38:49 +01007215 rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]);
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007216 if (!rule->arg.hlua_rule->args[i]) {
7217 memprintf(err, "out of memory error");
7218 return ACT_RET_PRS_ERR;
7219 }
7220 (*cur_arg)++;
7221 }
7222 rule->arg.hlua_rule->args[i] = NULL;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007223
Thierry FOURNIER42148732015-09-02 17:17:33 +02007224 rule->action = ACT_CUSTOM;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007225 rule->action_ptr = hlua_action;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02007226 return ACT_RET_PRS_OK;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007227}
7228
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007229static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px,
7230 struct act_rule *rule, char **err)
7231{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007232 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007233
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01007234 /* HTTP applets are forbidden in tcp-request rules.
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007235 * HTTP applet request requires everything initialized by
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01007236 * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER).
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007237 * The applet will be immediately initialized, but its before
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01007238 * the call of this analyzer.
7239 */
7240 if (rule->from != ACT_F_HTTP_REQ) {
7241 memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets");
7242 return ACT_RET_PRS_ERR;
7243 }
7244
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007245 /* Memory for the rule. */
7246 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
7247 if (!rule->arg.hlua_rule) {
7248 memprintf(err, "out of memory error");
7249 return ACT_RET_PRS_ERR;
7250 }
7251
7252 /* Reference the Lua function and store the reference. */
7253 rule->arg.hlua_rule->fcn = *fcn;
7254
7255 /* TODO: later accept arguments. */
7256 rule->arg.hlua_rule->args = NULL;
7257
7258 /* Add applet pointer in the rule. */
7259 rule->applet.obj_type = OBJ_TYPE_APPLET;
7260 rule->applet.name = fcn->name;
7261 rule->applet.init = hlua_applet_http_init;
7262 rule->applet.fct = hlua_applet_http_fct;
7263 rule->applet.release = hlua_applet_http_release;
7264 rule->applet.timeout = hlua_timeout_applet;
7265
7266 return ACT_RET_PRS_OK;
7267}
7268
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007269/* This function is an LUA binding used for registering
7270 * "sample-conv" functions. It expects a converter name used
7271 * in the haproxy configuration file, and an LUA function.
7272 */
7273__LJMP static int hlua_register_action(lua_State *L)
7274{
7275 struct action_kw_list *akl;
7276 const char *name;
7277 int ref;
7278 int len;
7279 struct hlua_function *fcn;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007280 int nargs;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007281
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007282 /* Initialise the number of expected arguments at 0. */
7283 nargs = 0;
7284
7285 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
7286 WILL_LJMP(luaL_error(L, "'register_action' needs between 3 and 4 arguments"));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007287
7288 /* First argument : converter name. */
7289 name = MAY_LJMP(luaL_checkstring(L, 1));
7290
7291 /* Second argument : environment. */
7292 if (lua_type(L, 2) != LUA_TTABLE)
7293 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
7294
7295 /* Third argument : lua function. */
7296 ref = MAY_LJMP(hlua_checkfunction(L, 3));
7297
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007298 /* Fourth argument : number of mandatory arguments expected on the configuration line. */
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007299 if (lua_gettop(L) >= 4)
7300 nargs = MAY_LJMP(luaL_checkinteger(L, 4));
7301
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007302 /* browse the second argument as an array. */
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007303 lua_pushnil(L);
7304 while (lua_next(L, 2) != 0) {
7305 if (lua_type(L, -1) != LUA_TSTRING)
7306 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
7307
7308 /* Check required environment. Only accepted "http" or "tcp". */
7309 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007310 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007311 if (!akl)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007312 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007313 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007314 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007315 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007316
7317 /* Fill fcn. */
7318 fcn->name = strdup(name);
7319 if (!fcn->name)
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 fcn->function_ref = ref;
7322
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007323 /* Set the expected number od arguments. */
7324 fcn->nargs = nargs;
7325
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007326 /* List head */
7327 akl->list.n = akl->list.p = NULL;
7328
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007329 /* action keyword. */
7330 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007331 akl->kw[0].kw = calloc(1, len);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007332 if (!akl->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007333 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007334
7335 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
7336
7337 akl->kw[0].match_pfx = 0;
7338 akl->kw[0].private = fcn;
7339 akl->kw[0].parse = action_register_lua;
7340
7341 /* select the action registering point. */
7342 if (strcmp(lua_tostring(L, -1), "tcp-req") == 0)
7343 tcp_req_cont_keywords_register(akl);
7344 else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0)
7345 tcp_res_cont_keywords_register(akl);
7346 else if (strcmp(lua_tostring(L, -1), "http-req") == 0)
7347 http_req_keywords_register(akl);
7348 else if (strcmp(lua_tostring(L, -1), "http-res") == 0)
7349 http_res_keywords_register(akl);
7350 else
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007351 WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. "
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007352 "'tcp-req', 'tcp-res', 'http-req' or 'http-res' "
7353 "are expected.", lua_tostring(L, -1)));
7354
7355 /* pop the environment string. */
7356 lua_pop(L, 1);
7357 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007358 return ACT_RET_PRS_OK;
7359}
7360
7361static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px,
7362 struct act_rule *rule, char **err)
7363{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007364 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007365
Christopher Faulet280f85b2019-07-15 15:02:04 +02007366 if (px->mode == PR_MODE_HTTP) {
7367 memprintf(err, "Lua TCP services cannot be used on HTTP proxies");
Christopher Fauletafd8f102018-11-08 11:34:21 +01007368 return ACT_RET_PRS_ERR;
7369 }
7370
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007371 /* Memory for the rule. */
7372 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
7373 if (!rule->arg.hlua_rule) {
7374 memprintf(err, "out of memory error");
7375 return ACT_RET_PRS_ERR;
7376 }
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007377
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007378 /* Reference the Lua function and store the reference. */
7379 rule->arg.hlua_rule->fcn = *fcn;
7380
7381 /* TODO: later accept arguments. */
7382 rule->arg.hlua_rule->args = NULL;
7383
7384 /* Add applet pointer in the rule. */
7385 rule->applet.obj_type = OBJ_TYPE_APPLET;
7386 rule->applet.name = fcn->name;
7387 rule->applet.init = hlua_applet_tcp_init;
7388 rule->applet.fct = hlua_applet_tcp_fct;
7389 rule->applet.release = hlua_applet_tcp_release;
7390 rule->applet.timeout = hlua_timeout_applet;
7391
7392 return 0;
7393}
7394
7395/* This function is an LUA binding used for registering
7396 * "sample-conv" functions. It expects a converter name used
7397 * in the haproxy configuration file, and an LUA function.
7398 */
7399__LJMP static int hlua_register_service(lua_State *L)
7400{
7401 struct action_kw_list *akl;
7402 const char *name;
7403 const char *env;
7404 int ref;
7405 int len;
7406 struct hlua_function *fcn;
7407
7408 MAY_LJMP(check_args(L, 3, "register_service"));
7409
7410 /* First argument : converter name. */
7411 name = MAY_LJMP(luaL_checkstring(L, 1));
7412
7413 /* Second argument : environment. */
7414 env = MAY_LJMP(luaL_checkstring(L, 2));
7415
7416 /* Third argument : lua function. */
7417 ref = MAY_LJMP(hlua_checkfunction(L, 3));
7418
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007419 /* Allocate and fill the sample fetch keyword struct. */
7420 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
7421 if (!akl)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007422 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007423 fcn = calloc(1, sizeof(*fcn));
7424 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007425 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007426
7427 /* Fill fcn. */
7428 len = strlen("<lua.>") + strlen(name) + 1;
7429 fcn->name = calloc(1, len);
7430 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007431 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007432 snprintf((char *)fcn->name, len, "<lua.%s>", name);
7433 fcn->function_ref = ref;
7434
7435 /* List head */
7436 akl->list.n = akl->list.p = NULL;
7437
7438 /* converter keyword. */
7439 len = strlen("lua.") + strlen(name) + 1;
7440 akl->kw[0].kw = calloc(1, len);
7441 if (!akl->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007442 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007443
7444 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
7445
Thierry FOURNIER / OZON.IO02564fd2016-11-12 11:07:05 +01007446 /* Check required environment. Only accepted "http" or "tcp". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007447 if (strcmp(env, "tcp") == 0)
7448 akl->kw[0].parse = action_register_service_tcp;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007449 else if (strcmp(env, "http") == 0)
7450 akl->kw[0].parse = action_register_service_http;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007451 else
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007452 WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. "
Eric Salamafe7456f2017-12-21 14:30:07 +01007453 "'tcp' or 'http' are expected.", env));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007454
7455 akl->kw[0].match_pfx = 0;
7456 akl->kw[0].private = fcn;
7457
7458 /* End of array. */
7459 memset(&akl->kw[1], 0, sizeof(*akl->kw));
7460
7461 /* Register this new converter */
7462 service_keywords_register(akl);
7463
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007464 return 0;
7465}
7466
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007467/* This function initialises Lua cli handler. It copies the
7468 * arguments in the Lua stack and create channel IO objects.
7469 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02007470static int hlua_cli_parse_fct(char **args, char *payload, struct appctx *appctx, void *private)
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007471{
7472 struct hlua *hlua;
7473 struct hlua_function *fcn;
7474 int i;
7475 const char *error;
7476
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007477 fcn = private;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007478 appctx->ctx.hlua_cli.fcn = private;
7479
Willy Tarreaubafbe012017-11-24 17:34:44 +01007480 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007481 if (!hlua) {
7482 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007483 return 1;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007484 }
7485 HLUA_INIT(hlua);
7486 appctx->ctx.hlua_cli.hlua = hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007487
7488 /* Create task used by signal to wakeup applets.
Ilya Shipitsind4259502020-04-08 01:07:56 +05007489 * We use the same wakeup function than the Lua applet_tcp and
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007490 * applet_http. It is absolutely compatible.
7491 */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01007492 appctx->ctx.hlua_cli.task = task_new(tid_bit);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007493 if (!appctx->ctx.hlua_cli.task) {
Thierry FOURNIERffbf5692016-12-16 11:14:06 +01007494 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007495 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007496 }
7497 appctx->ctx.hlua_cli.task->nice = 0;
7498 appctx->ctx.hlua_cli.task->context = appctx;
7499 appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup;
7500
7501 /* Initialises the Lua context */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01007502 if (!hlua_ctx_init(hlua, appctx->ctx.hlua_cli.task, 0)) {
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007503 SEND_ERR(NULL, "Lua cli '%s': can't initialize Lua context.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007504 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007505 }
7506
7507 /* The following Lua calls can fail. */
7508 if (!SET_SAFE_LJMP(hlua->T)) {
7509 if (lua_type(hlua->T, -1) == LUA_TSTRING)
7510 error = lua_tostring(hlua->T, -1);
7511 else
7512 error = "critical error";
7513 SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error);
7514 goto error;
7515 }
7516
7517 /* Check stack available size. */
7518 if (!lua_checkstack(hlua->T, 2)) {
7519 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7520 goto error;
7521 }
7522
7523 /* Restore the function in the stack. */
7524 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
7525
7526 /* Once the arguments parsed, the CLI is like an AppletTCP,
7527 * so push AppletTCP in the stack.
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007528 */
7529 if (!hlua_applet_tcp_new(hlua->T, appctx)) {
7530 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7531 goto error;
7532 }
7533 hlua->nargs = 1;
7534
7535 /* push keywords in the stack. */
7536 for (i = 0; *args[i]; i++) {
7537 /* Check stack available size. */
7538 if (!lua_checkstack(hlua->T, 1)) {
7539 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7540 goto error;
7541 }
7542 lua_pushstring(hlua->T, args[i]);
7543 hlua->nargs++;
7544 }
7545
7546 /* We must initialize the execution timeouts. */
7547 hlua->max_time = hlua_timeout_session;
7548
7549 /* At this point the execution is safe. */
7550 RESET_SAFE_LJMP(hlua->T);
7551
7552 /* It's ok */
7553 return 0;
7554
7555 /* It's not ok. */
7556error:
7557 RESET_SAFE_LJMP(hlua->T);
7558 hlua_ctx_destroy(hlua);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007559 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007560 return 1;
7561}
7562
7563static int hlua_cli_io_handler_fct(struct appctx *appctx)
7564{
7565 struct hlua *hlua;
7566 struct stream_interface *si;
7567 struct hlua_function *fcn;
7568
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007569 hlua = appctx->ctx.hlua_cli.hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007570 si = appctx->owner;
Willy Tarreau8ae4f752016-12-14 15:41:45 +01007571 fcn = appctx->ctx.hlua_cli.fcn;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007572
7573 /* If the stream is disconnect or closed, ldo nothing. */
7574 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
7575 return 1;
7576
7577 /* Execute the function. */
7578 switch (hlua_ctx_resume(hlua, 1)) {
7579
7580 /* finished. */
7581 case HLUA_E_OK:
7582 return 1;
7583
7584 /* yield. */
7585 case HLUA_E_AGAIN:
7586 /* We want write. */
7587 if (HLUA_IS_WAKERESWR(hlua))
Willy Tarreaudb398432018-11-15 11:08:52 +01007588 si_rx_room_blk(si);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007589 /* Set the timeout. */
7590 if (hlua->wake_time != TICK_ETERNITY)
7591 task_schedule(hlua->task, hlua->wake_time);
7592 return 0;
7593
7594 /* finished with error. */
7595 case HLUA_E_ERRMSG:
7596 /* Display log. */
7597 SEND_ERR(NULL, "Lua cli '%s': %s.\n",
7598 fcn->name, lua_tostring(hlua->T, -1));
7599 lua_pop(hlua->T, 1);
7600 return 1;
7601
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007602 case HLUA_E_ETMOUT:
7603 SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n",
7604 fcn->name);
7605 return 1;
7606
7607 case HLUA_E_NOMEM:
7608 SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n",
7609 fcn->name);
7610 return 1;
7611
7612 case HLUA_E_YIELD: /* unexpected */
7613 SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n",
7614 fcn->name);
7615 return 1;
7616
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007617 case HLUA_E_ERR:
7618 /* Display log. */
7619 SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n",
7620 fcn->name);
7621 return 1;
7622
7623 default:
7624 return 1;
7625 }
7626
7627 return 1;
7628}
7629
7630static void hlua_cli_io_release_fct(struct appctx *appctx)
7631{
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007632 hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007633 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007634}
7635
7636/* This function is an LUA binding used for registering
7637 * new keywords in the cli. It expects a list of keywords
7638 * which are the "path". It is limited to 5 keywords. A
7639 * description of the command, a function to be executed
7640 * for the parsing and a function for io handlers.
7641 */
7642__LJMP static int hlua_register_cli(lua_State *L)
7643{
7644 struct cli_kw_list *cli_kws;
7645 const char *message;
7646 int ref_io;
7647 int len;
7648 struct hlua_function *fcn;
7649 int index;
7650 int i;
7651
7652 MAY_LJMP(check_args(L, 3, "register_cli"));
7653
7654 /* First argument : an array of maximum 5 keywords. */
7655 if (!lua_istable(L, 1))
7656 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table"));
7657
7658 /* Second argument : string with contextual message. */
7659 message = MAY_LJMP(luaL_checkstring(L, 2));
7660
7661 /* Third and fourth argument : lua function. */
7662 ref_io = MAY_LJMP(hlua_checkfunction(L, 3));
7663
7664 /* Allocate and fill the sample fetch keyword struct. */
7665 cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2);
7666 if (!cli_kws)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007667 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007668 fcn = calloc(1, sizeof(*fcn));
7669 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007670 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007671
7672 /* Fill path. */
7673 index = 0;
7674 lua_pushnil(L);
7675 while(lua_next(L, 1) != 0) {
7676 if (index >= 5)
7677 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries"));
7678 if (lua_type(L, -1) != LUA_TSTRING)
7679 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings"));
7680 cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1));
7681 if (!cli_kws->kw[0].str_kw[index])
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007682 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007683 index++;
7684 lua_pop(L, 1);
7685 }
7686
7687 /* Copy help message. */
7688 cli_kws->kw[0].usage = strdup(message);
7689 if (!cli_kws->kw[0].usage)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007690 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007691
7692 /* Fill fcn io handler. */
7693 len = strlen("<lua.cli>") + 1;
7694 for (i = 0; i < index; i++)
7695 len += strlen(cli_kws->kw[0].str_kw[i]) + 1;
7696 fcn->name = calloc(1, len);
7697 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007698 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007699 strncat((char *)fcn->name, "<lua.cli", len);
7700 for (i = 0; i < index; i++) {
7701 strncat((char *)fcn->name, ".", len);
7702 strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len);
7703 }
7704 strncat((char *)fcn->name, ">", len);
7705 fcn->function_ref = ref_io;
7706
7707 /* Fill last entries. */
7708 cli_kws->kw[0].private = fcn;
7709 cli_kws->kw[0].parse = hlua_cli_parse_fct;
7710 cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct;
7711 cli_kws->kw[0].io_release = hlua_cli_io_release_fct;
7712
7713 /* Register this new converter */
7714 cli_register_kw(cli_kws);
7715
7716 return 0;
7717}
7718
Thierry FOURNIERbd413492015-03-03 16:52:26 +01007719static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx,
7720 struct proxy *defpx, const char *file, int line,
7721 char **err, unsigned int *timeout)
7722{
7723 const char *error;
7724
7725 error = parse_time_err(args[1], timeout, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02007726 if (error == PARSE_TIME_OVER) {
7727 memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)",
7728 args[1], args[0]);
7729 return -1;
7730 }
7731 else if (error == PARSE_TIME_UNDER) {
7732 memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)",
7733 args[1], args[0]);
7734 return -1;
7735 }
7736 else if (error) {
Thierry FOURNIERbd413492015-03-03 16:52:26 +01007737 memprintf(err, "%s: invalid timeout", args[0]);
7738 return -1;
7739 }
7740 return 0;
7741}
7742
7743static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx,
7744 struct proxy *defpx, const char *file, int line,
7745 char **err)
7746{
7747 return hlua_read_timeout(args, section_type, curpx, defpx,
7748 file, line, err, &hlua_timeout_session);
7749}
7750
7751static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx,
7752 struct proxy *defpx, const char *file, int line,
7753 char **err)
7754{
7755 return hlua_read_timeout(args, section_type, curpx, defpx,
7756 file, line, err, &hlua_timeout_task);
7757}
7758
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007759static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx,
7760 struct proxy *defpx, const char *file, int line,
7761 char **err)
7762{
7763 return hlua_read_timeout(args, section_type, curpx, defpx,
7764 file, line, err, &hlua_timeout_applet);
7765}
7766
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01007767static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx,
7768 struct proxy *defpx, const char *file, int line,
7769 char **err)
7770{
7771 char *error;
7772
7773 hlua_nb_instruction = strtoll(args[1], &error, 10);
7774 if (*error != '\0') {
7775 memprintf(err, "%s: invalid number", args[0]);
7776 return -1;
7777 }
7778 return 0;
7779}
7780
Willy Tarreau32f61e22015-03-18 17:54:59 +01007781static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx,
7782 struct proxy *defpx, const char *file, int line,
7783 char **err)
7784{
7785 char *error;
7786
7787 if (*(args[1]) == 0) {
7788 memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]);
7789 return -1;
7790 }
7791 hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L;
7792 if (*error != '\0') {
7793 memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error);
7794 return -1;
7795 }
7796 return 0;
7797}
7798
7799
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007800/* This function is called by the main configuration key "lua-load". It loads and
7801 * execute an lua file during the parsing of the HAProxy configuration file. It is
7802 * the main lua entry point.
7803 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007804 * This function runs with the HAProxy keywords API. It returns -1 if an error
7805 * occurs, otherwise it returns 0.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007806 *
7807 * In some error case, LUA set an error message in top of the stack. This function
7808 * returns this error message in the HAProxy logs and pop it from the stack.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007809 *
7810 * This function can fail with an abort() due to an Lua critical error.
7811 * We are in the configuration parsing process of HAProxy, this abort() is
7812 * tolerated.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007813 */
7814static int hlua_load(char **args, int section_type, struct proxy *curpx,
7815 struct proxy *defpx, const char *file, int line,
7816 char **err)
7817{
7818 int error;
7819
7820 /* Just load and compile the file. */
7821 error = luaL_loadfile(gL.T, args[1]);
7822 if (error) {
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007823 memprintf(err, "error in Lua file '%s': %s", args[1], lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007824 lua_pop(gL.T, 1);
7825 return -1;
7826 }
7827
7828 /* If no syntax error where detected, execute the code. */
7829 error = lua_pcall(gL.T, 0, LUA_MULTRET, 0);
7830 switch (error) {
7831 case LUA_OK:
7832 break;
7833 case LUA_ERRRUN:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007834 memprintf(err, "Lua runtime error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007835 lua_pop(gL.T, 1);
7836 return -1;
7837 case LUA_ERRMEM:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007838 memprintf(err, "Lua out of memory error.n");
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007839 return -1;
7840 case LUA_ERRERR:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007841 memprintf(err, "Lua message handler error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007842 lua_pop(gL.T, 1);
7843 return -1;
7844 case LUA_ERRGCMM:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007845 memprintf(err, "Lua garbage collector error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007846 lua_pop(gL.T, 1);
7847 return -1;
7848 default:
Ilya Shipitsind4259502020-04-08 01:07:56 +05007849 memprintf(err, "Lua unknown error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007850 lua_pop(gL.T, 1);
7851 return -1;
7852 }
7853
7854 return 0;
7855}
7856
Tim Duesterhusc9fc9f22020-01-12 13:55:39 +01007857/* Prepend the given <path> followed by a semicolon to the `package.<type>` variable
7858 * in the given <ctx>.
7859 */
7860static int hlua_prepend_path(struct hlua ctx, char *type, char *path)
7861{
7862 lua_getglobal(ctx.T, "package"); /* push package variable */
7863 lua_pushstring(ctx.T, path); /* push given path */
7864 lua_pushstring(ctx.T, ";"); /* push semicolon */
7865 lua_getfield(ctx.T, -3, type); /* push old path */
7866 lua_concat(ctx.T, 3); /* concatenate to new path */
7867 lua_setfield(ctx.T, -2, type); /* store new path */
7868 lua_pop(ctx.T, 1); /* pop package variable */
7869
7870 return 0;
7871}
7872
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01007873static int hlua_config_prepend_path(char **args, int section_type, struct proxy *curpx,
7874 struct proxy *defpx, const char *file, int line,
7875 char **err)
7876{
7877 char *path;
7878 char *type = "path";
7879 if (too_many_args(2, args, err, NULL)) {
7880 return -1;
7881 }
7882
7883 if (!(*args[1])) {
7884 memprintf(err, "'%s' expects to receive a <path> as argument", args[0]);
7885 return -1;
7886 }
7887 path = args[1];
7888
7889 if (*args[2]) {
7890 if (strcmp(args[2], "path") != 0 && strcmp(args[2], "cpath") != 0) {
7891 memprintf(err, "'%s' expects <type> to either be 'path' or 'cpath'", args[0]);
7892 return -1;
7893 }
7894 type = args[2];
7895 }
7896
7897 return hlua_prepend_path(gL, type, path);
7898}
7899
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007900/* configuration keywords declaration */
7901static struct cfg_kw_list cfg_kws = {{ },{
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01007902 { CFG_GLOBAL, "lua-prepend-path", hlua_config_prepend_path },
Thierry FOURNIERbd413492015-03-03 16:52:26 +01007903 { CFG_GLOBAL, "lua-load", hlua_load },
7904 { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout },
7905 { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout },
Thierry FOURNIER56da1012015-10-01 08:42:31 +02007906 { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout },
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01007907 { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield },
Willy Tarreau32f61e22015-03-18 17:54:59 +01007908 { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem },
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007909 { 0, NULL, NULL },
7910}};
7911
Willy Tarreau0108d902018-11-25 19:14:37 +01007912INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
7913
Christopher Fauletafd8f102018-11-08 11:34:21 +01007914
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007915/* This function can fail with an abort() due to an Lua critical error.
7916 * We are in the initialisation process of HAProxy, this abort() is
7917 * tolerated.
7918 */
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007919int hlua_post_init()
7920{
7921 struct hlua_init_function *init;
7922 const char *msg;
7923 enum hlua_exec ret;
Thierry Fournierfd107a22016-02-19 19:57:23 +01007924 const char *error;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007925
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007926 /* Call post initialisation function in safe environment. */
Thierry Fournier3d4a6752016-02-19 20:53:30 +01007927 if (!SET_SAFE_LJMP(gL.T)) {
7928 if (lua_type(gL.T, -1) == LUA_TSTRING)
7929 error = lua_tostring(gL.T, -1);
7930 else
7931 error = "critical error";
7932 fprintf(stderr, "Lua post-init: %s.\n", error);
7933 exit(1);
7934 }
Frédéric Lécaille54f2bcf2018-08-29 13:46:24 +02007935
7936#if USE_OPENSSL
7937 /* Initialize SSL server. */
7938 if (socket_ssl.xprt->prepare_srv) {
7939 int saved_used_backed = global.ssl_used_backend;
7940 // don't affect maxconn automatic computation
7941 socket_ssl.xprt->prepare_srv(&socket_ssl);
7942 global.ssl_used_backend = saved_used_backed;
7943 }
7944#endif
7945
Thierry Fournier3d4a6752016-02-19 20:53:30 +01007946 hlua_fcn_post_init(gL.T);
7947 RESET_SAFE_LJMP(gL.T);
7948
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007949 list_for_each_entry(init, &hlua_init_functions, l) {
7950 lua_rawgeti(gL.T, LUA_REGISTRYINDEX, init->function_ref);
7951 ret = hlua_ctx_resume(&gL, 0);
7952 switch (ret) {
7953 case HLUA_E_OK:
7954 lua_pop(gL.T, -1);
7955 return 1;
7956 case HLUA_E_AGAIN:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007957 ha_alert("Lua init: yield not allowed.\n");
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007958 return 0;
7959 case HLUA_E_ERRMSG:
7960 msg = lua_tostring(gL.T, -1);
Christopher Faulet767a84b2017-11-24 16:50:31 +01007961 ha_alert("lua init: %s.\n", msg);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007962 return 0;
7963 case HLUA_E_ERR:
7964 default:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007965 ha_alert("Lua init: unknown runtime error.\n");
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007966 return 0;
7967 }
7968 }
7969 return 1;
7970}
7971
Willy Tarreau32f61e22015-03-18 17:54:59 +01007972/* The memory allocator used by the Lua stack. <ud> is a pointer to the
7973 * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize>
7974 * is the previously allocated size or the kind of object in case of a new
7975 * allocation. <nsize> is the requested new size.
7976 */
7977static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
7978{
7979 struct hlua_mem_allocator *zone = ud;
7980
7981 if (nsize == 0) {
7982 /* it's a free */
7983 if (ptr)
7984 zone->allocated -= osize;
7985 free(ptr);
7986 return NULL;
7987 }
7988
7989 if (!ptr) {
7990 /* it's a new allocation */
7991 if (zone->limit && zone->allocated + nsize > zone->limit)
7992 return NULL;
7993
7994 ptr = malloc(nsize);
7995 if (ptr)
7996 zone->allocated += nsize;
7997 return ptr;
7998 }
7999
8000 /* it's a realloc */
8001 if (zone->limit && zone->allocated + nsize - osize > zone->limit)
8002 return NULL;
8003
8004 ptr = realloc(ptr, nsize);
8005 if (ptr)
8006 zone->allocated += nsize - osize;
8007 return ptr;
8008}
8009
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008010/* Ithis function can fail with an abort() due to an Lua critical error.
8011 * We are in the initialisation process of HAProxy, this abort() is
8012 * tolerated.
8013 */
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01008014void hlua_init(void)
8015{
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008016 int i;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008017 int idx;
8018 struct sample_fetch *sf;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008019 struct sample_conv *sc;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008020 char *p;
Thierry Fournierfd107a22016-02-19 19:57:23 +01008021 const char *error_msg;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008022#ifdef USE_OPENSSL
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008023 struct srv_kw *kw;
8024 int tmp_error;
8025 char *error;
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008026 char *args[] = { /* SSL client configuration. */
8027 "ssl",
8028 "verify",
8029 "none",
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008030 NULL
8031 };
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008032#endif
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008033
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008034 /* Init main lua stack. */
8035 gL.Mref = LUA_REFNIL;
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01008036 gL.flags = 0;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01008037 LIST_INIT(&gL.com);
Willy Tarreau42ef75f2017-04-12 21:40:29 +02008038 gL.T = lua_newstate(hlua_alloc, &hlua_global_allocator);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008039 hlua_sethlua(&gL);
8040 gL.Tref = LUA_REFNIL;
8041 gL.task = NULL;
8042
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08008043 /* From this point, until the end of the initialisation function,
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008044 * the Lua function can fail with an abort. We are in the initialisation
8045 * process of HAProxy, this abort() is tolerated.
8046 */
8047
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008048 /* Initialise lua. */
8049 luaL_openlibs(gL.T);
Tim Duesterhus541fe1e2020-01-12 13:55:41 +01008050#define HLUA_PREPEND_PATH_TOSTRING1(x) #x
8051#define HLUA_PREPEND_PATH_TOSTRING(x) HLUA_PREPEND_PATH_TOSTRING1(x)
8052#ifdef HLUA_PREPEND_PATH
8053 hlua_prepend_path(gL, "path", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_PATH));
8054#endif
8055#ifdef HLUA_PREPEND_CPATH
8056 hlua_prepend_path(gL, "cpath", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_CPATH));
8057#endif
8058#undef HLUA_PREPEND_PATH_TOSTRING
8059#undef HLUA_PREPEND_PATH_TOSTRING1
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008060
Thierry Fournier75933d42016-01-21 09:30:18 +01008061 /* Set safe environment for the initialisation. */
8062 if (!SET_SAFE_LJMP(gL.T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01008063 if (lua_type(gL.T, -1) == LUA_TSTRING)
8064 error_msg = lua_tostring(gL.T, -1);
8065 else
8066 error_msg = "critical error";
8067 fprintf(stderr, "Lua init: %s.\n", error_msg);
Thierry Fournier75933d42016-01-21 09:30:18 +01008068 exit(1);
8069 }
8070
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008071 /*
8072 *
8073 * Create "core" object.
8074 *
8075 */
8076
Thierry FOURNIERa2d8c652015-03-11 17:29:39 +01008077 /* This table entry is the object "core" base. */
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008078 lua_newtable(gL.T);
8079
8080 /* Push the loglevel constants. */
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008081 for (i = 0; i < NB_LOG_LEVELS; i++)
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008082 hlua_class_const_int(gL.T, log_levels[i], i);
8083
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008084 /* Register special functions. */
8085 hlua_class_function(gL.T, "register_init", hlua_register_init);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01008086 hlua_class_function(gL.T, "register_task", hlua_register_task);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01008087 hlua_class_function(gL.T, "register_fetches", hlua_register_fetches);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01008088 hlua_class_function(gL.T, "register_converters", hlua_register_converters);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02008089 hlua_class_function(gL.T, "register_action", hlua_register_action);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008090 hlua_class_function(gL.T, "register_service", hlua_register_service);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008091 hlua_class_function(gL.T, "register_cli", hlua_register_cli);
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01008092 hlua_class_function(gL.T, "yield", hlua_yield);
Willy Tarreau59551662015-03-10 14:23:13 +01008093 hlua_class_function(gL.T, "set_nice", hlua_set_nice);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01008094 hlua_class_function(gL.T, "sleep", hlua_sleep);
8095 hlua_class_function(gL.T, "msleep", hlua_msleep);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01008096 hlua_class_function(gL.T, "add_acl", hlua_add_acl);
8097 hlua_class_function(gL.T, "del_acl", hlua_del_acl);
8098 hlua_class_function(gL.T, "set_map", hlua_set_map);
8099 hlua_class_function(gL.T, "del_map", hlua_del_map);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008100 hlua_class_function(gL.T, "tcp", hlua_socket_new);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01008101 hlua_class_function(gL.T, "log", hlua_log);
8102 hlua_class_function(gL.T, "Debug", hlua_log_debug);
8103 hlua_class_function(gL.T, "Info", hlua_log_info);
8104 hlua_class_function(gL.T, "Warning", hlua_log_warning);
8105 hlua_class_function(gL.T, "Alert", hlua_log_alert);
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02008106 hlua_class_function(gL.T, "done", hlua_done);
Thierry Fournierfb0b5462016-01-21 09:28:58 +01008107 hlua_fcn_reg_core_fcn(gL.T);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008108
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008109 lua_setglobal(gL.T, "core");
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008110
8111 /*
8112 *
Christopher Faulet0f3c8902020-01-31 18:57:12 +01008113 * Create "act" object.
8114 *
8115 */
8116
8117 /* This table entry is the object "act" base. */
8118 lua_newtable(gL.T);
8119
8120 /* push action return constants */
8121 hlua_class_const_int(gL.T, "CONTINUE", ACT_RET_CONT);
8122 hlua_class_const_int(gL.T, "STOP", ACT_RET_STOP);
8123 hlua_class_const_int(gL.T, "YIELD", ACT_RET_YIELD);
8124 hlua_class_const_int(gL.T, "ERROR", ACT_RET_ERR);
8125 hlua_class_const_int(gL.T, "DONE", ACT_RET_DONE);
8126 hlua_class_const_int(gL.T, "DENY", ACT_RET_DENY);
8127 hlua_class_const_int(gL.T, "ABORT", ACT_RET_ABRT);
8128 hlua_class_const_int(gL.T, "INVALID", ACT_RET_INV);
8129
Christopher Faulet501465d2020-02-26 14:54:16 +01008130 hlua_class_function(gL.T, "wake_time", hlua_set_wake_time);
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01008131
Christopher Faulet0f3c8902020-01-31 18:57:12 +01008132 lua_setglobal(gL.T, "act");
8133
8134 /*
8135 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008136 * Register class Map
8137 *
8138 */
8139
8140 /* This table entry is the object "Map" base. */
8141 lua_newtable(gL.T);
8142
8143 /* register pattern types. */
8144 for (i=0; i<PAT_MATCH_NUM; i++)
8145 hlua_class_const_int(gL.T, pat_match_names[i], i);
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01008146 for (i=0; i<PAT_MATCH_NUM; i++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008147 snprintf(trash.area, trash.size, "_%s", pat_match_names[i]);
8148 hlua_class_const_int(gL.T, trash.area, i);
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01008149 }
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008150
8151 /* register constructor. */
8152 hlua_class_function(gL.T, "new", hlua_map_new);
8153
8154 /* Create and fill the metatable. */
8155 lua_newtable(gL.T);
8156
Ilya Shipitsind4259502020-04-08 01:07:56 +05008157 /* Create and fill the __index entry. */
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008158 lua_pushstring(gL.T, "__index");
8159 lua_newtable(gL.T);
8160
8161 /* Register . */
8162 hlua_class_function(gL.T, "lookup", hlua_map_lookup);
8163 hlua_class_function(gL.T, "slookup", hlua_map_slookup);
8164
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008165 lua_rawset(gL.T, -3);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008166
Thierry Fournier45e78d72016-02-19 18:34:46 +01008167 /* Register previous table in the registry with reference and named entry.
8168 * The function hlua_register_metatable() pops the stack, so we
8169 * previously create a copy of the table.
8170 */
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008171 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008172 class_map_ref = hlua_register_metatable(gL.T, CLASS_MAP);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008173
8174 /* Assign the metatable to the mai Map object. */
8175 lua_setmetatable(gL.T, -2);
8176
8177 /* Set a name to the table. */
8178 lua_setglobal(gL.T, "Map");
8179
8180 /*
8181 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008182 * Register class Channel
8183 *
8184 */
8185
8186 /* Create and fill the metatable. */
8187 lua_newtable(gL.T);
8188
Ilya Shipitsind4259502020-04-08 01:07:56 +05008189 /* Create and fill the __index entry. */
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008190 lua_pushstring(gL.T, "__index");
8191 lua_newtable(gL.T);
8192
8193 /* Register . */
8194 hlua_class_function(gL.T, "get", hlua_channel_get);
8195 hlua_class_function(gL.T, "dup", hlua_channel_dup);
8196 hlua_class_function(gL.T, "getline", hlua_channel_getline);
8197 hlua_class_function(gL.T, "set", hlua_channel_set);
8198 hlua_class_function(gL.T, "append", hlua_channel_append);
8199 hlua_class_function(gL.T, "send", hlua_channel_send);
8200 hlua_class_function(gL.T, "forward", hlua_channel_forward);
8201 hlua_class_function(gL.T, "get_in_len", hlua_channel_get_in_len);
8202 hlua_class_function(gL.T, "get_out_len", hlua_channel_get_out_len);
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01008203 hlua_class_function(gL.T, "is_full", hlua_channel_is_full);
Christopher Faulet2ac9ba22020-02-25 10:15:50 +01008204 hlua_class_function(gL.T, "is_resp", hlua_channel_is_resp);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008205
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008206 lua_rawset(gL.T, -3);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008207
8208 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008209 class_channel_ref = hlua_register_metatable(gL.T, CLASS_CHANNEL);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008210
8211 /*
8212 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008213 * Register class Fetches
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008214 *
8215 */
8216
8217 /* Create and fill the metatable. */
8218 lua_newtable(gL.T);
8219
Ilya Shipitsind4259502020-04-08 01:07:56 +05008220 /* Create and fill the __index entry. */
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008221 lua_pushstring(gL.T, "__index");
8222 lua_newtable(gL.T);
8223
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008224 /* Browse existing fetches and create the associated
8225 * object method.
8226 */
8227 sf = NULL;
8228 while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) {
8229
8230 /* Dont register the keywork if the arguments check function are
8231 * not safe during the runtime.
8232 */
8233 if ((sf->val_args != NULL) &&
8234 (sf->val_args != val_payload_lv) &&
8235 (sf->val_args != val_hdr))
8236 continue;
8237
8238 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
8239 * by an underscore.
8240 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008241 strncpy(trash.area, sf->kw, trash.size);
8242 trash.area[trash.size - 1] = '\0';
8243 for (p = trash.area; *p; p++)
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008244 if (*p == '.' || *p == '-' || *p == '+')
8245 *p = '_';
8246
8247 /* Register the function. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008248 lua_pushstring(gL.T, trash.area);
Willy Tarreau2ec22742015-03-10 14:27:20 +01008249 lua_pushlightuserdata(gL.T, sf);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008250 lua_pushcclosure(gL.T, hlua_run_sample_fetch, 1);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008251 lua_rawset(gL.T, -3);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008252 }
8253
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008254 lua_rawset(gL.T, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008255
8256 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008257 class_fetches_ref = hlua_register_metatable(gL.T, CLASS_FETCHES);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008258
8259 /*
8260 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008261 * Register class Converters
8262 *
8263 */
8264
8265 /* Create and fill the metatable. */
8266 lua_newtable(gL.T);
8267
8268 /* Create and fill the __index entry. */
8269 lua_pushstring(gL.T, "__index");
8270 lua_newtable(gL.T);
8271
8272 /* Browse existing converters and create the associated
8273 * object method.
8274 */
8275 sc = NULL;
8276 while ((sc = sample_conv_getnext(sc, &idx)) != NULL) {
8277 /* Dont register the keywork if the arguments check function are
8278 * not safe during the runtime.
8279 */
8280 if (sc->val_args != NULL)
8281 continue;
8282
8283 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
8284 * by an underscore.
8285 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008286 strncpy(trash.area, sc->kw, trash.size);
8287 trash.area[trash.size - 1] = '\0';
8288 for (p = trash.area; *p; p++)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008289 if (*p == '.' || *p == '-' || *p == '+')
8290 *p = '_';
8291
8292 /* Register the function. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008293 lua_pushstring(gL.T, trash.area);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008294 lua_pushlightuserdata(gL.T, sc);
8295 lua_pushcclosure(gL.T, hlua_run_sample_conv, 1);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008296 lua_rawset(gL.T, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008297 }
8298
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008299 lua_rawset(gL.T, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008300
8301 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008302 class_converters_ref = hlua_register_metatable(gL.T, CLASS_CONVERTERS);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008303
8304 /*
8305 *
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008306 * Register class HTTP
8307 *
8308 */
8309
8310 /* Create and fill the metatable. */
8311 lua_newtable(gL.T);
8312
Ilya Shipitsind4259502020-04-08 01:07:56 +05008313 /* Create and fill the __index entry. */
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008314 lua_pushstring(gL.T, "__index");
8315 lua_newtable(gL.T);
8316
8317 /* Register Lua functions. */
8318 hlua_class_function(gL.T, "req_get_headers",hlua_http_req_get_headers);
8319 hlua_class_function(gL.T, "req_del_header", hlua_http_req_del_hdr);
8320 hlua_class_function(gL.T, "req_rep_header", hlua_http_req_rep_hdr);
8321 hlua_class_function(gL.T, "req_rep_value", hlua_http_req_rep_val);
8322 hlua_class_function(gL.T, "req_add_header", hlua_http_req_add_hdr);
8323 hlua_class_function(gL.T, "req_set_header", hlua_http_req_set_hdr);
8324 hlua_class_function(gL.T, "req_set_method", hlua_http_req_set_meth);
8325 hlua_class_function(gL.T, "req_set_path", hlua_http_req_set_path);
8326 hlua_class_function(gL.T, "req_set_query", hlua_http_req_set_query);
8327 hlua_class_function(gL.T, "req_set_uri", hlua_http_req_set_uri);
8328
8329 hlua_class_function(gL.T, "res_get_headers",hlua_http_res_get_headers);
8330 hlua_class_function(gL.T, "res_del_header", hlua_http_res_del_hdr);
8331 hlua_class_function(gL.T, "res_rep_header", hlua_http_res_rep_hdr);
8332 hlua_class_function(gL.T, "res_rep_value", hlua_http_res_rep_val);
8333 hlua_class_function(gL.T, "res_add_header", hlua_http_res_add_hdr);
8334 hlua_class_function(gL.T, "res_set_header", hlua_http_res_set_hdr);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02008335 hlua_class_function(gL.T, "res_set_status", hlua_http_res_set_status);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008336
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008337 lua_rawset(gL.T, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008338
8339 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008340 class_http_ref = hlua_register_metatable(gL.T, CLASS_HTTP);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008341
8342 /*
8343 *
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008344 * Register class AppletTCP
8345 *
8346 */
8347
8348 /* Create and fill the metatable. */
8349 lua_newtable(gL.T);
8350
Ilya Shipitsind4259502020-04-08 01:07:56 +05008351 /* Create and fill the __index entry. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008352 lua_pushstring(gL.T, "__index");
8353 lua_newtable(gL.T);
8354
8355 /* Register Lua functions. */
Thierry FOURNIER / OZON.IO3e1d7912016-12-12 12:29:34 +01008356 hlua_class_function(gL.T, "getline", hlua_applet_tcp_getline);
8357 hlua_class_function(gL.T, "receive", hlua_applet_tcp_recv);
8358 hlua_class_function(gL.T, "send", hlua_applet_tcp_send);
8359 hlua_class_function(gL.T, "set_priv", hlua_applet_tcp_set_priv);
8360 hlua_class_function(gL.T, "get_priv", hlua_applet_tcp_get_priv);
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01008361 hlua_class_function(gL.T, "set_var", hlua_applet_tcp_set_var);
8362 hlua_class_function(gL.T, "unset_var", hlua_applet_tcp_unset_var);
8363 hlua_class_function(gL.T, "get_var", hlua_applet_tcp_get_var);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008364
8365 lua_settable(gL.T, -3);
8366
8367 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008368 class_applet_tcp_ref = hlua_register_metatable(gL.T, CLASS_APPLET_TCP);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008369
8370 /*
8371 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02008372 * Register class AppletHTTP
8373 *
8374 */
8375
8376 /* Create and fill the metatable. */
8377 lua_newtable(gL.T);
8378
Ilya Shipitsind4259502020-04-08 01:07:56 +05008379 /* Create and fill the __index entry. */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02008380 lua_pushstring(gL.T, "__index");
8381 lua_newtable(gL.T);
8382
8383 /* Register Lua functions. */
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01008384 hlua_class_function(gL.T, "set_priv", hlua_applet_http_set_priv);
8385 hlua_class_function(gL.T, "get_priv", hlua_applet_http_get_priv);
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01008386 hlua_class_function(gL.T, "set_var", hlua_applet_http_set_var);
8387 hlua_class_function(gL.T, "unset_var", hlua_applet_http_unset_var);
8388 hlua_class_function(gL.T, "get_var", hlua_applet_http_get_var);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02008389 hlua_class_function(gL.T, "getline", hlua_applet_http_getline);
8390 hlua_class_function(gL.T, "receive", hlua_applet_http_recv);
8391 hlua_class_function(gL.T, "send", hlua_applet_http_send);
8392 hlua_class_function(gL.T, "add_header", hlua_applet_http_addheader);
8393 hlua_class_function(gL.T, "set_status", hlua_applet_http_status);
8394 hlua_class_function(gL.T, "start_response", hlua_applet_http_start_response);
8395
8396 lua_settable(gL.T, -3);
8397
8398 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008399 class_applet_http_ref = hlua_register_metatable(gL.T, CLASS_APPLET_HTTP);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02008400
8401 /*
8402 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008403 * Register class TXN
8404 *
8405 */
8406
8407 /* Create and fill the metatable. */
8408 lua_newtable(gL.T);
8409
Ilya Shipitsind4259502020-04-08 01:07:56 +05008410 /* Create and fill the __index entry. */
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008411 lua_pushstring(gL.T, "__index");
8412 lua_newtable(gL.T);
8413
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01008414 /* Register Lua functions. */
Patrick Hemmer268a7072018-05-11 12:52:31 -04008415 hlua_class_function(gL.T, "set_priv", hlua_set_priv);
8416 hlua_class_function(gL.T, "get_priv", hlua_get_priv);
8417 hlua_class_function(gL.T, "set_var", hlua_set_var);
8418 hlua_class_function(gL.T, "unset_var", hlua_unset_var);
8419 hlua_class_function(gL.T, "get_var", hlua_get_var);
8420 hlua_class_function(gL.T, "done", hlua_txn_done);
Christopher Faulet700d9e82020-01-31 12:21:52 +01008421 hlua_class_function(gL.T, "reply", hlua_txn_reply_new);
Patrick Hemmer268a7072018-05-11 12:52:31 -04008422 hlua_class_function(gL.T, "set_loglevel", hlua_txn_set_loglevel);
8423 hlua_class_function(gL.T, "set_tos", hlua_txn_set_tos);
8424 hlua_class_function(gL.T, "set_mark", hlua_txn_set_mark);
8425 hlua_class_function(gL.T, "set_priority_class", hlua_txn_set_priority_class);
8426 hlua_class_function(gL.T, "set_priority_offset", hlua_txn_set_priority_offset);
8427 hlua_class_function(gL.T, "deflog", hlua_txn_deflog);
8428 hlua_class_function(gL.T, "log", hlua_txn_log);
8429 hlua_class_function(gL.T, "Debug", hlua_txn_log_debug);
8430 hlua_class_function(gL.T, "Info", hlua_txn_log_info);
8431 hlua_class_function(gL.T, "Warning", hlua_txn_log_warning);
8432 hlua_class_function(gL.T, "Alert", hlua_txn_log_alert);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01008433
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008434 lua_rawset(gL.T, -3);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008435
8436 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008437 class_txn_ref = hlua_register_metatable(gL.T, CLASS_TXN);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008438
8439 /*
8440 *
Christopher Faulet700d9e82020-01-31 12:21:52 +01008441 * Register class reply
8442 *
8443 */
8444 lua_newtable(gL.T);
8445 lua_pushstring(gL.T, "__index");
8446 lua_newtable(gL.T);
8447 hlua_class_function(gL.T, "set_status", hlua_txn_reply_set_status);
8448 hlua_class_function(gL.T, "add_header", hlua_txn_reply_add_header);
8449 hlua_class_function(gL.T, "del_header", hlua_txn_reply_del_header);
8450 hlua_class_function(gL.T, "set_body", hlua_txn_reply_set_body);
8451 lua_settable(gL.T, -3); /* Sets the __index entry. */
8452 class_txn_reply_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
8453
8454
8455 /*
8456 *
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008457 * Register class Socket
8458 *
8459 */
8460
8461 /* Create and fill the metatable. */
8462 lua_newtable(gL.T);
8463
Ilya Shipitsind4259502020-04-08 01:07:56 +05008464 /* Create and fill the __index entry. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008465 lua_pushstring(gL.T, "__index");
8466 lua_newtable(gL.T);
8467
Baptiste Assmann84bb4932015-03-02 21:40:06 +01008468#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008469 hlua_class_function(gL.T, "connect_ssl", hlua_socket_connect_ssl);
Baptiste Assmann84bb4932015-03-02 21:40:06 +01008470#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008471 hlua_class_function(gL.T, "connect", hlua_socket_connect);
8472 hlua_class_function(gL.T, "send", hlua_socket_send);
8473 hlua_class_function(gL.T, "receive", hlua_socket_receive);
8474 hlua_class_function(gL.T, "close", hlua_socket_close);
8475 hlua_class_function(gL.T, "getpeername", hlua_socket_getpeername);
8476 hlua_class_function(gL.T, "getsockname", hlua_socket_getsockname);
8477 hlua_class_function(gL.T, "setoption", hlua_socket_setoption);
8478 hlua_class_function(gL.T, "settimeout", hlua_socket_settimeout);
8479
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008480 lua_rawset(gL.T, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008481
8482 /* Register the garbage collector entry. */
8483 lua_pushstring(gL.T, "__gc");
8484 lua_pushcclosure(gL.T, hlua_socket_gc, 0);
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 previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008488 class_socket_ref = hlua_register_metatable(gL.T, CLASS_SOCKET);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008489
8490 /* Proxy and server configuration initialisation. */
8491 memset(&socket_proxy, 0, sizeof(socket_proxy));
8492 init_new_proxy(&socket_proxy);
8493 socket_proxy.parent = NULL;
8494 socket_proxy.last_change = now.tv_sec;
8495 socket_proxy.id = "LUA-SOCKET";
8496 socket_proxy.cap = PR_CAP_FE | PR_CAP_BE;
8497 socket_proxy.maxconn = 0;
8498 socket_proxy.accept = NULL;
8499 socket_proxy.options2 |= PR_O2_INDEPSTR;
8500 socket_proxy.srv = NULL;
8501 socket_proxy.conn_retries = 0;
8502 socket_proxy.timeout.connect = 5000; /* By default the timeout connection is 5s. */
8503
8504 /* Init TCP server: unchanged parameters */
8505 memset(&socket_tcp, 0, sizeof(socket_tcp));
8506 socket_tcp.next = NULL;
8507 socket_tcp.proxy = &socket_proxy;
8508 socket_tcp.obj_type = OBJ_TYPE_SERVER;
8509 LIST_INIT(&socket_tcp.actconns);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04008510 socket_tcp.pendconns = EB_ROOT;
Christopher Faulet40a007c2017-07-03 15:41:01 +02008511 socket_tcp.idle_conns = NULL;
8512 socket_tcp.safe_conns = NULL;
Emeric Brun52a91d32017-08-31 14:41:55 +02008513 socket_tcp.next_state = SRV_ST_RUNNING; /* early server setup */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008514 socket_tcp.last_change = 0;
8515 socket_tcp.id = "LUA-TCP-CONN";
8516 socket_tcp.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8517 socket_tcp.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8518 socket_tcp.pp_opts = 0; /* Remove proxy protocol. */
8519
8520 /* XXX: Copy default parameter from default server,
8521 * but the default server is not initialized.
8522 */
8523 socket_tcp.maxqueue = socket_proxy.defsrv.maxqueue;
8524 socket_tcp.minconn = socket_proxy.defsrv.minconn;
8525 socket_tcp.maxconn = socket_proxy.defsrv.maxconn;
8526 socket_tcp.slowstart = socket_proxy.defsrv.slowstart;
8527 socket_tcp.onerror = socket_proxy.defsrv.onerror;
8528 socket_tcp.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
8529 socket_tcp.onmarkedup = socket_proxy.defsrv.onmarkedup;
8530 socket_tcp.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
8531 socket_tcp.uweight = socket_proxy.defsrv.iweight;
8532 socket_tcp.iweight = socket_proxy.defsrv.iweight;
8533
8534 socket_tcp.check.status = HCHK_STATUS_INI;
8535 socket_tcp.check.rise = socket_proxy.defsrv.check.rise;
8536 socket_tcp.check.fall = socket_proxy.defsrv.check.fall;
8537 socket_tcp.check.health = socket_tcp.check.rise; /* socket, but will fall down at first failure */
8538 socket_tcp.check.server = &socket_tcp;
8539
8540 socket_tcp.agent.status = HCHK_STATUS_INI;
8541 socket_tcp.agent.rise = socket_proxy.defsrv.agent.rise;
8542 socket_tcp.agent.fall = socket_proxy.defsrv.agent.fall;
8543 socket_tcp.agent.health = socket_tcp.agent.rise; /* socket, but will fall down at first failure */
8544 socket_tcp.agent.server = &socket_tcp;
8545
Willy Tarreaua261e9b2016-12-22 20:44:00 +01008546 socket_tcp.xprt = xprt_get(XPRT_RAW);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008547
8548#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008549 /* Init TCP server: unchanged parameters */
8550 memset(&socket_ssl, 0, sizeof(socket_ssl));
8551 socket_ssl.next = NULL;
8552 socket_ssl.proxy = &socket_proxy;
8553 socket_ssl.obj_type = OBJ_TYPE_SERVER;
8554 LIST_INIT(&socket_ssl.actconns);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04008555 socket_ssl.pendconns = EB_ROOT;
Willy Tarreaub784b352019-02-07 14:48:24 +01008556 socket_ssl.idle_conns = NULL;
8557 socket_ssl.safe_conns = NULL;
Emeric Brun52a91d32017-08-31 14:41:55 +02008558 socket_ssl.next_state = SRV_ST_RUNNING; /* early server setup */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008559 socket_ssl.last_change = 0;
8560 socket_ssl.id = "LUA-SSL-CONN";
8561 socket_ssl.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8562 socket_ssl.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8563 socket_ssl.pp_opts = 0; /* Remove proxy protocol. */
8564
8565 /* XXX: Copy default parameter from default server,
8566 * but the default server is not initialized.
8567 */
8568 socket_ssl.maxqueue = socket_proxy.defsrv.maxqueue;
8569 socket_ssl.minconn = socket_proxy.defsrv.minconn;
8570 socket_ssl.maxconn = socket_proxy.defsrv.maxconn;
8571 socket_ssl.slowstart = socket_proxy.defsrv.slowstart;
8572 socket_ssl.onerror = socket_proxy.defsrv.onerror;
8573 socket_ssl.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
8574 socket_ssl.onmarkedup = socket_proxy.defsrv.onmarkedup;
8575 socket_ssl.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
8576 socket_ssl.uweight = socket_proxy.defsrv.iweight;
8577 socket_ssl.iweight = socket_proxy.defsrv.iweight;
8578
8579 socket_ssl.check.status = HCHK_STATUS_INI;
8580 socket_ssl.check.rise = socket_proxy.defsrv.check.rise;
8581 socket_ssl.check.fall = socket_proxy.defsrv.check.fall;
8582 socket_ssl.check.health = socket_ssl.check.rise; /* socket, but will fall down at first failure */
8583 socket_ssl.check.server = &socket_ssl;
8584
8585 socket_ssl.agent.status = HCHK_STATUS_INI;
8586 socket_ssl.agent.rise = socket_proxy.defsrv.agent.rise;
8587 socket_ssl.agent.fall = socket_proxy.defsrv.agent.fall;
8588 socket_ssl.agent.health = socket_ssl.agent.rise; /* socket, but will fall down at first failure */
8589 socket_ssl.agent.server = &socket_ssl;
8590
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008591 socket_ssl.use_ssl = 1;
Willy Tarreaua261e9b2016-12-22 20:44:00 +01008592 socket_ssl.xprt = xprt_get(XPRT_SSL);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008593
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008594 for (idx = 0; args[idx] != NULL; idx++) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008595 if ((kw = srv_find_kw(args[idx])) != NULL) { /* Maybe it's registered server keyword */
8596 /*
8597 *
8598 * If the keyword is not known, we can search in the registered
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08008599 * server keywords. This is useful to configure special SSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008600 * features like client certificates and ssl_verify.
8601 *
8602 */
8603 tmp_error = kw->parse(args, &idx, &socket_proxy, &socket_ssl, &error);
8604 if (tmp_error != 0) {
8605 fprintf(stderr, "INTERNAL ERROR: %s\n", error);
8606 abort(); /* This must be never arrives because the command line
8607 not editable by the user. */
8608 }
8609 idx += kw->skip;
8610 }
8611 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008612#endif
Thierry Fournier75933d42016-01-21 09:30:18 +01008613
8614 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01008615}
Willy Tarreaubb57d942016-12-21 19:04:56 +01008616
Willy Tarreau80713382018-11-26 10:19:54 +01008617static void hlua_register_build_options(void)
8618{
Willy Tarreaubb57d942016-12-21 19:04:56 +01008619 char *ptr = NULL;
Willy Tarreau80713382018-11-26 10:19:54 +01008620
Willy Tarreaubb57d942016-12-21 19:04:56 +01008621 memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE);
8622 hap_register_build_opts(ptr, 1);
8623}
Willy Tarreau80713382018-11-26 10:19:54 +01008624
8625INITCALL0(STG_REGISTER, hlua_register_build_options);