blob: 9eaa10b9a5bddd863a6ee9c48d65d42eeb899847 [file] [log] [blame]
Thierry Fourniere726b142016-02-11 17:57:57 +01001/*
2 * Lua unsafe core engine
3 *
4 * Copyright 2015-2016 Thierry Fournier <tfournier@arpalert.org>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +010013#include <ctype.h>
Mark Lakes56cc1252018-03-27 09:48:06 +020014#include <limits.h>
Thierry FOURNIERbabae282015-09-17 11:36:37 +020015#include <setjmp.h>
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +010016
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +010017#include <lauxlib.h>
18#include <lua.h>
19#include <lualib.h>
20
Thierry FOURNIER463119c2015-03-10 00:35:36 +010021#if !defined(LUA_VERSION_NUM) || LUA_VERSION_NUM < 503
22#error "Requires Lua 5.3 or later."
Cyril Bontédc0306e2015-03-02 00:08:40 +010023#endif
24
Thierry FOURNIER380d0932015-01-23 14:27:52 +010025#include <ebpttree.h>
26
27#include <common/cfgparse.h>
Willy Tarreaub059b892018-10-16 17:57:36 +020028#include <common/compiler.h>
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +020029#include <common/hathreads.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010030#include <common/initcall.h>
31#include <common/xref.h>
Christopher Faulet724a12c2018-12-13 22:12:15 +010032#include <common/h1.h>
Thierry FOURNIER380d0932015-01-23 14:27:52 +010033
William Lallemand9ed62032016-11-21 17:49:11 +010034#include <types/cli.h>
Thierry FOURNIER380d0932015-01-23 14:27:52 +010035#include <types/hlua.h>
36#include <types/proxy.h>
William Lallemand9ed62032016-11-21 17:49:11 +010037#include <types/stats.h>
Thierry FOURNIER380d0932015-01-23 14:27:52 +010038
Thierry FOURNIER55da1652015-01-23 11:36:30 +010039#include <proto/arg.h>
Willy Tarreau8a8d83b2015-04-13 13:24:54 +020040#include <proto/applet.h>
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010041#include <proto/channel.h>
William Lallemand9ed62032016-11-21 17:49:11 +010042#include <proto/cli.h>
Willy Tarreaua71f6422016-11-16 17:00:14 +010043#include <proto/connection.h>
William Lallemand9ed62032016-11-21 17:49:11 +010044#include <proto/stats.h>
Thierry FOURNIER9a819e72015-02-16 20:22:55 +010045#include <proto/hdr_idx.h>
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +010046#include <proto/hlua.h>
Thierry Fournierfb0b5462016-01-21 09:28:58 +010047#include <proto/hlua_fcn.h>
Willy Tarreau79e57332018-10-02 16:01:16 +020048#include <proto/http_fetch.h>
Christopher Faulet724a12c2018-12-13 22:12:15 +010049#include <proto/http_htx.h>
Willy Tarreau61c112a2018-10-02 16:43:32 +020050#include <proto/http_rules.h>
Thierry FOURNIER3def3932015-04-07 11:27:54 +020051#include <proto/map.h>
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010052#include <proto/obj_type.h>
Patrick Hemmer268a7072018-05-11 12:52:31 -040053#include <proto/queue.h>
Thierry FOURNIER83758bb2015-02-04 13:21:04 +010054#include <proto/pattern.h>
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +010055#include <proto/payload.h>
56#include <proto/proto_http.h>
57#include <proto/sample.h>
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010058#include <proto/server.h>
Willy Tarreaufeb76402015-04-03 14:10:06 +020059#include <proto/session.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020060#include <proto/stream.h>
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010061#include <proto/stream_interface.h>
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +010062#include <proto/task.h>
Willy Tarreau39713102016-11-25 15:49:32 +010063#include <proto/tcp_rules.h>
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +020064#include <proto/vars.h>
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +010065
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +010066/* Lua uses longjmp to perform yield or throwing errors. This
67 * macro is used only for identifying the function that can
68 * not return because a longjmp is executed.
69 * __LJMP marks a prototype of hlua file that can use longjmp.
70 * WILL_LJMP() marks an lua function that will use longjmp.
71 * MAY_LJMP() marks an lua function that may use longjmp.
72 */
73#define __LJMP
Willy Tarreau4e7cc332018-10-20 17:45:48 +020074#define WILL_LJMP(func) do { func; my_unreachable(); } while(0)
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +010075#define MAY_LJMP(func) func
76
Thierry FOURNIERbabae282015-09-17 11:36:37 +020077/* This couple of function executes securely some Lua calls outside of
78 * the lua runtime environment. Each Lua call can return a longjmp
79 * if it encounter a memory error.
80 *
81 * Lua documentation extract:
82 *
83 * If an error happens outside any protected environment, Lua calls
84 * a panic function (see lua_atpanic) and then calls abort, thus
85 * exiting the host application. Your panic function can avoid this
86 * exit by never returning (e.g., doing a long jump to your own
87 * recovery point outside Lua).
88 *
89 * The panic function runs as if it were a message handler (see
90 * §2.3); in particular, the error message is at the top of the
91 * stack. However, there is no guarantee about stack space. To push
92 * anything on the stack, the panic function must first check the
93 * available space (see §4.2).
94 *
95 * We must check all the Lua entry point. This includes:
96 * - The include/proto/hlua.h exported functions
97 * - the task wrapper function
98 * - The action wrapper function
99 * - The converters wrapper function
100 * - The sample-fetch wrapper functions
101 *
102 * It is tolerated that the initilisation function returns an abort.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800103 * Before each Lua abort, an error message is written on stderr.
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200104 *
105 * The macro SET_SAFE_LJMP initialise the longjmp. The Macro
106 * RESET_SAFE_LJMP reset the longjmp. These function must be macro
107 * because they must be exists in the program stack when the longjmp
108 * is called.
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +0200109 *
110 * Note that the Lua processing is not really thread safe. It provides
111 * heavy system which consists to add our own lock function in the Lua
112 * code and recompile the library. This system will probably not accepted
113 * by maintainers of various distribs.
114 *
115 * Our main excution point of the Lua is the function lua_resume(). A
116 * quick looking on the Lua sources displays a lua_lock() a the start
117 * of function and a lua_unlock() at the end of the function. So I
118 * conclude that the Lua thread safe mode just perform a mutex around
119 * all execution. So I prefer to do this in the HAProxy code, it will be
120 * easier for distro maintainers.
121 *
122 * Note that the HAProxy lua functions rounded by the macro SET_SAFE_LJMP
123 * and RESET_SAFE_LJMP manipulates the Lua stack, so it will be careful
124 * to set mutex around these functions.
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200125 */
Willy Tarreau86abe442018-11-25 20:12:18 +0100126__decl_spinlock(hlua_global_lock);
Thierry FOURNIERffbad792017-07-12 11:39:04 +0200127THREAD_LOCAL jmp_buf safe_ljmp_env;
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200128static int hlua_panic_safe(lua_State *L) { return 0; }
129static int hlua_panic_ljmp(lua_State *L) { longjmp(safe_ljmp_env, 1); }
130
131#define SET_SAFE_LJMP(__L) \
132 ({ \
133 int ret; \
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100134 HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200135 if (setjmp(safe_ljmp_env) != 0) { \
136 lua_atpanic(__L, hlua_panic_safe); \
137 ret = 0; \
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100138 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200139 } else { \
140 lua_atpanic(__L, hlua_panic_ljmp); \
141 ret = 1; \
142 } \
143 ret; \
144 })
145
146/* If we are the last function catching Lua errors, we
147 * must reset the panic function.
148 */
149#define RESET_SAFE_LJMP(__L) \
150 do { \
151 lua_atpanic(__L, hlua_panic_safe); \
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100152 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200153 } while(0)
154
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200155/* Applet status flags */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200156#define APPLET_DONE 0x01 /* applet processing is done. */
Christopher Faulet18c2e8d2019-03-01 12:02:08 +0100157/* unused: 0x02 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200158#define APPLET_HDR_SENT 0x04 /* Response header sent. */
159#define APPLET_CHUNKED 0x08 /* Use transfer encoding chunked. */
160#define APPLET_LAST_CHK 0x10 /* Last chunk sent. */
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +0100161#define APPLET_HTTP11 0x20 /* Last chunk sent. */
Christopher Faulet56a3d6e2019-02-27 22:06:23 +0100162#define APPLET_RSP_SENT 0x40 /* The response was fully sent */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200163
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100164/* The main Lua execution context. */
165struct hlua gL;
166
Thierry FOURNIERebed6e92016-12-16 11:54:07 +0100167/* This is the memory pool containing struct lua for applets
168 * (including cli).
169 */
Willy Tarreau8ceae722018-11-26 11:58:30 +0100170DECLARE_STATIC_POOL(pool_head_hlua, "hlua", sizeof(struct hlua));
Thierry FOURNIERebed6e92016-12-16 11:54:07 +0100171
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100172/* Used for Socket connection. */
173static struct proxy socket_proxy;
174static struct server socket_tcp;
175#ifdef USE_OPENSSL
176static struct server socket_ssl;
177#endif
178
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +0100179/* List head of the function called at the initialisation time. */
180struct list hlua_init_functions = LIST_HEAD_INIT(hlua_init_functions);
181
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +0100182/* The following variables contains the reference of the different
183 * Lua classes. These references are useful for identify metadata
184 * associated with an object.
185 */
Thierry FOURNIER65f34c62015-02-16 20:11:43 +0100186static int class_txn_ref;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100187static int class_socket_ref;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +0100188static int class_channel_ref;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +0100189static int class_fetches_ref;
Thierry FOURNIER594afe72015-03-10 23:58:30 +0100190static int class_converters_ref;
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100191static int class_http_ref;
Thierry FOURNIER3def3932015-04-07 11:27:54 +0200192static int class_map_ref;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200193static int class_applet_tcp_ref;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200194static int class_applet_http_ref;
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +0100195
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100196/* Global Lua execution timeout. By default Lua, execution linked
Willy Tarreau87b09662015-04-03 00:22:06 +0200197 * with stream (actions, sample-fetches and converters) have a
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100198 * short timeout. Lua linked with tasks doesn't have a timeout
199 * because a task may remain alive during all the haproxy execution.
200 */
201static unsigned int hlua_timeout_session = 4000; /* session timeout. */
202static unsigned int hlua_timeout_task = TICK_ETERNITY; /* task timeout. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200203static unsigned int hlua_timeout_applet = 4000; /* applet timeout. */
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100204
Thierry FOURNIERee9f8022015-03-03 17:37:37 +0100205/* Interrupts the Lua processing each "hlua_nb_instruction" instructions.
206 * it is used for preventing infinite loops.
207 *
208 * I test the scheer with an infinite loop containing one incrementation
209 * and one test. I run this loop between 10 seconds, I raise a ceil of
210 * 710M loops from one interrupt each 9000 instructions, so I fix the value
211 * to one interrupt each 10 000 instructions.
212 *
213 * configured | Number of
214 * instructions | loops executed
215 * between two | in milions
216 * forced yields |
217 * ---------------+---------------
218 * 10 | 160
219 * 500 | 670
220 * 1000 | 680
221 * 5000 | 700
222 * 7000 | 700
223 * 8000 | 700
224 * 9000 | 710 <- ceil
225 * 10000 | 710
226 * 100000 | 710
227 * 1000000 | 710
228 *
229 */
230static unsigned int hlua_nb_instruction = 10000;
231
Willy Tarreau32f61e22015-03-18 17:54:59 +0100232/* Descriptor for the memory allocation state. If limit is not null, it will
233 * be enforced on any memory allocation.
234 */
235struct hlua_mem_allocator {
236 size_t allocated;
237 size_t limit;
238};
239
240static struct hlua_mem_allocator hlua_global_allocator;
241
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200242static const char error_500[] =
Jarno Huuskonen16ad94a2017-01-09 14:17:10 +0200243 "HTTP/1.0 500 Internal Server Error\r\n"
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200244 "Cache-Control: no-cache\r\n"
245 "Connection: close\r\n"
246 "Content-Type: text/html\r\n"
247 "\r\n"
Joseph Herlant7fe15772018-11-15 10:07:32 -0800248 "<html><body><h1>500 Internal Server Error</h1>\nAn internal server error occurred.\n</body></html>\n";
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200249
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100250/* These functions converts types between HAProxy internal args or
251 * sample and LUA types. Another function permits to check if the
252 * LUA stack contains arguments according with an required ARG_T
253 * format.
254 */
255static int hlua_arg2lua(lua_State *L, const struct arg *arg);
256static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100257__LJMP static int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp,
David Carlier0c437f42016-04-27 16:21:56 +0100258 uint64_t mask, struct proxy *p);
Willy Tarreau5eadada2015-03-10 17:28:54 +0100259static int hlua_smp2lua(lua_State *L, struct sample *smp);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100260static int hlua_smp2lua_str(lua_State *L, struct sample *smp);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100261static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp);
262
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200263__LJMP static int hlua_http_get_headers(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg);
264
Thierry FOURNIER23bc3752015-09-11 19:15:43 +0200265#define SEND_ERR(__be, __fmt, __args...) \
266 do { \
267 send_log(__be, LOG_ERR, __fmt, ## __args); \
268 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) \
Christopher Faulet767a84b2017-11-24 16:50:31 +0100269 ha_alert(__fmt, ## __args); \
Thierry FOURNIER23bc3752015-09-11 19:15:43 +0200270 } while (0)
271
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100272/* Used to check an Lua function type in the stack. It creates and
273 * returns a reference of the function. This function throws an
274 * error if the rgument is not a "function".
275 */
276__LJMP unsigned int hlua_checkfunction(lua_State *L, int argno)
277{
278 if (!lua_isfunction(L, argno)) {
Thierry FOURNIERfd1e9552018-02-23 18:41:18 +0100279 const char *msg = lua_pushfstring(L, "function expected, got %s", luaL_typename(L, argno));
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100280 WILL_LJMP(luaL_argerror(L, argno, msg));
281 }
282 lua_pushvalue(L, argno);
283 return luaL_ref(L, LUA_REGISTRYINDEX);
284}
285
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200286/* Return the string that is of the top of the stack. */
287const char *hlua_get_top_error_string(lua_State *L)
288{
289 if (lua_gettop(L) < 1)
290 return "unknown error";
291 if (lua_type(L, -1) != LUA_TSTRING)
292 return "unknown error";
293 return lua_tostring(L, -1);
294}
295
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200296__LJMP static const char *hlua_traceback(lua_State *L)
297{
298 lua_Debug ar;
299 int level = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +0200300 struct buffer *msg = get_trash_chunk();
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200301 int filled = 0;
302
303 while (lua_getstack(L, level++, &ar)) {
304
305 /* Add separator */
306 if (filled)
307 chunk_appendf(msg, ", ");
308 filled = 1;
309
310 /* Fill fields:
311 * 'S': fills in the fields source, short_src, linedefined, lastlinedefined, and what;
312 * 'l': fills in the field currentline;
313 * 'n': fills in the field name and namewhat;
314 * 't': fills in the field istailcall;
315 */
316 lua_getinfo(L, "Slnt", &ar);
317
318 /* Append code localisation */
319 if (ar.currentline > 0)
320 chunk_appendf(msg, "%s:%d ", ar.short_src, ar.currentline);
321 else
322 chunk_appendf(msg, "%s ", ar.short_src);
323
324 /*
325 * Get function name
326 *
327 * if namewhat is no empty, name is defined.
328 * what contains "Lua" for Lua function, "C" for C function,
329 * or "main" for main code.
330 */
331 if (*ar.namewhat != '\0' && ar.name != NULL) /* is there a name from code? */
332 chunk_appendf(msg, "%s '%s'", ar.namewhat, ar.name); /* use it */
333
334 else if (*ar.what == 'm') /* "main", the code is not executed in a function */
335 chunk_appendf(msg, "main chunk");
336
337 else if (*ar.what != 'C') /* for Lua functions, use <file:line> */
338 chunk_appendf(msg, "C function line %d", ar.linedefined);
339
340 else /* nothing left... */
341 chunk_appendf(msg, "?");
342
343
344 /* Display tailed call */
345 if (ar.istailcall)
346 chunk_appendf(msg, " ...");
347 }
348
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200349 return msg->area;
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200350}
351
352
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100353/* This function check the number of arguments available in the
354 * stack. If the number of arguments available is not the same
355 * then <nb> an error is throwed.
356 */
357__LJMP static inline void check_args(lua_State *L, int nb, char *fcn)
358{
359 if (lua_gettop(L) == nb)
360 return;
361 WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb));
362}
363
Mark Lakes22154b42018-01-29 14:38:40 -0800364/* This function pushes an error string prefixed by the file name
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100365 * and the line number where the error is encountered.
366 */
367static int hlua_pusherror(lua_State *L, const char *fmt, ...)
368{
369 va_list argp;
370 va_start(argp, fmt);
371 luaL_where(L, 1);
372 lua_pushvfstring(L, fmt, argp);
373 va_end(argp);
374 lua_concat(L, 2);
375 return 1;
376}
377
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100378/* This functions is used with sample fetch and converters. It
379 * converts the HAProxy configuration argument in a lua stack
380 * values.
381 *
382 * It takes an array of "arg", and each entry of the array is
383 * converted and pushed in the LUA stack.
384 */
385static int hlua_arg2lua(lua_State *L, const struct arg *arg)
386{
387 switch (arg->type) {
388 case ARGT_SINT:
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100389 case ARGT_TIME:
390 case ARGT_SIZE:
Thierry FOURNIERf90838b2015-03-06 13:48:32 +0100391 lua_pushinteger(L, arg->data.sint);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100392 break;
393
394 case ARGT_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200395 lua_pushlstring(L, arg->data.str.area, arg->data.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100396 break;
397
398 case ARGT_IPV4:
399 case ARGT_IPV6:
400 case ARGT_MSK4:
401 case ARGT_MSK6:
402 case ARGT_FE:
403 case ARGT_BE:
404 case ARGT_TAB:
405 case ARGT_SRV:
406 case ARGT_USR:
407 case ARGT_MAP:
408 default:
409 lua_pushnil(L);
410 break;
411 }
412 return 1;
413}
414
415/* This function take one entrie in an LUA stack at the index "ud",
416 * and try to convert it in an HAProxy argument entry. This is useful
417 * with sample fetch wrappers. The input arguments are gived to the
418 * lua wrapper and converted as arg list by thi function.
419 */
420static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg)
421{
422 switch (lua_type(L, ud)) {
423
424 case LUA_TNUMBER:
425 case LUA_TBOOLEAN:
426 arg->type = ARGT_SINT;
427 arg->data.sint = lua_tointeger(L, ud);
428 break;
429
430 case LUA_TSTRING:
431 arg->type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200432 arg->data.str.area = (char *)lua_tolstring(L, ud, (size_t *)&arg->data.str.data);
Tim Duesterhusf371b832019-09-29 23:03:07 +0200433 /* We don't know the actual size of the underlying allocation, so be conservative. */
434 arg->data.str.size = arg->data.str.data;
435 arg->data.str.head = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100436 break;
437
438 case LUA_TUSERDATA:
439 case LUA_TNIL:
440 case LUA_TTABLE:
441 case LUA_TFUNCTION:
442 case LUA_TTHREAD:
443 case LUA_TLIGHTUSERDATA:
444 arg->type = ARGT_SINT;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +0200445 arg->data.sint = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100446 break;
447 }
448 return 1;
449}
450
451/* the following functions are used to convert a struct sample
452 * in Lua type. This useful to convert the return of the
453 * fetchs or converters.
454 */
Willy Tarreau5eadada2015-03-10 17:28:54 +0100455static int hlua_smp2lua(lua_State *L, struct sample *smp)
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100456{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200457 switch (smp->data.type) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100458 case SMP_T_SINT:
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100459 case SMP_T_BOOL:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200460 lua_pushinteger(L, smp->data.u.sint);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100461 break;
462
463 case SMP_T_BIN:
464 case SMP_T_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200465 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100466 break;
467
468 case SMP_T_METH:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200469 switch (smp->data.u.meth.meth) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100470 case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break;
471 case HTTP_METH_GET: lua_pushstring(L, "GET"); break;
472 case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break;
473 case HTTP_METH_POST: lua_pushstring(L, "POST"); break;
474 case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break;
475 case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break;
476 case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break;
477 case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break;
478 case HTTP_METH_OTHER:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200479 lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100480 break;
481 default:
482 lua_pushnil(L);
483 break;
484 }
485 break;
486
487 case SMP_T_IPV4:
488 case SMP_T_IPV6:
489 case SMP_T_ADDR: /* This type is never used to qualify a sample. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200490 if (sample_casts[smp->data.type][SMP_T_STR] &&
491 sample_casts[smp->data.type][SMP_T_STR](smp))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200492 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Willy Tarreau5eadada2015-03-10 17:28:54 +0100493 else
494 lua_pushnil(L);
495 break;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100496 default:
497 lua_pushnil(L);
498 break;
499 }
500 return 1;
501}
502
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100503/* the following functions are used to convert a struct sample
504 * in Lua strings. This is useful to convert the return of the
505 * fetchs or converters.
506 */
507static int hlua_smp2lua_str(lua_State *L, struct sample *smp)
508{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200509 switch (smp->data.type) {
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100510
511 case SMP_T_BIN:
512 case SMP_T_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200513 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100514 break;
515
516 case SMP_T_METH:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200517 switch (smp->data.u.meth.meth) {
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100518 case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break;
519 case HTTP_METH_GET: lua_pushstring(L, "GET"); break;
520 case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break;
521 case HTTP_METH_POST: lua_pushstring(L, "POST"); break;
522 case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break;
523 case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break;
524 case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break;
525 case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break;
526 case HTTP_METH_OTHER:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200527 lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100528 break;
529 default:
530 lua_pushstring(L, "");
531 break;
532 }
533 break;
534
535 case SMP_T_SINT:
536 case SMP_T_BOOL:
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100537 case SMP_T_IPV4:
538 case SMP_T_IPV6:
539 case SMP_T_ADDR: /* This type is never used to qualify a sample. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200540 if (sample_casts[smp->data.type][SMP_T_STR] &&
541 sample_casts[smp->data.type][SMP_T_STR](smp))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200542 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100543 else
544 lua_pushstring(L, "");
545 break;
546 default:
547 lua_pushstring(L, "");
548 break;
549 }
550 return 1;
551}
552
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100553/* the following functions are used to convert an Lua type in a
554 * struct sample. This is useful to provide data from a converter
555 * to the LUA code.
556 */
557static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp)
558{
559 switch (lua_type(L, ud)) {
560
561 case LUA_TNUMBER:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200562 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200563 smp->data.u.sint = lua_tointeger(L, ud);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100564 break;
565
566
567 case LUA_TBOOLEAN:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200568 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200569 smp->data.u.sint = lua_toboolean(L, ud);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100570 break;
571
572 case LUA_TSTRING:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200573 smp->data.type = SMP_T_STR;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100574 smp->flags |= SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200575 smp->data.u.str.area = (char *)lua_tolstring(L, ud, (size_t *)&smp->data.u.str.data);
Tim Duesterhusf371b832019-09-29 23:03:07 +0200576 /* We don't know the actual size of the underlying allocation, so be conservative. */
577 smp->data.u.str.size = smp->data.u.str.data;
578 smp->data.u.str.head = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100579 break;
580
581 case LUA_TUSERDATA:
582 case LUA_TNIL:
583 case LUA_TTABLE:
584 case LUA_TFUNCTION:
585 case LUA_TTHREAD:
586 case LUA_TLIGHTUSERDATA:
Thierry FOURNIER93405e12015-08-26 14:19:03 +0200587 case LUA_TNONE:
588 default:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200589 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200590 smp->data.u.sint = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100591 break;
592 }
593 return 1;
594}
595
596/* This function check the "argp" builded by another conversion function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800597 * is in accord with the expected argp defined by the "mask". The function
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100598 * returns true or false. It can be adjust the types if there compatibles.
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100599 *
600 * This function assumes thant the argp argument contains ARGM_NBARGS + 1
601 * entries.
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100602 */
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100603__LJMP int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp,
David Carlier0c437f42016-04-27 16:21:56 +0100604 uint64_t mask, struct proxy *p)
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100605{
606 int min_arg;
607 int idx;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100608 struct proxy *px;
609 char *sname, *pname;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100610
611 idx = 0;
612 min_arg = ARGM(mask);
613 mask >>= ARGM_BITS;
614
615 while (1) {
616
617 /* Check oversize. */
618 if (idx >= ARGM_NBARGS && argp[idx].type != ARGT_STOP) {
Cyril Bonté577a36a2015-03-02 00:08:38 +0100619 WILL_LJMP(luaL_argerror(L, first + idx, "Malformed argument mask"));
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100620 }
621
622 /* Check for mandatory arguments. */
623 if (argp[idx].type == ARGT_STOP) {
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100624 if (idx < min_arg) {
625
626 /* If miss other argument than the first one, we return an error. */
627 if (idx > 0)
628 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
629
630 /* If first argument have a certain type, some default values
631 * may be used. See the function smp_resolve_args().
632 */
633 switch (mask & ARGT_MASK) {
634
635 case ARGT_FE:
636 if (!(p->cap & PR_CAP_FE))
637 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
638 argp[idx].data.prx = p;
639 argp[idx].type = ARGT_FE;
640 argp[idx+1].type = ARGT_STOP;
641 break;
642
643 case ARGT_BE:
644 if (!(p->cap & PR_CAP_BE))
645 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
646 argp[idx].data.prx = p;
647 argp[idx].type = ARGT_BE;
648 argp[idx+1].type = ARGT_STOP;
649 break;
650
651 case ARGT_TAB:
652 argp[idx].data.prx = p;
653 argp[idx].type = ARGT_TAB;
654 argp[idx+1].type = ARGT_STOP;
655 break;
656
657 default:
658 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
659 break;
660 }
661 }
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100662 return 0;
663 }
664
665 /* Check for exceed the number of requiered argument. */
666 if ((mask & ARGT_MASK) == ARGT_STOP &&
667 argp[idx].type != ARGT_STOP) {
668 WILL_LJMP(luaL_argerror(L, first + idx, "Last argument expected"));
669 }
670
671 if ((mask & ARGT_MASK) == ARGT_STOP &&
672 argp[idx].type == ARGT_STOP) {
673 return 0;
674 }
675
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100676 /* Convert some argument types. */
677 switch (mask & ARGT_MASK) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100678 case ARGT_SINT:
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100679 if (argp[idx].type != ARGT_SINT)
680 WILL_LJMP(luaL_argerror(L, first + idx, "integer expected"));
681 argp[idx].type = ARGT_SINT;
682 break;
683
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100684 case ARGT_TIME:
685 if (argp[idx].type != ARGT_SINT)
686 WILL_LJMP(luaL_argerror(L, first + idx, "integer expected"));
Thierry FOURNIER29176f32015-07-07 00:41:29 +0200687 argp[idx].type = ARGT_TIME;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100688 break;
689
690 case ARGT_SIZE:
691 if (argp[idx].type != ARGT_SINT)
692 WILL_LJMP(luaL_argerror(L, first + idx, "integer expected"));
Thierry FOURNIER29176f32015-07-07 00:41:29 +0200693 argp[idx].type = ARGT_SIZE;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100694 break;
695
696 case ARGT_FE:
697 if (argp[idx].type != ARGT_STR)
698 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200699 memcpy(trash.area, argp[idx].data.str.area,
700 argp[idx].data.str.data);
701 trash.area[argp[idx].data.str.data] = 0;
702 argp[idx].data.prx = proxy_fe_by_name(trash.area);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100703 if (!argp[idx].data.prx)
704 WILL_LJMP(luaL_argerror(L, first + idx, "frontend doesn't exist"));
705 argp[idx].type = ARGT_FE;
706 break;
707
708 case ARGT_BE:
709 if (argp[idx].type != ARGT_STR)
710 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200711 memcpy(trash.area, argp[idx].data.str.area,
712 argp[idx].data.str.data);
713 trash.area[argp[idx].data.str.data] = 0;
714 argp[idx].data.prx = proxy_be_by_name(trash.area);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100715 if (!argp[idx].data.prx)
716 WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist"));
717 argp[idx].type = ARGT_BE;
718 break;
719
720 case ARGT_TAB:
721 if (argp[idx].type != ARGT_STR)
722 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200723 memcpy(trash.area, argp[idx].data.str.area,
724 argp[idx].data.str.data);
725 trash.area[argp[idx].data.str.data] = 0;
Tim Duesterhuse351e762019-09-29 23:03:09 +0200726 argp[idx].data.t = stktable_find_by_name(trash.area);
727 if (!argp[idx].data.t)
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100728 WILL_LJMP(luaL_argerror(L, first + idx, "table doesn't exist"));
729 argp[idx].type = ARGT_TAB;
730 break;
731
732 case ARGT_SRV:
733 if (argp[idx].type != ARGT_STR)
734 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200735 memcpy(trash.area, argp[idx].data.str.area,
736 argp[idx].data.str.data);
737 trash.area[argp[idx].data.str.data] = 0;
738 sname = strrchr(trash.area, '/');
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100739 if (sname) {
740 *sname++ = '\0';
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200741 pname = trash.area;
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200742 px = proxy_be_by_name(pname);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100743 if (!px)
744 WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist"));
745 }
746 else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200747 sname = trash.area;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100748 px = p;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100749 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100750 argp[idx].data.srv = findserver(px, sname);
751 if (!argp[idx].data.srv)
752 WILL_LJMP(luaL_argerror(L, first + idx, "server doesn't exist"));
753 argp[idx].type = ARGT_SRV;
754 break;
755
756 case ARGT_IPV4:
Christopher Faulete7f1fcf2020-08-07 09:07:26 +0200757 if (argp[idx].type != ARGT_STR)
758 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200759 memcpy(trash.area, argp[idx].data.str.area,
760 argp[idx].data.str.data);
761 trash.area[argp[idx].data.str.data] = 0;
762 if (inet_pton(AF_INET, trash.area, &argp[idx].data.ipv4))
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100763 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 address"));
764 argp[idx].type = ARGT_IPV4;
765 break;
766
767 case ARGT_MSK4:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200768 memcpy(trash.area, argp[idx].data.str.area,
769 argp[idx].data.str.data);
770 trash.area[argp[idx].data.str.data] = 0;
771 if (!str2mask(trash.area, &argp[idx].data.ipv4))
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100772 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 mask"));
773 argp[idx].type = ARGT_MSK4;
774 break;
775
776 case ARGT_IPV6:
Christopher Faulete7f1fcf2020-08-07 09:07:26 +0200777 if (argp[idx].type != ARGT_STR)
778 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200779 memcpy(trash.area, argp[idx].data.str.area,
780 argp[idx].data.str.data);
781 trash.area[argp[idx].data.str.data] = 0;
782 if (inet_pton(AF_INET6, trash.area, &argp[idx].data.ipv6))
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100783 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 address"));
784 argp[idx].type = ARGT_IPV6;
785 break;
786
787 case ARGT_MSK6:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200788 memcpy(trash.area, argp[idx].data.str.area,
789 argp[idx].data.str.data);
790 trash.area[argp[idx].data.str.data] = 0;
791 if (!str2mask6(trash.area, &argp[idx].data.ipv6))
Tim Duesterhusb814da62018-01-25 16:24:50 +0100792 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 mask"));
793 argp[idx].type = ARGT_MSK6;
794 break;
795
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100796 case ARGT_MAP:
797 case ARGT_REG:
798 case ARGT_USR:
799 WILL_LJMP(luaL_argerror(L, first + idx, "type not yet supported"));
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100800 break;
801 }
802
803 /* Check for type of argument. */
804 if ((mask & ARGT_MASK) != argp[idx].type) {
805 const char *msg = lua_pushfstring(L, "'%s' expected, got '%s'",
806 arg_type_names[(mask & ARGT_MASK)],
807 arg_type_names[argp[idx].type & ARGT_MASK]);
808 WILL_LJMP(luaL_argerror(L, first + idx, msg));
809 }
810
811 /* Next argument. */
812 mask >>= ARGT_BITS;
813 idx++;
814 }
815}
816
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100817/*
818 * The following functions are used to make correspondance between the the
819 * executed lua pointer and the "struct hlua *" that contain the context.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100820 *
821 * - hlua_gethlua : return the hlua context associated with an lua_State.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100822 * - hlua_sethlua : create the association between hlua context and lua_state.
823 */
824static inline struct hlua *hlua_gethlua(lua_State *L)
825{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +0100826 struct hlua **hlua = lua_getextraspace(L);
827 return *hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100828}
829static inline void hlua_sethlua(struct hlua *hlua)
830{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +0100831 struct hlua **hlua_store = lua_getextraspace(hlua->T);
832 *hlua_store = hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100833}
834
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100835/* This function is used to send logs. It try to send on screen (stderr)
836 * and on the default syslog server.
837 */
838static inline void hlua_sendlog(struct proxy *px, int level, const char *msg)
839{
840 struct tm tm;
841 char *p;
842
843 /* Cleanup the log message. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200844 p = trash.area;
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100845 for (; *msg != '\0'; msg++, p++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200846 if (p >= trash.area + trash.size - 1) {
Thierry FOURNIERccf00632015-09-16 12:47:03 +0200847 /* Break the message if exceed the buffer size. */
848 *(p-4) = ' ';
849 *(p-3) = '.';
850 *(p-2) = '.';
851 *(p-1) = '.';
852 break;
853 }
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100854 if (isprint(*msg))
855 *p = *msg;
856 else
857 *p = '.';
858 }
859 *p = '\0';
860
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200861 send_log(px, level, "%s\n", trash.area);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100862 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
Willy Tarreaua678b432015-08-28 10:14:59 +0200863 get_localtime(date.tv_sec, &tm);
864 fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n",
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100865 log_levels[level], tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec,
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200866 (int)getpid(), trash.area);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100867 fflush(stderr);
868 }
869}
870
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100871/* This function just ensure that the yield will be always
872 * returned with a timeout and permit to set some flags
873 */
874__LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx,
Thierry FOURNIERf90838b2015-03-06 13:48:32 +0100875 lua_KFunction k, int timeout, unsigned int flags)
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100876{
877 struct hlua *hlua = hlua_gethlua(L);
878
879 /* Set the wake timeout. If timeout is required, we set
880 * the expiration time.
881 */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +0200882 hlua->wake_time = timeout;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100883
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +0100884 hlua->flags |= flags;
885
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100886 /* Process the yield. */
Willy Tarreau9635e032018-10-16 17:52:55 +0200887 MAY_LJMP(lua_yieldk(L, nresults, ctx, k));
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100888}
889
Willy Tarreau87b09662015-04-03 00:22:06 +0200890/* This function initialises the Lua environment stored in the stream.
891 * It must be called at the start of the stream. This function creates
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100892 * an LUA coroutine. It can not be use to crete the main LUA context.
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200893 *
894 * This function is particular. it initialises a new Lua thread. If the
895 * initialisation fails (example: out of memory error), the lua function
896 * throws an error (longjmp).
897 *
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100898 * In some case (at least one), this function can be called from safe
899 * environement, so we must not initialise it. While the support of
900 * threads appear, the safe environment set a lock to ensure only one
901 * Lua execution at a time. If we initialize safe environment in another
902 * safe environmenet, we have a dead lock.
903 *
904 * set "already_safe" true if the context is initialized form safe
905 * Lua fonction.
906 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800907 * This function manipulates two Lua stacks: the main and the thread. Only
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200908 * the main stack can fail. The thread is not manipulated. This function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800909 * MUST NOT manipulate the created thread stack state, because it is not
910 * proctected against errors thrown by the thread stack.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100911 */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100912int hlua_ctx_init(struct hlua *lua, struct task *task, int already_safe)
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100913{
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100914 if (!already_safe) {
915 if (!SET_SAFE_LJMP(gL.T)) {
916 lua->Tref = LUA_REFNIL;
917 return 0;
918 }
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200919 }
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100920 lua->Mref = LUA_REFNIL;
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +0100921 lua->flags = 0;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100922 LIST_INIT(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100923 lua->T = lua_newthread(gL.T);
924 if (!lua->T) {
925 lua->Tref = LUA_REFNIL;
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100926 if (!already_safe)
927 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100928 return 0;
929 }
930 hlua_sethlua(lua);
931 lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
932 lua->task = task;
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100933 if (!already_safe)
934 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100935 return 1;
936}
937
Willy Tarreau87b09662015-04-03 00:22:06 +0200938/* Used to destroy the Lua coroutine when the attached stream or task
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100939 * is destroyed. The destroy also the memory context. The struct "lua"
940 * is not freed.
941 */
942void hlua_ctx_destroy(struct hlua *lua)
943{
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100944 if (!lua)
Thierry FOURNIERa718b292015-03-04 16:48:34 +0100945 return;
946
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100947 if (!lua->T)
948 goto end;
949
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100950 /* Purge all the pending signals. */
Thierry FOURNIERd6975962017-07-12 14:31:10 +0200951 notification_purge(&lua->com);
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100952
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200953 if (!SET_SAFE_LJMP(lua->T))
954 return;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100955 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200956 RESET_SAFE_LJMP(lua->T);
Thierry FOURNIER5a50a852015-09-23 16:59:28 +0200957
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200958 if (!SET_SAFE_LJMP(gL.T))
959 return;
960 luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref);
961 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER5a50a852015-09-23 16:59:28 +0200962 /* Forces a garbage collecting process. If the Lua program is finished
963 * without error, we run the GC on the thread pointer. Its freed all
964 * the unused memory.
965 * If the thread is finnish with an error or is currently yielded,
966 * it seems that the GC applied on the thread doesn't clean anything,
967 * so e run the GC on the main thread.
968 * NOTE: maybe this action locks all the Lua threads untiml the en of
969 * the garbage collection.
970 */
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +0200971 if (lua->flags & HLUA_MUST_GC) {
Thierry FOURNIER7bd10d52017-07-17 00:44:40 +0200972 if (!SET_SAFE_LJMP(gL.T))
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200973 return;
Thierry FOURNIER7bd10d52017-07-17 00:44:40 +0200974 lua_gc(gL.T, LUA_GCCOLLECT, 0);
975 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +0200976 }
Thierry FOURNIER5a50a852015-09-23 16:59:28 +0200977
Thierry FOURNIERa7b536b2015-09-21 22:50:24 +0200978 lua->T = NULL;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100979
980end:
Willy Tarreaubafbe012017-11-24 17:34:44 +0100981 pool_free(pool_head_hlua, lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100982}
983
984/* This function is used to restore the Lua context when a coroutine
985 * fails. This function copy the common memory between old coroutine
986 * and the new coroutine. The old coroutine is destroyed, and its
987 * replaced by the new coroutine.
988 * If the flag "keep_msg" is set, the last entry of the old is assumed
989 * as string error message and it is copied in the new stack.
990 */
991static int hlua_ctx_renew(struct hlua *lua, int keep_msg)
992{
993 lua_State *T;
994 int new_ref;
995
996 /* Renew the main LUA stack doesn't have sense. */
997 if (lua == &gL)
998 return 0;
999
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001000 /* New Lua coroutine. */
1001 T = lua_newthread(gL.T);
1002 if (!T)
1003 return 0;
1004
1005 /* Copy last error message. */
1006 if (keep_msg)
1007 lua_xmove(lua->T, T, 1);
1008
1009 /* Copy data between the coroutines. */
1010 lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref);
1011 lua_xmove(lua->T, T, 1);
1012 new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Valur poped. */
1013
1014 /* Destroy old data. */
1015 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
1016
1017 /* The thread is garbage collected by Lua. */
1018 luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref);
1019
1020 /* Fill the struct with the new coroutine values. */
1021 lua->Mref = new_ref;
1022 lua->T = T;
1023 lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
1024
1025 /* Set context. */
1026 hlua_sethlua(lua);
1027
1028 return 1;
1029}
1030
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001031void hlua_hook(lua_State *L, lua_Debug *ar)
1032{
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001033 struct hlua *hlua = hlua_gethlua(L);
1034
1035 /* Lua cannot yield when its returning from a function,
1036 * so, we can fix the interrupt hook to 1 instruction,
1037 * expecting that the function is finnished.
1038 */
1039 if (lua_gethookmask(L) & LUA_MASKRET) {
1040 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1);
1041 return;
1042 }
1043
1044 /* restore the interrupt condition. */
1045 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
1046
1047 /* If we interrupt the Lua processing in yieldable state, we yield.
1048 * If the state is not yieldable, trying yield causes an error.
1049 */
1050 if (lua_isyieldable(L))
Willy Tarreau9635e032018-10-16 17:52:55 +02001051 MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD));
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001052
Thierry FOURNIERa85cfb12015-03-13 14:50:06 +01001053 /* If we cannot yield, update the clock and check the timeout. */
1054 tv_update_date(0, 1);
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001055 hlua->run_time += now_ms - hlua->start_time;
1056 if (hlua->max_time && hlua->run_time >= hlua->max_time) {
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001057 lua_pushfstring(L, "execution timeout");
1058 WILL_LJMP(lua_error(L));
1059 }
1060
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001061 /* Update the start time. */
1062 hlua->start_time = now_ms;
1063
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001064 /* Try to interrupt the process at the end of the current
1065 * unyieldable function.
1066 */
1067 lua_sethook(hlua->T, hlua_hook, LUA_MASKRET|LUA_MASKCOUNT, hlua_nb_instruction);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001068}
1069
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001070/* This function start or resumes the Lua stack execution. If the flag
1071 * "yield_allowed" if no set and the LUA stack execution returns a yield
1072 * The function return an error.
1073 *
1074 * The function can returns 4 values:
1075 * - HLUA_E_OK : The execution is terminated without any errors.
1076 * - HLUA_E_AGAIN : The execution must continue at the next associated
1077 * task wakeup.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001078 * - HLUA_E_ERRMSG : An error has occurred, an error message is set in
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001079 * the top of the stack.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001080 * - HLUA_E_ERR : An error has occurred without error message.
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001081 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001082 * If an error occurred, the stack is renewed and it is ready to run new
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001083 * LUA code.
1084 */
1085static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed)
1086{
Christopher Faulet20699902020-07-28 10:33:25 +02001087#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504
1088 int nres;
1089#endif
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001090 int ret;
1091 const char *msg;
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001092 const char *trace;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001093
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001094 /* Initialise run time counter. */
1095 if (!HLUA_IS_RUNNING(lua))
1096 lua->run_time = 0;
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001097
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001098 /* Lock the whole Lua execution. This lock must be before the
1099 * label "resume_execution".
1100 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001101 HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock);
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001102
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001103resume_execution:
1104
1105 /* This hook interrupts the Lua processing each 'hlua_nb_instruction'
1106 * instructions. it is used for preventing infinite loops.
1107 */
1108 lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
1109
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +01001110 /* Remove all flags except the running flags. */
Thierry FOURNIER2f3867f2015-09-28 01:02:01 +02001111 HLUA_SET_RUN(lua);
1112 HLUA_CLR_CTRLYIELD(lua);
1113 HLUA_CLR_WAKERESWR(lua);
1114 HLUA_CLR_WAKEREQWR(lua);
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +01001115
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001116 /* Update the start time. */
1117 lua->start_time = now_ms;
1118
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001119 /* Call the function. */
Christopher Faulet20699902020-07-28 10:33:25 +02001120#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504
1121 ret = lua_resume(lua->T, gL.T, lua->nargs, &nres);
1122#else
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001123 ret = lua_resume(lua->T, gL.T, lua->nargs);
Christopher Faulet20699902020-07-28 10:33:25 +02001124#endif
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001125 switch (ret) {
1126
1127 case LUA_OK:
1128 ret = HLUA_E_OK;
1129 break;
1130
1131 case LUA_YIELD:
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001132 /* Check if the execution timeout is expired. It it is the case, we
1133 * break the Lua execution.
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001134 */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001135 tv_update_date(0, 1);
1136 lua->run_time += now_ms - lua->start_time;
1137 if (lua->max_time && lua->run_time > lua->max_time) {
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001138 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001139 ret = HLUA_E_ETMOUT;
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001140 break;
1141 }
1142 /* Process the forced yield. if the general yield is not allowed or
1143 * if no task were associated this the current Lua execution
1144 * coroutine, we resume the execution. Else we want to return in the
1145 * scheduler and we want to be waked up again, to continue the
1146 * current Lua execution. So we schedule our own task.
1147 */
1148 if (HLUA_IS_CTRLYIELDING(lua)) {
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001149 if (!yield_allowed || !lua->task)
1150 goto resume_execution;
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001151 task_wakeup(lua->task, TASK_WOKEN_MSG);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001152 }
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001153 if (!yield_allowed) {
1154 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001155 ret = HLUA_E_YIELD;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001156 break;
1157 }
1158 ret = HLUA_E_AGAIN;
1159 break;
1160
1161 case LUA_ERRRUN:
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001162
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001163 /* Special exit case. The traditional exit is returned as an error
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001164 * because the errors ares the only one mean to return immediately
1165 * from and lua execution.
1166 */
1167 if (lua->flags & HLUA_EXIT) {
1168 ret = HLUA_E_OK;
Thierry FOURNIERe1587b32015-08-28 09:54:13 +02001169 hlua_ctx_renew(lua, 0);
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001170 break;
1171 }
1172
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001173 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001174 if (!lua_checkstack(lua->T, 1)) {
1175 ret = HLUA_E_ERR;
1176 break;
1177 }
1178 msg = lua_tostring(lua->T, -1);
1179 lua_settop(lua->T, 0); /* Empty the stack. */
1180 lua_pop(lua->T, 1);
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001181 trace = hlua_traceback(lua->T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001182 if (msg)
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001183 lua_pushfstring(lua->T, "runtime error: %s from %s", msg, trace);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001184 else
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001185 lua_pushfstring(lua->T, "unknown runtime error from %s", trace);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001186 ret = HLUA_E_ERRMSG;
1187 break;
1188
1189 case LUA_ERRMEM:
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_NOMEM;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001193 break;
1194
1195 case LUA_ERRERR:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001196 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001197 if (!lua_checkstack(lua->T, 1)) {
1198 ret = HLUA_E_ERR;
1199 break;
1200 }
1201 msg = lua_tostring(lua->T, -1);
1202 lua_settop(lua->T, 0); /* Empty the stack. */
1203 lua_pop(lua->T, 1);
1204 if (msg)
1205 lua_pushfstring(lua->T, "message handler error: %s", msg);
1206 else
1207 lua_pushfstring(lua->T, "message handler error");
1208 ret = HLUA_E_ERRMSG;
1209 break;
1210
1211 default:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001212 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001213 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001214 ret = HLUA_E_ERR;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001215 break;
1216 }
1217
Thierry FOURNIER6ab4d8e2015-09-27 22:17:19 +02001218 /* This GC permits to destroy some object when a Lua timeout strikes. */
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +02001219 if (lua->flags & HLUA_MUST_GC &&
1220 ret != HLUA_E_AGAIN)
Thierry FOURNIER6ab4d8e2015-09-27 22:17:19 +02001221 lua_gc(lua->T, LUA_GCCOLLECT, 0);
1222
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001223 switch (ret) {
1224 case HLUA_E_AGAIN:
1225 break;
1226
1227 case HLUA_E_ERRMSG:
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001228 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001229 hlua_ctx_renew(lua, 1);
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001230 HLUA_CLR_RUN(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001231 break;
1232
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001233 case HLUA_E_ETMOUT:
1234 case HLUA_E_NOMEM:
1235 case HLUA_E_YIELD:
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001236 case HLUA_E_ERR:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001237 HLUA_CLR_RUN(lua);
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001238 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001239 hlua_ctx_renew(lua, 0);
1240 break;
1241
1242 case HLUA_E_OK:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001243 HLUA_CLR_RUN(lua);
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001244 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001245 break;
1246 }
1247
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001248 /* This is the main exit point, remove the Lua lock. */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001249 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock);
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001250
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001251 return ret;
1252}
1253
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001254/* This function exit the current code. */
1255__LJMP static int hlua_done(lua_State *L)
1256{
1257 struct hlua *hlua = hlua_gethlua(L);
1258
1259 hlua->flags |= HLUA_EXIT;
1260 WILL_LJMP(lua_error(L));
1261
1262 return 0;
1263}
1264
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001265/* This function is an LUA binding. It provides a function
1266 * for deleting ACL from a referenced ACL file.
1267 */
1268__LJMP static int hlua_del_acl(lua_State *L)
1269{
1270 const char *name;
1271 const char *key;
1272 struct pat_ref *ref;
1273
1274 MAY_LJMP(check_args(L, 2, "del_acl"));
1275
1276 name = MAY_LJMP(luaL_checkstring(L, 1));
1277 key = MAY_LJMP(luaL_checkstring(L, 2));
1278
1279 ref = pat_ref_lookup(name);
1280 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001281 WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001282
Christopher Faulet9fa1f4e2020-06-03 18:39:16 +02001283 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001284 pat_ref_delete(ref, key);
Christopher Faulet9fa1f4e2020-06-03 18:39:16 +02001285 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001286 return 0;
1287}
1288
1289/* This function is an LUA binding. It provides a function
1290 * for deleting map entry from a referenced map file.
1291 */
1292static int hlua_del_map(lua_State *L)
1293{
1294 const char *name;
1295 const char *key;
1296 struct pat_ref *ref;
1297
1298 MAY_LJMP(check_args(L, 2, "del_map"));
1299
1300 name = MAY_LJMP(luaL_checkstring(L, 1));
1301 key = MAY_LJMP(luaL_checkstring(L, 2));
1302
1303 ref = pat_ref_lookup(name);
1304 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001305 WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001306
Christopher Faulet9fa1f4e2020-06-03 18:39:16 +02001307 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001308 pat_ref_delete(ref, key);
Christopher Faulet9fa1f4e2020-06-03 18:39:16 +02001309 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001310 return 0;
1311}
1312
1313/* This function is an LUA binding. It provides a function
1314 * for adding ACL pattern from a referenced ACL file.
1315 */
1316static int hlua_add_acl(lua_State *L)
1317{
1318 const char *name;
1319 const char *key;
1320 struct pat_ref *ref;
1321
1322 MAY_LJMP(check_args(L, 2, "add_acl"));
1323
1324 name = MAY_LJMP(luaL_checkstring(L, 1));
1325 key = MAY_LJMP(luaL_checkstring(L, 2));
1326
1327 ref = pat_ref_lookup(name);
1328 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001329 WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001330
Christopher Faulet9fa1f4e2020-06-03 18:39:16 +02001331 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001332 if (pat_ref_find_elt(ref, key) == NULL)
1333 pat_ref_add(ref, key, NULL, NULL);
Christopher Faulet9fa1f4e2020-06-03 18:39:16 +02001334 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001335 return 0;
1336}
1337
1338/* This function is an LUA binding. It provides a function
1339 * for setting map pattern and sample from a referenced map
1340 * file.
1341 */
1342static int hlua_set_map(lua_State *L)
1343{
1344 const char *name;
1345 const char *key;
1346 const char *value;
1347 struct pat_ref *ref;
1348
1349 MAY_LJMP(check_args(L, 3, "set_map"));
1350
1351 name = MAY_LJMP(luaL_checkstring(L, 1));
1352 key = MAY_LJMP(luaL_checkstring(L, 2));
1353 value = MAY_LJMP(luaL_checkstring(L, 3));
1354
1355 ref = pat_ref_lookup(name);
1356 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001357 WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001358
Christopher Faulet9fa1f4e2020-06-03 18:39:16 +02001359 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001360 if (pat_ref_find_elt(ref, key) != NULL)
1361 pat_ref_set(ref, key, value, NULL);
1362 else
1363 pat_ref_add(ref, key, value, NULL);
Christopher Faulet9fa1f4e2020-06-03 18:39:16 +02001364 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001365 return 0;
1366}
1367
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001368/* A class is a lot of memory that contain data. This data can be a table,
1369 * an integer or user data. This data is associated with a metatable. This
1370 * metatable have an original version registred in the global context with
1371 * the name of the object (_G[<name>] = <metable> ).
1372 *
1373 * A metable is a table that modify the standard behavior of a standard
1374 * access to the associated data. The entries of this new metatable are
1375 * defined as is:
1376 *
1377 * http://lua-users.org/wiki/MetatableEvents
1378 *
1379 * __index
1380 *
1381 * we access an absent field in a table, the result is nil. This is
1382 * true, but it is not the whole truth. Actually, such access triggers
1383 * the interpreter to look for an __index metamethod: If there is no
1384 * such method, as usually happens, then the access results in nil;
1385 * otherwise, the metamethod will provide the result.
1386 *
1387 * Control 'prototype' inheritance. When accessing "myTable[key]" and
1388 * the key does not appear in the table, but the metatable has an __index
1389 * property:
1390 *
1391 * - if the value is a function, the function is called, passing in the
1392 * table and the key; the return value of that function is returned as
1393 * the result.
1394 *
1395 * - if the value is another table, the value of the key in that table is
1396 * asked for and returned (and if it doesn't exist in that table, but that
1397 * table's metatable has an __index property, then it continues on up)
1398 *
1399 * - Use "rawget(myTable,key)" to skip this metamethod.
1400 *
1401 * http://www.lua.org/pil/13.4.1.html
1402 *
1403 * __newindex
1404 *
1405 * Like __index, but control property assignment.
1406 *
1407 * __mode - Control weak references. A string value with one or both
1408 * of the characters 'k' and 'v' which specifies that the the
1409 * keys and/or values in the table are weak references.
1410 *
1411 * __call - Treat a table like a function. When a table is followed by
1412 * parenthesis such as "myTable( 'foo' )" and the metatable has
1413 * a __call key pointing to a function, that function is invoked
1414 * (passing any specified arguments) and the return value is
1415 * returned.
1416 *
1417 * __metatable - Hide the metatable. When "getmetatable( myTable )" is
1418 * called, if the metatable for myTable has a __metatable
1419 * key, the value of that key is returned instead of the
1420 * actual metatable.
1421 *
1422 * __tostring - Control string representation. When the builtin
1423 * "tostring( myTable )" function is called, if the metatable
1424 * for myTable has a __tostring property set to a function,
1425 * that function is invoked (passing myTable to it) and the
1426 * return value is used as the string representation.
1427 *
1428 * __len - Control table length. When the table length is requested using
1429 * the length operator ( '#' ), if the metatable for myTable has
1430 * a __len key pointing to a function, that function is invoked
1431 * (passing myTable to it) and the return value used as the value
1432 * of "#myTable".
1433 *
1434 * __gc - Userdata finalizer code. When userdata is set to be garbage
1435 * collected, if the metatable has a __gc field pointing to a
1436 * function, that function is first invoked, passing the userdata
1437 * to it. The __gc metamethod is not called for tables.
1438 * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html)
1439 *
1440 * Special metamethods for redefining standard operators:
1441 * http://www.lua.org/pil/13.1.html
1442 *
1443 * __add "+"
1444 * __sub "-"
1445 * __mul "*"
1446 * __div "/"
1447 * __unm "!"
1448 * __pow "^"
1449 * __concat ".."
1450 *
1451 * Special methods for redfining standar relations
1452 * http://www.lua.org/pil/13.2.html
1453 *
1454 * __eq "=="
1455 * __lt "<"
1456 * __le "<="
1457 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001458
1459/*
1460 *
1461 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001462 * Class Map
1463 *
1464 *
1465 */
1466
1467/* Returns a struct hlua_map if the stack entry "ud" is
1468 * a class session, otherwise it throws an error.
1469 */
1470__LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud)
1471{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001472 return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001473}
1474
1475/* This function is the map constructor. It don't need
1476 * the class Map object. It creates and return a new Map
1477 * object. It must be called only during "body" or "init"
1478 * context because it process some filesystem accesses.
1479 */
1480__LJMP static int hlua_map_new(struct lua_State *L)
1481{
1482 const char *fn;
1483 int match = PAT_MATCH_STR;
1484 struct sample_conv conv;
1485 const char *file = "";
1486 int line = 0;
1487 lua_Debug ar;
1488 char *err = NULL;
1489 struct arg args[2];
1490
1491 if (lua_gettop(L) < 1 || lua_gettop(L) > 2)
1492 WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument."));
1493
1494 fn = MAY_LJMP(luaL_checkstring(L, 1));
1495
1496 if (lua_gettop(L) >= 2) {
1497 match = MAY_LJMP(luaL_checkinteger(L, 2));
1498 if (match < 0 || match >= PAT_MATCH_NUM)
1499 WILL_LJMP(luaL_error(L, "'new' needs a valid match method."));
1500 }
1501
1502 /* Get Lua filename and line number. */
1503 if (lua_getstack(L, 1, &ar)) { /* check function at level */
1504 lua_getinfo(L, "Sl", &ar); /* get info about it */
1505 if (ar.currentline > 0) { /* is there info? */
1506 file = ar.short_src;
1507 line = ar.currentline;
1508 }
1509 }
1510
1511 /* fill fake sample_conv struct. */
1512 conv.kw = ""; /* unused. */
1513 conv.process = NULL; /* unused. */
1514 conv.arg_mask = 0; /* unused. */
1515 conv.val_args = NULL; /* unused. */
1516 conv.out_type = SMP_T_STR;
1517 conv.private = (void *)(long)match;
1518 switch (match) {
1519 case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break;
1520 case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break;
1521 case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break;
1522 case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break;
1523 case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break;
1524 case PAT_MATCH_END: conv.in_type = SMP_T_STR; break;
1525 case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001526 case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001527 case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break;
1528 default:
1529 WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode."));
1530 }
1531
1532 /* fill fake args. */
1533 args[0].type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001534 args[0].data.str.area = (char *)fn;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001535 args[1].type = ARGT_STOP;
1536
1537 /* load the map. */
1538 if (!sample_load_map(args, &conv, file, line, &err)) {
1539 /* error case: we cant use luaL_error because we must
1540 * free the err variable.
1541 */
1542 luaL_where(L, 1);
1543 lua_pushfstring(L, "'new': %s.", err);
1544 lua_concat(L, 2);
1545 free(err);
1546 WILL_LJMP(lua_error(L));
1547 }
1548
1549 /* create the lua object. */
1550 lua_newtable(L);
1551 lua_pushlightuserdata(L, args[0].data.map);
1552 lua_rawseti(L, -2, 0);
1553
1554 /* Pop a class Map metatable and affect it to the userdata. */
1555 lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref);
1556 lua_setmetatable(L, -2);
1557
1558
1559 return 1;
1560}
1561
1562__LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str)
1563{
1564 struct map_descriptor *desc;
1565 struct pattern *pat;
1566 struct sample smp;
1567
1568 MAY_LJMP(check_args(L, 2, "lookup"));
1569 desc = MAY_LJMP(hlua_checkmap(L, 1));
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001570 if (desc->pat.expect_type == SMP_T_SINT) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001571 smp.data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001572 smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001573 }
1574 else {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001575 smp.data.type = SMP_T_STR;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001576 smp.flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001577 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 +02001578 }
1579
1580 pat = pattern_exec_match(&desc->pat, &smp, 1);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001581 if (!pat || !pat->data) {
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001582 if (str)
1583 lua_pushstring(L, "");
1584 else
1585 lua_pushnil(L);
1586 return 1;
1587 }
1588
1589 /* The Lua pattern must return a string, so we can't check the returned type */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001590 lua_pushlstring(L, pat->data->u.str.area, pat->data->u.str.data);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001591 return 1;
1592}
1593
1594__LJMP static int hlua_map_lookup(struct lua_State *L)
1595{
1596 return _hlua_map_lookup(L, 0);
1597}
1598
1599__LJMP static int hlua_map_slookup(struct lua_State *L)
1600{
1601 return _hlua_map_lookup(L, 1);
1602}
1603
1604/*
1605 *
1606 *
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001607 * Class Socket
1608 *
1609 *
1610 */
1611
1612__LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud)
1613{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001614 return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001615}
1616
1617/* This function is the handler called for each I/O on the established
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001618 * connection. It is used for notify space available to send or data
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001619 * received.
1620 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001621static void hlua_socket_handler(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001622{
Willy Tarreau00a37f02015-04-13 12:05:19 +02001623 struct stream_interface *si = appctx->owner;
Olivier Houchard9aaf7782017-09-13 18:30:23 +02001624 struct connection *c = cs_conn(objt_cs(si_opposite(si)->end));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001625
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001626 if (appctx->ctx.hlua_cosocket.die) {
1627 si_shutw(si);
1628 si_shutr(si);
1629 si_ic(si)->flags |= CF_READ_NULL;
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001630 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1631 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001632 stream_shutdown(si_strm(si), SF_ERR_KILLED);
1633 }
1634
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001635 /* If the connection object is not available, close all the
1636 * streams and wakeup everything waiting for.
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001637 */
1638 if (!c) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001639 si_shutw(si);
1640 si_shutr(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01001641 si_ic(si)->flags |= CF_READ_NULL;
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001642 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1643 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Willy Tarreaud4da1962015-04-20 01:31:23 +02001644 return;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001645 }
1646
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001647 /* If we cant write, wakeup the pending write signals. */
1648 if (channel_output_closed(si_ic(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001649 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001650
1651 /* If we cant read, wakeup the pending read signals. */
1652 if (channel_input_closed(si_oc(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001653 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001654
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001655 /* if the connection is not estabkished, inform the stream that we want
1656 * to be notified whenever the connection completes.
1657 */
1658 if (!(c->flags & CO_FL_CONNECTED)) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01001659 si_cant_get(si);
Willy Tarreau12c24232018-12-06 15:29:50 +01001660 si_rx_conn_blk(si);
Willy Tarreau3367d412018-11-15 10:57:41 +01001661 si_rx_endp_more(si);
Willy Tarreaud4da1962015-04-20 01:31:23 +02001662 return;
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001663 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001664
1665 /* This function is called after the connect. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01001666 appctx->ctx.hlua_cosocket.connected = 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001667
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001668 /* Wake the tasks which wants to write if the buffer have available space. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001669 if (channel_may_recv(si_ic(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001670 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001671
1672 /* Wake the tasks which wants to read if the buffer contains data. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001673 if (!channel_is_empty(si_oc(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001674 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001675
1676 /* Some data were injected in the buffer, notify the stream
1677 * interface.
1678 */
1679 if (!channel_is_empty(si_ic(si)))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01001680 si_update(si);
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001681
1682 /* If write notifications are registered, we considers we want
Willy Tarreau3367d412018-11-15 10:57:41 +01001683 * to write, so we clear the blocking flag.
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001684 */
1685 if (notification_registered(&appctx->ctx.hlua_cosocket.wake_on_write))
Willy Tarreau3367d412018-11-15 10:57:41 +01001686 si_rx_endp_more(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001687}
1688
Willy Tarreau87b09662015-04-03 00:22:06 +02001689/* This function is called when the "struct stream" is destroyed.
1690 * Remove the link from the object to this stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001691 * Wake all the pending signals.
1692 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001693static void hlua_socket_release(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001694{
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001695 struct xref *peer;
1696
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001697 /* Remove my link in the original object. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001698 peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref);
1699 if (peer)
1700 xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001701
1702 /* Wake all the task waiting for me. */
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001703 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1704 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001705}
1706
1707/* If the garbage collectio of the object is launch, nobody
Willy Tarreau87b09662015-04-03 00:22:06 +02001708 * uses this object. If the stream does not exists, just quit.
1709 * Send the shutdown signal to the stream. In some cases,
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001710 * pending signal can rest in the read and write lists. destroy
1711 * it.
1712 */
1713__LJMP static int hlua_socket_gc(lua_State *L)
1714{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001715 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001716 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001717 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001718
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001719 MAY_LJMP(check_args(L, 1, "__gc"));
1720
1721 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001722 peer = xref_get_peer_and_lock(&socket->xref);
1723 if (!peer)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001724 return 0;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001725 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001726
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001727 /* Set the flag which destroy the session. */
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001728 appctx->ctx.hlua_cosocket.die = 1;
1729 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001730
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001731 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001732 xref_disconnect(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001733 return 0;
1734}
1735
1736/* The close function send shutdown signal and break the
Willy Tarreau87b09662015-04-03 00:22:06 +02001737 * links between the stream and the object.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001738 */
sada05ed3302018-05-11 11:48:18 -07001739__LJMP static int hlua_socket_close_helper(lua_State *L)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001740{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001741 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001742 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001743 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001744
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001745 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001746
1747 /* Check if we run on the same thread than the xreator thread.
1748 * We cannot access to the socket if the thread is different.
1749 */
1750 if (socket->tid != tid)
1751 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1752
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001753 peer = xref_get_peer_and_lock(&socket->xref);
1754 if (!peer)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001755 return 0;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001756 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001757
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001758 /* Set the flag which destroy the session. */
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001759 appctx->ctx.hlua_cosocket.die = 1;
1760 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001761
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001762 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001763 xref_disconnect(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001764 return 0;
1765}
1766
sada05ed3302018-05-11 11:48:18 -07001767/* The close function calls close_helper.
1768 */
1769__LJMP static int hlua_socket_close(lua_State *L)
1770{
1771 MAY_LJMP(check_args(L, 1, "close"));
1772 return hlua_socket_close_helper(L);
1773}
1774
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001775/* This Lua function assumes that the stack contain three parameters.
1776 * 1 - USERDATA containing a struct socket
1777 * 2 - INTEGER with values of the macro defined below
1778 * If the integer is -1, we must read at most one line.
1779 * If the integer is -2, we ust read all the data until the
1780 * end of the stream.
1781 * If the integer is positive value, we must read a number of
1782 * bytes corresponding to this value.
1783 */
1784#define HLSR_READ_LINE (-1)
1785#define HLSR_READ_ALL (-2)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001786__LJMP static int hlua_socket_receive_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001787{
1788 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
1789 int wanted = lua_tointeger(L, 2);
1790 struct hlua *hlua = hlua_gethlua(L);
1791 struct appctx *appctx;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001792 size_t len;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001793 int nblk;
Willy Tarreau206ba832018-06-14 15:27:31 +02001794 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001795 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02001796 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001797 size_t len2;
Thierry FOURNIER00543922015-03-09 18:35:06 +01001798 int skip_at_end = 0;
Willy Tarreau81389672015-03-10 12:03:52 +01001799 struct channel *oc;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001800 struct stream_interface *si;
1801 struct stream *s;
1802 struct xref *peer;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001803 int missing_bytes;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001804
1805 /* Check if this lua stack is schedulable. */
1806 if (!hlua || !hlua->task)
1807 WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in "
1808 "'frontend', 'backend' or 'task'"));
1809
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001810 /* Check if we run on the same thread than the xreator thread.
1811 * We cannot access to the socket if the thread is different.
1812 */
1813 if (socket->tid != tid)
1814 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1815
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001816 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001817 peer = xref_get_peer_and_lock(&socket->xref);
1818 if (!peer)
1819 goto no_peer;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001820 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
1821 si = appctx->owner;
1822 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001823
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001824 oc = &s->res;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001825 if (wanted == HLSR_READ_LINE) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001826 /* Read line. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001827 nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001828 if (nblk < 0) /* Connection close. */
1829 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001830 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001831 goto connection_empty;
Thierry FOURNIER00543922015-03-09 18:35:06 +01001832
1833 /* remove final \r\n. */
1834 if (nblk == 1) {
1835 if (blk1[len1-1] == '\n') {
1836 len1--;
1837 skip_at_end++;
1838 if (blk1[len1-1] == '\r') {
1839 len1--;
1840 skip_at_end++;
1841 }
1842 }
1843 }
1844 else {
1845 if (blk2[len2-1] == '\n') {
1846 len2--;
1847 skip_at_end++;
1848 if (blk2[len2-1] == '\r') {
1849 len2--;
1850 skip_at_end++;
1851 }
1852 }
1853 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001854 }
1855
1856 else if (wanted == HLSR_READ_ALL) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001857 /* Read all the available data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001858 nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001859 if (nblk < 0) /* Connection close. */
1860 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001861 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001862 goto connection_empty;
1863 }
1864
1865 else {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001866 /* Read a block of data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001867 nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001868 if (nblk < 0) /* Connection close. */
1869 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001870 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001871 goto connection_empty;
1872
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001873 missing_bytes = wanted - socket->b.n;
1874 if (len1 > missing_bytes) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001875 nblk = 1;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001876 len1 = missing_bytes;
1877 } if (nblk == 2 && len1 + len2 > missing_bytes)
1878 len2 = missing_bytes - len1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001879 }
1880
1881 len = len1;
1882
1883 luaL_addlstring(&socket->b, blk1, len1);
1884 if (nblk == 2) {
1885 len += len2;
1886 luaL_addlstring(&socket->b, blk2, len2);
1887 }
1888
1889 /* Consume data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001890 co_skip(oc, len + skip_at_end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001891
1892 /* Don't wait anything. */
Thierry FOURNIER7e4ee472018-05-25 15:03:50 +02001893 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001894
1895 /* If the pattern reclaim to read all the data
1896 * in the connection, got out.
1897 */
1898 if (wanted == HLSR_READ_ALL)
1899 goto connection_empty;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001900 else if (wanted >= 0 && socket->b.n < wanted)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001901 goto connection_empty;
1902
1903 /* Return result. */
1904 luaL_pushresult(&socket->b);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001905 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001906 return 1;
1907
1908connection_closed:
1909
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001910 xref_unlock(&socket->xref, peer);
1911
1912no_peer:
1913
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001914 /* If the buffer containds data. */
1915 if (socket->b.n > 0) {
1916 luaL_pushresult(&socket->b);
1917 return 1;
1918 }
1919 lua_pushnil(L);
1920 lua_pushstring(L, "connection closed.");
1921 return 2;
1922
1923connection_empty:
1924
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001925 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) {
1926 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001927 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001928 }
1929 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02001930 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_receive_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001931 return 0;
1932}
1933
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001934/* This Lua function gets two parameters. The first one can be string
1935 * or a number. If the string is "*l", the user requires one line. If
1936 * the string is "*a", the user requires all the contents of the stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001937 * If the value is a number, the user require a number of bytes equal
1938 * to the value. The default value is "*l" (a line).
1939 *
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001940 * This parameter with a variable type is converted in integer. This
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001941 * integer takes this values:
1942 * -1 : read a line
1943 * -2 : read all the stream
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001944 * >0 : amount of bytes.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001945 *
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001946 * The second parameter is optional. It contains a string that must be
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001947 * concatenated with the read data.
1948 */
1949__LJMP static int hlua_socket_receive(struct lua_State *L)
1950{
1951 int wanted = HLSR_READ_LINE;
1952 const char *pattern;
1953 int type;
1954 char *error;
1955 size_t len;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001956 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001957
1958 if (lua_gettop(L) < 1 || lua_gettop(L) > 3)
1959 WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments."));
1960
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001961 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001962
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001963 /* Check if we run on the same thread than the xreator thread.
1964 * We cannot access to the socket if the thread is different.
1965 */
1966 if (socket->tid != tid)
1967 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1968
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001969 /* check for pattern. */
1970 if (lua_gettop(L) >= 2) {
1971 type = lua_type(L, 2);
1972 if (type == LUA_TSTRING) {
1973 pattern = lua_tostring(L, 2);
1974 if (strcmp(pattern, "*a") == 0)
1975 wanted = HLSR_READ_ALL;
1976 else if (strcmp(pattern, "*l") == 0)
1977 wanted = HLSR_READ_LINE;
1978 else {
1979 wanted = strtoll(pattern, &error, 10);
1980 if (*error != '\0')
1981 WILL_LJMP(luaL_error(L, "Unsupported pattern."));
1982 }
1983 }
1984 else if (type == LUA_TNUMBER) {
1985 wanted = lua_tointeger(L, 2);
1986 if (wanted < 0)
1987 WILL_LJMP(luaL_error(L, "Unsupported size."));
1988 }
1989 }
1990
1991 /* Set pattern. */
1992 lua_pushinteger(L, wanted);
Tim Duesterhusc6e377e2018-01-04 19:32:13 +01001993
1994 /* Check if we would replace the top by itself. */
1995 if (lua_gettop(L) != 2)
1996 lua_replace(L, 2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001997
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001998 /* init buffer, and fill it with prefix. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001999 luaL_buffinit(L, &socket->b);
2000
2001 /* Check prefix. */
2002 if (lua_gettop(L) >= 3) {
2003 if (lua_type(L, 3) != LUA_TSTRING)
2004 WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix"));
2005 pattern = lua_tolstring(L, 3, &len);
2006 luaL_addlstring(&socket->b, pattern, len);
2007 }
2008
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002009 return __LJMP(hlua_socket_receive_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002010}
2011
2012/* Write the Lua input string in the output buffer.
Mark Lakes22154b42018-01-29 14:38:40 -08002013 * This function returns a yield if no space is available.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002014 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002015static int hlua_socket_write_yield(struct lua_State *L,int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002016{
2017 struct hlua_socket *socket;
2018 struct hlua *hlua = hlua_gethlua(L);
2019 struct appctx *appctx;
2020 size_t buf_len;
2021 const char *buf;
2022 int len;
2023 int send_len;
2024 int sent;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002025 struct xref *peer;
2026 struct stream_interface *si;
2027 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002028
2029 /* Check if this lua stack is schedulable. */
2030 if (!hlua || !hlua->task)
2031 WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in "
2032 "'frontend', 'backend' or 'task'"));
2033
2034 /* Get object */
2035 socket = MAY_LJMP(hlua_checksocket(L, 1));
2036 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002037 sent = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002038
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002039 /* Check if we run on the same thread than the xreator thread.
2040 * We cannot access to the socket if the thread is different.
2041 */
2042 if (socket->tid != tid)
2043 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2044
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002045 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002046 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002047 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002048 lua_pushinteger(L, -1);
2049 return 1;
2050 }
2051 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2052 si = appctx->owner;
2053 s = si_strm(si);
2054
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002055 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002056 if (channel_output_closed(&s->req)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002057 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002058 lua_pushinteger(L, -1);
2059 return 1;
2060 }
2061
2062 /* Update the input buffer data. */
2063 buf += sent;
2064 send_len = buf_len - sent;
2065
2066 /* All the data are sent. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002067 if (sent >= buf_len) {
2068 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002069 return 1; /* Implicitly return the length sent. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002070 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002071
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002072 /* Check if the buffer is available because HAProxy doesn't allocate
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01002073 * the request buffer if its not required.
2074 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002075 if (s->req.buf.size == 0) {
Willy Tarreau581abd32018-10-25 10:21:41 +02002076 if (!si_alloc_ibuf(si, &appctx->buffer_wait))
Christopher Faulet33834b12016-12-19 09:29:06 +01002077 goto hlua_socket_write_yield_return;
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01002078 }
2079
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002080 /* Check for available space. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002081 len = b_room(&s->req.buf);
Christopher Faulet33834b12016-12-19 09:29:06 +01002082 if (len <= 0) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002083 goto hlua_socket_write_yield_return;
Christopher Faulet33834b12016-12-19 09:29:06 +01002084 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002085
2086 /* send data */
2087 if (len < send_len)
2088 send_len = len;
Thierry FOURNIER66b89192018-05-27 01:14:47 +02002089 len = ci_putblk(&s->req, buf, send_len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002090
2091 /* "Not enough space" (-1), "Buffer too little to contain
2092 * the data" (-2) are not expected because the available length
2093 * is tested.
2094 * Other unknown error are also not expected.
2095 */
2096 if (len <= 0) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01002097 if (len == -1)
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002098 s->req.flags |= CF_WAKE_WRITE;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002099
sada05ed3302018-05-11 11:48:18 -07002100 MAY_LJMP(hlua_socket_close_helper(L));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002101 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002102 lua_pushinteger(L, -1);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002103 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002104 return 1;
2105 }
2106
2107 /* update buffers. */
Thierry FOURNIER101b9762018-05-27 01:27:40 +02002108 appctx_wakeup(appctx);
Willy Tarreaude70fa12015-09-26 11:25:05 +02002109
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002110 s->req.rex = TICK_ETERNITY;
2111 s->res.wex = TICK_ETERNITY;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002112
2113 /* Update length sent. */
2114 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002115 lua_pushinteger(L, sent + len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002116
2117 /* All the data buffer is sent ? */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002118 if (sent + len >= buf_len) {
2119 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002120 return 1;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002121 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002122
2123hlua_socket_write_yield_return:
Thierry FOURNIERba42fcd2018-05-27 00:59:48 +02002124 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2125 xref_unlock(&socket->xref, peer);
2126 WILL_LJMP(luaL_error(L, "out of memory"));
2127 }
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002128 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002129 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_write_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002130 return 0;
2131}
2132
2133/* This function initiate the send of data. It just check the input
2134 * parameters and push an integer in the Lua stack that contain the
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002135 * amount of data written to the buffer. This is used by the function
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002136 * "hlua_socket_write_yield" that can yield.
2137 *
2138 * The Lua function gets between 3 and 4 parameters. The first one is
2139 * the associated object. The second is a string buffer. The third is
2140 * a facultative integer that represents where is the buffer position
2141 * of the start of the data that can send. The first byte is the
2142 * position "1". The default value is "1". The fourth argument is a
2143 * facultative integer that represents where is the buffer position
2144 * of the end of the data that can send. The default is the last byte.
2145 */
2146static int hlua_socket_send(struct lua_State *L)
2147{
2148 int i;
2149 int j;
2150 const char *buf;
2151 size_t buf_len;
2152
2153 /* Check number of arguments. */
2154 if (lua_gettop(L) < 2 || lua_gettop(L) > 4)
2155 WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments"));
2156
2157 /* Get the string. */
2158 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
2159
2160 /* Get and check j. */
2161 if (lua_gettop(L) == 4) {
2162 j = MAY_LJMP(luaL_checkinteger(L, 4));
2163 if (j < 0)
2164 j = buf_len + j + 1;
2165 if (j > buf_len)
2166 j = buf_len + 1;
2167 lua_pop(L, 1);
2168 }
2169 else
2170 j = buf_len;
2171
2172 /* Get and check i. */
2173 if (lua_gettop(L) == 3) {
2174 i = MAY_LJMP(luaL_checkinteger(L, 3));
2175 if (i < 0)
2176 i = buf_len + i + 1;
2177 if (i > buf_len)
2178 i = buf_len + 1;
2179 lua_pop(L, 1);
2180 } else
2181 i = 1;
2182
2183 /* Check bth i and j. */
2184 if (i > j) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002185 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002186 return 1;
2187 }
2188 if (i == 0 && j == 0) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002189 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002190 return 1;
2191 }
2192 if (i == 0)
2193 i = 1;
2194 if (j == 0)
2195 j = 1;
2196
2197 /* Pop the string. */
2198 lua_pop(L, 1);
2199
2200 /* Update the buffer length. */
2201 buf += i - 1;
2202 buf_len = j - i + 1;
2203 lua_pushlstring(L, buf, buf_len);
2204
2205 /* This unsigned is used to remember the amount of sent data. */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002206 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002207
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002208 return MAY_LJMP(hlua_socket_write_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002209}
2210
Willy Tarreau22b0a682015-06-17 19:43:49 +02002211#define SOCKET_INFO_MAX_LEN sizeof("[0000:0000:0000:0000:0000:0000:0000:0000]:12345")
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002212__LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr)
2213{
2214 static char buffer[SOCKET_INFO_MAX_LEN];
2215 int ret;
2216 int len;
2217 char *p;
2218
2219 ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1);
2220 if (ret <= 0) {
2221 lua_pushnil(L);
2222 return 1;
2223 }
2224
2225 if (ret == AF_UNIX) {
2226 lua_pushstring(L, buffer+1);
2227 return 1;
2228 }
2229 else if (ret == AF_INET6) {
2230 buffer[0] = '[';
2231 len = strlen(buffer);
2232 buffer[len] = ']';
2233 len++;
2234 buffer[len] = ':';
2235 len++;
2236 p = buffer;
2237 }
2238 else if (ret == AF_INET) {
2239 p = buffer + 1;
2240 len = strlen(p);
2241 p[len] = ':';
2242 len++;
2243 }
2244 else {
2245 lua_pushnil(L);
2246 return 1;
2247 }
2248
2249 if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) {
2250 lua_pushnil(L);
2251 return 1;
2252 }
2253
2254 lua_pushstring(L, p);
2255 return 1;
2256}
2257
2258/* Returns information about the peer of the connection. */
2259__LJMP static int hlua_socket_getpeername(struct lua_State *L)
2260{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002261 struct hlua_socket *socket;
2262 struct connection *conn;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002263 struct xref *peer;
2264 struct appctx *appctx;
2265 struct stream_interface *si;
2266 struct stream *s;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002267 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002268
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002269 MAY_LJMP(check_args(L, 1, "getpeername"));
2270
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002271 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002272
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002273 /* Check if we run on the same thread than the xreator thread.
2274 * We cannot access to the socket if the thread is different.
2275 */
2276 if (socket->tid != tid)
2277 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2278
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002279 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002280 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002281 if (!peer) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002282 lua_pushnil(L);
2283 return 1;
2284 }
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002285 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2286 si = appctx->owner;
2287 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002288
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002289 conn = cs_conn(objt_cs(s->si[1].end));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002290 if (!conn) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002291 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002292 lua_pushnil(L);
2293 return 1;
2294 }
2295
Willy Tarreaua71f6422016-11-16 17:00:14 +01002296 conn_get_to_addr(conn);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002297 if (!(conn->flags & CO_FL_ADDR_TO_SET)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002298 xref_unlock(&socket->xref, peer);
Willy Tarreaua71f6422016-11-16 17:00:14 +01002299 lua_pushnil(L);
2300 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002301 }
2302
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002303 ret = MAY_LJMP(hlua_socket_info(L, &conn->addr.to));
2304 xref_unlock(&socket->xref, peer);
2305 return ret;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002306}
2307
2308/* Returns information about my connection side. */
2309static int hlua_socket_getsockname(struct lua_State *L)
2310{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002311 struct hlua_socket *socket;
2312 struct connection *conn;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002313 struct appctx *appctx;
2314 struct xref *peer;
2315 struct stream_interface *si;
2316 struct stream *s;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002317 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002318
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002319 MAY_LJMP(check_args(L, 1, "getsockname"));
2320
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002321 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002322
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002323 /* Check if we run on the same thread than the xreator thread.
2324 * We cannot access to the socket if the thread is different.
2325 */
2326 if (socket->tid != tid)
2327 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2328
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002329 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002330 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002331 if (!peer) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002332 lua_pushnil(L);
2333 return 1;
2334 }
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002335 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
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002339 conn = cs_conn(objt_cs(s->si[1].end));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002340 if (!conn) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002341 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002342 lua_pushnil(L);
2343 return 1;
2344 }
2345
Willy Tarreaua71f6422016-11-16 17:00:14 +01002346 conn_get_from_addr(conn);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002347 if (!(conn->flags & CO_FL_ADDR_FROM_SET)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002348 xref_unlock(&socket->xref, peer);
Willy Tarreaua71f6422016-11-16 17:00:14 +01002349 lua_pushnil(L);
2350 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002351 }
2352
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002353 ret = hlua_socket_info(L, &conn->addr.from);
2354 xref_unlock(&socket->xref, peer);
2355 return ret;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002356}
2357
2358/* This struct define the applet. */
Willy Tarreau30576452015-04-13 13:50:30 +02002359static struct applet update_applet = {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002360 .obj_type = OBJ_TYPE_APPLET,
2361 .name = "<LUA_TCP>",
2362 .fct = hlua_socket_handler,
2363 .release = hlua_socket_release,
2364};
2365
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002366__LJMP static int hlua_socket_connect_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002367{
2368 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
2369 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002370 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002371 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002372 struct stream_interface *si;
2373 struct stream *s;
2374
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002375 /* Check if we run on the same thread than the xreator thread.
2376 * We cannot access to the socket if the thread is different.
2377 */
2378 if (socket->tid != tid)
2379 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2380
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002381 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002382 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002383 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002384 lua_pushnil(L);
2385 lua_pushstring(L, "Can't connect");
2386 return 2;
2387 }
2388 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2389 si = appctx->owner;
2390 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002391
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002392 /* Check if we run on the same thread than the xreator thread.
2393 * We cannot access to the socket if the thread is different.
2394 */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002395 if (socket->tid != tid) {
2396 xref_unlock(&socket->xref, peer);
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002397 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002398 }
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002399
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002400 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002401 if (!hlua || channel_output_closed(&s->req)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002402 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002403 lua_pushnil(L);
2404 lua_pushstring(L, "Can't connect");
2405 return 2;
2406 }
2407
Willy Tarreaue09101e2018-10-16 17:37:12 +02002408 appctx = __objt_appctx(s->si[0].end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002409
2410 /* Check for connection established. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002411 if (appctx->ctx.hlua_cosocket.connected) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002412 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002413 lua_pushinteger(L, 1);
2414 return 1;
2415 }
2416
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002417 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2418 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002419 WILL_LJMP(luaL_error(L, "out of memory error"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002420 }
2421 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002422 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002423 return 0;
2424}
2425
2426/* This function fail or initite the connection. */
2427__LJMP static int hlua_socket_connect(struct lua_State *L)
2428{
2429 struct hlua_socket *socket;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002430 int port = -1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002431 const char *ip;
2432 struct connection *conn;
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002433 struct hlua *hlua;
2434 struct appctx *appctx;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002435 int low, high;
2436 struct sockaddr_storage *addr;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002437 struct xref *peer;
2438 struct stream_interface *si;
2439 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002440
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002441 if (lua_gettop(L) < 2)
2442 WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments"));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002443
2444 /* Get args. */
2445 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002446
2447 /* Check if we run on the same thread than the xreator thread.
2448 * We cannot access to the socket if the thread is different.
2449 */
2450 if (socket->tid != tid)
2451 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2452
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002453 ip = MAY_LJMP(luaL_checkstring(L, 2));
Tim Duesterhus6edab862018-01-06 19:04:45 +01002454 if (lua_gettop(L) >= 3) {
2455 luaL_Buffer b;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002456 port = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002457
Tim Duesterhus6edab862018-01-06 19:04:45 +01002458 /* Force the ip to end with a colon, to support IPv6 addresses
2459 * that are not enclosed within square brackets.
2460 */
2461 if (port > 0) {
2462 luaL_buffinit(L, &b);
2463 luaL_addstring(&b, ip);
2464 luaL_addchar(&b, ':');
2465 luaL_pushresult(&b);
2466 ip = lua_tolstring(L, lua_gettop(L), NULL);
2467 }
2468 }
2469
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002470 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002471 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002472 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002473 lua_pushnil(L);
2474 return 1;
2475 }
2476 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2477 si = appctx->owner;
2478 s = si_strm(si);
2479
2480 /* Initialise connection. */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002481 conn = cs_conn(si_alloc_cs(&s->si[1], NULL));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002482 if (!conn) {
2483 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002484 WILL_LJMP(luaL_error(L, "connect: internal error"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002485 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002486
Willy Tarreau3adac082015-09-26 17:51:09 +02002487 /* needed for the connection not to be closed */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002488 conn->target = s->target;
Willy Tarreau3adac082015-09-26 17:51:09 +02002489
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002490 /* Parse ip address. */
Willy Tarreau48ef4c92017-01-06 18:32:38 +01002491 addr = str2sa_range(ip, NULL, &low, &high, NULL, NULL, NULL, 0);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002492 if (!addr) {
2493 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002494 WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002495 }
2496 if (low != high) {
2497 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002498 WILL_LJMP(luaL_error(L, "connect: port ranges not supported : address '%s'", ip));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002499 }
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002500 memcpy(&conn->addr.to, addr, sizeof(struct sockaddr_storage));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002501
2502 /* Set port. */
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002503 if (low == 0) {
2504 if (conn->addr.to.ss_family == AF_INET) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002505 if (port == -1) {
2506 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002507 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002508 }
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002509 ((struct sockaddr_in *)&conn->addr.to)->sin_port = htons(port);
2510 } else if (conn->addr.to.ss_family == AF_INET6) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002511 if (port == -1) {
2512 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002513 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002514 }
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002515 ((struct sockaddr_in6 *)&conn->addr.to)->sin6_port = htons(port);
2516 }
2517 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002518
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002519 hlua = hlua_gethlua(L);
Willy Tarreaue09101e2018-10-16 17:37:12 +02002520 appctx = __objt_appctx(s->si[0].end);
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002521
2522 /* inform the stream that we want to be notified whenever the
2523 * connection completes.
2524 */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01002525 si_cant_get(&s->si[0]);
Willy Tarreau3367d412018-11-15 10:57:41 +01002526 si_rx_endp_more(&s->si[0]);
Thierry FOURNIER8c8fbbe2015-09-26 17:02:35 +02002527 appctx_wakeup(appctx);
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002528
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +02002529 hlua->flags |= HLUA_MUST_GC;
2530
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002531 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2532 xref_unlock(&socket->xref, peer);
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002533 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002534 }
2535 xref_unlock(&socket->xref, peer);
PiBa-NL706d5ee2018-05-05 23:51:42 +02002536
2537 task_wakeup(s->task, TASK_WOKEN_INIT);
2538 /* Return yield waiting for connection. */
2539
Willy Tarreau9635e032018-10-16 17:52:55 +02002540 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002541
2542 return 0;
2543}
2544
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002545#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002546__LJMP static int hlua_socket_connect_ssl(struct lua_State *L)
2547{
2548 struct hlua_socket *socket;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002549 struct xref *peer;
2550 struct appctx *appctx;
2551 struct stream_interface *si;
2552 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002553
2554 MAY_LJMP(check_args(L, 3, "connect_ssl"));
2555 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002556
2557 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002558 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002559 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002560 lua_pushnil(L);
2561 return 1;
2562 }
2563 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2564 si = appctx->owner;
2565 s = si_strm(si);
2566
2567 s->target = &socket_ssl.obj_type;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002568 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002569 return MAY_LJMP(hlua_socket_connect(L));
2570}
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002571#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002572
2573__LJMP static int hlua_socket_setoption(struct lua_State *L)
2574{
2575 return 0;
2576}
2577
2578__LJMP static int hlua_socket_settimeout(struct lua_State *L)
2579{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002580 struct hlua_socket *socket;
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002581 int tmout;
Mark Lakes56cc1252018-03-27 09:48:06 +02002582 double dtmout;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002583 struct xref *peer;
2584 struct appctx *appctx;
2585 struct stream_interface *si;
2586 struct stream *s;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002587
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002588 MAY_LJMP(check_args(L, 2, "settimeout"));
2589
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002590 socket = MAY_LJMP(hlua_checksocket(L, 1));
Mark Lakes56cc1252018-03-27 09:48:06 +02002591
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002592 /* convert the timeout to millis */
2593 dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002594
Thierry Fournier17a921b2018-03-08 09:59:02 +01002595 /* Check for negative values */
Mark Lakes56cc1252018-03-27 09:48:06 +02002596 if (dtmout < 0)
Thierry Fournier17a921b2018-03-08 09:59:02 +01002597 WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values"));
2598
Mark Lakes56cc1252018-03-27 09:48:06 +02002599 if (dtmout > INT_MAX) /* overflow check */
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002600 WILL_LJMP(luaL_error(L, "settimeout: cannot set values larger than %d ms", INT_MAX));
Mark Lakes56cc1252018-03-27 09:48:06 +02002601
2602 tmout = MS_TO_TICKS((int)dtmout);
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002603 if (tmout == 0)
2604 tmout++; /* very small timeouts are adjusted to a minium of 1ms */
Mark Lakes56cc1252018-03-27 09:48:06 +02002605
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002606 /* Check if we run on the same thread than the xreator thread.
2607 * We cannot access to the socket if the thread is different.
2608 */
2609 if (socket->tid != tid)
2610 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2611
Mark Lakes56cc1252018-03-27 09:48:06 +02002612 /* check for connection break. If some data were read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002613 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002614 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002615 hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts.");
2616 WILL_LJMP(lua_error(L));
2617 return 0;
2618 }
2619 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2620 si = appctx->owner;
2621 s = si_strm(si);
2622
Cyril Bonté7bb63452018-08-17 23:51:02 +02002623 s->sess->fe->timeout.connect = tmout;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002624 s->req.rto = tmout;
2625 s->req.wto = tmout;
2626 s->res.rto = tmout;
2627 s->res.wto = tmout;
Cyril Bonté7bb63452018-08-17 23:51:02 +02002628 s->req.rex = tick_add_ifset(now_ms, tmout);
2629 s->req.wex = tick_add_ifset(now_ms, tmout);
2630 s->res.rex = tick_add_ifset(now_ms, tmout);
2631 s->res.wex = tick_add_ifset(now_ms, tmout);
2632
2633 s->task->expire = tick_add_ifset(now_ms, tmout);
2634 task_queue(s->task);
2635
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002636 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002637
Thierry Fourniere9636f12018-03-08 09:54:32 +01002638 lua_pushinteger(L, 1);
Tim Duesterhus119a5f12018-01-06 19:16:25 +01002639 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002640}
2641
2642__LJMP static int hlua_socket_new(lua_State *L)
2643{
2644 struct hlua_socket *socket;
2645 struct appctx *appctx;
Willy Tarreau15b5e142015-04-04 14:38:25 +02002646 struct session *sess;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002647 struct stream *strm;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002648
2649 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002650 if (!lua_checkstack(L, 3)) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002651 hlua_pusherror(L, "socket: full stack");
2652 goto out_fail_conf;
2653 }
2654
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002655 /* Create the object: obj[0] = userdata. */
2656 lua_newtable(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002657 socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket)));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002658 lua_rawseti(L, -2, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002659 memset(socket, 0, sizeof(*socket));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002660 socket->tid = tid;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002661
Thierry FOURNIER4a6170c2015-03-09 17:07:10 +01002662 /* Check if the various memory pools are intialized. */
Willy Tarreaubafbe012017-11-24 17:34:44 +01002663 if (!pool_head_stream || !pool_head_buffer) {
Thierry FOURNIER4a6170c2015-03-09 17:07:10 +01002664 hlua_pusherror(L, "socket: uninitialized pools.");
2665 goto out_fail_conf;
2666 }
2667
Willy Tarreau87b09662015-04-03 00:22:06 +02002668 /* Pop a class stream metatable and affect it to the userdata. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002669 lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref);
2670 lua_setmetatable(L, -2);
2671
Willy Tarreaud420a972015-04-06 00:39:18 +02002672 /* Create the applet context */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01002673 appctx = appctx_new(&update_applet, tid_bit);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002674 if (!appctx) {
2675 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaufeb76402015-04-03 14:10:06 +02002676 goto out_fail_conf;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002677 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002678
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002679 appctx->ctx.hlua_cosocket.connected = 0;
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02002680 appctx->ctx.hlua_cosocket.die = 0;
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002681 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write);
2682 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read);
Willy Tarreaub2bf8332015-04-04 15:58:58 +02002683
Willy Tarreaud420a972015-04-06 00:39:18 +02002684 /* Now create a session, task and stream for this applet */
2685 sess = session_new(&socket_proxy, NULL, &appctx->obj_type);
2686 if (!sess) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002687 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002688 goto out_fail_sess;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002689 }
2690
Willy Tarreau87787ac2017-08-28 16:22:54 +02002691 strm = stream_new(sess, &appctx->obj_type);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002692 if (!strm) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002693 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002694 goto out_fail_stream;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002695 }
2696
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002697 /* Initialise cross reference between stream and Lua socket object. */
2698 xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002699
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002700 /* Configure "right" stream interface. this "si" is used to connect
2701 * and retrieve data from the server. The connection is initialized
2702 * with the "struct server".
2703 */
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002704 si_set_state(&strm->si[1], SI_ST_ASS);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002705
2706 /* Force destination server. */
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002707 strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET | SF_BE_ASSIGNED;
2708 strm->target = &socket_tcp.obj_type;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002709
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002710 return 1;
2711
Willy Tarreaud420a972015-04-06 00:39:18 +02002712 out_fail_stream:
Willy Tarreau11c36242015-04-04 15:54:03 +02002713 session_free(sess);
Willy Tarreaud420a972015-04-06 00:39:18 +02002714 out_fail_sess:
2715 appctx_free(appctx);
2716 out_fail_conf:
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002717 WILL_LJMP(lua_error(L));
2718 return 0;
2719}
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01002720
2721/*
2722 *
2723 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002724 * Class Channel
2725 *
2726 *
2727 */
2728
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002729/* This function is called before the Lua execution. It stores
2730 * the differents parsers state before executing some Lua code.
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002731 */
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002732static inline void consistency_set(struct stream *stream, int opt, struct hlua_consistency *c)
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002733{
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002734 c->mode = stream->be->mode;
2735 switch (c->mode) {
2736 case PR_MODE_HTTP:
2737 c->data.http.dir = opt & SMP_OPT_DIR;
2738 if (c->data.http.dir == SMP_OPT_DIR_REQ)
2739 c->data.http.state = stream->txn->req.msg_state;
2740 else
2741 c->data.http.state = stream->txn->rsp.msg_state;
2742 break;
2743 default:
2744 break;
2745 }
2746}
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002747
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002748/* This function is called after the Lua execution. it
2749 * returns true if the parser state is consistent, otherwise,
2750 * it return false.
2751 *
2752 * In HTTP mode, the parser state must be in the same state
2753 * or greater when we exit the function. Even if we do a
2754 * control yield. This prevent to break the HTTP message
2755 * from the Lua code.
2756 */
2757static inline int consistency_check(struct stream *stream, int opt, struct hlua_consistency *c)
2758{
2759 if (c->mode != stream->be->mode)
2760 return 0;
2761
2762 switch (c->mode) {
2763 case PR_MODE_HTTP:
2764 if (c->data.http.dir != (opt & SMP_OPT_DIR))
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002765 return 0;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002766 if (c->data.http.dir == SMP_OPT_DIR_REQ)
2767 return stream->txn->req.msg_state >= c->data.http.state;
2768 else
2769 return stream->txn->rsp.msg_state >= c->data.http.state;
2770 default:
2771 return 1;
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002772 }
2773 return 1;
2774}
2775
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002776/* Returns the struct hlua_channel join to the class channel in the
2777 * stack entry "ud" or throws an argument error.
2778 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002779__LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002780{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02002781 return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002782}
2783
Willy Tarreau47860ed2015-03-10 14:07:50 +01002784/* Pushes the channel onto the top of the stack. If the stask does not have a
2785 * free slots, the function fails and returns 0;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002786 */
Willy Tarreau2a71af42015-03-10 13:51:50 +01002787static int hlua_channel_new(lua_State *L, struct channel *channel)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002788{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002789 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002790 if (!lua_checkstack(L, 3))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002791 return 0;
2792
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002793 lua_newtable(L);
Willy Tarreau47860ed2015-03-10 14:07:50 +01002794 lua_pushlightuserdata(L, channel);
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002795 lua_rawseti(L, -2, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002796
2797 /* Pop a class sesison metatable and affect it to the userdata. */
2798 lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref);
2799 lua_setmetatable(L, -2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002800 return 1;
2801}
2802
2803/* Duplicate all the data present in the input channel and put it
2804 * in a string LUA variables. Returns -1 and push a nil value in
2805 * the stack if the channel is closed and all the data are consumed,
2806 * returns 0 if no data are available, otherwise it returns the length
2807 * of the builded string.
2808 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002809static inline int _hlua_channel_dup(struct channel *chn, lua_State *L)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002810{
2811 char *blk1;
2812 char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02002813 size_t len1;
2814 size_t len2;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002815 int ret;
2816 luaL_Buffer b;
2817
Willy Tarreau06d80a92017-10-19 14:32:15 +02002818 ret = ci_getblk_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002819 if (unlikely(ret == 0))
2820 return 0;
2821
2822 if (unlikely(ret < 0)) {
2823 lua_pushnil(L);
2824 return -1;
2825 }
2826
2827 luaL_buffinit(L, &b);
2828 luaL_addlstring(&b, blk1, len1);
2829 if (unlikely(ret == 2))
2830 luaL_addlstring(&b, blk2, len2);
2831 luaL_pushresult(&b);
2832
2833 if (unlikely(ret == 2))
2834 return len1 + len2;
2835 return len1;
2836}
2837
2838/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2839 * a yield. This function keep the data in the buffer.
2840 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002841__LJMP static int hlua_channel_dup_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002842{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002843 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002844
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002845 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2846
Christopher Faulet3f829a42018-12-13 21:56:45 +01002847 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2848 WILL_LJMP(lua_error(L));
2849
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002850 if (_hlua_channel_dup(chn, L) == 0)
Willy Tarreau9635e032018-10-16 17:52:55 +02002851 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_dup_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002852 return 1;
2853}
2854
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002855/* Check arguments for the function "hlua_channel_dup_yield". */
2856__LJMP static int hlua_channel_dup(lua_State *L)
2857{
2858 MAY_LJMP(check_args(L, 1, "dup"));
2859 MAY_LJMP(hlua_checkchannel(L, 1));
2860 return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0));
2861}
2862
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002863/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2864 * a yield. This function consumes the data in the buffer. It returns
2865 * a string containing the data or a nil pointer if no data are available
2866 * and the channel is closed.
2867 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002868__LJMP static int hlua_channel_get_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002869{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002870 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002871 int ret;
2872
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002873 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002874
Christopher Faulet3f829a42018-12-13 21:56:45 +01002875 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2876 WILL_LJMP(lua_error(L));
2877
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002878 ret = _hlua_channel_dup(chn, L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002879 if (unlikely(ret == 0))
Willy Tarreau9635e032018-10-16 17:52:55 +02002880 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002881
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002882 if (unlikely(ret == -1))
2883 return 1;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002884
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002885 b_sub(&chn->buf, ret);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002886 return 1;
2887}
2888
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002889/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002890__LJMP static int hlua_channel_get(lua_State *L)
2891{
2892 MAY_LJMP(check_args(L, 1, "get"));
2893 MAY_LJMP(hlua_checkchannel(L, 1));
2894 return MAY_LJMP(hlua_channel_get_yield(L, 0, 0));
2895}
2896
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002897/* This functions consumes and returns one line. If the channel is closed,
2898 * and the last data does not contains a final '\n', the data are returned
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002899 * without the final '\n'. When no more data are available, it returns nil
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002900 * value.
2901 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002902__LJMP static int hlua_channel_getline_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002903{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002904 char *blk1;
2905 char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02002906 size_t len1;
2907 size_t len2;
2908 size_t len;
Willy Tarreau47860ed2015-03-10 14:07:50 +01002909 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002910 int ret;
2911 luaL_Buffer b;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002912
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002913 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2914
Christopher Faulet3f829a42018-12-13 21:56:45 +01002915 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2916 WILL_LJMP(lua_error(L));
2917
Willy Tarreau06d80a92017-10-19 14:32:15 +02002918 ret = ci_getline_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002919 if (ret == 0)
Willy Tarreau9635e032018-10-16 17:52:55 +02002920 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_getline_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002921
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002922 if (ret == -1) {
2923 lua_pushnil(L);
2924 return 1;
2925 }
2926
2927 luaL_buffinit(L, &b);
2928 luaL_addlstring(&b, blk1, len1);
2929 len = len1;
2930 if (unlikely(ret == 2)) {
2931 luaL_addlstring(&b, blk2, len2);
2932 len += len2;
2933 }
2934 luaL_pushresult(&b);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002935 b_rep_blk(&chn->buf, ci_head(chn), ci_head(chn) + len, NULL, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002936 return 1;
2937}
2938
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002939/* Check arguments for the function "hlua_channel_getline_yield". */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002940__LJMP static int hlua_channel_getline(lua_State *L)
2941{
2942 MAY_LJMP(check_args(L, 1, "getline"));
2943 MAY_LJMP(hlua_checkchannel(L, 1));
2944 return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0));
2945}
2946
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002947/* This function takes a string as input, and append it at the
2948 * input side of channel. If the data is too big, but a space
2949 * is probably available after sending some data, the function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002950 * yields. If the data is bigger than the buffer, or if the
2951 * channel is closed, it returns -1. Otherwise, it returns the
2952 * amount of data written.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002953 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002954__LJMP static int hlua_channel_append_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002955{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002956 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002957 size_t len;
2958 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
2959 int l = MAY_LJMP(luaL_checkinteger(L, 3));
2960 int ret;
2961 int max;
2962
Christopher Faulet3f829a42018-12-13 21:56:45 +01002963 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2964 WILL_LJMP(lua_error(L));
2965
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002966 /* Check if the buffer is available because HAProxy doesn't allocate
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002967 * the request buffer if its not required.
2968 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002969 if (chn->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +01002970 si_rx_buff_blk(chn_prod(chn));
Willy Tarreau9635e032018-10-16 17:52:55 +02002971 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002972 }
2973
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002974 max = channel_recv_limit(chn) - b_data(&chn->buf);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002975 if (max > len - l)
2976 max = len - l;
2977
Willy Tarreau06d80a92017-10-19 14:32:15 +02002978 ret = ci_putblk(chn, str + l, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002979 if (ret == -2 || ret == -3) {
2980 lua_pushinteger(L, -1);
2981 return 1;
2982 }
Willy Tarreaubc18da12015-03-13 14:00:47 +01002983 if (ret == -1) {
2984 chn->flags |= CF_WAKE_WRITE;
Willy Tarreau9635e032018-10-16 17:52:55 +02002985 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Willy Tarreaubc18da12015-03-13 14:00:47 +01002986 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002987 l += ret;
2988 lua_pop(L, 1);
2989 lua_pushinteger(L, l);
2990
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002991 max = channel_recv_limit(chn) - b_data(&chn->buf);
Willy Tarreaua79021a2018-06-15 18:07:57 +02002992 if (max == 0 && co_data(chn) == 0) {
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002993 /* There are no space available, and the output buffer is empty.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002994 * in this case, we cannot add more data, so we cannot yield,
2995 * we return the amount of copyied data.
2996 */
2997 return 1;
2998 }
2999 if (l < len)
Willy Tarreau9635e032018-10-16 17:52:55 +02003000 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003001 return 1;
3002}
3003
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003004/* Just a wrapper of "hlua_channel_append_yield". It returns the length
3005 * of the written string, or -1 if the channel is closed or if the
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003006 * buffer size is too little for the data.
3007 */
3008__LJMP static int hlua_channel_append(lua_State *L)
3009{
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003010 size_t len;
3011
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003012 MAY_LJMP(check_args(L, 2, "append"));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003013 MAY_LJMP(hlua_checkchannel(L, 1));
3014 MAY_LJMP(luaL_checklstring(L, 2, &len));
3015 MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003016 lua_pushinteger(L, 0);
3017
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003018 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003019}
3020
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003021/* Just a wrapper of "hlua_channel_append_yield". This wrapper starts
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003022 * his process by cleaning the buffer. The result is a replacement
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003023 * of the current data. It returns the length of the written string,
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003024 * or -1 if the channel is closed or if the buffer size is too
3025 * little for the data.
3026 */
3027__LJMP static int hlua_channel_set(lua_State *L)
3028{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003029 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003030
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003031 MAY_LJMP(check_args(L, 2, "set"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003032 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003033 lua_pushinteger(L, 0);
3034
Christopher Faulet3f829a42018-12-13 21:56:45 +01003035 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
3036 WILL_LJMP(lua_error(L));
3037
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003038 b_set_data(&chn->buf, co_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003039
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003040 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003041}
3042
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003043/* Append data in the output side of the buffer. This data is immediately
3044 * sent. The function returns the amount of data written. If the buffer
3045 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003046 * if the channel is closed.
3047 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003048__LJMP static int hlua_channel_send_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003049{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003050 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003051 size_t len;
3052 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
3053 int l = MAY_LJMP(luaL_checkinteger(L, 3));
3054 int max;
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003055 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003056
Christopher Faulet3f829a42018-12-13 21:56:45 +01003057 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
3058 WILL_LJMP(lua_error(L));
3059
Willy Tarreau47860ed2015-03-10 14:07:50 +01003060 if (unlikely(channel_output_closed(chn))) {
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003061 lua_pushinteger(L, -1);
3062 return 1;
3063 }
3064
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003065 /* Check if the buffer is available because HAProxy doesn't allocate
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01003066 * the request buffer if its not required.
3067 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003068 if (chn->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +01003069 si_rx_buff_blk(chn_prod(chn));
Willy Tarreau9635e032018-10-16 17:52:55 +02003070 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01003071 }
3072
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003073 /* The written data will be immediately sent, so we can check
3074 * the available space without taking in account the reserve.
3075 * The reserve is guaranteed for the processing of incoming
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003076 * data, because the buffer will be flushed.
3077 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003078 max = b_room(&chn->buf);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003079
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003080 /* If there is no space available, and the output buffer is empty.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003081 * in this case, we cannot add more data, so we cannot yield,
3082 * we return the amount of copyied data.
3083 */
Willy Tarreaua79021a2018-06-15 18:07:57 +02003084 if (max == 0 && co_data(chn) == 0)
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003085 return 1;
3086
3087 /* Adjust the real required length. */
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003088 if (max > len - l)
3089 max = len - l;
3090
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003091 /* The buffer available size may be not contiguous. This test
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003092 * detects a non contiguous buffer and realign it.
3093 */
Willy Tarreau3f679992018-06-15 15:06:42 +02003094 if (ci_space_for_replace(chn) < max)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003095 channel_slow_realign(chn, trash.area);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003096
3097 /* Copy input data in the buffer. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003098 max = b_rep_blk(&chn->buf, ci_head(chn), ci_head(chn), str + l, max);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003099
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003100 /* buffer replace considers that the input part is filled.
3101 * so, I must forward these new data in the output part.
3102 */
Willy Tarreaubcbd3932018-06-06 07:13:22 +02003103 c_adv(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003104
3105 l += max;
3106 lua_pop(L, 1);
3107 lua_pushinteger(L, l);
3108
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003109 /* If there is no space available, and the output buffer is empty.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003110 * in this case, we cannot add more data, so we cannot yield,
3111 * we return the amount of copyied data.
3112 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003113 max = b_room(&chn->buf);
Willy Tarreaua79021a2018-06-15 18:07:57 +02003114 if (max == 0 && co_data(chn) == 0)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003115 return 1;
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003116
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003117 if (l < len) {
3118 /* If we are waiting for space in the response buffer, we
3119 * must set the flag WAKERESWR. This flag required the task
3120 * wake up if any activity is detected on the response buffer.
3121 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003122 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003123 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01003124 else
3125 HLUA_SET_WAKEREQWR(hlua);
Willy Tarreau9635e032018-10-16 17:52:55 +02003126 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003127 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003128
3129 return 1;
3130}
3131
3132/* Just a wraper of "_hlua_channel_send". This wrapper permits
3133 * yield the LUA process, and resume it without checking the
3134 * input arguments.
3135 */
3136__LJMP static int hlua_channel_send(lua_State *L)
3137{
3138 MAY_LJMP(check_args(L, 2, "send"));
3139 lua_pushinteger(L, 0);
3140
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003141 return MAY_LJMP(hlua_channel_send_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003142}
3143
3144/* This function forward and amount of butes. The data pass from
3145 * the input side of the buffer to the output side, and can be
3146 * forwarded. This function never fails.
3147 *
3148 * The Lua function takes an amount of bytes to be forwarded in
3149 * imput. It returns the number of bytes forwarded.
3150 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003151__LJMP static int hlua_channel_forward_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003152{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003153 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003154 int len;
3155 int l;
3156 int max;
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003157 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003158
3159 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Faulet3f829a42018-12-13 21:56:45 +01003160
3161 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
3162 WILL_LJMP(lua_error(L));
3163
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003164 len = MAY_LJMP(luaL_checkinteger(L, 2));
3165 l = MAY_LJMP(luaL_checkinteger(L, -1));
3166
3167 max = len - l;
Willy Tarreaua79021a2018-06-15 18:07:57 +02003168 if (max > ci_data(chn))
3169 max = ci_data(chn);
Willy Tarreau47860ed2015-03-10 14:07:50 +01003170 channel_forward(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003171 l += max;
3172
3173 lua_pop(L, 1);
3174 lua_pushinteger(L, l);
3175
3176 /* Check if it miss bytes to forward. */
3177 if (l < len) {
3178 /* The the input channel or the output channel are closed, we
3179 * must return the amount of data forwarded.
3180 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003181 if (channel_input_closed(chn) || channel_output_closed(chn))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003182 return 1;
3183
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003184 /* If we are waiting for space data in the response buffer, we
3185 * must set the flag WAKERESWR. This flag required the task
3186 * wake up if any activity is detected on the response buffer.
3187 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003188 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003189 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01003190 else
3191 HLUA_SET_WAKEREQWR(hlua);
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003192
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003193 /* Otherwise, we can yield waiting for new data in the inpout side. */
Willy Tarreau9635e032018-10-16 17:52:55 +02003194 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_forward_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003195 }
3196
3197 return 1;
3198}
3199
3200/* Just check the input and prepare the stack for the previous
3201 * function "hlua_channel_forward_yield"
3202 */
3203__LJMP static int hlua_channel_forward(lua_State *L)
3204{
3205 MAY_LJMP(check_args(L, 2, "forward"));
3206 MAY_LJMP(hlua_checkchannel(L, 1));
3207 MAY_LJMP(luaL_checkinteger(L, 2));
3208
3209 lua_pushinteger(L, 0);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003210 return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003211}
3212
3213/* Just returns the number of bytes available in the input
3214 * side of the buffer. This function never fails.
3215 */
3216__LJMP static int hlua_channel_get_in_len(lua_State *L)
3217{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003218 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003219
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003220 MAY_LJMP(check_args(L, 1, "get_in_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003221 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Fauleta3ceac12018-12-14 13:39:09 +01003222 if (IS_HTX_STRM(chn_strm(chn))) {
3223 struct htx *htx = htxbuf(&chn->buf);
3224 lua_pushinteger(L, htx->data - co_data(chn));
3225 }
3226 else
3227 lua_pushinteger(L, ci_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003228 return 1;
3229}
3230
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003231/* Returns true if the channel is full. */
3232__LJMP static int hlua_channel_is_full(lua_State *L)
3233{
3234 struct channel *chn;
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003235
3236 MAY_LJMP(check_args(L, 1, "is_full"));
3237 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Faulet0136ebb2020-02-26 11:59:19 +01003238 /* ignore the reserve, we are not on a producer side (ie in an
3239 * applet).
3240 */
3241 lua_pushboolean(L, channel_full(chn, 0));
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003242 return 1;
3243}
3244
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003245/* Just returns the number of bytes available in the output
3246 * side of the buffer. This function never fails.
3247 */
3248__LJMP static int hlua_channel_get_out_len(lua_State *L)
3249{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003250 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003251
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003252 MAY_LJMP(check_args(L, 1, "get_out_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003253 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Willy Tarreaua79021a2018-06-15 18:07:57 +02003254 lua_pushinteger(L, co_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003255 return 1;
3256}
3257
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003258/*
3259 *
3260 *
3261 * Class Fetches
3262 *
3263 *
3264 */
3265
3266/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003267 * a class stream, otherwise it throws an error.
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003268 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003269__LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003270{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003271 return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003272}
3273
3274/* This function creates and push in the stack a fetch object according
3275 * with a current TXN.
3276 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003277static int hlua_fetches_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003278{
Willy Tarreau7073c472015-04-06 11:15:40 +02003279 struct hlua_smp *hsmp;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003280
3281 /* Check stack size. */
3282 if (!lua_checkstack(L, 3))
3283 return 0;
3284
3285 /* Create the object: obj[0] = userdata.
3286 * Note that the base of the Fetches object is the
3287 * transaction object.
3288 */
3289 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003290 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003291 lua_rawseti(L, -2, 0);
3292
Willy Tarreau7073c472015-04-06 11:15:40 +02003293 hsmp->s = txn->s;
3294 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003295 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003296 hsmp->flags = flags;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003297
3298 /* Pop a class sesison metatable and affect it to the userdata. */
3299 lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref);
3300 lua_setmetatable(L, -2);
3301
3302 return 1;
3303}
3304
3305/* This function is an LUA binding. It is called with each sample-fetch.
3306 * It uses closure argument to store the associated sample-fetch. It
3307 * returns only one argument or throws an error. An error is thrown
3308 * only if an error is encountered during the argument parsing. If
3309 * the "sample-fetch" function fails, nil is returned.
3310 */
3311__LJMP static int hlua_run_sample_fetch(lua_State *L)
3312{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003313 struct hlua_smp *hsmp;
Willy Tarreau2ec22742015-03-10 14:27:20 +01003314 struct sample_fetch *f;
Frédéric Lécaillef874a832018-06-15 13:56:04 +02003315 struct arg args[ARGM_NBARGS + 1] = {{0}};
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003316 int i;
3317 struct sample smp;
3318
3319 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003320 f = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003321
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003322 /* Get traditional arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003323 hsmp = MAY_LJMP(hlua_checkfetches(L, 1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003324
Thierry FOURNIERca988662015-12-20 18:43:03 +01003325 /* Check execution authorization. */
3326 if (f->use & SMP_USE_HTTP_ANY &&
3327 !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) {
3328 lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which "
3329 "is not available in Lua services", f->kw);
3330 WILL_LJMP(lua_error(L));
3331 }
3332
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003333 /* Get extra arguments. */
3334 for (i = 0; i < lua_gettop(L) - 1; i++) {
3335 if (i >= ARGM_NBARGS)
3336 break;
3337 hlua_lua2arg(L, i + 2, &args[i]);
3338 }
3339 args[i].type = ARGT_STOP;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003340 args[i].data.str.area = NULL;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003341
3342 /* Check arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003343 MAY_LJMP(hlua_lua2arg_check(L, 2, args, f->arg_mask, hsmp->p));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003344
3345 /* Run the special args checker. */
Willy Tarreau2ec22742015-03-10 14:27:20 +01003346 if (f->val_args && !f->val_args(args, NULL)) {
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003347 lua_pushfstring(L, "error in arguments");
3348 WILL_LJMP(lua_error(L));
3349 }
3350
3351 /* Initialise the sample. */
3352 memset(&smp, 0, sizeof(smp));
3353
3354 /* Run the sample fetch process. */
Willy Tarreau1777ea62016-03-10 16:15:46 +01003355 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003356 if (!f->process(args, &smp, f->kw, f->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003357 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003358 lua_pushstring(L, "");
3359 else
3360 lua_pushnil(L);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003361 return 1;
3362 }
3363
3364 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003365 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003366 hlua_smp2lua_str(L, &smp);
3367 else
3368 hlua_smp2lua(L, &smp);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003369 return 1;
3370}
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003371
3372/*
3373 *
3374 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003375 * Class Converters
3376 *
3377 *
3378 */
3379
3380/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003381 * a class stream, otherwise it throws an error.
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003382 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003383__LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003384{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003385 return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003386}
3387
3388/* This function creates and push in the stack a Converters object
3389 * according with a current TXN.
3390 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003391static int hlua_converters_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003392{
Willy Tarreau7073c472015-04-06 11:15:40 +02003393 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003394
3395 /* Check stack size. */
3396 if (!lua_checkstack(L, 3))
3397 return 0;
3398
3399 /* Create the object: obj[0] = userdata.
3400 * Note that the base of the Converters object is the
3401 * same than the TXN object.
3402 */
3403 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003404 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003405 lua_rawseti(L, -2, 0);
3406
Willy Tarreau7073c472015-04-06 11:15:40 +02003407 hsmp->s = txn->s;
3408 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003409 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003410 hsmp->flags = flags;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003411
Willy Tarreau87b09662015-04-03 00:22:06 +02003412 /* Pop a class stream metatable and affect it to the table. */
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003413 lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref);
3414 lua_setmetatable(L, -2);
3415
3416 return 1;
3417}
3418
3419/* This function is an LUA binding. It is called with each converter.
3420 * It uses closure argument to store the associated converter. It
3421 * returns only one argument or throws an error. An error is thrown
3422 * only if an error is encountered during the argument parsing. If
3423 * the converter function function fails, nil is returned.
3424 */
3425__LJMP static int hlua_run_sample_conv(lua_State *L)
3426{
Willy Tarreauda5f1082015-04-06 11:17:13 +02003427 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003428 struct sample_conv *conv;
Frédéric Lécaillef874a832018-06-15 13:56:04 +02003429 struct arg args[ARGM_NBARGS + 1] = {{0}};
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003430 int i;
3431 struct sample smp;
3432
3433 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003434 conv = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003435
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003436 /* Get traditional arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003437 hsmp = MAY_LJMP(hlua_checkconverters(L, 1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003438
3439 /* Get extra arguments. */
3440 for (i = 0; i < lua_gettop(L) - 2; i++) {
3441 if (i >= ARGM_NBARGS)
3442 break;
3443 hlua_lua2arg(L, i + 3, &args[i]);
3444 }
3445 args[i].type = ARGT_STOP;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003446 args[i].data.str.area = NULL;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003447
3448 /* Check arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003449 MAY_LJMP(hlua_lua2arg_check(L, 3, args, conv->arg_mask, hsmp->p));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003450
3451 /* Run the special args checker. */
3452 if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) {
3453 hlua_pusherror(L, "error in arguments");
3454 WILL_LJMP(lua_error(L));
3455 }
3456
3457 /* Initialise the sample. */
3458 if (!hlua_lua2smp(L, 2, &smp)) {
3459 hlua_pusherror(L, "error in the input argument");
3460 WILL_LJMP(lua_error(L));
3461 }
3462
Willy Tarreau1777ea62016-03-10 16:15:46 +01003463 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
3464
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003465 /* Apply expected cast. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003466 if (!sample_casts[smp.data.type][conv->in_type]) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003467 hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'",
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003468 smp_to_type[smp.data.type], smp_to_type[conv->in_type]);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003469 WILL_LJMP(lua_error(L));
3470 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003471 if (sample_casts[smp.data.type][conv->in_type] != c_none &&
3472 !sample_casts[smp.data.type][conv->in_type](&smp)) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003473 hlua_pusherror(L, "error during the input argument casting");
3474 WILL_LJMP(lua_error(L));
3475 }
3476
3477 /* Run the sample conversion process. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003478 if (!conv->process(args, &smp, conv->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003479 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003480 lua_pushstring(L, "");
3481 else
Willy Tarreaua678b432015-08-28 10:14:59 +02003482 lua_pushnil(L);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003483 return 1;
3484 }
3485
3486 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003487 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003488 hlua_smp2lua_str(L, &smp);
3489 else
3490 hlua_smp2lua(L, &smp);
Willy Tarreaua678b432015-08-28 10:14:59 +02003491 return 1;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003492}
3493
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003494/*
3495 *
3496 *
3497 * Class AppletTCP
3498 *
3499 *
3500 */
3501
3502/* Returns a struct hlua_txn if the stack entry "ud" is
3503 * a class stream, otherwise it throws an error.
3504 */
3505__LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud)
3506{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003507 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003508}
3509
3510/* This function creates and push in the stack an Applet object
3511 * according with a current TXN.
3512 */
3513static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx)
3514{
3515 struct hlua_appctx *appctx;
3516 struct stream_interface *si = ctx->owner;
3517 struct stream *s = si_strm(si);
3518 struct proxy *p = s->be;
3519
3520 /* Check stack size. */
3521 if (!lua_checkstack(L, 3))
3522 return 0;
3523
3524 /* Create the object: obj[0] = userdata.
3525 * Note that the base of the Converters object is the
3526 * same than the TXN object.
3527 */
3528 lua_newtable(L);
3529 appctx = lua_newuserdata(L, sizeof(*appctx));
3530 lua_rawseti(L, -2, 0);
3531 appctx->appctx = ctx;
3532 appctx->htxn.s = s;
3533 appctx->htxn.p = p;
3534
3535 /* Create the "f" field that contains a list of fetches. */
3536 lua_pushstring(L, "f");
3537 if (!hlua_fetches_new(L, &appctx->htxn, 0))
3538 return 0;
3539 lua_settable(L, -3);
3540
3541 /* Create the "sf" field that contains a list of stringsafe fetches. */
3542 lua_pushstring(L, "sf");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003543 if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003544 return 0;
3545 lua_settable(L, -3);
3546
3547 /* Create the "c" field that contains a list of converters. */
3548 lua_pushstring(L, "c");
3549 if (!hlua_converters_new(L, &appctx->htxn, 0))
3550 return 0;
3551 lua_settable(L, -3);
3552
3553 /* Create the "sc" field that contains a list of stringsafe converters. */
3554 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003555 if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003556 return 0;
3557 lua_settable(L, -3);
3558
3559 /* Pop a class stream metatable and affect it to the table. */
3560 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref);
3561 lua_setmetatable(L, -2);
3562
3563 return 1;
3564}
3565
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003566__LJMP static int hlua_applet_tcp_set_var(lua_State *L)
3567{
3568 struct hlua_appctx *appctx;
3569 struct stream *s;
3570 const char *name;
3571 size_t len;
3572 struct sample smp;
3573
3574 MAY_LJMP(check_args(L, 3, "set_var"));
3575
3576 /* It is useles to retrieve the stream, but this function
3577 * runs only in a stream context.
3578 */
3579 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3580 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3581 s = appctx->htxn.s;
3582
3583 /* Converts the third argument in a sample. */
3584 hlua_lua2smp(L, 3, &smp);
3585
3586 /* Store the sample in a variable. */
3587 smp_set_owner(&smp, s->be, s->sess, s, 0);
3588 vars_set_by_name(name, len, &smp);
3589 return 0;
3590}
3591
3592__LJMP static int hlua_applet_tcp_unset_var(lua_State *L)
3593{
3594 struct hlua_appctx *appctx;
3595 struct stream *s;
3596 const char *name;
3597 size_t len;
3598 struct sample smp;
3599
3600 MAY_LJMP(check_args(L, 2, "unset_var"));
3601
3602 /* It is useles to retrieve the stream, but this function
3603 * runs only in a stream context.
3604 */
3605 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3606 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3607 s = appctx->htxn.s;
3608
3609 /* Unset the variable. */
3610 smp_set_owner(&smp, s->be, s->sess, s, 0);
3611 vars_unset_by_name(name, len, &smp);
3612 return 0;
3613}
3614
3615__LJMP static int hlua_applet_tcp_get_var(lua_State *L)
3616{
3617 struct hlua_appctx *appctx;
3618 struct stream *s;
3619 const char *name;
3620 size_t len;
3621 struct sample smp;
3622
3623 MAY_LJMP(check_args(L, 2, "get_var"));
3624
3625 /* It is useles to retrieve the stream, but this function
3626 * runs only in a stream context.
3627 */
3628 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3629 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3630 s = appctx->htxn.s;
3631
3632 smp_set_owner(&smp, s->be, s->sess, s, 0);
3633 if (!vars_get_by_name(name, len, &smp)) {
3634 lua_pushnil(L);
3635 return 1;
3636 }
3637
3638 return hlua_smp2lua(L, &smp);
3639}
3640
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003641__LJMP static int hlua_applet_tcp_set_priv(lua_State *L)
3642{
3643 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3644 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003645 struct hlua *hlua;
3646
3647 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003648 if (!s->hlua)
3649 return 0;
3650 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003651
3652 MAY_LJMP(check_args(L, 2, "set_priv"));
3653
3654 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02003655 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003656
3657 /* Get and store new value. */
3658 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
3659 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
3660
3661 return 0;
3662}
3663
3664__LJMP static int hlua_applet_tcp_get_priv(lua_State *L)
3665{
3666 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3667 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003668 struct hlua *hlua;
3669
3670 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003671 if (!s->hlua) {
3672 lua_pushnil(L);
3673 return 1;
3674 }
3675 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003676
3677 /* Push configuration index in the stack. */
3678 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
3679
3680 return 1;
3681}
3682
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003683/* If expected data not yet available, it returns a yield. This function
3684 * consumes the data in the buffer. It returns a string containing the
3685 * data. This string can be empty.
3686 */
3687__LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx)
3688{
3689 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3690 struct stream_interface *si = appctx->appctx->owner;
3691 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02003692 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003693 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02003694 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003695 size_t len2;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003696
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003697 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003698 ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003699
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003700 /* Data not yet available. return yield. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003701 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003702 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003703 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_getline_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003704 }
3705
3706 /* End of data: commit the total strings and return. */
3707 if (ret < 0) {
3708 luaL_pushresult(&appctx->b);
3709 return 1;
3710 }
3711
3712 /* Ensure that the block 2 length is usable. */
3713 if (ret == 1)
3714 len2 = 0;
3715
3716 /* dont check the max length read and dont check. */
3717 luaL_addlstring(&appctx->b, blk1, len1);
3718 luaL_addlstring(&appctx->b, blk2, len2);
3719
3720 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003721 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003722 luaL_pushresult(&appctx->b);
3723 return 1;
3724}
3725
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003726/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003727__LJMP static int hlua_applet_tcp_getline(lua_State *L)
3728{
3729 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3730
3731 /* Initialise the string catenation. */
3732 luaL_buffinit(L, &appctx->b);
3733
3734 return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0));
3735}
3736
3737/* If expected data not yet available, it returns a yield. This function
3738 * consumes the data in the buffer. It returns a string containing the
3739 * data. This string can be empty.
3740 */
3741__LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx)
3742{
3743 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3744 struct stream_interface *si = appctx->appctx->owner;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003745 size_t len = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003746 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02003747 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003748 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02003749 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003750 size_t len2;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003751
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003752 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003753 ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003754
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003755 /* Data not yet available. return yield. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003756 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003757 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003758 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003759 }
3760
3761 /* End of data: commit the total strings and return. */
3762 if (ret < 0) {
3763 luaL_pushresult(&appctx->b);
3764 return 1;
3765 }
3766
3767 /* Ensure that the block 2 length is usable. */
3768 if (ret == 1)
3769 len2 = 0;
3770
3771 if (len == -1) {
3772
3773 /* If len == -1, catenate all the data avalaile and
3774 * yield because we want to get all the data until
3775 * the end of data stream.
3776 */
3777 luaL_addlstring(&appctx->b, blk1, len1);
3778 luaL_addlstring(&appctx->b, blk2, len2);
Willy Tarreau06d80a92017-10-19 14:32:15 +02003779 co_skip(si_oc(si), len1 + len2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003780 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003781 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003782
3783 } else {
3784
3785 /* Copy the fisrt block caping to the length required. */
3786 if (len1 > len)
3787 len1 = len;
3788 luaL_addlstring(&appctx->b, blk1, len1);
3789 len -= len1;
3790
3791 /* Copy the second block. */
3792 if (len2 > len)
3793 len2 = len;
3794 luaL_addlstring(&appctx->b, blk2, len2);
3795 len -= len2;
3796
3797 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003798 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003799
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003800 /* If there is no other data available, yield waiting for new data. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003801 if (len > 0) {
3802 lua_pushinteger(L, len);
3803 lua_replace(L, 2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003804 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003805 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003806 }
3807
3808 /* return the result. */
3809 luaL_pushresult(&appctx->b);
3810 return 1;
3811 }
3812
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003813 /* we never execute this */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003814 hlua_pusherror(L, "Lua: internal error");
3815 WILL_LJMP(lua_error(L));
3816 return 0;
3817}
3818
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003819/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003820__LJMP static int hlua_applet_tcp_recv(lua_State *L)
3821{
3822 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3823 int len = -1;
3824
3825 if (lua_gettop(L) > 2)
3826 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
3827 if (lua_gettop(L) >= 2) {
3828 len = MAY_LJMP(luaL_checkinteger(L, 2));
3829 lua_pop(L, 1);
3830 }
3831
3832 /* Confirm or set the required length */
3833 lua_pushinteger(L, len);
3834
3835 /* Initialise the string catenation. */
3836 luaL_buffinit(L, &appctx->b);
3837
3838 return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0));
3839}
3840
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003841/* Append data in the output side of the buffer. This data is immediately
3842 * sent. The function returns the amount of data written. If the buffer
3843 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003844 * if the channel is closed.
3845 */
3846__LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx)
3847{
3848 size_t len;
3849 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3850 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
3851 int l = MAY_LJMP(luaL_checkinteger(L, 3));
3852 struct stream_interface *si = appctx->appctx->owner;
3853 struct channel *chn = si_ic(si);
3854 int max;
3855
3856 /* Get the max amount of data which can write as input in the channel. */
3857 max = channel_recv_max(chn);
3858 if (max > (len - l))
3859 max = len - l;
3860
3861 /* Copy data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003862 ci_putblk(chn, str + l, max);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003863
3864 /* update counters. */
3865 l += max;
3866 lua_pop(L, 1);
3867 lua_pushinteger(L, l);
3868
3869 /* If some data is not send, declares the situation to the
3870 * applet, and returns a yield.
3871 */
3872 if (l < len) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003873 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003874 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003875 }
3876
3877 return 1;
3878}
3879
3880/* Just a wraper of "hlua_applet_tcp_send_yield". This wrapper permits
3881 * yield the LUA process, and resume it without checking the
3882 * input arguments.
3883 */
3884__LJMP static int hlua_applet_tcp_send(lua_State *L)
3885{
3886 MAY_LJMP(check_args(L, 2, "send"));
3887 lua_pushinteger(L, 0);
3888
3889 return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0));
3890}
3891
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003892/*
3893 *
3894 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003895 * Class AppletHTTP
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003896 *
3897 *
3898 */
3899
3900/* Returns a struct hlua_txn if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003901 * a class stream, otherwise it throws an error.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003902 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003903__LJMP static struct hlua_appctx *hlua_checkapplet_http(lua_State *L, int ud)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003904{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003905 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003906}
3907
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003908/* This function creates and push in the stack an Applet object
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003909 * according with a current TXN.
3910 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003911static int hlua_applet_http_new(lua_State *L, struct appctx *ctx)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003912{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003913 struct hlua_appctx *appctx;
Thierry FOURNIER841475e2015-12-11 17:10:09 +01003914 struct hlua_txn htxn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003915 struct stream_interface *si = ctx->owner;
3916 struct stream *s = si_strm(si);
3917 struct proxy *px = s->be;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003918
3919 /* Check stack size. */
3920 if (!lua_checkstack(L, 3))
3921 return 0;
3922
3923 /* Create the object: obj[0] = userdata.
3924 * Note that the base of the Converters object is the
3925 * same than the TXN object.
3926 */
3927 lua_newtable(L);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003928 appctx = lua_newuserdata(L, sizeof(*appctx));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003929 lua_rawseti(L, -2, 0);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003930 appctx->appctx = ctx;
3931 appctx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */
Robin H. Johnson52f5db22017-01-01 13:10:52 -08003932 appctx->appctx->ctx.hlua_apphttp.reason = NULL; /* Use default reason based on status */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003933 appctx->htxn.s = s;
3934 appctx->htxn.p = px;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003935
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003936 /* Create the "f" field that contains a list of fetches. */
3937 lua_pushstring(L, "f");
3938 if (!hlua_fetches_new(L, &appctx->htxn, 0))
3939 return 0;
3940 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003941
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003942 /* Create the "sf" field that contains a list of stringsafe fetches. */
3943 lua_pushstring(L, "sf");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003944 if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003945 return 0;
3946 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003947
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003948 /* Create the "c" field that contains a list of converters. */
3949 lua_pushstring(L, "c");
3950 if (!hlua_converters_new(L, &appctx->htxn, 0))
3951 return 0;
3952 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003953
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003954 /* Create the "sc" field that contains a list of stringsafe converters. */
3955 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003956 if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003957 return 0;
3958 lua_settable(L, -3);
Willy Tarreaueee5b512015-04-03 23:46:31 +02003959
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003960 if (IS_HTX_STRM(s)) {
3961 /* HTX version */
3962 struct htx *htx = htxbuf(&s->req.buf);
Christopher Faulet29f17582019-05-23 11:03:26 +02003963 struct htx_blk *blk;
3964 struct htx_sl *sl;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003965 struct ist path;
3966 unsigned long long len = 0;
3967 int32_t pos;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003968
Christopher Faulet29f17582019-05-23 11:03:26 +02003969 blk = htx_get_first_blk(htx);
Christopher Faulet43a686d2019-11-18 15:50:25 +01003970 BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL);
Christopher Faulet29f17582019-05-23 11:03:26 +02003971 sl = htx_get_blk_ptr(htx, blk);
3972
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003973 /* Stores the request method. */
3974 lua_pushstring(L, "method");
3975 lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl));
3976 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003977
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003978 /* Stores the http version. */
3979 lua_pushstring(L, "version");
3980 lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl));
3981 lua_settable(L, -3);
Thierry FOURNIER841475e2015-12-11 17:10:09 +01003982
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003983 /* creates an array of headers. hlua_http_get_headers() crates and push
3984 * the array on the top of the stack.
3985 */
3986 lua_pushstring(L, "headers");
3987 htxn.s = s;
3988 htxn.p = px;
3989 htxn.dir = SMP_OPT_DIR_REQ;
3990 if (!hlua_http_get_headers(L, &htxn, &htxn.s->txn->req))
3991 return 0;
3992 lua_settable(L, -3);
3993
3994 path = http_get_path(htx_sl_req_uri(sl));
3995 if (path.ptr) {
3996 char *p, *q, *end;
3997
3998 p = path.ptr;
3999 end = path.ptr + path.len;
4000 q = p;
4001 while (q < end && *q != '?')
4002 q++;
4003
4004 /* Stores the request path. */
4005 lua_pushstring(L, "path");
4006 lua_pushlstring(L, p, q - p);
4007 lua_settable(L, -3);
4008
4009 /* Stores the query string. */
4010 lua_pushstring(L, "qs");
4011 if (*q == '?')
4012 q++;
4013 lua_pushlstring(L, q, end - q);
4014 lua_settable(L, -3);
4015 }
4016
Christopher Fauleta3f15502019-05-13 15:27:23 +02004017 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004018 struct htx_blk *blk = htx_get_blk(htx, pos);
4019 enum htx_blk_type type = htx_get_blk_type(blk);
4020
Christopher Faulet54b5e212019-06-04 10:08:28 +02004021 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004022 break;
4023 if (type == HTX_BLK_DATA)
4024 len += htx_get_blksz(blk);
4025 }
4026 if (htx->extra != ULLONG_MAX)
4027 len += htx->extra;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004028
Thierry FOURNIER7d388632017-02-22 02:06:16 +01004029 /* Stores the request path. */
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004030 lua_pushstring(L, "length");
4031 lua_pushinteger(L, len);
4032 lua_settable(L, -3);
4033 }
4034 else {
4035 /* Legacy HTTP version */
4036 struct http_txn *txn = s->txn;
4037 const char *path;
4038 const char *end;
4039 const char *p;
4040
4041 /* Stores the request method. */
4042 lua_pushstring(L, "method");
4043 lua_pushlstring(L, ci_head(txn->req.chn), txn->req.sl.rq.m_l);
Thierry FOURNIER7d388632017-02-22 02:06:16 +01004044 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004045
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004046 /* Stores the http version. */
4047 lua_pushstring(L, "version");
4048 lua_pushlstring(L, ci_head(txn->req.chn) + txn->req.sl.rq.v, txn->req.sl.rq.v_l);
Thierry FOURNIER7d388632017-02-22 02:06:16 +01004049 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01004050
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004051 /* creates an array of headers. hlua_http_get_headers() crates and push
4052 * the array on the top of the stack.
4053 */
4054 lua_pushstring(L, "headers");
4055 htxn.s = s;
4056 htxn.p = px;
4057 htxn.dir = SMP_OPT_DIR_REQ;
4058 if (!hlua_http_get_headers(L, &htxn, &htxn.s->txn->req))
4059 return 0;
4060 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01004061
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004062 /* Get path and qs */
4063 path = http_txn_get_path(txn);
4064 if (path) {
4065 end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
4066 p = path;
4067 while (p < end && *p != '?')
4068 p++;
4069
4070 /* Stores the request path. */
4071 lua_pushstring(L, "path");
4072 lua_pushlstring(L, path, p - path);
4073 lua_settable(L, -3);
4074
4075 /* Stores the query string. */
4076 lua_pushstring(L, "qs");
4077 if (*p == '?')
4078 p++;
4079 lua_pushlstring(L, p, end - p);
4080 lua_settable(L, -3);
4081 }
4082
4083 /* Stores the request path. */
4084 lua_pushstring(L, "length");
4085 lua_pushinteger(L, txn->req.body_len);
4086 lua_settable(L, -3);
4087 }
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01004088
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004089 /* Create an empty array of HTTP request headers. */
4090 lua_pushstring(L, "response");
4091 lua_newtable(L);
4092 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01004093
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004094 /* Pop a class stream metatable and affect it to the table. */
4095 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref);
4096 lua_setmetatable(L, -2);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004097
4098 return 1;
4099}
4100
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004101__LJMP static int hlua_applet_http_set_var(lua_State *L)
4102{
4103 struct hlua_appctx *appctx;
4104 struct stream *s;
4105 const char *name;
4106 size_t len;
4107 struct sample smp;
4108
4109 MAY_LJMP(check_args(L, 3, "set_var"));
4110
4111 /* It is useles to retrieve the stream, but this function
4112 * runs only in a stream context.
4113 */
4114 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4115 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4116 s = appctx->htxn.s;
4117
4118 /* Converts the third argument in a sample. */
4119 hlua_lua2smp(L, 3, &smp);
4120
4121 /* Store the sample in a variable. */
4122 smp_set_owner(&smp, s->be, s->sess, s, 0);
4123 vars_set_by_name(name, len, &smp);
4124 return 0;
4125}
4126
4127__LJMP static int hlua_applet_http_unset_var(lua_State *L)
4128{
4129 struct hlua_appctx *appctx;
4130 struct stream *s;
4131 const char *name;
4132 size_t len;
4133 struct sample smp;
4134
4135 MAY_LJMP(check_args(L, 2, "unset_var"));
4136
4137 /* It is useles to retrieve the stream, but this function
4138 * runs only in a stream context.
4139 */
4140 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4141 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4142 s = appctx->htxn.s;
4143
4144 /* Unset the variable. */
4145 smp_set_owner(&smp, s->be, s->sess, s, 0);
4146 vars_unset_by_name(name, len, &smp);
4147 return 0;
4148}
4149
4150__LJMP static int hlua_applet_http_get_var(lua_State *L)
4151{
4152 struct hlua_appctx *appctx;
4153 struct stream *s;
4154 const char *name;
4155 size_t len;
4156 struct sample smp;
4157
4158 MAY_LJMP(check_args(L, 2, "get_var"));
4159
4160 /* It is useles to retrieve the stream, but this function
4161 * runs only in a stream context.
4162 */
4163 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4164 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4165 s = appctx->htxn.s;
4166
4167 smp_set_owner(&smp, s->be, s->sess, s, 0);
4168 if (!vars_get_by_name(name, len, &smp)) {
4169 lua_pushnil(L);
4170 return 1;
4171 }
4172
4173 return hlua_smp2lua(L, &smp);
4174}
4175
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004176__LJMP static int hlua_applet_http_set_priv(lua_State *L)
4177{
4178 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4179 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004180 struct hlua *hlua;
4181
4182 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004183 if (!s->hlua)
4184 return 0;
4185 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004186
4187 MAY_LJMP(check_args(L, 2, "set_priv"));
4188
4189 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02004190 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004191
4192 /* Get and store new value. */
4193 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
4194 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
4195
4196 return 0;
4197}
4198
4199__LJMP static int hlua_applet_http_get_priv(lua_State *L)
4200{
4201 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4202 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004203 struct hlua *hlua;
4204
4205 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004206 if (!s->hlua) {
4207 lua_pushnil(L);
4208 return 1;
4209 }
4210 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004211
4212 /* Push configuration index in the stack. */
4213 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
4214
4215 return 1;
4216}
4217
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004218/* If expected data not yet available, it returns a yield. This function
4219 * consumes the data in the buffer. It returns a string containing the
4220 * data. This string can be empty.
4221 */
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004222__LJMP static int hlua_applet_htx_getline_yield(lua_State *L, int status, lua_KContext ctx)
4223{
4224 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4225 struct stream_interface *si = appctx->appctx->owner;
4226 struct channel *req = si_oc(si);
4227 struct htx *htx;
4228 struct htx_blk *blk;
4229 size_t count;
4230 int stop = 0;
4231
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004232 htx = htx_from_buf(&req->buf);
4233 count = co_data(req);
Christopher Fauleta3f15502019-05-13 15:27:23 +02004234 blk = htx_get_first_blk(htx);
Christopher Fauletcc26b132018-12-18 21:20:57 +01004235
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004236 while (count && !stop && blk) {
4237 enum htx_blk_type type = htx_get_blk_type(blk);
4238 uint32_t sz = htx_get_blksz(blk);
4239 struct ist v;
4240 uint32_t vlen;
4241 char *nl;
4242
Christopher Faulete461e342018-12-18 16:43:35 +01004243 if (type == HTX_BLK_EOM) {
4244 stop = 1;
4245 break;
4246 }
4247
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004248 vlen = sz;
4249 if (vlen > count) {
4250 if (type != HTX_BLK_DATA)
4251 break;
4252 vlen = count;
4253 }
4254
4255 switch (type) {
4256 case HTX_BLK_UNUSED:
4257 break;
4258
4259 case HTX_BLK_DATA:
4260 v = htx_get_blk_value(htx, blk);
4261 v.len = vlen;
4262 nl = istchr(v, '\n');
4263 if (nl != NULL) {
4264 stop = 1;
4265 vlen = nl - v.ptr + 1;
4266 }
4267 luaL_addlstring(&appctx->b, v.ptr, vlen);
4268 break;
4269
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004270 case HTX_BLK_TLR:
4271 case HTX_BLK_EOM:
4272 stop = 1;
4273 break;
4274
4275 default:
4276 break;
4277 }
4278
4279 co_set_data(req, co_data(req) - vlen);
4280 count -= vlen;
4281 if (sz == vlen)
4282 blk = htx_remove_blk(htx, blk);
4283 else {
4284 htx_cut_data_blk(htx, blk, vlen);
4285 break;
4286 }
4287 }
4288
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004289 htx_to_buf(htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004290 if (!stop) {
4291 si_cant_get(si);
4292 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_htx_getline_yield, TICK_ETERNITY, 0));
4293 }
4294
4295 /* return the result. */
4296 luaL_pushresult(&appctx->b);
4297 return 1;
4298}
4299
4300
4301/* If expected data not yet available, it returns a yield. This function
4302 * consumes the data in the buffer. It returns a string containing the
4303 * data. This string can be empty.
4304 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004305__LJMP static int hlua_applet_http_getline_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004306{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004307 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4308 struct stream_interface *si = appctx->appctx->owner;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004309 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02004310 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02004311 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02004312 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02004313 size_t len2;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004314
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004315 /* Check for the end of the data. */
4316 if (appctx->appctx->ctx.hlua_apphttp.left_bytes <= 0) {
4317 luaL_pushresult(&appctx->b);
4318 return 1;
4319 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004320
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004321 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004322 ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004323
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004324 /* Data not yet available. return yield. */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004325 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01004326 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004327 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_getline_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004328 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004329
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004330 /* End of data: commit the total strings and return. */
4331 if (ret < 0) {
4332 luaL_pushresult(&appctx->b);
4333 return 1;
4334 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004335
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004336 /* Ensure that the block 2 length is usable. */
4337 if (ret == 1)
4338 len2 = 0;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004339
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004340 /* Copy the fisrt block caping to the length required. */
4341 if (len1 > appctx->appctx->ctx.hlua_apphttp.left_bytes)
4342 len1 = appctx->appctx->ctx.hlua_apphttp.left_bytes;
4343 luaL_addlstring(&appctx->b, blk1, len1);
4344 appctx->appctx->ctx.hlua_apphttp.left_bytes -= len1;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004345
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004346 /* Copy the second block. */
4347 if (len2 > appctx->appctx->ctx.hlua_apphttp.left_bytes)
4348 len2 = appctx->appctx->ctx.hlua_apphttp.left_bytes;
4349 luaL_addlstring(&appctx->b, blk2, len2);
4350 appctx->appctx->ctx.hlua_apphttp.left_bytes -= len2;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004351
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004352 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004353 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004354 luaL_pushresult(&appctx->b);
4355 return 1;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004356}
4357
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004358/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004359__LJMP static int hlua_applet_http_getline(lua_State *L)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004360{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004361 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004362
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004363 /* Initialise the string catenation. */
4364 luaL_buffinit(L, &appctx->b);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004365
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004366 if (IS_HTX_STRM(si_strm(appctx->appctx->owner)))
4367 return MAY_LJMP(hlua_applet_htx_getline_yield(L, 0, 0));
4368 else
4369 return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004370}
4371
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004372/* If expected data not yet available, it returns a yield. This function
4373 * consumes the data in the buffer. It returns a string containing the
4374 * data. This string can be empty.
4375 */
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004376__LJMP static int hlua_applet_htx_recv_yield(lua_State *L, int status, lua_KContext ctx)
4377{
4378 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4379 struct stream_interface *si = appctx->appctx->owner;
4380 struct channel *req = si_oc(si);
4381 struct htx *htx;
4382 struct htx_blk *blk;
4383 size_t count;
4384 int len;
4385
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004386 htx = htx_from_buf(&req->buf);
4387 len = MAY_LJMP(luaL_checkinteger(L, 2));
4388 count = co_data(req);
Christopher Faulet29f17582019-05-23 11:03:26 +02004389 blk = htx_get_head_blk(htx);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004390 while (count && len && blk) {
4391 enum htx_blk_type type = htx_get_blk_type(blk);
4392 uint32_t sz = htx_get_blksz(blk);
4393 struct ist v;
4394 uint32_t vlen;
4395
Christopher Faulete461e342018-12-18 16:43:35 +01004396 if (type == HTX_BLK_EOM) {
4397 len = 0;
4398 break;
4399 }
4400
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004401 vlen = sz;
4402 if (len > 0 && vlen > len)
4403 vlen = len;
4404 if (vlen > count) {
4405 if (type != HTX_BLK_DATA)
4406 break;
4407 vlen = count;
4408 }
4409
4410 switch (type) {
4411 case HTX_BLK_UNUSED:
4412 break;
4413
4414 case HTX_BLK_DATA:
4415 v = htx_get_blk_value(htx, blk);
4416 luaL_addlstring(&appctx->b, v.ptr, vlen);
4417 break;
4418
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004419 case HTX_BLK_TLR:
4420 case HTX_BLK_EOM:
4421 len = 0;
4422 break;
4423
4424 default:
4425 break;
4426 }
4427
4428 co_set_data(req, co_data(req) - vlen);
4429 count -= vlen;
4430 if (len > 0)
4431 len -= vlen;
4432 if (sz == vlen)
4433 blk = htx_remove_blk(htx, blk);
4434 else {
4435 htx_cut_data_blk(htx, blk, vlen);
4436 break;
4437 }
4438 }
4439
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004440 htx_to_buf(htx, &req->buf);
4441
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004442 /* If we are no other data available, yield waiting for new data. */
4443 if (len) {
4444 if (len > 0) {
4445 lua_pushinteger(L, len);
4446 lua_replace(L, 2);
4447 }
4448 si_cant_get(si);
4449 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_htx_recv_yield, TICK_ETERNITY, 0));
4450 }
4451
4452 /* return the result. */
4453 luaL_pushresult(&appctx->b);
4454 return 1;
4455}
4456
4457/* If expected data not yet available, it returns a yield. This function
4458 * consumes the data in the buffer. It returns a string containing the
4459 * data. This string can be empty.
4460 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004461__LJMP static int hlua_applet_http_recv_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004462{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004463 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4464 struct stream_interface *si = appctx->appctx->owner;
4465 int len = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004466 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02004467 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02004468 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02004469 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02004470 size_t len2;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004471
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004472 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004473 ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004474
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004475 /* Data not yet available. return yield. */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004476 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01004477 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004478 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004479 }
4480
4481 /* End of data: commit the total strings and return. */
4482 if (ret < 0) {
4483 luaL_pushresult(&appctx->b);
4484 return 1;
4485 }
4486
4487 /* Ensure that the block 2 length is usable. */
4488 if (ret == 1)
4489 len2 = 0;
4490
4491 /* Copy the fisrt block caping to the length required. */
4492 if (len1 > len)
4493 len1 = len;
4494 luaL_addlstring(&appctx->b, blk1, len1);
4495 len -= len1;
4496
4497 /* Copy the second block. */
4498 if (len2 > len)
4499 len2 = len;
4500 luaL_addlstring(&appctx->b, blk2, len2);
4501 len -= len2;
4502
4503 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004504 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004505 if (appctx->appctx->ctx.hlua_apphttp.left_bytes != -1)
4506 appctx->appctx->ctx.hlua_apphttp.left_bytes -= len;
4507
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004508 /* If we are no other data available, yield waiting for new data. */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004509 if (len > 0) {
4510 lua_pushinteger(L, len);
4511 lua_replace(L, 2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01004512 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004513 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004514 }
4515
4516 /* return the result. */
4517 luaL_pushresult(&appctx->b);
4518 return 1;
4519}
4520
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004521/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004522__LJMP static int hlua_applet_http_recv(lua_State *L)
4523{
4524 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4525 int len = -1;
4526
4527 /* Check arguments. */
4528 if (lua_gettop(L) > 2)
4529 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
4530 if (lua_gettop(L) >= 2) {
4531 len = MAY_LJMP(luaL_checkinteger(L, 2));
4532 lua_pop(L, 1);
4533 }
4534
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004535 if (IS_HTX_STRM(si_strm(appctx->appctx->owner))) {
4536 /* HTX version */
4537 lua_pushinteger(L, len);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004538
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004539 /* Initialise the string catenation. */
4540 luaL_buffinit(L, &appctx->b);
4541
4542 return MAY_LJMP(hlua_applet_htx_recv_yield(L, 0, 0));
4543 }
4544 else {
4545 /* Legacy HTTP version */
4546 /* Check the required length */
4547 if (len == -1 || len > appctx->appctx->ctx.hlua_apphttp.left_bytes)
4548 len = appctx->appctx->ctx.hlua_apphttp.left_bytes;
4549 lua_pushinteger(L, len);
4550
4551 /* Initialise the string catenation. */
4552 luaL_buffinit(L, &appctx->b);
4553
4554 return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0));
4555 }
4556}
4557
4558/* Append data in the output side of the buffer. This data is immediately
4559 * sent. The function returns the amount of data written. If the buffer
4560 * cannot contain the data, the function yields. The function returns -1
4561 * if the channel is closed.
4562 */
4563__LJMP static int hlua_applet_htx_send_yield(lua_State *L, int status, lua_KContext ctx)
4564{
4565 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4566 struct stream_interface *si = appctx->appctx->owner;
4567 struct channel *res = si_ic(si);
4568 struct htx *htx = htx_from_buf(&res->buf);
4569 const char *data;
4570 size_t len;
4571 int l = MAY_LJMP(luaL_checkinteger(L, 3));
4572 int max;
4573
Christopher Faulet4e963012019-07-03 11:39:30 +02004574 max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx));
Christopher Faulet4b0e9b22019-01-09 12:16:58 +01004575 if (!max)
4576 goto snd_yield;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004577
4578 data = MAY_LJMP(luaL_checklstring(L, 2, &len));
4579
4580 /* Get the max amount of data which can write as input in the channel. */
4581 if (max > (len - l))
4582 max = len - l;
4583
4584 /* Copy data. */
Willy Tarreau0a7ef022019-05-28 10:30:11 +02004585 max = htx_add_data(htx, ist2(data + l, max));
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01004586 channel_add_input(res, max);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004587
4588 /* update counters. */
4589 l += max;
4590 lua_pop(L, 1);
4591 lua_pushinteger(L, l);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004592
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004593 /* If some data is not send, declares the situation to the
4594 * applet, and returns a yield.
4595 */
4596 if (l < len) {
Christopher Faulet4b0e9b22019-01-09 12:16:58 +01004597 snd_yield:
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004598 htx_to_buf(htx, &res->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004599 si_rx_room_blk(si);
4600 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_htx_send_yield, TICK_ETERNITY, 0));
4601 }
4602
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004603 htx_to_buf(htx, &res->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004604 return 1;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004605}
4606
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004607/* Append data in the output side of the buffer. This data is immediately
4608 * sent. The function returns the amount of data written. If the buffer
4609 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004610 * if the channel is closed.
4611 */
4612__LJMP static int hlua_applet_http_send_yield(lua_State *L, int status, lua_KContext ctx)
4613{
4614 size_t len;
4615 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4616 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
4617 int l = MAY_LJMP(luaL_checkinteger(L, 3));
4618 struct stream_interface *si = appctx->appctx->owner;
4619 struct channel *chn = si_ic(si);
4620 int max;
4621
4622 /* Get the max amount of data which can write as input in the channel. */
4623 max = channel_recv_max(chn);
4624 if (max > (len - l))
4625 max = len - l;
4626
4627 /* Copy data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004628 ci_putblk(chn, str + l, max);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004629
4630 /* update counters. */
4631 l += max;
4632 lua_pop(L, 1);
4633 lua_pushinteger(L, l);
4634
4635 /* If some data is not send, declares the situation to the
4636 * applet, and returns a yield.
4637 */
4638 if (l < len) {
Willy Tarreaudb398432018-11-15 11:08:52 +01004639 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004640 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004641 }
4642
4643 return 1;
4644}
4645
4646/* Just a wraper of "hlua_applet_send_yield". This wrapper permits
4647 * yield the LUA process, and resume it without checking the
4648 * input arguments.
4649 */
4650__LJMP static int hlua_applet_http_send(lua_State *L)
4651{
4652 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004653
4654 /* We want to send some data. Headers must be sent. */
4655 if (!(appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
4656 hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data.");
4657 WILL_LJMP(lua_error(L));
4658 }
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004659
4660 if (IS_HTX_STRM(si_strm(appctx->appctx->owner))) {
4661 /* HTX version */
4662 /* This interger is used for followinf the amount of data sent. */
4663 lua_pushinteger(L, 0);
4664
4665 return MAY_LJMP(hlua_applet_htx_send_yield(L, 0, 0));
4666 }
4667 else {
4668 /* Legacy HTTP version */
4669 size_t len;
4670 char hex[10];
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004671
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004672 MAY_LJMP(luaL_checklstring(L, 2, &len));
4673
4674 /* If transfer encoding chunked is selected, we surround the data
4675 * by chunk data.
4676 */
4677 if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_CHUNKED) {
4678 snprintf(hex, 9, "%x", (unsigned int)len);
4679 lua_pushfstring(L, "%s\r\n", hex);
4680 lua_insert(L, 2); /* swap the last 2 entries. */
4681 lua_pushstring(L, "\r\n");
4682 lua_concat(L, 3);
4683 }
4684
4685 /* This interger is used for followinf the amount of data sent. */
4686 lua_pushinteger(L, 0);
4687
4688 return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0));
4689 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004690}
4691
4692__LJMP static int hlua_applet_http_addheader(lua_State *L)
4693{
4694 const char *name;
4695 int ret;
4696
4697 MAY_LJMP(hlua_checkapplet_http(L, 1));
4698 name = MAY_LJMP(luaL_checkstring(L, 2));
4699 MAY_LJMP(luaL_checkstring(L, 3));
4700
4701 /* Push in the stack the "response" entry. */
4702 ret = lua_getfield(L, 1, "response");
4703 if (ret != LUA_TTABLE) {
4704 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] "
4705 "is expected as an array. %s found", lua_typename(L, ret));
4706 WILL_LJMP(lua_error(L));
4707 }
4708
4709 /* check if the header is already registered if it is not
4710 * the case, register it.
4711 */
4712 ret = lua_getfield(L, -1, name);
4713 if (ret == LUA_TNIL) {
4714
4715 /* Entry not found. */
4716 lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */
4717
4718 /* Insert the new header name in the array in the top of the stack.
4719 * It left the new array in the top of the stack.
4720 */
4721 lua_newtable(L);
4722 lua_pushvalue(L, 2);
4723 lua_pushvalue(L, -2);
4724 lua_settable(L, -4);
4725
4726 } else if (ret != LUA_TTABLE) {
4727
4728 /* corruption error. */
4729 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] "
4730 "is expected as an array. %s found", name, lua_typename(L, ret));
4731 WILL_LJMP(lua_error(L));
4732 }
4733
4734 /* Now the top od thestack is an array of values. We push
4735 * the header value as new entry.
4736 */
4737 lua_pushvalue(L, 3);
4738 ret = lua_rawlen(L, -2);
4739 lua_rawseti(L, -2, ret + 1);
4740 lua_pushboolean(L, 1);
4741 return 1;
4742}
4743
4744__LJMP static int hlua_applet_http_status(lua_State *L)
4745{
4746 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4747 int status = MAY_LJMP(luaL_checkinteger(L, 2));
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004748 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004749
4750 if (status < 100 || status > 599) {
4751 lua_pushboolean(L, 0);
4752 return 1;
4753 }
4754
4755 appctx->appctx->ctx.hlua_apphttp.status = status;
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004756 appctx->appctx->ctx.hlua_apphttp.reason = reason;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004757 lua_pushboolean(L, 1);
4758 return 1;
4759}
4760
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004761
4762__LJMP static int hlua_applet_htx_send_response(lua_State *L)
4763{
4764 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4765 struct stream_interface *si = appctx->appctx->owner;
4766 struct channel *res = si_ic(si);
4767 struct htx *htx;
4768 struct htx_sl *sl;
4769 struct h1m h1m;
4770 const char *status, *reason;
4771 const char *name, *value;
4772 size_t nlen, vlen;
4773 unsigned int flags;
4774
4775 /* Send the message at once. */
4776 htx = htx_from_buf(&res->buf);
4777 h1m_init_res(&h1m);
4778
4779 /* Use the same http version than the request. */
4780 status = ultoa_r(appctx->appctx->ctx.hlua_apphttp.status, trash.area, trash.size);
4781 reason = appctx->appctx->ctx.hlua_apphttp.reason;
4782 if (reason == NULL)
4783 reason = http_get_reason(appctx->appctx->ctx.hlua_apphttp.status);
4784 if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) {
4785 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11);
4786 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason));
4787 }
4788 else {
4789 flags = HTX_SL_F_IS_RESP;
4790 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason));
4791 }
4792 if (!sl) {
4793 hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n",
4794 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4795 WILL_LJMP(lua_error(L));
4796 }
4797 sl->info.res.status = appctx->appctx->ctx.hlua_apphttp.status;
4798
4799 /* Get the array associated to the field "response" in the object AppletHTTP. */
4800 lua_pushvalue(L, 0);
4801 if (lua_getfield(L, 1, "response") != LUA_TTABLE) {
4802 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n",
4803 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4804 WILL_LJMP(lua_error(L));
4805 }
4806
4807 /* Browse the list of headers. */
4808 lua_pushnil(L);
4809 while(lua_next(L, -2) != 0) {
4810 /* We expect a string as -2. */
4811 if (lua_type(L, -2) != LUA_TSTRING) {
4812 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n",
4813 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4814 lua_typename(L, lua_type(L, -2)));
4815 WILL_LJMP(lua_error(L));
4816 }
4817 name = lua_tolstring(L, -2, &nlen);
4818
4819 /* We expect an array as -1. */
4820 if (lua_type(L, -1) != LUA_TTABLE) {
4821 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n",
4822 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4823 name,
4824 lua_typename(L, lua_type(L, -1)));
4825 WILL_LJMP(lua_error(L));
4826 }
4827
4828 /* Browse the table who is on the top of the stack. */
4829 lua_pushnil(L);
4830 while(lua_next(L, -2) != 0) {
4831 int id;
4832
4833 /* We expect a number as -2. */
4834 if (lua_type(L, -2) != LUA_TNUMBER) {
4835 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n",
4836 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4837 name,
4838 lua_typename(L, lua_type(L, -2)));
4839 WILL_LJMP(lua_error(L));
4840 }
4841 id = lua_tointeger(L, -2);
4842
4843 /* We expect a string as -2. */
4844 if (lua_type(L, -1) != LUA_TSTRING) {
4845 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n",
4846 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4847 name, id,
4848 lua_typename(L, lua_type(L, -1)));
4849 WILL_LJMP(lua_error(L));
4850 }
4851 value = lua_tolstring(L, -1, &vlen);
4852
4853 /* Simple Protocol checks. */
4854 if (isteqi(ist2(name, nlen), ist("transfer-encoding")))
4855 h1_parse_xfer_enc_header(&h1m, ist2(name, nlen));
4856 else if (isteqi(ist2(name, nlen), ist("content-length"))) {
4857 struct ist v = ist2(value, vlen);
4858 int ret;
4859
4860 ret = h1_parse_cont_len_header(&h1m, &v);
4861 if (ret < 0) {
4862 hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n",
4863 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4864 name);
4865 WILL_LJMP(lua_error(L));
4866 }
4867 else if (ret == 0)
4868 goto next; /* Skip it */
4869 }
4870
4871 /* Add a new header */
4872 if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) {
4873 hlua_pusherror(L, "Lua applet http '%s': Failed to add header '%s' in the response.\n",
4874 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4875 name);
4876 WILL_LJMP(lua_error(L));
4877 }
4878 next:
4879 /* Remove the array from the stack, and get next element with a remaining string. */
4880 lua_pop(L, 1);
4881 }
4882
4883 /* Remove the array from the stack, and get next element with a remaining string. */
4884 lua_pop(L, 1);
4885 }
4886
4887 if (h1m.flags & H1_MF_CHNK)
4888 h1m.flags &= ~H1_MF_CLEN;
4889 if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK))
4890 h1m.flags |= H1_MF_XFER_LEN;
4891
4892 /* Uset HTX start-line flags */
4893 if (h1m.flags & H1_MF_XFER_ENC)
4894 flags |= HTX_SL_F_XFER_ENC;
4895 if (h1m.flags & H1_MF_XFER_LEN) {
4896 flags |= HTX_SL_F_XFER_LEN;
4897 if (h1m.flags & H1_MF_CHNK)
4898 flags |= HTX_SL_F_CHNK;
4899 else if (h1m.flags & H1_MF_CLEN)
4900 flags |= HTX_SL_F_CLEN;
4901 if (h1m.body_len == 0)
4902 flags |= HTX_SL_F_BODYLESS;
4903 }
4904 sl->flags |= flags;
4905
4906 /* If we dont have a content-length set, and the HTTP version is 1.1
4907 * and the status code implies the presence of a message body, we must
4908 * announce a transfer encoding chunked. This is required by haproxy
4909 * for the keepalive compliance. If the applet annouces a transfer-encoding
4910 * chunked itslef, don't do anything.
4911 */
4912 if ((flags & (HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN)) == HTX_SL_F_VER_11 &&
4913 appctx->appctx->ctx.hlua_apphttp.status >= 200 &&
4914 appctx->appctx->ctx.hlua_apphttp.status != 204 &&
4915 appctx->appctx->ctx.hlua_apphttp.status != 304) {
4916 /* Add a new header */
4917 sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN);
4918 if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) {
4919 hlua_pusherror(L, "Lua applet http '%s': Failed to add header 'transfer-encoding' in the response.\n",
4920 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4921 WILL_LJMP(lua_error(L));
4922 }
4923 }
4924
4925 /* Finalize headers. */
4926 if (!htx_add_endof(htx, HTX_BLK_EOH)) {
4927 hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n",
4928 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4929 WILL_LJMP(lua_error(L));
4930 }
4931
4932 if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) {
4933 b_reset(&res->buf);
4934 hlua_pusherror(L, "Lua: 'start_response': response header block too big");
4935 WILL_LJMP(lua_error(L));
4936 }
4937
4938 htx_to_buf(htx, &res->buf);
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01004939 channel_add_input(res, htx->data);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004940
4941 /* Headers sent, set the flag. */
4942 appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT;
4943 return 0;
4944
4945}
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004946/* We will build the status line and the headers of the HTTP response.
4947 * We will try send at once if its not possible, we give back the hand
4948 * waiting for more room.
4949 */
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004950__LJMP static int hlua_applet_htx_start_response_yield(lua_State *L, int status, lua_KContext ctx)
4951{
4952 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4953 struct stream_interface *si = appctx->appctx->owner;
4954 struct channel *res = si_ic(si);
4955
4956 if (co_data(res)) {
4957 si_rx_room_blk(si);
4958 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_htx_start_response_yield, TICK_ETERNITY, 0));
4959 }
4960 return MAY_LJMP(hlua_applet_htx_send_response(L));
4961}
4962
4963
4964__LJMP static int hlua_applet_htx_start_response(lua_State *L)
4965{
4966 return MAY_LJMP(hlua_applet_htx_start_response_yield(L, 0, 0));
4967}
4968
4969/* We will build the status line and the headers of the HTTP response.
4970 * We will try send at once if its not possible, we give back the hand
4971 * waiting for more room.
4972 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004973__LJMP static int hlua_applet_http_start_response_yield(lua_State *L, int status, lua_KContext ctx)
4974{
4975 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4976 struct stream_interface *si = appctx->appctx->owner;
4977 struct channel *chn = si_ic(si);
4978 int ret;
4979 size_t len;
4980 const char *msg;
4981
4982 /* Get the message as the first argument on the stack. */
4983 msg = MAY_LJMP(luaL_checklstring(L, 2, &len));
4984
4985 /* Send the message at once. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004986 ret = ci_putblk(chn, msg, len);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004987
4988 /* if ret == -2 or -3 the channel closed or the message si too
4989 * big for the buffers.
4990 */
4991 if (ret == -2 || ret == -3) {
4992 hlua_pusherror(L, "Lua: 'start_response': response header block too big");
4993 WILL_LJMP(lua_error(L));
4994 }
4995
4996 /* If ret is -1, we dont have room in the buffer, so we yield. */
4997 if (ret == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01004998 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004999 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_start_response_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005000 }
5001
5002 /* Headers sent, set the flag. */
5003 appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT;
5004 return 0;
5005}
5006
5007__LJMP static int hlua_applet_http_start_response(lua_State *L)
5008{
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005009 struct buffer *tmp;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005010 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005011 const char *name;
Willy Tarreaua3294632017-08-23 11:24:47 +02005012 size_t name_len;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005013 const char *value;
Willy Tarreaua3294632017-08-23 11:24:47 +02005014 size_t value_len;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005015 int id;
Willy Tarreauc9f4ea02017-08-23 09:32:06 +02005016 long long hdr_contentlength = -1;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005017 int hdr_chunked = 0;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005018 const char *reason;
5019
5020 if (IS_HTX_STRM(si_strm(appctx->appctx->owner)))
5021 return MAY_LJMP(hlua_applet_htx_start_response(L));
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005022
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005023 reason = appctx->appctx->ctx.hlua_apphttp.reason;
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005024 if (reason == NULL)
Willy Tarreau04f1e2d2018-09-10 18:04:24 +02005025 reason = http_get_reason(appctx->appctx->ctx.hlua_apphttp.status);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005026
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005027 tmp = get_trash_chunk();
5028
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005029 /* Use the same http version than the request. */
5030 chunk_appendf(tmp, "HTTP/1.%c %d %s\r\n",
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +01005031 appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11 ? '1' : '0',
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005032 appctx->appctx->ctx.hlua_apphttp.status,
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005033 reason);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005034
5035 /* Get the array associated to the field "response" in the object AppletHTTP. */
5036 lua_pushvalue(L, 0);
5037 if (lua_getfield(L, 1, "response") != LUA_TTABLE) {
5038 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n",
5039 appctx->appctx->rule->arg.hlua_rule->fcn.name);
5040 WILL_LJMP(lua_error(L));
5041 }
5042
5043 /* Browse the list of headers. */
5044 lua_pushnil(L);
5045 while(lua_next(L, -2) != 0) {
5046
5047 /* We expect a string as -2. */
5048 if (lua_type(L, -2) != LUA_TSTRING) {
5049 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n",
5050 appctx->appctx->rule->arg.hlua_rule->fcn.name,
5051 lua_typename(L, lua_type(L, -2)));
5052 WILL_LJMP(lua_error(L));
5053 }
Willy Tarreaua3294632017-08-23 11:24:47 +02005054 name = lua_tolstring(L, -2, &name_len);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005055
5056 /* We expect an array as -1. */
5057 if (lua_type(L, -1) != LUA_TTABLE) {
5058 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n",
5059 appctx->appctx->rule->arg.hlua_rule->fcn.name,
5060 name,
5061 lua_typename(L, lua_type(L, -1)));
5062 WILL_LJMP(lua_error(L));
5063 }
5064
5065 /* Browse the table who is on the top of the stack. */
5066 lua_pushnil(L);
5067 while(lua_next(L, -2) != 0) {
5068
5069 /* We expect a number as -2. */
5070 if (lua_type(L, -2) != LUA_TNUMBER) {
5071 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n",
5072 appctx->appctx->rule->arg.hlua_rule->fcn.name,
5073 name,
5074 lua_typename(L, lua_type(L, -2)));
5075 WILL_LJMP(lua_error(L));
5076 }
5077 id = lua_tointeger(L, -2);
5078
5079 /* We expect a string as -2. */
5080 if (lua_type(L, -1) != LUA_TSTRING) {
5081 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n",
5082 appctx->appctx->rule->arg.hlua_rule->fcn.name,
5083 name, id,
5084 lua_typename(L, lua_type(L, -1)));
5085 WILL_LJMP(lua_error(L));
5086 }
Willy Tarreaua3294632017-08-23 11:24:47 +02005087 value = lua_tolstring(L, -1, &value_len);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005088
5089 /* Catenate a new header. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005090 if (tmp->data + name_len + 2 + value_len + 2 < tmp->size) {
5091 memcpy(tmp->area + tmp->data, name, name_len);
5092 tmp->data += name_len;
5093 tmp->area[tmp->data++] = ':';
5094 tmp->area[tmp->data++] = ' ';
Willy Tarreaua3294632017-08-23 11:24:47 +02005095
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005096 memcpy(tmp->area + tmp->data, value,
5097 value_len);
5098 tmp->data += value_len;
5099 tmp->area[tmp->data++] = '\r';
5100 tmp->area[tmp->data++] = '\n';
Willy Tarreaua3294632017-08-23 11:24:47 +02005101 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005102
5103 /* Protocol checks. */
5104
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005105 /* Copy the header content length. The length conversion
Willy Tarreauc9f4ea02017-08-23 09:32:06 +02005106 * is done without control. If it contains a bad value,
5107 * the content-length remains negative so that we can
5108 * switch to either chunked encoding or close.
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005109 */
Willy Tarreaua3294632017-08-23 11:24:47 +02005110 if (name_len == 14 && strcasecmp("content-length", name) == 0)
Willy Tarreauc9f4ea02017-08-23 09:32:06 +02005111 strl2llrc(value, strlen(value), &hdr_contentlength);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005112
5113 /* Check if the client annouces a transfer-encoding chunked it self. */
Willy Tarreaua3294632017-08-23 11:24:47 +02005114 if (name_len == 17 && value_len == 7 &&
5115 strcasecmp("transfer-encoding", name) == 0 &&
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005116 strcasecmp("chunked", value) == 0)
5117 hdr_chunked = 1;
5118
5119 /* Remove the array from the stack, and get next element with a remaining string. */
5120 lua_pop(L, 1);
5121 }
5122
5123 /* Remove the array from the stack, and get next element with a remaining string. */
5124 lua_pop(L, 1);
5125 }
5126
Willy Tarreau06c75fe2017-08-23 09:10:38 +02005127 /* If we dont have a content-length set, and the HTTP version is 1.1
5128 * and the status code implies the presence of a message body, we must
5129 * announce a transfer encoding chunked. This is required by haproxy
5130 * for the keepalive compliance. If the applet annouces a transfer-encoding
5131 * chunked itslef, don't do anything.
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005132 */
Willy Tarreauc9f4ea02017-08-23 09:32:06 +02005133 if (hdr_contentlength < 0 && hdr_chunked == 0 &&
Willy Tarreau06c75fe2017-08-23 09:10:38 +02005134 (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) &&
5135 appctx->appctx->ctx.hlua_apphttp.status >= 200 &&
5136 appctx->appctx->ctx.hlua_apphttp.status != 204 &&
5137 appctx->appctx->ctx.hlua_apphttp.status != 304) {
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005138 chunk_appendf(tmp, "Transfer-encoding: chunked\r\n");
5139 appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_CHUNKED;
5140 }
5141
5142 /* Finalize headers. */
5143 chunk_appendf(tmp, "\r\n");
5144
5145 /* Remove the last entry and the array of headers */
5146 lua_pop(L, 2);
5147
5148 /* Push the headers block. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005149 lua_pushlstring(L, tmp->area, tmp->data);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005150
5151 return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0));
5152}
5153
5154/*
5155 *
5156 *
5157 * Class HTTP
5158 *
5159 *
5160 */
5161
5162/* Returns a struct hlua_txn if the stack entry "ud" is
5163 * a class stream, otherwise it throws an error.
5164 */
5165__LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud)
5166{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02005167 return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005168}
5169
5170/* This function creates and push in the stack a HTTP object
5171 * according with a current TXN.
5172 */
5173static int hlua_http_new(lua_State *L, struct hlua_txn *txn)
5174{
5175 struct hlua_txn *htxn;
5176
5177 /* Check stack size. */
5178 if (!lua_checkstack(L, 3))
5179 return 0;
5180
5181 /* Create the object: obj[0] = userdata.
5182 * Note that the base of the Converters object is the
5183 * same than the TXN object.
5184 */
5185 lua_newtable(L);
5186 htxn = lua_newuserdata(L, sizeof(*htxn));
5187 lua_rawseti(L, -2, 0);
5188
5189 htxn->s = txn->s;
5190 htxn->p = txn->p;
Christopher Faulet256b69a2019-05-23 11:14:21 +02005191 htxn->dir = txn->dir;
5192 htxn->flags = txn->flags;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005193
5194 /* Pop a class stream metatable and affect it to the table. */
5195 lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref);
5196 lua_setmetatable(L, -2);
5197
5198 return 1;
5199}
5200
5201/* This function creates ans returns an array of HTTP headers.
5202 * This function does not fails. It is used as wrapper with the
5203 * 2 following functions.
5204 */
5205__LJMP static int hlua_http_get_headers(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg)
5206{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005207 /* Create the table. */
5208 lua_newtable(L);
5209
5210 if (!htxn->s->txn)
5211 return 1;
5212
Christopher Faulet724a12c2018-12-13 22:12:15 +01005213 if (IS_HTX_STRM(htxn->s)) {
5214 /* HTX version */
5215 struct htx *htx = htxbuf(&msg->chn->buf);
5216 int32_t pos;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005217
Christopher Fauleta3f15502019-05-13 15:27:23 +02005218 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet724a12c2018-12-13 22:12:15 +01005219 struct htx_blk *blk = htx_get_blk(htx, pos);
5220 enum htx_blk_type type = htx_get_blk_type(blk);
5221 struct ist n, v;
5222 int len;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005223
Christopher Faulet724a12c2018-12-13 22:12:15 +01005224 if (type == HTX_BLK_HDR) {
5225 n = htx_get_blk_name(htx,blk);
5226 v = htx_get_blk_value(htx, blk);
5227 }
5228 else if (type == HTX_BLK_EOH)
5229 break;
5230 else
5231 continue;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005232
Christopher Faulet724a12c2018-12-13 22:12:15 +01005233 /* Check for existing entry:
5234 * assume that the table is on the top of the stack, and
5235 * push the key in the stack, the function lua_gettable()
5236 * perform the lookup.
5237 */
5238 lua_pushlstring(L, n.ptr, n.len);
5239 lua_gettable(L, -2);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005240
Christopher Faulet724a12c2018-12-13 22:12:15 +01005241 switch (lua_type(L, -1)) {
5242 case LUA_TNIL:
5243 /* Table not found, create it. */
5244 lua_pop(L, 1); /* remove the nil value. */
5245 lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */
5246 lua_newtable(L); /* create and push empty table. */
5247 lua_pushlstring(L, v.ptr, v.len); /* push header value. */
5248 lua_rawseti(L, -2, 0); /* index header value (pop it). */
5249 lua_rawset(L, -3); /* index new table with header name (pop the values). */
5250 break;
5251
5252 case LUA_TTABLE:
5253 /* Entry found: push the value in the table. */
5254 len = lua_rawlen(L, -1);
5255 lua_pushlstring(L, v.ptr, v.len); /* push header value. */
5256 lua_rawseti(L, -2, len+1); /* index header value (pop it). */
5257 lua_pop(L, 1); /* remove the table (it is stored in the main table). */
5258 break;
5259
5260 default:
5261 /* Other cases are errors. */
5262 hlua_pusherror(L, "internal error during the parsing of headers.");
5263 WILL_LJMP(lua_error(L));
5264 }
5265 }
5266 }
5267 else {
5268 /* Legacy HTTP version */
5269 const char *cur_ptr, *cur_next, *p;
5270 int old_idx, cur_idx;
5271 struct hdr_idx_elem *cur_hdr;
5272 const char *hn, *hv;
5273 int hnl, hvl;
5274 const char *in;
5275 char *out;
5276 int len;
5277
5278 /* Build array of headers. */
5279 old_idx = 0;
5280 cur_next = ci_head(msg->chn) + hdr_idx_first_pos(&htxn->s->txn->hdr_idx);
5281
5282 while (1) {
5283 cur_idx = htxn->s->txn->hdr_idx.v[old_idx].next;
5284 if (!cur_idx)
5285 break;
5286 old_idx = cur_idx;
5287
5288 cur_hdr = &htxn->s->txn->hdr_idx.v[cur_idx];
5289 cur_ptr = cur_next;
5290 cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1;
5291
5292 /* Now we have one full header at cur_ptr of len cur_hdr->len,
5293 * and the next header starts at cur_next. We'll check
5294 * this header in the list as well as against the default
5295 * rule.
5296 */
5297
5298 /* look for ': *'. */
5299 hn = cur_ptr;
5300 for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++);
5301 if (p >= cur_ptr+cur_hdr->len)
5302 continue;
5303 hnl = p - hn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005304 p++;
Christopher Faulet724a12c2018-12-13 22:12:15 +01005305 while (p < cur_ptr+cur_hdr->len && ( *p == ' ' || *p == '\t' ))
5306 p++;
5307 if (p >= cur_ptr+cur_hdr->len)
5308 continue;
5309 hv = p;
5310 hvl = cur_ptr+cur_hdr->len-p;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005311
Christopher Faulet724a12c2018-12-13 22:12:15 +01005312 /* Lowercase the key. Don't check the size of trash, it have
5313 * the size of one buffer and the input data contains in one
5314 * buffer.
5315 */
5316 out = trash.area;
5317 for (in=hn; in<hn+hnl; in++, out++)
5318 *out = tolower(*in);
5319 *out = '\0';
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005320
Christopher Faulet724a12c2018-12-13 22:12:15 +01005321 /* Check for existing entry:
5322 * assume that the table is on the top of the stack, and
5323 * push the key in the stack, the function lua_gettable()
5324 * perform the lookup.
5325 */
5326 lua_pushlstring(L, trash.area, hnl);
5327 lua_gettable(L, -2);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005328
Christopher Faulet724a12c2018-12-13 22:12:15 +01005329 switch (lua_type(L, -1)) {
5330 case LUA_TNIL:
5331 /* Table not found, create it. */
5332 lua_pop(L, 1); /* remove the nil value. */
5333 lua_pushlstring(L, trash.area, hnl); /* push the header name as key. */
5334 lua_newtable(L); /* create and push empty table. */
5335 lua_pushlstring(L, hv, hvl); /* push header value. */
5336 lua_rawseti(L, -2, 0); /* index header value (pop it). */
5337 lua_rawset(L, -3); /* index new table with header name (pop the values). */
5338 break;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005339
Christopher Faulet724a12c2018-12-13 22:12:15 +01005340 case LUA_TTABLE:
5341 /* Entry found: push the value in the table. */
5342 len = lua_rawlen(L, -1);
5343 lua_pushlstring(L, hv, hvl); /* push header value. */
5344 lua_rawseti(L, -2, len+1); /* index header value (pop it). */
5345 lua_pop(L, 1); /* remove the table (it is stored in the main table). */
5346 break;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005347
Christopher Faulet724a12c2018-12-13 22:12:15 +01005348 default:
5349 /* Other cases are errors. */
5350 hlua_pusherror(L, "internal error during the parsing of headers.");
5351 WILL_LJMP(lua_error(L));
5352 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005353 }
5354 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005355 return 1;
5356}
5357
5358__LJMP static int hlua_http_req_get_headers(lua_State *L)
5359{
5360 struct hlua_txn *htxn;
5361
5362 MAY_LJMP(check_args(L, 1, "req_get_headers"));
5363 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5364
Christopher Faulet2351ca22019-07-26 16:31:34 +02005365 if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005366 WILL_LJMP(lua_error(L));
5367
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005368 return hlua_http_get_headers(L, htxn, &htxn->s->txn->req);
5369}
5370
5371__LJMP static int hlua_http_res_get_headers(lua_State *L)
5372{
5373 struct hlua_txn *htxn;
5374
5375 MAY_LJMP(check_args(L, 1, "res_get_headers"));
5376 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5377
Christopher Faulet2351ca22019-07-26 16:31:34 +02005378 if (htxn->dir != SMP_OPT_DIR_RES || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005379 WILL_LJMP(lua_error(L));
5380
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005381 return hlua_http_get_headers(L, htxn, &htxn->s->txn->rsp);
5382}
5383
5384/* This function replace full header, or just a value in
5385 * the request or in the response. It is a wrapper fir the
5386 * 4 following functions.
5387 */
5388__LJMP static inline int hlua_http_rep_hdr(lua_State *L, struct hlua_txn *htxn,
5389 struct http_msg *msg, int action)
5390{
5391 size_t name_len;
5392 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
5393 const char *reg = MAY_LJMP(luaL_checkstring(L, 3));
5394 const char *value = MAY_LJMP(luaL_checkstring(L, 4));
Dragan Dosen26743032019-04-30 15:54:36 +02005395 struct my_regex *re;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005396
Dragan Dosen26743032019-04-30 15:54:36 +02005397 if (!(re = regex_comp(reg, 1, 1, NULL)))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005398 WILL_LJMP(luaL_argerror(L, 3, "invalid regex"));
5399
Christopher Faulet5a8549e2019-06-17 13:36:06 +02005400 if (IS_HTX_STRM(htxn->s)) {
5401 struct htx *htx = htxbuf(&msg->chn->buf);
5402
5403 htx_transform_header_str(htxn->s, msg->chn, htx, ist2(name, name_len), value, re, action);
5404 }
5405 else
5406 http_transform_header_str(htxn->s, msg, name, name_len, value, re, action);
Dragan Dosen26743032019-04-30 15:54:36 +02005407 regex_free(re);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005408 return 0;
5409}
5410
5411__LJMP static int hlua_http_req_rep_hdr(lua_State *L)
5412{
5413 struct hlua_txn *htxn;
5414
5415 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
5416 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5417
Christopher Faulet2351ca22019-07-26 16:31:34 +02005418 if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005419 WILL_LJMP(lua_error(L));
5420
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005421 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_HDR));
5422}
5423
5424__LJMP static int hlua_http_res_rep_hdr(lua_State *L)
5425{
5426 struct hlua_txn *htxn;
5427
5428 MAY_LJMP(check_args(L, 4, "res_rep_hdr"));
5429 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5430
Christopher Faulet2351ca22019-07-26 16:31:34 +02005431 if (htxn->dir != SMP_OPT_DIR_RES || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005432 WILL_LJMP(lua_error(L));
5433
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005434 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->rsp, ACT_HTTP_REPLACE_HDR));
5435}
5436
5437__LJMP static int hlua_http_req_rep_val(lua_State *L)
5438{
5439 struct hlua_txn *htxn;
5440
5441 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
5442 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5443
Christopher Faulet2351ca22019-07-26 16:31:34 +02005444 if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005445 WILL_LJMP(lua_error(L));
5446
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005447 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_VAL));
5448}
5449
5450__LJMP static int hlua_http_res_rep_val(lua_State *L)
5451{
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005452 struct hlua_txn *htxn;
5453
5454 MAY_LJMP(check_args(L, 4, "res_rep_val"));
5455 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5456
Christopher Faulet2351ca22019-07-26 16:31:34 +02005457 if (htxn->dir != SMP_OPT_DIR_RES || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005458 WILL_LJMP(lua_error(L));
5459
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02005460 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->rsp, ACT_HTTP_REPLACE_VAL));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005461}
5462
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08005463/* This function deletes all the occurrences of an header.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005464 * It is a wrapper for the 2 following functions.
5465 */
5466__LJMP static inline int hlua_http_del_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg)
5467{
5468 size_t len;
5469 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005470
Christopher Faulet724a12c2018-12-13 22:12:15 +01005471 if (IS_HTX_STRM(htxn->s)) {
5472 /* HTX version */
5473 struct htx *htx = htxbuf(&msg->chn->buf);
5474 struct http_hdr_ctx ctx;
5475
5476 ctx.blk = NULL;
5477 while (http_find_header(htx, ist2(name, len), &ctx, 1))
5478 http_remove_header(htx, &ctx);
5479 }
5480 else {
5481 /* Legacy HTTP version */
5482 struct hdr_ctx ctx;
5483 struct http_txn *txn = htxn->s->txn;
5484
5485 ctx.idx = 0;
5486 while (http_find_header2(name, len, ci_head(msg->chn), &txn->hdr_idx, &ctx))
5487 http_remove_header2(msg, &txn->hdr_idx, &ctx);
5488 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005489 return 0;
5490}
5491
5492__LJMP static int hlua_http_req_del_hdr(lua_State *L)
5493{
5494 struct hlua_txn *htxn;
5495
5496 MAY_LJMP(check_args(L, 2, "req_del_hdr"));
5497 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5498
Christopher Faulet2351ca22019-07-26 16:31:34 +02005499 if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005500 WILL_LJMP(lua_error(L));
5501
Willy Tarreaueee5b512015-04-03 23:46:31 +02005502 return hlua_http_del_hdr(L, htxn, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005503}
5504
5505__LJMP static int hlua_http_res_del_hdr(lua_State *L)
5506{
5507 struct hlua_txn *htxn;
5508
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005509 MAY_LJMP(check_args(L, 2, "res_del_hdr"));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005510 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5511
Christopher Faulet2351ca22019-07-26 16:31:34 +02005512 if (htxn->dir != SMP_OPT_DIR_RES || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005513 WILL_LJMP(lua_error(L));
5514
Willy Tarreaueee5b512015-04-03 23:46:31 +02005515 return hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005516}
5517
5518/* This function adds an header. It is a wrapper used by
5519 * the 2 following functions.
5520 */
5521__LJMP static inline int hlua_http_add_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg)
5522{
5523 size_t name_len;
5524 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
5525 size_t value_len;
5526 const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len));
5527 char *p;
5528
Christopher Faulet724a12c2018-12-13 22:12:15 +01005529 if (IS_HTX_STRM(htxn->s)) {
5530 /* HTX version */
5531 struct htx *htx = htxbuf(&msg->chn->buf);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005532
Christopher Faulet724a12c2018-12-13 22:12:15 +01005533 lua_pushboolean(L, http_add_header(htx, ist2(name, name_len),
5534 ist2(value, value_len)));
5535 }
5536 else {
5537 /* Legacy HTTP version */
5538 /* Check length. */
5539 trash.data = value_len + name_len + 2;
5540 if (trash.data > trash.size)
5541 return 0;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005542
Christopher Faulet724a12c2018-12-13 22:12:15 +01005543 /* Creates the header string. */
5544 p = trash.area;
5545 memcpy(p, name, name_len);
5546 p += name_len;
5547 *p = ':';
5548 p++;
5549 *p = ' ';
5550 p++;
5551 memcpy(p, value, value_len);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005552
Christopher Faulet724a12c2018-12-13 22:12:15 +01005553 lua_pushboolean(L, http_header_add_tail2(msg, &htxn->s->txn->hdr_idx,
5554 trash.area, trash.data) != 0);
5555 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005556 return 0;
5557}
5558
5559__LJMP static int hlua_http_req_add_hdr(lua_State *L)
5560{
5561 struct hlua_txn *htxn;
5562
5563 MAY_LJMP(check_args(L, 3, "req_add_hdr"));
5564 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5565
Christopher Faulet2351ca22019-07-26 16:31:34 +02005566 if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005567 WILL_LJMP(lua_error(L));
5568
Willy Tarreaueee5b512015-04-03 23:46:31 +02005569 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005570}
5571
5572__LJMP static int hlua_http_res_add_hdr(lua_State *L)
5573{
5574 struct hlua_txn *htxn;
5575
5576 MAY_LJMP(check_args(L, 3, "res_add_hdr"));
5577 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5578
Christopher Faulet2351ca22019-07-26 16:31:34 +02005579 if (htxn->dir != SMP_OPT_DIR_RES || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005580 WILL_LJMP(lua_error(L));
5581
Willy Tarreaueee5b512015-04-03 23:46:31 +02005582 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005583}
5584
5585static int hlua_http_req_set_hdr(lua_State *L)
5586{
5587 struct hlua_txn *htxn;
5588
5589 MAY_LJMP(check_args(L, 3, "req_set_hdr"));
5590 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5591
Christopher Faulet2351ca22019-07-26 16:31:34 +02005592 if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005593 WILL_LJMP(lua_error(L));
5594
Willy Tarreaueee5b512015-04-03 23:46:31 +02005595 hlua_http_del_hdr(L, htxn, &htxn->s->txn->req);
5596 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005597}
5598
5599static int hlua_http_res_set_hdr(lua_State *L)
5600{
5601 struct hlua_txn *htxn;
5602
5603 MAY_LJMP(check_args(L, 3, "res_set_hdr"));
5604 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5605
Christopher Faulet2351ca22019-07-26 16:31:34 +02005606 if (htxn->dir != SMP_OPT_DIR_RES || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005607 WILL_LJMP(lua_error(L));
5608
Willy Tarreaueee5b512015-04-03 23:46:31 +02005609 hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp);
5610 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005611}
5612
5613/* This function set the method. */
5614static int hlua_http_req_set_meth(lua_State *L)
5615{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005616 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005617 size_t name_len;
5618 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005619
Christopher Faulet2351ca22019-07-26 16:31:34 +02005620 if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005621 WILL_LJMP(lua_error(L));
5622
Willy Tarreau987e3fb2015-04-04 01:09:08 +02005623 lua_pushboolean(L, http_replace_req_line(0, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005624 return 1;
5625}
5626
5627/* This function set the method. */
5628static int hlua_http_req_set_path(lua_State *L)
5629{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005630 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005631 size_t name_len;
5632 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02005633
Christopher Faulet2351ca22019-07-26 16:31:34 +02005634 if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005635 WILL_LJMP(lua_error(L));
5636
Willy Tarreau987e3fb2015-04-04 01:09:08 +02005637 lua_pushboolean(L, http_replace_req_line(1, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005638 return 1;
5639}
5640
5641/* This function set the query-string. */
5642static int hlua_http_req_set_query(lua_State *L)
5643{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005644 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005645 size_t name_len;
5646 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005647
Christopher Faulet2351ca22019-07-26 16:31:34 +02005648 if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005649 WILL_LJMP(lua_error(L));
5650
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005651 /* Check length. */
5652 if (name_len > trash.size - 1) {
5653 lua_pushboolean(L, 0);
5654 return 1;
5655 }
5656
5657 /* Add the mark question as prefix. */
5658 chunk_reset(&trash);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005659 trash.area[trash.data++] = '?';
5660 memcpy(trash.area + trash.data, name, name_len);
5661 trash.data += name_len;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005662
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005663 lua_pushboolean(L,
5664 http_replace_req_line(2, trash.area, trash.data, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005665 return 1;
5666}
5667
5668/* This function set the uri. */
5669static int hlua_http_req_set_uri(lua_State *L)
5670{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005671 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005672 size_t name_len;
5673 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005674
Christopher Faulet2351ca22019-07-26 16:31:34 +02005675 if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005676 WILL_LJMP(lua_error(L));
5677
Willy Tarreau987e3fb2015-04-04 01:09:08 +02005678 lua_pushboolean(L, http_replace_req_line(3, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005679 return 1;
5680}
5681
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005682/* This function set the response code & optionally reason. */
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005683static int hlua_http_res_set_status(lua_State *L)
5684{
5685 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5686 unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2));
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005687 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005688
Christopher Faulet2351ca22019-07-26 16:31:34 +02005689 if (htxn->dir != SMP_OPT_DIR_RES || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005690 WILL_LJMP(lua_error(L));
5691
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005692 http_set_status(code, reason, htxn->s);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005693 return 0;
5694}
5695
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005696/*
5697 *
5698 *
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005699 * Class TXN
5700 *
5701 *
5702 */
5703
5704/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02005705 * a class stream, otherwise it throws an error.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005706 */
5707__LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud)
5708{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02005709 return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref));
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005710}
5711
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005712__LJMP static int hlua_set_var(lua_State *L)
5713{
5714 struct hlua_txn *htxn;
5715 const char *name;
5716 size_t len;
5717 struct sample smp;
5718
5719 MAY_LJMP(check_args(L, 3, "set_var"));
5720
5721 /* It is useles to retrieve the stream, but this function
5722 * runs only in a stream context.
5723 */
5724 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5725 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5726
5727 /* Converts the third argument in a sample. */
5728 hlua_lua2smp(L, 3, &smp);
5729
5730 /* Store the sample in a variable. */
Willy Tarreau7560dd42016-03-10 16:28:58 +01005731 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Willy Tarreau6204cd92016-03-10 16:33:04 +01005732 vars_set_by_name(name, len, &smp);
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005733 return 0;
5734}
5735
Christopher Faulet85d79c92016-11-09 16:54:56 +01005736__LJMP static int hlua_unset_var(lua_State *L)
5737{
5738 struct hlua_txn *htxn;
5739 const char *name;
5740 size_t len;
5741 struct sample smp;
5742
5743 MAY_LJMP(check_args(L, 2, "unset_var"));
5744
5745 /* It is useles to retrieve the stream, but this function
5746 * runs only in a stream context.
5747 */
5748 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5749 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5750
5751 /* Unset the variable. */
5752 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
5753 vars_unset_by_name(name, len, &smp);
5754 return 0;
5755}
5756
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005757__LJMP static int hlua_get_var(lua_State *L)
5758{
5759 struct hlua_txn *htxn;
5760 const char *name;
5761 size_t len;
5762 struct sample smp;
5763
5764 MAY_LJMP(check_args(L, 2, "get_var"));
5765
5766 /* It is useles to retrieve the stream, but this function
5767 * runs only in a stream context.
5768 */
5769 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5770 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5771
Willy Tarreau7560dd42016-03-10 16:28:58 +01005772 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Willy Tarreau6204cd92016-03-10 16:33:04 +01005773 if (!vars_get_by_name(name, len, &smp)) {
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005774 lua_pushnil(L);
5775 return 1;
5776 }
5777
5778 return hlua_smp2lua(L, &smp);
5779}
5780
Willy Tarreau59551662015-03-10 14:23:13 +01005781__LJMP static int hlua_set_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005782{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005783 struct hlua *hlua;
5784
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005785 MAY_LJMP(check_args(L, 2, "set_priv"));
5786
Willy Tarreau87b09662015-04-03 00:22:06 +02005787 /* It is useles to retrieve the stream, but this function
5788 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005789 */
5790 MAY_LJMP(hlua_checktxn(L, 1));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005791 hlua = hlua_gethlua(L);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005792
5793 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02005794 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005795
5796 /* Get and store new value. */
5797 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
5798 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
5799
5800 return 0;
5801}
5802
Willy Tarreau59551662015-03-10 14:23:13 +01005803__LJMP static int hlua_get_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005804{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005805 struct hlua *hlua;
5806
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005807 MAY_LJMP(check_args(L, 1, "get_priv"));
5808
Willy Tarreau87b09662015-04-03 00:22:06 +02005809 /* It is useles to retrieve the stream, but this function
5810 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005811 */
5812 MAY_LJMP(hlua_checktxn(L, 1));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005813 hlua = hlua_gethlua(L);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005814
5815 /* Push configuration index in the stack. */
5816 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
5817
5818 return 1;
5819}
5820
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005821/* Create stack entry containing a class TXN. This function
5822 * return 0 if the stack does not contains free slots,
5823 * otherwise it returns 1.
5824 */
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005825static int hlua_txn_new(lua_State *L, struct stream *s, struct proxy *p, int dir, int flags)
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005826{
Willy Tarreaude491382015-04-06 11:04:28 +02005827 struct hlua_txn *htxn;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005828
5829 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005830 if (!lua_checkstack(L, 3))
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005831 return 0;
5832
5833 /* NOTE: The allocation never fails. The failure
5834 * throw an error, and the function never returns.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08005835 * if the throw is not available, the process is aborted.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005836 */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005837 /* Create the object: obj[0] = userdata. */
5838 lua_newtable(L);
Willy Tarreaude491382015-04-06 11:04:28 +02005839 htxn = lua_newuserdata(L, sizeof(*htxn));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005840 lua_rawseti(L, -2, 0);
5841
Willy Tarreaude491382015-04-06 11:04:28 +02005842 htxn->s = s;
5843 htxn->p = p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01005844 htxn->dir = dir;
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005845 htxn->flags = flags;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005846
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005847 /* Create the "f" field that contains a list of fetches. */
5848 lua_pushstring(L, "f");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005849 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005850 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005851 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005852
5853 /* Create the "sf" field that contains a list of stringsafe fetches. */
5854 lua_pushstring(L, "sf");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005855 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP | HLUA_F_AS_STRING))
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005856 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005857 lua_rawset(L, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005858
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005859 /* Create the "c" field that contains a list of converters. */
5860 lua_pushstring(L, "c");
Willy Tarreaude491382015-04-06 11:04:28 +02005861 if (!hlua_converters_new(L, htxn, 0))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005862 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005863 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005864
5865 /* Create the "sc" field that contains a list of stringsafe converters. */
5866 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01005867 if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING))
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005868 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005869 lua_rawset(L, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005870
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005871 /* Create the "req" field that contains the request channel object. */
5872 lua_pushstring(L, "req");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005873 if (!hlua_channel_new(L, &s->req))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005874 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005875 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005876
5877 /* Create the "res" field that contains the response channel object. */
5878 lua_pushstring(L, "res");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005879 if (!hlua_channel_new(L, &s->res))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005880 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005881 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005882
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005883 /* Creates the HTTP object is the current proxy allows http. */
5884 lua_pushstring(L, "http");
5885 if (p->mode == PR_MODE_HTTP) {
Willy Tarreaude491382015-04-06 11:04:28 +02005886 if (!hlua_http_new(L, htxn))
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005887 return 0;
5888 }
5889 else
5890 lua_pushnil(L);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005891 lua_rawset(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005892
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005893 /* Pop a class sesison metatable and affect it to the userdata. */
5894 lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref);
5895 lua_setmetatable(L, -2);
5896
5897 return 1;
5898}
5899
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005900__LJMP static int hlua_txn_deflog(lua_State *L)
5901{
5902 const char *msg;
5903 struct hlua_txn *htxn;
5904
5905 MAY_LJMP(check_args(L, 2, "deflog"));
5906 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5907 msg = MAY_LJMP(luaL_checkstring(L, 2));
5908
5909 hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg);
5910 return 0;
5911}
5912
5913__LJMP static int hlua_txn_log(lua_State *L)
5914{
5915 int level;
5916 const char *msg;
5917 struct hlua_txn *htxn;
5918
5919 MAY_LJMP(check_args(L, 3, "log"));
5920 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5921 level = MAY_LJMP(luaL_checkinteger(L, 2));
5922 msg = MAY_LJMP(luaL_checkstring(L, 3));
5923
5924 if (level < 0 || level >= NB_LOG_LEVELS)
5925 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
5926
5927 hlua_sendlog(htxn->s->be, level, msg);
5928 return 0;
5929}
5930
5931__LJMP static int hlua_txn_log_debug(lua_State *L)
5932{
5933 const char *msg;
5934 struct hlua_txn *htxn;
5935
5936 MAY_LJMP(check_args(L, 2, "Debug"));
5937 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5938 msg = MAY_LJMP(luaL_checkstring(L, 2));
5939 hlua_sendlog(htxn->s->be, LOG_DEBUG, msg);
5940 return 0;
5941}
5942
5943__LJMP static int hlua_txn_log_info(lua_State *L)
5944{
5945 const char *msg;
5946 struct hlua_txn *htxn;
5947
5948 MAY_LJMP(check_args(L, 2, "Info"));
5949 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5950 msg = MAY_LJMP(luaL_checkstring(L, 2));
5951 hlua_sendlog(htxn->s->be, LOG_INFO, msg);
5952 return 0;
5953}
5954
5955__LJMP static int hlua_txn_log_warning(lua_State *L)
5956{
5957 const char *msg;
5958 struct hlua_txn *htxn;
5959
5960 MAY_LJMP(check_args(L, 2, "Warning"));
5961 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5962 msg = MAY_LJMP(luaL_checkstring(L, 2));
5963 hlua_sendlog(htxn->s->be, LOG_WARNING, msg);
5964 return 0;
5965}
5966
5967__LJMP static int hlua_txn_log_alert(lua_State *L)
5968{
5969 const char *msg;
5970 struct hlua_txn *htxn;
5971
5972 MAY_LJMP(check_args(L, 2, "Alert"));
5973 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5974 msg = MAY_LJMP(luaL_checkstring(L, 2));
5975 hlua_sendlog(htxn->s->be, LOG_ALERT, msg);
5976 return 0;
5977}
5978
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005979__LJMP static int hlua_txn_set_loglevel(lua_State *L)
5980{
5981 struct hlua_txn *htxn;
5982 int ll;
5983
5984 MAY_LJMP(check_args(L, 2, "set_loglevel"));
5985 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5986 ll = MAY_LJMP(luaL_checkinteger(L, 2));
5987
5988 if (ll < 0 || ll > 7)
5989 WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7"));
5990
5991 htxn->s->logs.level = ll;
5992 return 0;
5993}
5994
5995__LJMP static int hlua_txn_set_tos(lua_State *L)
5996{
5997 struct hlua_txn *htxn;
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005998 int tos;
5999
6000 MAY_LJMP(check_args(L, 2, "set_tos"));
6001 htxn = MAY_LJMP(hlua_checktxn(L, 1));
6002 tos = MAY_LJMP(luaL_checkinteger(L, 2));
6003
Willy Tarreau1a18b542018-12-11 16:37:42 +01006004 conn_set_tos(objt_conn(htxn->s->sess->origin), tos);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01006005 return 0;
6006}
6007
6008__LJMP static int hlua_txn_set_mark(lua_State *L)
6009{
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01006010 struct hlua_txn *htxn;
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01006011 int mark;
6012
6013 MAY_LJMP(check_args(L, 2, "set_mark"));
6014 htxn = MAY_LJMP(hlua_checktxn(L, 1));
6015 mark = MAY_LJMP(luaL_checkinteger(L, 2));
6016
Lukas Tribusb59291a2019-08-11 18:03:45 +02006017 conn_set_mark(objt_conn(htxn->s->sess->origin), mark);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01006018 return 0;
6019}
6020
Patrick Hemmer268a7072018-05-11 12:52:31 -04006021__LJMP static int hlua_txn_set_priority_class(lua_State *L)
6022{
6023 struct hlua_txn *htxn;
6024
6025 MAY_LJMP(check_args(L, 2, "set_priority_class"));
6026 htxn = MAY_LJMP(hlua_checktxn(L, 1));
6027 htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2)));
6028 return 0;
6029}
6030
6031__LJMP static int hlua_txn_set_priority_offset(lua_State *L)
6032{
6033 struct hlua_txn *htxn;
6034
6035 MAY_LJMP(check_args(L, 2, "set_priority_offset"));
6036 htxn = MAY_LJMP(hlua_checktxn(L, 1));
6037 htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2)));
6038 return 0;
6039}
6040
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01006041/* This function is an Lua binding that send pending data
6042 * to the client, and close the stream interface.
6043 */
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02006044__LJMP static int hlua_txn_done(lua_State *L)
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01006045{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02006046 struct hlua_txn *htxn;
Thierry FOURNIER9bd52d42016-07-14 11:45:33 +02006047 struct hlua *hlua;
Willy Tarreau81389672015-03-10 12:03:52 +01006048 struct channel *ic, *oc;
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01006049
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006050 MAY_LJMP(check_args(L, 1, "close"));
Willy Tarreaub2ccb562015-04-06 11:11:15 +02006051 htxn = MAY_LJMP(hlua_checktxn(L, 1));
Thierry FOURNIER9bd52d42016-07-14 11:45:33 +02006052 hlua = hlua_gethlua(L);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01006053
Thierry FOURNIERab00df62016-07-14 11:42:37 +02006054 /* If the flags NOTERM is set, we cannot terminate the http
6055 * session, so we just end the execution of the current
6056 * lua code.
6057 */
6058 if (htxn->flags & HLUA_TXN_NOTERM) {
6059 WILL_LJMP(hlua_done(L));
6060 return 0;
6061 }
6062
Willy Tarreaub2ccb562015-04-06 11:11:15 +02006063 ic = &htxn->s->req;
6064 oc = &htxn->s->res;
Willy Tarreau81389672015-03-10 12:03:52 +01006065
Christopher Faulet72c69272019-07-26 16:40:24 +02006066 if (IS_HTX_STRM(htxn->s)) {
6067 htxn->s->txn->status = 0;
6068 http_reply_and_close(htxn->s, 0, NULL);
6069 ic->analysers &= AN_REQ_FLT_END;
6070 oc->analysers &= AN_RES_FLT_END;
6071 }
Christopher Fauletb58fb792019-07-16 10:52:40 +02006072 else {
6073 if (htxn->s->txn) {
6074 /* HTTP mode, let's stay in sync with the stream */
6075 b_del(&ic->buf, htxn->s->txn->req.sov);
6076 htxn->s->txn->req.next -= htxn->s->txn->req.sov;
6077 htxn->s->txn->req.sov = 0;
6078
6079 ic->analysers &= AN_REQ_HTTP_XFER_BODY;
6080 oc->analysers = AN_RES_HTTP_XFER_BODY;
6081 htxn->s->txn->req.msg_state = HTTP_MSG_CLOSED;
6082 htxn->s->txn->rsp.msg_state = HTTP_MSG_DONE;
Willy Tarreau630ef452015-08-28 10:06:15 +02006083
Willy Tarreau630ef452015-08-28 10:06:15 +02006084 /* Note that if we want to support keep-alive, we need
6085 * to bypass the close/shutr_now calls below, but that
6086 * may only be done if the HTTP request was already
6087 * processed and the connection header is known (ie
6088 * not during TCP rules).
6089 */
Christopher Fauletb58fb792019-07-16 10:52:40 +02006090 }
Willy Tarreau630ef452015-08-28 10:06:15 +02006091
Christopher Fauletb58fb792019-07-16 10:52:40 +02006092 channel_auto_read(ic);
6093 channel_abort(ic);
6094 channel_auto_close(ic);
6095 channel_erase(ic);
Thierry FOURNIER10ec2142015-08-24 17:23:45 +02006096
Christopher Fauletb58fb792019-07-16 10:52:40 +02006097 oc->wex = tick_add_ifset(now_ms, oc->wto);
6098 channel_auto_read(oc);
6099 channel_auto_close(oc);
6100 channel_shutr_now(oc);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01006101
Christopher Fauletb58fb792019-07-16 10:52:40 +02006102 ic->analysers = 0;
6103 }
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02006104
Christopher Faulet72c69272019-07-26 16:40:24 +02006105 if (!(htxn->s->flags & SF_ERR_MASK)) // this is not really an error but it is
6106 htxn->s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
6107
Thierry FOURNIER9bd52d42016-07-14 11:45:33 +02006108 hlua->flags |= HLUA_STOP;
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02006109 WILL_LJMP(hlua_done(L));
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01006110 return 0;
6111}
6112
6113__LJMP static int hlua_log(lua_State *L)
6114{
6115 int level;
6116 const char *msg;
6117
6118 MAY_LJMP(check_args(L, 2, "log"));
6119 level = MAY_LJMP(luaL_checkinteger(L, 1));
6120 msg = MAY_LJMP(luaL_checkstring(L, 2));
6121
6122 if (level < 0 || level >= NB_LOG_LEVELS)
6123 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
6124
6125 hlua_sendlog(NULL, level, msg);
6126 return 0;
6127}
6128
6129__LJMP static int hlua_log_debug(lua_State *L)
6130{
6131 const char *msg;
6132
6133 MAY_LJMP(check_args(L, 1, "debug"));
6134 msg = MAY_LJMP(luaL_checkstring(L, 1));
6135 hlua_sendlog(NULL, LOG_DEBUG, msg);
6136 return 0;
6137}
6138
6139__LJMP static int hlua_log_info(lua_State *L)
6140{
6141 const char *msg;
6142
6143 MAY_LJMP(check_args(L, 1, "info"));
6144 msg = MAY_LJMP(luaL_checkstring(L, 1));
6145 hlua_sendlog(NULL, LOG_INFO, msg);
6146 return 0;
6147}
6148
6149__LJMP static int hlua_log_warning(lua_State *L)
6150{
6151 const char *msg;
6152
6153 MAY_LJMP(check_args(L, 1, "warning"));
6154 msg = MAY_LJMP(luaL_checkstring(L, 1));
6155 hlua_sendlog(NULL, LOG_WARNING, msg);
6156 return 0;
6157}
6158
6159__LJMP static int hlua_log_alert(lua_State *L)
6160{
6161 const char *msg;
6162
6163 MAY_LJMP(check_args(L, 1, "alert"));
6164 msg = MAY_LJMP(luaL_checkstring(L, 1));
6165 hlua_sendlog(NULL, LOG_ALERT, msg);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01006166 return 0;
6167}
6168
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006169__LJMP static int hlua_sleep_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006170{
6171 int wakeup_ms = lua_tointeger(L, -1);
6172 if (now_ms < wakeup_ms)
Willy Tarreau9635e032018-10-16 17:52:55 +02006173 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006174 return 0;
6175}
6176
6177__LJMP static int hlua_sleep(lua_State *L)
6178{
6179 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006180 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006181
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006182 MAY_LJMP(check_args(L, 1, "sleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006183
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006184 delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006185 wakeup_ms = tick_add(now_ms, delay);
6186 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006187
Willy Tarreau9635e032018-10-16 17:52:55 +02006188 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006189 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006190}
6191
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006192__LJMP static int hlua_msleep(lua_State *L)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006193{
6194 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006195 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006196
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006197 MAY_LJMP(check_args(L, 1, "msleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006198
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006199 delay = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006200 wakeup_ms = tick_add(now_ms, delay);
6201 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006202
Willy Tarreau9635e032018-10-16 17:52:55 +02006203 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006204 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006205}
6206
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01006207/* This functionis an LUA binding. it permits to give back
6208 * the hand at the HAProxy scheduler. It is used when the
6209 * LUA processing consumes a lot of time.
6210 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006211__LJMP static int hlua_yield_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006212{
6213 return 0;
6214}
6215
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01006216__LJMP static int hlua_yield(lua_State *L)
6217{
Willy Tarreau9635e032018-10-16 17:52:55 +02006218 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_yield_yield, TICK_ETERNITY, HLUA_CTRLYIELD));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006219 return 0;
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01006220}
6221
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006222/* This function change the nice of the currently executed
6223 * task. It is used set low or high priority at the current
6224 * task.
6225 */
Willy Tarreau59551662015-03-10 14:23:13 +01006226__LJMP static int hlua_set_nice(lua_State *L)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006227{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006228 struct hlua *hlua;
6229 int nice;
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006230
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006231 MAY_LJMP(check_args(L, 1, "set_nice"));
6232 hlua = hlua_gethlua(L);
6233 nice = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006234
6235 /* If he task is not set, I'm in a start mode. */
6236 if (!hlua || !hlua->task)
6237 return 0;
6238
6239 if (nice < -1024)
6240 nice = -1024;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006241 else if (nice > 1024)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006242 nice = 1024;
6243
6244 hlua->task->nice = nice;
6245 return 0;
6246}
6247
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006248/* This function is used as a callback of a task. It is called by the
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006249 * HAProxy task subsystem when the task is awaked. The LUA runtime can
6250 * return an E_AGAIN signal, the emmiter of this signal must set a
6251 * signal to wake the task.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006252 *
6253 * Task wrapper are longjmp safe because the only one Lua code
6254 * executed is the safe hlua_ctx_resume();
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006255 */
Willy Tarreau83a5ff42019-08-21 14:14:50 +02006256struct task *hlua_process_task(struct task *task, void *context, unsigned short state)
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006257{
Olivier Houchard9f6af332018-05-25 14:04:04 +02006258 struct hlua *hlua = context;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006259 enum hlua_exec status;
6260
Christopher Faulet5bc99722018-04-25 10:34:45 +02006261 if (task->thread_mask == MAX_THREADS_MASK)
6262 task_set_affinity(task, tid_bit);
6263
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006264 /* If it is the first call to the task, we must initialize the
6265 * execution timeouts.
6266 */
6267 if (!HLUA_IS_RUNNING(hlua))
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02006268 hlua->max_time = hlua_timeout_task;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006269
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006270 /* Execute the Lua code. */
6271 status = hlua_ctx_resume(hlua, 1);
6272
6273 switch (status) {
6274 /* finished or yield */
6275 case HLUA_E_OK:
6276 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006277 task_destroy(task);
Tim Duesterhuscd235c62018-04-24 13:56:01 +02006278 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006279 break;
6280
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006281 case HLUA_E_AGAIN: /* co process or timeout wake me later. */
Thierry FOURNIERcb146882017-12-10 17:10:57 +01006282 notification_gc(&hlua->com);
PiBa-NLfe971b32018-05-02 22:27:14 +02006283 task->expire = hlua->wake_time;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006284 break;
6285
6286 /* finished with error. */
6287 case HLUA_E_ERRMSG:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006288 SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006289 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006290 task_destroy(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02006291 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006292 break;
6293
6294 case HLUA_E_ERR:
6295 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006296 SEND_ERR(NULL, "Lua task: unknown error.\n");
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006297 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006298 task_destroy(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02006299 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006300 break;
6301 }
Emeric Brun253e53e2017-10-17 18:58:40 +02006302 return task;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006303}
6304
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006305/* This function is an LUA binding that register LUA function to be
6306 * executed after the HAProxy configuration parsing and before the
6307 * HAProxy scheduler starts. This function expect only one LUA
6308 * argument that is a function. This function returns nothing, but
6309 * throws if an error is encountered.
6310 */
6311__LJMP static int hlua_register_init(lua_State *L)
6312{
6313 struct hlua_init_function *init;
6314 int ref;
6315
6316 MAY_LJMP(check_args(L, 1, "register_init"));
6317
6318 ref = MAY_LJMP(hlua_checkfunction(L, 1));
6319
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006320 init = calloc(1, sizeof(*init));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006321 if (!init)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006322 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006323
6324 init->function_ref = ref;
6325 LIST_ADDQ(&hlua_init_functions, &init->l);
6326 return 0;
6327}
6328
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006329/* This functio is an LUA binding. It permits to register a task
6330 * executed in parallel of the main HAroxy activity. The task is
6331 * created and it is set in the HAProxy scheduler. It can be called
6332 * from the "init" section, "post init" or during the runtime.
6333 *
6334 * Lua prototype:
6335 *
6336 * <none> core.register_task(<function>)
6337 */
6338static int hlua_register_task(lua_State *L)
6339{
6340 struct hlua *hlua;
6341 struct task *task;
6342 int ref;
6343
6344 MAY_LJMP(check_args(L, 1, "register_task"));
6345
6346 ref = MAY_LJMP(hlua_checkfunction(L, 1));
6347
Willy Tarreaubafbe012017-11-24 17:34:44 +01006348 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006349 if (!hlua)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006350 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006351
Emeric Brunc60def82017-09-27 14:59:38 +02006352 task = task_new(MAX_THREADS_MASK);
Willy Tarreaue09101e2018-10-16 17:37:12 +02006353 if (!task)
6354 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
6355
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006356 task->context = hlua;
6357 task->process = hlua_process_task;
6358
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006359 if (!hlua_ctx_init(hlua, task, 1))
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006360 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006361
6362 /* Restore the function in the stack. */
6363 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref);
6364 hlua->nargs = 0;
6365
6366 /* Schedule task. */
6367 task_schedule(task, now_ms);
6368
6369 return 0;
6370}
6371
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006372/* Wrapper called by HAProxy to execute an LUA converter. This wrapper
6373 * doesn't allow "yield" functions because the HAProxy engine cannot
6374 * resume converters.
6375 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006376static int hlua_sample_conv_wrapper(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006377{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006378 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006379 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006380 const char *error;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006381
Willy Tarreaube508f12016-03-10 11:47:01 +01006382 if (!stream)
6383 return 0;
6384
Willy Tarreau87b09662015-04-03 00:22:06 +02006385 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006386 * Lua context can be not initialized. This behavior
6387 * permits to save performances because a systematic
6388 * Lua initialization cause 5% performances loss.
6389 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006390 if (!stream->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006391 stream->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006392 if (!stream->hlua) {
6393 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
6394 return 0;
6395 }
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006396 if (!hlua_ctx_init(stream->hlua, stream->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006397 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
6398 return 0;
6399 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006400 }
6401
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006402 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006403 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006404
6405 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006406 if (!SET_SAFE_LJMP(stream->hlua->T)) {
6407 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
6408 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006409 else
6410 error = "critical error";
6411 SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006412 return 0;
6413 }
6414
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006415 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006416 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006417 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006418 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006419 return 0;
6420 }
6421
6422 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006423 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006424
6425 /* convert input sample and pust-it in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006426 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006427 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006428 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006429 return 0;
6430 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006431 hlua_smp2lua(stream->hlua->T, smp);
6432 stream->hlua->nargs = 1;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006433
6434 /* push keywords in the stack. */
6435 if (arg_p) {
6436 for (; arg_p->type != ARGT_STOP; arg_p++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006437 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006438 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006439 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006440 return 0;
6441 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006442 hlua_arg2lua(stream->hlua->T, arg_p);
6443 stream->hlua->nargs++;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006444 }
6445 }
6446
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006447 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006448 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006449
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006450 /* At this point the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006451 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006452 }
6453
6454 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006455 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006456 /* finished. */
6457 case HLUA_E_OK:
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02006458 /* If the stack is empty, the function fails. */
6459 if (lua_gettop(stream->hlua->T) <= 0)
6460 return 0;
6461
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006462 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006463 hlua_lua2smp(stream->hlua->T, -1, smp);
6464 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006465 return 1;
6466
6467 /* yield. */
6468 case HLUA_E_AGAIN:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006469 SEND_ERR(stream->be, "Lua converter '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006470 return 0;
6471
6472 /* finished with error. */
6473 case HLUA_E_ERRMSG:
6474 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006475 SEND_ERR(stream->be, "Lua converter '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006476 fcn->name, lua_tostring(stream->hlua->T, -1));
6477 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006478 return 0;
6479
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006480 case HLUA_E_ETMOUT:
6481 SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name);
6482 return 0;
6483
6484 case HLUA_E_NOMEM:
6485 SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name);
6486 return 0;
6487
6488 case HLUA_E_YIELD:
6489 SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name);
6490 return 0;
6491
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006492 case HLUA_E_ERR:
6493 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006494 SEND_ERR(stream->be, "Lua converter '%s' returns an unknown error.\n", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006495
6496 default:
6497 return 0;
6498 }
6499}
6500
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006501/* Wrapper called by HAProxy to execute a sample-fetch. this wrapper
6502 * doesn't allow "yield" functions because the HAProxy engine cannot
Willy Tarreaube508f12016-03-10 11:47:01 +01006503 * resume sample-fetches. This function will be called by the sample
6504 * fetch engine to call lua-based fetch operations.
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006505 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +02006506static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp,
6507 const char *kw, void *private)
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006508{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006509 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006510 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006511 const char *error;
Willy Tarreau83061a82018-07-13 11:56:34 +02006512 const struct buffer msg = { };
Christopher Fauletff96b8b2019-07-26 15:09:53 +02006513 unsigned int hflags = HLUA_TXN_NOTERM;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006514
Willy Tarreaube508f12016-03-10 11:47:01 +01006515 if (!stream)
6516 return 0;
Christopher Fauletafd8f102018-11-08 11:34:21 +01006517
Willy Tarreau87b09662015-04-03 00:22:06 +02006518 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006519 * Lua context can be not initialized. This behavior
6520 * permits to save performances because a systematic
6521 * Lua initialization cause 5% performances loss.
6522 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006523 if (!stream->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006524 stream->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006525 if (!stream->hlua) {
6526 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
6527 return 0;
6528 }
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006529 if (!hlua_ctx_init(stream->hlua, stream->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006530 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
6531 return 0;
6532 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006533 }
6534
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006535 consistency_set(stream, smp->opt, &stream->hlua->cons);
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006536
Christopher Fauletff96b8b2019-07-26 15:09:53 +02006537 if (stream->be->mode == PR_MODE_HTTP) {
6538 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
6539 hflags |= ((stream->txn->req.msg_state < HTTP_MSG_BODY) ? 0 : HLUA_TXN_HTTP_RDY);
6540 else
6541 hflags |= ((stream->txn->rsp.msg_state < HTTP_MSG_BODY) ? 0 : HLUA_TXN_HTTP_RDY);
6542 }
6543
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006544 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006545 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006546
6547 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006548 if (!SET_SAFE_LJMP(stream->hlua->T)) {
6549 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
6550 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006551 else
6552 error = "critical error";
6553 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006554 return 0;
6555 }
6556
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006557 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006558 if (!lua_checkstack(stream->hlua->T, 2)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006559 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006560 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006561 return 0;
6562 }
6563
6564 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006565 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006566
6567 /* push arguments in the stack. */
Christopher Fauletff96b8b2019-07-26 15:09:53 +02006568 if (!hlua_txn_new(stream->hlua->T, stream, smp->px, smp->opt & SMP_OPT_DIR, hflags)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006569 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006570 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006571 return 0;
6572 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006573 stream->hlua->nargs = 1;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006574
6575 /* push keywords in the stack. */
6576 for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) {
6577 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006578 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006579 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006580 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006581 return 0;
6582 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006583 hlua_arg2lua(stream->hlua->T, arg_p);
6584 stream->hlua->nargs++;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006585 }
6586
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006587 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006588 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006589
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006590 /* At this point the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006591 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006592 }
6593
6594 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006595 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006596 /* finished. */
6597 case HLUA_E_OK:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006598 if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) {
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006599 si_retnclose(&stream->si[0], &msg);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02006600 return 0;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006601 }
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02006602 /* If the stack is empty, the function fails. */
6603 if (lua_gettop(stream->hlua->T) <= 0)
6604 return 0;
6605
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006606 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006607 hlua_lua2smp(stream->hlua->T, -1, smp);
6608 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006609
6610 /* Set the end of execution flag. */
6611 smp->flags &= ~SMP_F_MAY_CHANGE;
6612 return 1;
6613
6614 /* yield. */
6615 case HLUA_E_AGAIN:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006616 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006617 si_retnclose(&stream->si[0], &msg);
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006618 SEND_ERR(smp->px, "Lua sample-fetch '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006619 return 0;
6620
6621 /* finished with error. */
6622 case HLUA_E_ERRMSG:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006623 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006624 si_retnclose(&stream->si[0], &msg);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006625 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006626 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006627 fcn->name, lua_tostring(stream->hlua->T, -1));
6628 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006629 return 0;
6630
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006631 case HLUA_E_ETMOUT:
6632 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006633 si_retnclose(&stream->si[0], &msg);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006634 SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name);
6635 return 0;
6636
6637 case HLUA_E_NOMEM:
6638 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006639 si_retnclose(&stream->si[0], &msg);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006640 SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name);
6641 return 0;
6642
6643 case HLUA_E_YIELD:
6644 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006645 si_retnclose(&stream->si[0], &msg);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006646 SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name);
6647 return 0;
6648
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006649 case HLUA_E_ERR:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006650 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006651 si_retnclose(&stream->si[0], &msg);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006652 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006653 SEND_ERR(smp->px, "Lua sample-fetch '%s' returns an unknown error.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006654
6655 default:
6656 return 0;
6657 }
6658}
6659
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006660/* This function is an LUA binding used for registering
6661 * "sample-conv" functions. It expects a converter name used
6662 * in the haproxy configuration file, and an LUA function.
6663 */
6664__LJMP static int hlua_register_converters(lua_State *L)
6665{
6666 struct sample_conv_kw_list *sck;
6667 const char *name;
6668 int ref;
6669 int len;
6670 struct hlua_function *fcn;
6671
6672 MAY_LJMP(check_args(L, 2, "register_converters"));
6673
6674 /* First argument : converter name. */
6675 name = MAY_LJMP(luaL_checkstring(L, 1));
6676
6677 /* Second argument : lua function. */
6678 ref = MAY_LJMP(hlua_checkfunction(L, 2));
6679
6680 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006681 sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006682 if (!sck)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006683 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006684 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006685 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006686 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006687
6688 /* Fill fcn. */
6689 fcn->name = strdup(name);
6690 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006691 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006692 fcn->function_ref = ref;
6693
6694 /* List head */
6695 sck->list.n = sck->list.p = NULL;
6696
6697 /* converter keyword. */
6698 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006699 sck->kw[0].kw = calloc(1, len);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006700 if (!sck->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006701 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006702
6703 snprintf((char *)sck->kw[0].kw, len, "lua.%s", name);
6704 sck->kw[0].process = hlua_sample_conv_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01006705 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 +01006706 sck->kw[0].val_args = NULL;
6707 sck->kw[0].in_type = SMP_T_STR;
6708 sck->kw[0].out_type = SMP_T_STR;
6709 sck->kw[0].private = fcn;
6710
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006711 /* Register this new converter */
6712 sample_register_convs(sck);
6713
6714 return 0;
6715}
6716
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006717/* This function is an LUA binding used for registering
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006718 * "sample-fetch" functions. It expects a converter name used
6719 * in the haproxy configuration file, and an LUA function.
6720 */
6721__LJMP static int hlua_register_fetches(lua_State *L)
6722{
6723 const char *name;
6724 int ref;
6725 int len;
6726 struct sample_fetch_kw_list *sfk;
6727 struct hlua_function *fcn;
6728
6729 MAY_LJMP(check_args(L, 2, "register_fetches"));
6730
6731 /* First argument : sample-fetch name. */
6732 name = MAY_LJMP(luaL_checkstring(L, 1));
6733
6734 /* Second argument : lua function. */
6735 ref = MAY_LJMP(hlua_checkfunction(L, 2));
6736
6737 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006738 sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006739 if (!sfk)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006740 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006741 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006742 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006743 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006744
6745 /* Fill fcn. */
6746 fcn->name = strdup(name);
6747 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006748 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006749 fcn->function_ref = ref;
6750
6751 /* List head */
6752 sfk->list.n = sfk->list.p = NULL;
6753
6754 /* sample-fetch keyword. */
6755 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006756 sfk->kw[0].kw = calloc(1, len);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006757 if (!sfk->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006758 return luaL_error(L, "Lua out of memory error.");
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006759
6760 snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name);
6761 sfk->kw[0].process = hlua_sample_fetch_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01006762 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 +01006763 sfk->kw[0].val_args = NULL;
6764 sfk->kw[0].out_type = SMP_T_STR;
6765 sfk->kw[0].use = SMP_USE_HTTP_ANY;
6766 sfk->kw[0].val = 0;
6767 sfk->kw[0].private = fcn;
6768
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006769 /* Register this new fetch. */
6770 sample_register_fetches(sfk);
6771
6772 return 0;
6773}
6774
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006775/* This function is a wrapper to execute each LUA function declared
6776 * as an action wrapper during the initialisation period. This function
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006777 * return ACT_RET_CONT if the processing is finished (with or without
6778 * error) and return ACT_RET_YIELD if the function must be called again
6779 * because the LUA returns a yield.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006780 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006781static enum act_return hlua_action(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +02006782 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006783{
6784 char **arg;
Christopher Fauletff96b8b2019-07-26 15:09:53 +02006785 unsigned int hflags = 0;
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02006786 int dir;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006787 const char *error;
Willy Tarreau83061a82018-07-13 11:56:34 +02006788 const struct buffer msg = { };
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006789
6790 switch (rule->from) {
Christopher Fauletff96b8b2019-07-26 15:09:53 +02006791 case ACT_F_TCP_REQ_CNT: ; dir = SMP_OPT_DIR_REQ; break;
6792 case ACT_F_TCP_RES_CNT: ; dir = SMP_OPT_DIR_RES; break;
6793 case ACT_F_HTTP_REQ: hflags = HLUA_TXN_HTTP_RDY ; dir = SMP_OPT_DIR_REQ; break;
6794 case ACT_F_HTTP_RES: hflags = HLUA_TXN_HTTP_RDY ; dir = SMP_OPT_DIR_RES; break;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006795 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006796 SEND_ERR(px, "Lua: internal error while execute action.\n");
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006797 return ACT_RET_CONT;
6798 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006799
Willy Tarreau87b09662015-04-03 00:22:06 +02006800 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006801 * Lua context can be not initialized. This behavior
6802 * permits to save performances because a systematic
6803 * Lua initialization cause 5% performances loss.
6804 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006805 if (!s->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006806 s->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006807 if (!s->hlua) {
6808 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
6809 rule->arg.hlua_rule->fcn.name);
6810 return ACT_RET_CONT;
6811 }
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006812 if (!hlua_ctx_init(s->hlua, s->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006813 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
6814 rule->arg.hlua_rule->fcn.name);
6815 return ACT_RET_CONT;
6816 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006817 }
6818
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006819 consistency_set(s, dir, &s->hlua->cons);
6820
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006821 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006822 if (!HLUA_IS_RUNNING(s->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006823
6824 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006825 if (!SET_SAFE_LJMP(s->hlua->T)) {
6826 if (lua_type(s->hlua->T, -1) == LUA_TSTRING)
6827 error = lua_tostring(s->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006828 else
6829 error = "critical error";
6830 SEND_ERR(px, "Lua function '%s': %s.\n",
6831 rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006832 return ACT_RET_CONT;
6833 }
6834
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006835 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006836 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006837 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006838 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006839 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006840 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006841 }
6842
6843 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006844 lua_rawgeti(s->hlua->T, LUA_REGISTRYINDEX, rule->arg.hlua_rule->fcn.function_ref);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006845
Willy Tarreau87b09662015-04-03 00:22:06 +02006846 /* Create and and push object stream in the stack. */
Christopher Fauletff96b8b2019-07-26 15:09:53 +02006847 if (!hlua_txn_new(s->hlua->T, s, px, dir, hflags)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006848 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006849 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006850 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006851 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006852 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006853 s->hlua->nargs = 1;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006854
6855 /* push keywords in the stack. */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006856 for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006857 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006858 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006859 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006860 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006861 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006862 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006863 lua_pushstring(s->hlua->T, *arg);
6864 s->hlua->nargs++;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006865 }
6866
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006867 /* Now the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006868 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006869
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006870 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006871 s->hlua->max_time = hlua_timeout_session;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006872 }
6873
Christopher Faulet719ddc82020-06-02 18:46:07 +02006874 /* Always reset the analyse expiration timeout for the corresponding
6875 * channel in case the lua script yield, to be sure to not keep an
6876 * expired timeout.
6877 */
6878 if (dir == SMP_OPT_DIR_REQ)
6879 s->req.analyse_exp = TICK_ETERNITY;
6880 else
6881 s->res.analyse_exp = TICK_ETERNITY;
6882
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006883 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006884 switch (hlua_ctx_resume(s->hlua, !(flags & ACT_FLAG_FINAL))) {
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006885 /* finished. */
6886 case HLUA_E_OK:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006887 if (!consistency_check(s, dir, &s->hlua->cons)) {
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006888 si_retnclose(&s->si[0], &msg);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02006889 return ACT_RET_ERR;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006890 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006891 if (s->hlua->flags & HLUA_STOP)
Christopher Faulet4629d082019-07-04 11:27:15 +02006892 return ACT_RET_DONE;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006893 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006894
6895 /* yield. */
6896 case HLUA_E_AGAIN:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006897 /* Set timeout in the required channel. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006898 if (s->hlua->wake_time != TICK_ETERNITY) {
Christopher Fauletea0b0e22019-08-14 23:19:45 +02006899 if (dir == SMP_OPT_DIR_REQ)
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006900 s->req.analyse_exp = s->hlua->wake_time;
Christopher Fauletff96b8b2019-07-26 15:09:53 +02006901 else
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006902 s->res.analyse_exp = s->hlua->wake_time;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006903 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006904 /* Some actions can be wake up when a "write" event
6905 * is detected on a response channel. This is useful
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006906 * only for actions targeted on the requests.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006907 */
Christopher Fauletb22f6502019-07-26 14:54:52 +02006908 if (HLUA_IS_WAKERESWR(s->hlua))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006909 s->res.flags |= CF_WAKE_WRITE;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006910 if (HLUA_IS_WAKEREQWR(s->hlua))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006911 s->req.flags |= CF_WAKE_WRITE;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006912 /* We can quit the function without consistency check
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006913 * because HAProxy is not able to manipulate data, it
6914 * is only allowed to call me again. */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006915 return ACT_RET_YIELD;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006916
6917 /* finished with error. */
6918 case HLUA_E_ERRMSG:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006919 if (!consistency_check(s, dir, &s->hlua->cons)) {
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006920 si_retnclose(&s->si[0], &msg);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02006921 return ACT_RET_ERR;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006922 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006923 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006924 SEND_ERR(px, "Lua function '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006925 rule->arg.hlua_rule->fcn.name, lua_tostring(s->hlua->T, -1));
6926 lua_pop(s->hlua->T, 1);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006927 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006928
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006929 case HLUA_E_ETMOUT:
6930 if (!consistency_check(s, dir, &s->hlua->cons)) {
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006931 si_retnclose(&s->si[0], &msg);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006932 return ACT_RET_ERR;
6933 }
6934 SEND_ERR(px, "Lua function '%s': execution timeout.\n", rule->arg.hlua_rule->fcn.name);
6935 return 0;
6936
6937 case HLUA_E_NOMEM:
6938 if (!consistency_check(s, dir, &s->hlua->cons)) {
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006939 si_retnclose(&s->si[0], &msg);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006940 return ACT_RET_ERR;
6941 }
6942 SEND_ERR(px, "Lua function '%s': out of memory error.\n", rule->arg.hlua_rule->fcn.name);
6943 return 0;
6944
6945 case HLUA_E_YIELD:
6946 if (!consistency_check(s, dir, &s->hlua->cons)) {
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006947 si_retnclose(&s->si[0], &msg);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006948 return ACT_RET_ERR;
6949 }
6950 SEND_ERR(px, "Lua function '%s': aborting Lua processing on expired timeout.\n",
6951 rule->arg.hlua_rule->fcn.name);
6952 return 0;
6953
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006954 case HLUA_E_ERR:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006955 if (!consistency_check(s, dir, &s->hlua->cons)) {
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006956 si_retnclose(&s->si[0], &msg);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02006957 return ACT_RET_ERR;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006958 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006959 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006960 SEND_ERR(px, "Lua function '%s' return an unknown error.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006961 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006962
6963 default:
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006964 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006965 }
6966}
6967
Olivier Houchard9f6af332018-05-25 14:04:04 +02006968struct task *hlua_applet_wakeup(struct task *t, void *context, unsigned short state)
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006969{
Olivier Houchard9f6af332018-05-25 14:04:04 +02006970 struct appctx *ctx = context;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006971
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006972 appctx_wakeup(ctx);
Willy Tarreaud9587412017-08-23 16:07:33 +02006973 t->expire = TICK_ETERNITY;
Willy Tarreaud1aa41f2017-07-21 16:41:56 +02006974 return t;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006975}
6976
6977static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
6978{
6979 struct stream_interface *si = ctx->owner;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006980 struct hlua *hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006981 struct task *task;
6982 char **arg;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006983 const char *error;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006984
Willy Tarreaubafbe012017-11-24 17:34:44 +01006985 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006986 if (!hlua) {
6987 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
6988 ctx->rule->arg.hlua_rule->fcn.name);
6989 return 0;
6990 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006991 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006992 ctx->ctx.hlua_apptcp.hlua = hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006993 ctx->ctx.hlua_apptcp.flags = 0;
6994
6995 /* Create task used by signal to wakeup applets. */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01006996 task = task_new(tid_bit);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006997 if (!task) {
6998 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
6999 ctx->rule->arg.hlua_rule->fcn.name);
7000 return 0;
7001 }
7002 task->nice = 0;
7003 task->context = ctx;
7004 task->process = hlua_applet_wakeup;
7005 ctx->ctx.hlua_apptcp.task = task;
7006
7007 /* In the execution wrappers linked with a stream, the
7008 * Lua context can be not initialized. This behavior
7009 * permits to save performances because a systematic
7010 * Lua initialization cause 5% performances loss.
7011 */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01007012 if (!hlua_ctx_init(hlua, task, 0)) {
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007013 SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n",
7014 ctx->rule->arg.hlua_rule->fcn.name);
7015 return 0;
7016 }
7017
7018 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02007019 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007020
7021 /* The following Lua calls can fail. */
7022 if (!SET_SAFE_LJMP(hlua->T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01007023 if (lua_type(hlua->T, -1) == LUA_TSTRING)
7024 error = lua_tostring(hlua->T, -1);
7025 else
7026 error = "critical error";
7027 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
7028 ctx->rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007029 return 0;
7030 }
7031
7032 /* Check stack available size. */
7033 if (!lua_checkstack(hlua->T, 1)) {
7034 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
7035 ctx->rule->arg.hlua_rule->fcn.name);
7036 RESET_SAFE_LJMP(hlua->T);
7037 return 0;
7038 }
7039
7040 /* Restore the function in the stack. */
7041 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref);
7042
7043 /* Create and and push object stream in the stack. */
7044 if (!hlua_applet_tcp_new(hlua->T, ctx)) {
7045 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
7046 ctx->rule->arg.hlua_rule->fcn.name);
7047 RESET_SAFE_LJMP(hlua->T);
7048 return 0;
7049 }
7050 hlua->nargs = 1;
7051
7052 /* push keywords in the stack. */
7053 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
7054 if (!lua_checkstack(hlua->T, 1)) {
7055 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
7056 ctx->rule->arg.hlua_rule->fcn.name);
7057 RESET_SAFE_LJMP(hlua->T);
7058 return 0;
7059 }
7060 lua_pushstring(hlua->T, *arg);
7061 hlua->nargs++;
7062 }
7063
7064 RESET_SAFE_LJMP(hlua->T);
7065
7066 /* Wakeup the applet ASAP. */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01007067 si_cant_get(si);
Willy Tarreau3367d412018-11-15 10:57:41 +01007068 si_rx_endp_more(si);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007069
7070 return 1;
7071}
7072
Willy Tarreau83a5ff42019-08-21 14:14:50 +02007073void hlua_applet_tcp_fct(struct appctx *ctx)
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007074{
7075 struct stream_interface *si = ctx->owner;
7076 struct stream *strm = si_strm(si);
7077 struct channel *res = si_ic(si);
7078 struct act_rule *rule = ctx->rule;
7079 struct proxy *px = strm->be;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007080 struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007081
7082 /* The applet execution is already done. */
Olivier Houchard594c8c52018-08-28 14:41:31 +02007083 if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) {
7084 /* eat the whole request */
7085 co_skip(si_oc(si), co_data(si_oc(si)));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007086 return;
Olivier Houchard594c8c52018-08-28 14:41:31 +02007087 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007088
7089 /* If the stream is disconnect or closed, ldo nothing. */
7090 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
7091 return;
7092
7093 /* Execute the function. */
7094 switch (hlua_ctx_resume(hlua, 1)) {
7095 /* finished. */
7096 case HLUA_E_OK:
7097 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
7098
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007099 /* eat the whole request */
Willy Tarreaua79021a2018-06-15 18:07:57 +02007100 co_skip(si_oc(si), co_data(si_oc(si)));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007101 res->flags |= CF_READ_NULL;
7102 si_shutr(si);
7103 return;
7104
7105 /* yield. */
7106 case HLUA_E_AGAIN:
Thierry Fournier0164f202016-02-20 17:47:43 +01007107 if (hlua->wake_time != TICK_ETERNITY)
7108 task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007109 return;
7110
7111 /* finished with error. */
7112 case HLUA_E_ERRMSG:
7113 /* Display log. */
7114 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
7115 rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1));
7116 lua_pop(hlua->T, 1);
7117 goto error;
7118
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007119 case HLUA_E_ETMOUT:
7120 SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n",
7121 rule->arg.hlua_rule->fcn.name);
7122 goto error;
7123
7124 case HLUA_E_NOMEM:
7125 SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n",
7126 rule->arg.hlua_rule->fcn.name);
7127 goto error;
7128
7129 case HLUA_E_YIELD: /* unexpected */
7130 SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n",
7131 rule->arg.hlua_rule->fcn.name);
7132 goto error;
7133
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007134 case HLUA_E_ERR:
7135 /* Display log. */
7136 SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n",
7137 rule->arg.hlua_rule->fcn.name);
7138 goto error;
7139
7140 default:
7141 goto error;
7142 }
7143
7144error:
7145
7146 /* For all other cases, just close the stream. */
7147 si_shutw(si);
7148 si_shutr(si);
7149 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
7150}
7151
7152static void hlua_applet_tcp_release(struct appctx *ctx)
7153{
Olivier Houchard3f795f72019-04-17 22:51:06 +02007154 task_destroy(ctx->ctx.hlua_apptcp.task);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007155 ctx->ctx.hlua_apptcp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007156 hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007157 ctx->ctx.hlua_apptcp.hlua = NULL;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007158}
7159
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007160/* The function returns 1 if the initialisation is complete, 0 if
7161 * an errors occurs and -1 if more data are required for initializing
7162 * the applet.
7163 */
7164static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
7165{
7166 struct stream_interface *si = ctx->owner;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007167 struct http_txn *txn;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007168 struct hlua *hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007169 char **arg;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007170 struct task *task;
Thierry Fournierfd107a22016-02-19 19:57:23 +01007171 const char *error;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007172
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007173 txn = strm->txn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007174
Willy Tarreau0078bfc2015-10-07 20:20:28 +02007175 /* We want two things in HTTP mode :
7176 * - enforce server-close mode if we were in keep-alive, so that the
7177 * applet is released after each response ;
7178 * - enable request body transfer to the applet in order to resync
7179 * with the response body.
7180 */
7181 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)
7182 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreau0078bfc2015-10-07 20:20:28 +02007183
Willy Tarreaubafbe012017-11-24 17:34:44 +01007184 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007185 if (!hlua) {
7186 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
7187 ctx->rule->arg.hlua_rule->fcn.name);
7188 return 0;
7189 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007190 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007191 ctx->ctx.hlua_apphttp.hlua = hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007192 ctx->ctx.hlua_apphttp.left_bytes = -1;
7193 ctx->ctx.hlua_apphttp.flags = 0;
7194
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +01007195 if (txn->req.flags & HTTP_MSGF_VER_11)
7196 ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11;
7197
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007198 /* Create task used by signal to wakeup applets. */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01007199 task = task_new(tid_bit);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007200 if (!task) {
7201 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
7202 ctx->rule->arg.hlua_rule->fcn.name);
7203 return 0;
7204 }
7205 task->nice = 0;
7206 task->context = ctx;
7207 task->process = hlua_applet_wakeup;
7208 ctx->ctx.hlua_apphttp.task = task;
7209
7210 /* In the execution wrappers linked with a stream, the
7211 * Lua context can be not initialized. This behavior
7212 * permits to save performances because a systematic
7213 * Lua initialization cause 5% performances loss.
7214 */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01007215 if (!hlua_ctx_init(hlua, task, 0)) {
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007216 SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n",
7217 ctx->rule->arg.hlua_rule->fcn.name);
7218 return 0;
7219 }
7220
7221 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02007222 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007223
7224 /* The following Lua calls can fail. */
7225 if (!SET_SAFE_LJMP(hlua->T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01007226 if (lua_type(hlua->T, -1) == LUA_TSTRING)
7227 error = lua_tostring(hlua->T, -1);
7228 else
7229 error = "critical error";
7230 SEND_ERR(px, "Lua applet http '%s': %s.\n",
7231 ctx->rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007232 return 0;
7233 }
7234
7235 /* Check stack available size. */
7236 if (!lua_checkstack(hlua->T, 1)) {
7237 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
7238 ctx->rule->arg.hlua_rule->fcn.name);
7239 RESET_SAFE_LJMP(hlua->T);
7240 return 0;
7241 }
7242
7243 /* Restore the function in the stack. */
7244 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref);
7245
7246 /* Create and and push object stream in the stack. */
7247 if (!hlua_applet_http_new(hlua->T, ctx)) {
7248 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
7249 ctx->rule->arg.hlua_rule->fcn.name);
7250 RESET_SAFE_LJMP(hlua->T);
7251 return 0;
7252 }
7253 hlua->nargs = 1;
7254
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007255 /* push keywords in the stack. */
7256 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
7257 if (!lua_checkstack(hlua->T, 1)) {
7258 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
7259 ctx->rule->arg.hlua_rule->fcn.name);
7260 RESET_SAFE_LJMP(hlua->T);
7261 return 0;
7262 }
7263 lua_pushstring(hlua->T, *arg);
7264 hlua->nargs++;
7265 }
7266
7267 RESET_SAFE_LJMP(hlua->T);
7268
7269 /* Wakeup the applet when data is ready for read. */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01007270 si_cant_get(si);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007271
7272 return 1;
7273}
7274
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007275static void hlua_applet_htx_fct(struct appctx *ctx)
7276{
7277 struct stream_interface *si = ctx->owner;
7278 struct stream *strm = si_strm(si);
7279 struct channel *req = si_oc(si);
7280 struct channel *res = si_ic(si);
7281 struct act_rule *rule = ctx->rule;
7282 struct proxy *px = strm->be;
7283 struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua;
7284 struct htx *req_htx, *res_htx;
7285
7286 res_htx = htx_from_buf(&res->buf);
7287
7288 /* If the stream is disconnect or closed, ldo nothing. */
7289 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
7290 goto out;
7291
7292 /* Check if the input buffer is avalaible. */
7293 if (!b_size(&res->buf)) {
7294 si_rx_room_blk(si);
7295 goto out;
7296 }
7297 /* check that the output is not closed */
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007298 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007299 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7300
7301 /* Set the currently running flag. */
7302 if (!HLUA_IS_RUNNING(hlua) &&
7303 !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
7304 struct htx_blk *blk;
7305 size_t count = co_data(req);
7306
7307 if (!count) {
7308 si_cant_get(si);
7309 goto out;
7310 }
7311
7312 /* We need to flush the request header. This left the body for
7313 * the Lua.
7314 */
7315 req_htx = htx_from_buf(&req->buf);
Christopher Fauleta3f15502019-05-13 15:27:23 +02007316 blk = htx_get_first_blk(req_htx);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007317 while (count && blk) {
7318 enum htx_blk_type type = htx_get_blk_type(blk);
7319 uint32_t sz = htx_get_blksz(blk);
7320
7321 if (sz > count) {
7322 si_cant_get(si);
Christopher Faulet0ae79d02019-02-27 21:36:59 +01007323 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007324 goto out;
7325 }
7326
7327 count -= sz;
7328 co_set_data(req, co_data(req) - sz);
7329 blk = htx_remove_blk(req_htx, blk);
7330
7331 if (type == HTX_BLK_EOH)
7332 break;
7333 }
Christopher Faulet0ae79d02019-02-27 21:36:59 +01007334 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007335 }
7336
7337 /* Executes The applet if it is not done. */
7338 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
7339
7340 /* Execute the function. */
7341 switch (hlua_ctx_resume(hlua, 1)) {
7342 /* finished. */
7343 case HLUA_E_OK:
7344 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7345 break;
7346
7347 /* yield. */
7348 case HLUA_E_AGAIN:
7349 if (hlua->wake_time != TICK_ETERNITY)
7350 task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time);
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007351 goto out;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007352
7353 /* finished with error. */
7354 case HLUA_E_ERRMSG:
7355 /* Display log. */
7356 SEND_ERR(px, "Lua applet http '%s': %s.\n",
7357 rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1));
7358 lua_pop(hlua->T, 1);
7359 goto error;
7360
7361 case HLUA_E_ETMOUT:
7362 SEND_ERR(px, "Lua applet http '%s': execution timeout.\n",
7363 rule->arg.hlua_rule->fcn.name);
7364 goto error;
7365
7366 case HLUA_E_NOMEM:
7367 SEND_ERR(px, "Lua applet http '%s': out of memory error.\n",
7368 rule->arg.hlua_rule->fcn.name);
7369 goto error;
7370
7371 case HLUA_E_YIELD: /* unexpected */
7372 SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n",
7373 rule->arg.hlua_rule->fcn.name);
7374 goto error;
7375
7376 case HLUA_E_ERR:
7377 /* Display log. */
7378 SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n",
7379 rule->arg.hlua_rule->fcn.name);
7380 goto error;
7381
7382 default:
7383 goto error;
7384 }
7385 }
7386
7387 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007388 if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT)
7389 goto done;
7390
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007391 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT))
7392 goto error;
7393
Christopher Faulet54b5e212019-06-04 10:08:28 +02007394 /* Don't add TLR because mux-h1 will take care of it */
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007395 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
7396 si_rx_room_blk(si);
7397 goto out;
7398 }
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01007399 channel_add_input(res, 1);
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007400 strm->txn->status = ctx->ctx.hlua_apphttp.status;
7401 ctx->ctx.hlua_apphttp.flags |= APPLET_RSP_SENT;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007402 }
7403
7404 done:
7405 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007406 if (!(res->flags & CF_SHUTR)) {
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007407 res->flags |= CF_READ_NULL;
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007408 si_shutr(si);
7409 }
7410
7411 /* eat the whole request */
7412 if (co_data(req)) {
7413 req_htx = htx_from_buf(&req->buf);
7414 co_htx_skip(req, req_htx, co_data(req));
7415 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007416 }
7417 }
7418
7419 out:
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007420 htx_to_buf(res_htx, &res->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007421 return;
7422
7423 error:
7424
7425 /* If we are in HTTP mode, and we are not send any
7426 * data, return a 500 server error in best effort:
7427 * if there is no room available in the buffer,
7428 * just close the connection.
7429 */
7430 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
7431 struct buffer *err = &htx_err_chunks[HTTP_ERR_500];
7432
7433 channel_erase(res);
7434 res->buf.data = b_data(err);
7435 memcpy(res->buf.area, b_head(err), b_data(err));
7436 res_htx = htx_from_buf(&res->buf);
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01007437 channel_add_input(res, res_htx->data);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007438 }
7439 if (!(strm->flags & SF_ERR_MASK))
7440 strm->flags |= SF_ERR_RESOURCE;
7441 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7442 goto done;
7443}
7444
Willy Tarreau83a5ff42019-08-21 14:14:50 +02007445void hlua_applet_http_fct(struct appctx *ctx)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007446{
7447 struct stream_interface *si = ctx->owner;
7448 struct stream *strm = si_strm(si);
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007449 struct channel *req = si_oc(si);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007450 struct channel *res = si_ic(si);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007451 struct act_rule *rule = ctx->rule;
7452 struct proxy *px = strm->be;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007453 struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua;
Willy Tarreau206ba832018-06-14 15:27:31 +02007454 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02007455 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02007456 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02007457 size_t len2;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007458 int ret;
7459
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007460 if (IS_HTX_STRM(strm))
7461 return hlua_applet_htx_fct(ctx);
7462
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007463 /* If the stream is disconnect or closed, ldo nothing. */
7464 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007465 goto out;
7466
7467 /* Check if the input buffer is avalaible. */
7468 if (!b_size(&res->buf)) {
7469 si_rx_room_blk(si);
7470 goto out;
7471 }
7472 /* check that the output is not closed */
7473 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
7474 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007475
7476 /* Set the currently running flag. */
7477 if (!HLUA_IS_RUNNING(hlua) &&
7478 !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007479 /* Store the max amount of bytes that we can read. */
7480 ctx->ctx.hlua_apphttp.left_bytes = strm->txn->req.body_len;
7481
7482 /* We need to flush the request header. This left the body
7483 * for the Lua.
7484 */
7485
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007486 /* Read the maximum amount of data available. */
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007487 ret = co_getblk_nc(req, &blk1, &len1, &blk2, &len2);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007488 if (ret == -1)
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007489 goto out;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007490
7491 /* No data available, ask for more data. */
7492 if (ret == 1)
7493 len2 = 0;
7494 if (ret == 0)
7495 len1 = 0;
Thierry FOURNIER70d318c2018-06-30 10:37:33 +02007496 if (len1 + len2 < strm->txn->req.eoh + strm->txn->req.eol) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01007497 si_cant_get(si);
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007498 goto out;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007499 }
7500
7501 /* skip the requests bytes. */
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007502 co_skip(req, strm->txn->req.eoh + strm->txn->req.eol);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007503 }
7504
7505 /* Executes The applet if it is not done. */
7506 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
7507
7508 /* Execute the function. */
7509 switch (hlua_ctx_resume(hlua, 1)) {
7510 /* finished. */
7511 case HLUA_E_OK:
7512 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7513 break;
7514
7515 /* yield. */
7516 case HLUA_E_AGAIN:
Thierry Fournier0164f202016-02-20 17:47:43 +01007517 if (hlua->wake_time != TICK_ETERNITY)
7518 task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time);
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007519 goto out;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007520
7521 /* finished with error. */
7522 case HLUA_E_ERRMSG:
7523 /* Display log. */
7524 SEND_ERR(px, "Lua applet http '%s': %s.\n",
7525 rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1));
7526 lua_pop(hlua->T, 1);
7527 goto error;
7528
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007529 case HLUA_E_ETMOUT:
7530 SEND_ERR(px, "Lua applet http '%s': execution timeout.\n",
7531 rule->arg.hlua_rule->fcn.name);
7532 goto error;
7533
7534 case HLUA_E_NOMEM:
7535 SEND_ERR(px, "Lua applet http '%s': out of memory error.\n",
7536 rule->arg.hlua_rule->fcn.name);
7537 goto error;
7538
7539 case HLUA_E_YIELD: /* unexpected */
7540 SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n",
7541 rule->arg.hlua_rule->fcn.name);
7542 goto error;
7543
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007544 case HLUA_E_ERR:
7545 /* Display log. */
7546 SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n",
7547 rule->arg.hlua_rule->fcn.name);
7548 goto error;
7549
7550 default:
7551 goto error;
7552 }
7553 }
7554
7555 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007556 if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT)
7557 goto done;
7558
Christopher Fauletcc26b132018-12-18 21:20:57 +01007559 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT))
7560 goto error;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007561
7562 /* We must send the final chunk. */
7563 if (ctx->ctx.hlua_apphttp.flags & APPLET_CHUNKED &&
7564 !(ctx->ctx.hlua_apphttp.flags & APPLET_LAST_CHK)) {
7565
7566 /* sent last chunk at once. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02007567 ret = ci_putblk(res, "0\r\n\r\n", 5);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007568
7569 /* critical error. */
7570 if (ret == -2 || ret == -3) {
7571 SEND_ERR(px, "Lua applet http '%s'cannont send last chunk.\n",
7572 rule->arg.hlua_rule->fcn.name);
7573 goto error;
7574 }
7575
7576 /* no enough space error. */
7577 if (ret == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01007578 si_rx_room_blk(si);
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007579 goto out;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007580 }
7581
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007582 strm->txn->status = ctx->ctx.hlua_apphttp.status;
7583 ctx->ctx.hlua_apphttp.flags |= (APPLET_LAST_CHK|APPLET_RSP_SENT);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007584 }
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007585 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007586
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007587 done:
7588 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
7589 if (!(res->flags & CF_SHUTR)) {
7590 res->flags |= CF_READ_NULL;
7591 si_shutr(si);
7592 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007593
7594 /* eat the whole request */
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007595 if (co_data(req))
7596 co_skip(req, co_data(req));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007597 }
7598
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007599 out:
7600 return;
7601
7602 error:
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007603
7604 /* If we are in HTTP mode, and we are not send any
7605 * data, return a 500 server error in best effort:
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007606 * if there is no room available in the buffer,
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007607 * just close the connection.
7608 */
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007609 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
7610 channel_erase(res);
7611 ci_putblk(res, error_500, strlen(error_500));
7612 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007613 if (!(strm->flags & SF_ERR_MASK))
7614 strm->flags |= SF_ERR_RESOURCE;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007615 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007616 goto done;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007617}
7618
7619static void hlua_applet_http_release(struct appctx *ctx)
7620{
Olivier Houchard3f795f72019-04-17 22:51:06 +02007621 task_destroy(ctx->ctx.hlua_apphttp.task);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007622 ctx->ctx.hlua_apphttp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007623 hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007624 ctx->ctx.hlua_apphttp.hlua = NULL;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007625}
7626
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007627/* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in
7628 * succes case, else return ACT_RET_PRS_ERR.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007629 *
7630 * This function can fail with an abort() due to an Lua critical error.
7631 * We are in the configuration parsing process of HAProxy, this abort() is
7632 * tolerated.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007633 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007634static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px,
7635 struct act_rule *rule, char **err)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007636{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007637 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007638 int i;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007639
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007640 /* Memory for the rule. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007641 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007642 if (!rule->arg.hlua_rule) {
7643 memprintf(err, "out of memory error");
Thierry FOURNIERafa80492015-08-19 09:04:15 +02007644 return ACT_RET_PRS_ERR;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007645 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007646
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007647 /* Memory for arguments. */
7648 rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, sizeof(char *));
7649 if (!rule->arg.hlua_rule->args) {
7650 memprintf(err, "out of memory error");
7651 return ACT_RET_PRS_ERR;
7652 }
7653
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007654 /* Reference the Lua function and store the reference. */
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007655 rule->arg.hlua_rule->fcn = *fcn;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007656
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007657 /* Expect some arguments */
7658 for (i = 0; i < fcn->nargs; i++) {
Thierry FOURNIER1725c2e2019-01-06 19:38:49 +01007659 if (*args[*cur_arg] == '\0') {
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007660 memprintf(err, "expect %d arguments", fcn->nargs);
7661 return ACT_RET_PRS_ERR;
7662 }
Thierry FOURNIER1725c2e2019-01-06 19:38:49 +01007663 rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]);
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007664 if (!rule->arg.hlua_rule->args[i]) {
7665 memprintf(err, "out of memory error");
7666 return ACT_RET_PRS_ERR;
7667 }
7668 (*cur_arg)++;
7669 }
7670 rule->arg.hlua_rule->args[i] = NULL;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007671
Thierry FOURNIER42148732015-09-02 17:17:33 +02007672 rule->action = ACT_CUSTOM;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007673 rule->action_ptr = hlua_action;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02007674 return ACT_RET_PRS_OK;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007675}
7676
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007677static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px,
7678 struct act_rule *rule, char **err)
7679{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007680 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007681
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01007682 /* HTTP applets are forbidden in tcp-request rules.
7683 * HTTP applet request requires everything initilized by
7684 * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER).
7685 * The applet will be immediately initilized, but its before
7686 * the call of this analyzer.
7687 */
7688 if (rule->from != ACT_F_HTTP_REQ) {
7689 memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets");
7690 return ACT_RET_PRS_ERR;
7691 }
7692
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007693 /* Memory for the rule. */
7694 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
7695 if (!rule->arg.hlua_rule) {
7696 memprintf(err, "out of memory error");
7697 return ACT_RET_PRS_ERR;
7698 }
7699
7700 /* Reference the Lua function and store the reference. */
7701 rule->arg.hlua_rule->fcn = *fcn;
7702
7703 /* TODO: later accept arguments. */
7704 rule->arg.hlua_rule->args = NULL;
7705
7706 /* Add applet pointer in the rule. */
7707 rule->applet.obj_type = OBJ_TYPE_APPLET;
7708 rule->applet.name = fcn->name;
7709 rule->applet.init = hlua_applet_http_init;
7710 rule->applet.fct = hlua_applet_http_fct;
7711 rule->applet.release = hlua_applet_http_release;
7712 rule->applet.timeout = hlua_timeout_applet;
7713
7714 return ACT_RET_PRS_OK;
7715}
7716
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007717/* This function is an LUA binding used for registering
7718 * "sample-conv" functions. It expects a converter name used
7719 * in the haproxy configuration file, and an LUA function.
7720 */
7721__LJMP static int hlua_register_action(lua_State *L)
7722{
7723 struct action_kw_list *akl;
7724 const char *name;
7725 int ref;
7726 int len;
7727 struct hlua_function *fcn;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007728 int nargs;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007729
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007730 /* Initialise the number of expected arguments at 0. */
7731 nargs = 0;
7732
7733 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
7734 WILL_LJMP(luaL_error(L, "'register_action' needs between 3 and 4 arguments"));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007735
7736 /* First argument : converter name. */
7737 name = MAY_LJMP(luaL_checkstring(L, 1));
7738
7739 /* Second argument : environment. */
7740 if (lua_type(L, 2) != LUA_TTABLE)
7741 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
7742
7743 /* Third argument : lua function. */
7744 ref = MAY_LJMP(hlua_checkfunction(L, 3));
7745
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007746 /* Fourth argument : number of mandatory arguments expected on the configuration line. */
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007747 if (lua_gettop(L) >= 4)
7748 nargs = MAY_LJMP(luaL_checkinteger(L, 4));
7749
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007750 /* browse the second argument as an array. */
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007751 lua_pushnil(L);
7752 while (lua_next(L, 2) != 0) {
7753 if (lua_type(L, -1) != LUA_TSTRING)
7754 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
7755
7756 /* Check required environment. Only accepted "http" or "tcp". */
7757 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007758 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007759 if (!akl)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007760 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007761 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007762 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007763 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007764
7765 /* Fill fcn. */
7766 fcn->name = strdup(name);
7767 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007768 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007769 fcn->function_ref = ref;
7770
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007771 /* Set the expected number od arguments. */
7772 fcn->nargs = nargs;
7773
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007774 /* List head */
7775 akl->list.n = akl->list.p = NULL;
7776
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007777 /* action keyword. */
7778 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007779 akl->kw[0].kw = calloc(1, len);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007780 if (!akl->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007781 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007782
7783 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
7784
7785 akl->kw[0].match_pfx = 0;
7786 akl->kw[0].private = fcn;
7787 akl->kw[0].parse = action_register_lua;
7788
7789 /* select the action registering point. */
7790 if (strcmp(lua_tostring(L, -1), "tcp-req") == 0)
7791 tcp_req_cont_keywords_register(akl);
7792 else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0)
7793 tcp_res_cont_keywords_register(akl);
7794 else if (strcmp(lua_tostring(L, -1), "http-req") == 0)
7795 http_req_keywords_register(akl);
7796 else if (strcmp(lua_tostring(L, -1), "http-res") == 0)
7797 http_res_keywords_register(akl);
7798 else
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007799 WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. "
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007800 "'tcp-req', 'tcp-res', 'http-req' or 'http-res' "
7801 "are expected.", lua_tostring(L, -1)));
7802
7803 /* pop the environment string. */
7804 lua_pop(L, 1);
7805 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007806 return ACT_RET_PRS_OK;
7807}
7808
7809static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px,
7810 struct act_rule *rule, char **err)
7811{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007812 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007813
Christopher Fauleteb709802019-04-11 22:04:08 +02007814 if (px->mode == PR_MODE_HTTP && (px->options2 & PR_O2_USE_HTX)) {
Christopher Fauletafd8f102018-11-08 11:34:21 +01007815 memprintf(err, "Lua services cannot be used when the HTX internal representation is enabled");
7816 return ACT_RET_PRS_ERR;
7817 }
7818
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007819 /* Memory for the rule. */
7820 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
7821 if (!rule->arg.hlua_rule) {
7822 memprintf(err, "out of memory error");
7823 return ACT_RET_PRS_ERR;
7824 }
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007825
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007826 /* Reference the Lua function and store the reference. */
7827 rule->arg.hlua_rule->fcn = *fcn;
7828
7829 /* TODO: later accept arguments. */
7830 rule->arg.hlua_rule->args = NULL;
7831
7832 /* Add applet pointer in the rule. */
7833 rule->applet.obj_type = OBJ_TYPE_APPLET;
7834 rule->applet.name = fcn->name;
7835 rule->applet.init = hlua_applet_tcp_init;
7836 rule->applet.fct = hlua_applet_tcp_fct;
7837 rule->applet.release = hlua_applet_tcp_release;
7838 rule->applet.timeout = hlua_timeout_applet;
7839
7840 return 0;
7841}
7842
7843/* This function is an LUA binding used for registering
7844 * "sample-conv" functions. It expects a converter name used
7845 * in the haproxy configuration file, and an LUA function.
7846 */
7847__LJMP static int hlua_register_service(lua_State *L)
7848{
7849 struct action_kw_list *akl;
7850 const char *name;
7851 const char *env;
7852 int ref;
7853 int len;
7854 struct hlua_function *fcn;
7855
7856 MAY_LJMP(check_args(L, 3, "register_service"));
7857
7858 /* First argument : converter name. */
7859 name = MAY_LJMP(luaL_checkstring(L, 1));
7860
7861 /* Second argument : environment. */
7862 env = MAY_LJMP(luaL_checkstring(L, 2));
7863
7864 /* Third argument : lua function. */
7865 ref = MAY_LJMP(hlua_checkfunction(L, 3));
7866
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007867 /* Allocate and fill the sample fetch keyword struct. */
7868 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
7869 if (!akl)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007870 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007871 fcn = calloc(1, sizeof(*fcn));
7872 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007873 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007874
7875 /* Fill fcn. */
7876 len = strlen("<lua.>") + strlen(name) + 1;
7877 fcn->name = calloc(1, len);
7878 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007879 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007880 snprintf((char *)fcn->name, len, "<lua.%s>", name);
7881 fcn->function_ref = ref;
7882
7883 /* List head */
7884 akl->list.n = akl->list.p = NULL;
7885
7886 /* converter keyword. */
7887 len = strlen("lua.") + strlen(name) + 1;
7888 akl->kw[0].kw = calloc(1, len);
7889 if (!akl->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007890 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007891
7892 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
7893
Thierry FOURNIER / OZON.IO02564fd2016-11-12 11:07:05 +01007894 /* Check required environment. Only accepted "http" or "tcp". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007895 if (strcmp(env, "tcp") == 0)
7896 akl->kw[0].parse = action_register_service_tcp;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007897 else if (strcmp(env, "http") == 0)
7898 akl->kw[0].parse = action_register_service_http;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007899 else
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007900 WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. "
Eric Salamafe7456f2017-12-21 14:30:07 +01007901 "'tcp' or 'http' are expected.", env));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007902
7903 akl->kw[0].match_pfx = 0;
7904 akl->kw[0].private = fcn;
7905
7906 /* End of array. */
7907 memset(&akl->kw[1], 0, sizeof(*akl->kw));
7908
7909 /* Register this new converter */
7910 service_keywords_register(akl);
7911
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007912 return 0;
7913}
7914
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007915/* This function initialises Lua cli handler. It copies the
7916 * arguments in the Lua stack and create channel IO objects.
7917 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02007918static int hlua_cli_parse_fct(char **args, char *payload, struct appctx *appctx, void *private)
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007919{
7920 struct hlua *hlua;
7921 struct hlua_function *fcn;
7922 int i;
7923 const char *error;
7924
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007925 fcn = private;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007926 appctx->ctx.hlua_cli.fcn = private;
7927
Willy Tarreaubafbe012017-11-24 17:34:44 +01007928 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007929 if (!hlua) {
7930 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007931 return 1;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007932 }
7933 HLUA_INIT(hlua);
7934 appctx->ctx.hlua_cli.hlua = hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007935
7936 /* Create task used by signal to wakeup applets.
7937 * We use the same wakeup fonction than the Lua applet_tcp and
7938 * applet_http. It is absolutely compatible.
7939 */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01007940 appctx->ctx.hlua_cli.task = task_new(tid_bit);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007941 if (!appctx->ctx.hlua_cli.task) {
Thierry FOURNIERffbf5692016-12-16 11:14:06 +01007942 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007943 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007944 }
7945 appctx->ctx.hlua_cli.task->nice = 0;
7946 appctx->ctx.hlua_cli.task->context = appctx;
7947 appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup;
7948
7949 /* Initialises the Lua context */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01007950 if (!hlua_ctx_init(hlua, appctx->ctx.hlua_cli.task, 0)) {
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007951 SEND_ERR(NULL, "Lua cli '%s': can't initialize Lua context.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007952 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007953 }
7954
7955 /* The following Lua calls can fail. */
7956 if (!SET_SAFE_LJMP(hlua->T)) {
7957 if (lua_type(hlua->T, -1) == LUA_TSTRING)
7958 error = lua_tostring(hlua->T, -1);
7959 else
7960 error = "critical error";
7961 SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error);
7962 goto error;
7963 }
7964
7965 /* Check stack available size. */
7966 if (!lua_checkstack(hlua->T, 2)) {
7967 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7968 goto error;
7969 }
7970
7971 /* Restore the function in the stack. */
7972 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
7973
7974 /* Once the arguments parsed, the CLI is like an AppletTCP,
7975 * so push AppletTCP in the stack.
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007976 */
7977 if (!hlua_applet_tcp_new(hlua->T, appctx)) {
7978 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7979 goto error;
7980 }
7981 hlua->nargs = 1;
7982
7983 /* push keywords in the stack. */
7984 for (i = 0; *args[i]; i++) {
7985 /* Check stack available size. */
7986 if (!lua_checkstack(hlua->T, 1)) {
7987 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7988 goto error;
7989 }
7990 lua_pushstring(hlua->T, args[i]);
7991 hlua->nargs++;
7992 }
7993
7994 /* We must initialize the execution timeouts. */
7995 hlua->max_time = hlua_timeout_session;
7996
7997 /* At this point the execution is safe. */
7998 RESET_SAFE_LJMP(hlua->T);
7999
8000 /* It's ok */
8001 return 0;
8002
8003 /* It's not ok. */
8004error:
8005 RESET_SAFE_LJMP(hlua->T);
8006 hlua_ctx_destroy(hlua);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01008007 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008008 return 1;
8009}
8010
8011static int hlua_cli_io_handler_fct(struct appctx *appctx)
8012{
8013 struct hlua *hlua;
8014 struct stream_interface *si;
8015 struct hlua_function *fcn;
8016
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01008017 hlua = appctx->ctx.hlua_cli.hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008018 si = appctx->owner;
Willy Tarreau8ae4f752016-12-14 15:41:45 +01008019 fcn = appctx->ctx.hlua_cli.fcn;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008020
8021 /* If the stream is disconnect or closed, ldo nothing. */
8022 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
8023 return 1;
8024
8025 /* Execute the function. */
8026 switch (hlua_ctx_resume(hlua, 1)) {
8027
8028 /* finished. */
8029 case HLUA_E_OK:
8030 return 1;
8031
8032 /* yield. */
8033 case HLUA_E_AGAIN:
8034 /* We want write. */
8035 if (HLUA_IS_WAKERESWR(hlua))
Willy Tarreaudb398432018-11-15 11:08:52 +01008036 si_rx_room_blk(si);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008037 /* Set the timeout. */
8038 if (hlua->wake_time != TICK_ETERNITY)
8039 task_schedule(hlua->task, hlua->wake_time);
8040 return 0;
8041
8042 /* finished with error. */
8043 case HLUA_E_ERRMSG:
8044 /* Display log. */
8045 SEND_ERR(NULL, "Lua cli '%s': %s.\n",
8046 fcn->name, lua_tostring(hlua->T, -1));
8047 lua_pop(hlua->T, 1);
8048 return 1;
8049
Thierry Fournierd5b073c2018-05-21 19:42:47 +02008050 case HLUA_E_ETMOUT:
8051 SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n",
8052 fcn->name);
8053 return 1;
8054
8055 case HLUA_E_NOMEM:
8056 SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n",
8057 fcn->name);
8058 return 1;
8059
8060 case HLUA_E_YIELD: /* unexpected */
8061 SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n",
8062 fcn->name);
8063 return 1;
8064
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008065 case HLUA_E_ERR:
8066 /* Display log. */
8067 SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n",
8068 fcn->name);
8069 return 1;
8070
8071 default:
8072 return 1;
8073 }
8074
8075 return 1;
8076}
8077
8078static void hlua_cli_io_release_fct(struct appctx *appctx)
8079{
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01008080 hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01008081 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008082}
8083
8084/* This function is an LUA binding used for registering
8085 * new keywords in the cli. It expects a list of keywords
8086 * which are the "path". It is limited to 5 keywords. A
8087 * description of the command, a function to be executed
8088 * for the parsing and a function for io handlers.
8089 */
8090__LJMP static int hlua_register_cli(lua_State *L)
8091{
8092 struct cli_kw_list *cli_kws;
8093 const char *message;
8094 int ref_io;
8095 int len;
8096 struct hlua_function *fcn;
8097 int index;
8098 int i;
8099
8100 MAY_LJMP(check_args(L, 3, "register_cli"));
8101
8102 /* First argument : an array of maximum 5 keywords. */
8103 if (!lua_istable(L, 1))
8104 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table"));
8105
8106 /* Second argument : string with contextual message. */
8107 message = MAY_LJMP(luaL_checkstring(L, 2));
8108
8109 /* Third and fourth argument : lua function. */
8110 ref_io = MAY_LJMP(hlua_checkfunction(L, 3));
8111
8112 /* Allocate and fill the sample fetch keyword struct. */
8113 cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2);
8114 if (!cli_kws)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008115 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008116 fcn = calloc(1, sizeof(*fcn));
8117 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008118 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008119
8120 /* Fill path. */
8121 index = 0;
8122 lua_pushnil(L);
8123 while(lua_next(L, 1) != 0) {
8124 if (index >= 5)
8125 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries"));
8126 if (lua_type(L, -1) != LUA_TSTRING)
8127 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings"));
8128 cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1));
8129 if (!cli_kws->kw[0].str_kw[index])
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008130 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008131 index++;
8132 lua_pop(L, 1);
8133 }
8134
8135 /* Copy help message. */
8136 cli_kws->kw[0].usage = strdup(message);
8137 if (!cli_kws->kw[0].usage)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008138 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008139
8140 /* Fill fcn io handler. */
8141 len = strlen("<lua.cli>") + 1;
8142 for (i = 0; i < index; i++)
8143 len += strlen(cli_kws->kw[0].str_kw[i]) + 1;
8144 fcn->name = calloc(1, len);
8145 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008146 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008147 strncat((char *)fcn->name, "<lua.cli", len);
8148 for (i = 0; i < index; i++) {
8149 strncat((char *)fcn->name, ".", len);
8150 strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len);
8151 }
8152 strncat((char *)fcn->name, ">", len);
8153 fcn->function_ref = ref_io;
8154
8155 /* Fill last entries. */
8156 cli_kws->kw[0].private = fcn;
8157 cli_kws->kw[0].parse = hlua_cli_parse_fct;
8158 cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct;
8159 cli_kws->kw[0].io_release = hlua_cli_io_release_fct;
8160
8161 /* Register this new converter */
8162 cli_register_kw(cli_kws);
8163
8164 return 0;
8165}
8166
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008167static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx,
8168 struct proxy *defpx, const char *file, int line,
8169 char **err, unsigned int *timeout)
8170{
8171 const char *error;
8172
8173 error = parse_time_err(args[1], timeout, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02008174 if (error == PARSE_TIME_OVER) {
8175 memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)",
8176 args[1], args[0]);
8177 return -1;
8178 }
8179 else if (error == PARSE_TIME_UNDER) {
8180 memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)",
8181 args[1], args[0]);
8182 return -1;
8183 }
8184 else if (error) {
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008185 memprintf(err, "%s: invalid timeout", args[0]);
8186 return -1;
8187 }
8188 return 0;
8189}
8190
8191static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx,
8192 struct proxy *defpx, const char *file, int line,
8193 char **err)
8194{
8195 return hlua_read_timeout(args, section_type, curpx, defpx,
8196 file, line, err, &hlua_timeout_session);
8197}
8198
8199static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx,
8200 struct proxy *defpx, const char *file, int line,
8201 char **err)
8202{
8203 return hlua_read_timeout(args, section_type, curpx, defpx,
8204 file, line, err, &hlua_timeout_task);
8205}
8206
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008207static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx,
8208 struct proxy *defpx, const char *file, int line,
8209 char **err)
8210{
8211 return hlua_read_timeout(args, section_type, curpx, defpx,
8212 file, line, err, &hlua_timeout_applet);
8213}
8214
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01008215static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx,
8216 struct proxy *defpx, const char *file, int line,
8217 char **err)
8218{
8219 char *error;
8220
8221 hlua_nb_instruction = strtoll(args[1], &error, 10);
8222 if (*error != '\0') {
8223 memprintf(err, "%s: invalid number", args[0]);
8224 return -1;
8225 }
8226 return 0;
8227}
8228
Willy Tarreau32f61e22015-03-18 17:54:59 +01008229static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx,
8230 struct proxy *defpx, const char *file, int line,
8231 char **err)
8232{
8233 char *error;
8234
8235 if (*(args[1]) == 0) {
8236 memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]);
8237 return -1;
8238 }
8239 hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L;
8240 if (*error != '\0') {
8241 memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error);
8242 return -1;
8243 }
8244 return 0;
8245}
8246
8247
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008248/* This function is called by the main configuration key "lua-load". It loads and
8249 * execute an lua file during the parsing of the HAProxy configuration file. It is
8250 * the main lua entry point.
8251 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08008252 * This function runs with the HAProxy keywords API. It returns -1 if an error
8253 * occurs, otherwise it returns 0.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008254 *
8255 * In some error case, LUA set an error message in top of the stack. This function
8256 * returns this error message in the HAProxy logs and pop it from the stack.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008257 *
8258 * This function can fail with an abort() due to an Lua critical error.
8259 * We are in the configuration parsing process of HAProxy, this abort() is
8260 * tolerated.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008261 */
8262static int hlua_load(char **args, int section_type, struct proxy *curpx,
8263 struct proxy *defpx, const char *file, int line,
8264 char **err)
8265{
8266 int error;
8267
8268 /* Just load and compile the file. */
8269 error = luaL_loadfile(gL.T, args[1]);
8270 if (error) {
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008271 memprintf(err, "error in Lua file '%s': %s", args[1], lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008272 lua_pop(gL.T, 1);
8273 return -1;
8274 }
8275
8276 /* If no syntax error where detected, execute the code. */
8277 error = lua_pcall(gL.T, 0, LUA_MULTRET, 0);
8278 switch (error) {
8279 case LUA_OK:
8280 break;
8281 case LUA_ERRRUN:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008282 memprintf(err, "Lua runtime error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008283 lua_pop(gL.T, 1);
8284 return -1;
8285 case LUA_ERRMEM:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008286 memprintf(err, "Lua out of memory error.n");
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008287 return -1;
8288 case LUA_ERRERR:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008289 memprintf(err, "Lua message handler error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008290 lua_pop(gL.T, 1);
8291 return -1;
Christopher Faulet20699902020-07-28 10:33:25 +02008292#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008293 case LUA_ERRGCMM:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008294 memprintf(err, "Lua garbage collector error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008295 lua_pop(gL.T, 1);
8296 return -1;
Christopher Faulet20699902020-07-28 10:33:25 +02008297#endif
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008298 default:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008299 memprintf(err, "Lua unknonwn error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008300 lua_pop(gL.T, 1);
8301 return -1;
8302 }
8303
8304 return 0;
8305}
8306
8307/* configuration keywords declaration */
8308static struct cfg_kw_list cfg_kws = {{ },{
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008309 { CFG_GLOBAL, "lua-load", hlua_load },
8310 { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout },
8311 { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout },
Thierry FOURNIER56da1012015-10-01 08:42:31 +02008312 { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout },
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01008313 { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield },
Willy Tarreau32f61e22015-03-18 17:54:59 +01008314 { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem },
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008315 { 0, NULL, NULL },
8316}};
8317
Willy Tarreau0108d902018-11-25 19:14:37 +01008318INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
8319
Christopher Fauletafd8f102018-11-08 11:34:21 +01008320
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008321/* This function can fail with an abort() due to an Lua critical error.
8322 * We are in the initialisation process of HAProxy, this abort() is
8323 * tolerated.
8324 */
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008325int hlua_post_init()
8326{
8327 struct hlua_init_function *init;
8328 const char *msg;
8329 enum hlua_exec ret;
Thierry Fournierfd107a22016-02-19 19:57:23 +01008330 const char *error;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008331
Thierry Fournier3d4a6752016-02-19 20:53:30 +01008332 /* Call post initialisation function in safe environement. */
8333 if (!SET_SAFE_LJMP(gL.T)) {
8334 if (lua_type(gL.T, -1) == LUA_TSTRING)
8335 error = lua_tostring(gL.T, -1);
8336 else
8337 error = "critical error";
8338 fprintf(stderr, "Lua post-init: %s.\n", error);
8339 exit(1);
8340 }
Frédéric Lécaille54f2bcf2018-08-29 13:46:24 +02008341
8342#if USE_OPENSSL
8343 /* Initialize SSL server. */
8344 if (socket_ssl.xprt->prepare_srv) {
8345 int saved_used_backed = global.ssl_used_backend;
8346 // don't affect maxconn automatic computation
8347 socket_ssl.xprt->prepare_srv(&socket_ssl);
8348 global.ssl_used_backend = saved_used_backed;
8349 }
8350#endif
8351
Thierry Fournier3d4a6752016-02-19 20:53:30 +01008352 hlua_fcn_post_init(gL.T);
8353 RESET_SAFE_LJMP(gL.T);
8354
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008355 list_for_each_entry(init, &hlua_init_functions, l) {
8356 lua_rawgeti(gL.T, LUA_REGISTRYINDEX, init->function_ref);
8357 ret = hlua_ctx_resume(&gL, 0);
8358 switch (ret) {
8359 case HLUA_E_OK:
8360 lua_pop(gL.T, -1);
8361 return 1;
8362 case HLUA_E_AGAIN:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008363 ha_alert("Lua init: yield not allowed.\n");
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008364 return 0;
8365 case HLUA_E_ERRMSG:
8366 msg = lua_tostring(gL.T, -1);
Christopher Faulet767a84b2017-11-24 16:50:31 +01008367 ha_alert("lua init: %s.\n", msg);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008368 return 0;
8369 case HLUA_E_ERR:
8370 default:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008371 ha_alert("Lua init: unknown runtime error.\n");
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008372 return 0;
8373 }
8374 }
8375 return 1;
8376}
8377
Willy Tarreau32f61e22015-03-18 17:54:59 +01008378/* The memory allocator used by the Lua stack. <ud> is a pointer to the
8379 * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize>
8380 * is the previously allocated size or the kind of object in case of a new
8381 * allocation. <nsize> is the requested new size.
8382 */
8383static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
8384{
8385 struct hlua_mem_allocator *zone = ud;
8386
8387 if (nsize == 0) {
8388 /* it's a free */
8389 if (ptr)
8390 zone->allocated -= osize;
8391 free(ptr);
8392 return NULL;
8393 }
8394
8395 if (!ptr) {
8396 /* it's a new allocation */
8397 if (zone->limit && zone->allocated + nsize > zone->limit)
8398 return NULL;
8399
8400 ptr = malloc(nsize);
8401 if (ptr)
8402 zone->allocated += nsize;
8403 return ptr;
8404 }
8405
8406 /* it's a realloc */
8407 if (zone->limit && zone->allocated + nsize - osize > zone->limit)
8408 return NULL;
8409
8410 ptr = realloc(ptr, nsize);
8411 if (ptr)
8412 zone->allocated += nsize - osize;
8413 return ptr;
8414}
8415
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008416/* Ithis function can fail with an abort() due to an Lua critical error.
8417 * We are in the initialisation process of HAProxy, this abort() is
8418 * tolerated.
8419 */
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01008420void hlua_init(void)
8421{
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008422 int i;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008423 int idx;
8424 struct sample_fetch *sf;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008425 struct sample_conv *sc;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008426 char *p;
Thierry Fournierfd107a22016-02-19 19:57:23 +01008427 const char *error_msg;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008428#ifdef USE_OPENSSL
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008429 struct srv_kw *kw;
8430 int tmp_error;
8431 char *error;
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008432 char *args[] = { /* SSL client configuration. */
8433 "ssl",
8434 "verify",
8435 "none",
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008436 NULL
8437 };
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008438#endif
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008439
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008440 /* Init main lua stack. */
8441 gL.Mref = LUA_REFNIL;
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01008442 gL.flags = 0;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01008443 LIST_INIT(&gL.com);
Willy Tarreau42ef75f2017-04-12 21:40:29 +02008444 gL.T = lua_newstate(hlua_alloc, &hlua_global_allocator);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008445 hlua_sethlua(&gL);
8446 gL.Tref = LUA_REFNIL;
8447 gL.task = NULL;
8448
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08008449 /* From this point, until the end of the initialisation function,
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008450 * the Lua function can fail with an abort. We are in the initialisation
8451 * process of HAProxy, this abort() is tolerated.
8452 */
8453
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008454 /* Initialise lua. */
8455 luaL_openlibs(gL.T);
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008456
Thierry Fournier75933d42016-01-21 09:30:18 +01008457 /* Set safe environment for the initialisation. */
8458 if (!SET_SAFE_LJMP(gL.T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01008459 if (lua_type(gL.T, -1) == LUA_TSTRING)
8460 error_msg = lua_tostring(gL.T, -1);
8461 else
8462 error_msg = "critical error";
8463 fprintf(stderr, "Lua init: %s.\n", error_msg);
Thierry Fournier75933d42016-01-21 09:30:18 +01008464 exit(1);
8465 }
8466
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008467 /*
8468 *
8469 * Create "core" object.
8470 *
8471 */
8472
Thierry FOURNIERa2d8c652015-03-11 17:29:39 +01008473 /* This table entry is the object "core" base. */
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008474 lua_newtable(gL.T);
8475
8476 /* Push the loglevel constants. */
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008477 for (i = 0; i < NB_LOG_LEVELS; i++)
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008478 hlua_class_const_int(gL.T, log_levels[i], i);
8479
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008480 /* Register special functions. */
8481 hlua_class_function(gL.T, "register_init", hlua_register_init);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01008482 hlua_class_function(gL.T, "register_task", hlua_register_task);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01008483 hlua_class_function(gL.T, "register_fetches", hlua_register_fetches);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01008484 hlua_class_function(gL.T, "register_converters", hlua_register_converters);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02008485 hlua_class_function(gL.T, "register_action", hlua_register_action);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008486 hlua_class_function(gL.T, "register_service", hlua_register_service);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008487 hlua_class_function(gL.T, "register_cli", hlua_register_cli);
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01008488 hlua_class_function(gL.T, "yield", hlua_yield);
Willy Tarreau59551662015-03-10 14:23:13 +01008489 hlua_class_function(gL.T, "set_nice", hlua_set_nice);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01008490 hlua_class_function(gL.T, "sleep", hlua_sleep);
8491 hlua_class_function(gL.T, "msleep", hlua_msleep);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01008492 hlua_class_function(gL.T, "add_acl", hlua_add_acl);
8493 hlua_class_function(gL.T, "del_acl", hlua_del_acl);
8494 hlua_class_function(gL.T, "set_map", hlua_set_map);
8495 hlua_class_function(gL.T, "del_map", hlua_del_map);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008496 hlua_class_function(gL.T, "tcp", hlua_socket_new);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01008497 hlua_class_function(gL.T, "log", hlua_log);
8498 hlua_class_function(gL.T, "Debug", hlua_log_debug);
8499 hlua_class_function(gL.T, "Info", hlua_log_info);
8500 hlua_class_function(gL.T, "Warning", hlua_log_warning);
8501 hlua_class_function(gL.T, "Alert", hlua_log_alert);
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02008502 hlua_class_function(gL.T, "done", hlua_done);
Thierry Fournierfb0b5462016-01-21 09:28:58 +01008503 hlua_fcn_reg_core_fcn(gL.T);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008504
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008505 lua_setglobal(gL.T, "core");
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008506
8507 /*
8508 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008509 * Register class Map
8510 *
8511 */
8512
8513 /* This table entry is the object "Map" base. */
8514 lua_newtable(gL.T);
8515
8516 /* register pattern types. */
8517 for (i=0; i<PAT_MATCH_NUM; i++)
8518 hlua_class_const_int(gL.T, pat_match_names[i], i);
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01008519 for (i=0; i<PAT_MATCH_NUM; i++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008520 snprintf(trash.area, trash.size, "_%s", pat_match_names[i]);
8521 hlua_class_const_int(gL.T, trash.area, i);
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01008522 }
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008523
8524 /* register constructor. */
8525 hlua_class_function(gL.T, "new", hlua_map_new);
8526
8527 /* Create and fill the metatable. */
8528 lua_newtable(gL.T);
8529
8530 /* Create and fille the __index entry. */
8531 lua_pushstring(gL.T, "__index");
8532 lua_newtable(gL.T);
8533
8534 /* Register . */
8535 hlua_class_function(gL.T, "lookup", hlua_map_lookup);
8536 hlua_class_function(gL.T, "slookup", hlua_map_slookup);
8537
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008538 lua_rawset(gL.T, -3);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008539
Thierry Fournier45e78d72016-02-19 18:34:46 +01008540 /* Register previous table in the registry with reference and named entry.
8541 * The function hlua_register_metatable() pops the stack, so we
8542 * previously create a copy of the table.
8543 */
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008544 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008545 class_map_ref = hlua_register_metatable(gL.T, CLASS_MAP);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008546
8547 /* Assign the metatable to the mai Map object. */
8548 lua_setmetatable(gL.T, -2);
8549
8550 /* Set a name to the table. */
8551 lua_setglobal(gL.T, "Map");
8552
8553 /*
8554 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008555 * Register class Channel
8556 *
8557 */
8558
8559 /* Create and fill the metatable. */
8560 lua_newtable(gL.T);
8561
8562 /* Create and fille the __index entry. */
8563 lua_pushstring(gL.T, "__index");
8564 lua_newtable(gL.T);
8565
8566 /* Register . */
8567 hlua_class_function(gL.T, "get", hlua_channel_get);
8568 hlua_class_function(gL.T, "dup", hlua_channel_dup);
8569 hlua_class_function(gL.T, "getline", hlua_channel_getline);
8570 hlua_class_function(gL.T, "set", hlua_channel_set);
8571 hlua_class_function(gL.T, "append", hlua_channel_append);
8572 hlua_class_function(gL.T, "send", hlua_channel_send);
8573 hlua_class_function(gL.T, "forward", hlua_channel_forward);
8574 hlua_class_function(gL.T, "get_in_len", hlua_channel_get_in_len);
8575 hlua_class_function(gL.T, "get_out_len", hlua_channel_get_out_len);
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01008576 hlua_class_function(gL.T, "is_full", hlua_channel_is_full);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008577
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008578 lua_rawset(gL.T, -3);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008579
8580 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008581 class_channel_ref = hlua_register_metatable(gL.T, CLASS_CHANNEL);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008582
8583 /*
8584 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008585 * Register class Fetches
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008586 *
8587 */
8588
8589 /* Create and fill the metatable. */
8590 lua_newtable(gL.T);
8591
8592 /* Create and fille the __index entry. */
8593 lua_pushstring(gL.T, "__index");
8594 lua_newtable(gL.T);
8595
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008596 /* Browse existing fetches and create the associated
8597 * object method.
8598 */
8599 sf = NULL;
8600 while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) {
8601
8602 /* Dont register the keywork if the arguments check function are
8603 * not safe during the runtime.
8604 */
8605 if ((sf->val_args != NULL) &&
8606 (sf->val_args != val_payload_lv) &&
8607 (sf->val_args != val_hdr))
8608 continue;
8609
8610 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
8611 * by an underscore.
8612 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008613 strncpy(trash.area, sf->kw, trash.size);
8614 trash.area[trash.size - 1] = '\0';
8615 for (p = trash.area; *p; p++)
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008616 if (*p == '.' || *p == '-' || *p == '+')
8617 *p = '_';
8618
8619 /* Register the function. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008620 lua_pushstring(gL.T, trash.area);
Willy Tarreau2ec22742015-03-10 14:27:20 +01008621 lua_pushlightuserdata(gL.T, sf);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008622 lua_pushcclosure(gL.T, hlua_run_sample_fetch, 1);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008623 lua_rawset(gL.T, -3);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008624 }
8625
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008626 lua_rawset(gL.T, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008627
8628 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008629 class_fetches_ref = hlua_register_metatable(gL.T, CLASS_FETCHES);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008630
8631 /*
8632 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008633 * Register class Converters
8634 *
8635 */
8636
8637 /* Create and fill the metatable. */
8638 lua_newtable(gL.T);
8639
8640 /* Create and fill the __index entry. */
8641 lua_pushstring(gL.T, "__index");
8642 lua_newtable(gL.T);
8643
8644 /* Browse existing converters and create the associated
8645 * object method.
8646 */
8647 sc = NULL;
8648 while ((sc = sample_conv_getnext(sc, &idx)) != NULL) {
8649 /* Dont register the keywork if the arguments check function are
8650 * not safe during the runtime.
8651 */
8652 if (sc->val_args != NULL)
8653 continue;
8654
8655 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
8656 * by an underscore.
8657 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008658 strncpy(trash.area, sc->kw, trash.size);
8659 trash.area[trash.size - 1] = '\0';
8660 for (p = trash.area; *p; p++)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008661 if (*p == '.' || *p == '-' || *p == '+')
8662 *p = '_';
8663
8664 /* Register the function. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008665 lua_pushstring(gL.T, trash.area);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008666 lua_pushlightuserdata(gL.T, sc);
8667 lua_pushcclosure(gL.T, hlua_run_sample_conv, 1);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008668 lua_rawset(gL.T, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008669 }
8670
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008671 lua_rawset(gL.T, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008672
8673 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008674 class_converters_ref = hlua_register_metatable(gL.T, CLASS_CONVERTERS);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008675
8676 /*
8677 *
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008678 * Register class HTTP
8679 *
8680 */
8681
8682 /* Create and fill the metatable. */
8683 lua_newtable(gL.T);
8684
8685 /* Create and fille the __index entry. */
8686 lua_pushstring(gL.T, "__index");
8687 lua_newtable(gL.T);
8688
8689 /* Register Lua functions. */
8690 hlua_class_function(gL.T, "req_get_headers",hlua_http_req_get_headers);
8691 hlua_class_function(gL.T, "req_del_header", hlua_http_req_del_hdr);
8692 hlua_class_function(gL.T, "req_rep_header", hlua_http_req_rep_hdr);
8693 hlua_class_function(gL.T, "req_rep_value", hlua_http_req_rep_val);
8694 hlua_class_function(gL.T, "req_add_header", hlua_http_req_add_hdr);
8695 hlua_class_function(gL.T, "req_set_header", hlua_http_req_set_hdr);
8696 hlua_class_function(gL.T, "req_set_method", hlua_http_req_set_meth);
8697 hlua_class_function(gL.T, "req_set_path", hlua_http_req_set_path);
8698 hlua_class_function(gL.T, "req_set_query", hlua_http_req_set_query);
8699 hlua_class_function(gL.T, "req_set_uri", hlua_http_req_set_uri);
8700
8701 hlua_class_function(gL.T, "res_get_headers",hlua_http_res_get_headers);
8702 hlua_class_function(gL.T, "res_del_header", hlua_http_res_del_hdr);
8703 hlua_class_function(gL.T, "res_rep_header", hlua_http_res_rep_hdr);
8704 hlua_class_function(gL.T, "res_rep_value", hlua_http_res_rep_val);
8705 hlua_class_function(gL.T, "res_add_header", hlua_http_res_add_hdr);
8706 hlua_class_function(gL.T, "res_set_header", hlua_http_res_set_hdr);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02008707 hlua_class_function(gL.T, "res_set_status", hlua_http_res_set_status);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008708
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008709 lua_rawset(gL.T, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008710
8711 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008712 class_http_ref = hlua_register_metatable(gL.T, CLASS_HTTP);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008713
8714 /*
8715 *
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008716 * Register class AppletTCP
8717 *
8718 */
8719
8720 /* Create and fill the metatable. */
8721 lua_newtable(gL.T);
8722
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008723 /* Create and fille the __index entry. */
8724 lua_pushstring(gL.T, "__index");
8725 lua_newtable(gL.T);
8726
8727 /* Register Lua functions. */
Thierry FOURNIER / OZON.IO3e1d7912016-12-12 12:29:34 +01008728 hlua_class_function(gL.T, "getline", hlua_applet_tcp_getline);
8729 hlua_class_function(gL.T, "receive", hlua_applet_tcp_recv);
8730 hlua_class_function(gL.T, "send", hlua_applet_tcp_send);
8731 hlua_class_function(gL.T, "set_priv", hlua_applet_tcp_set_priv);
8732 hlua_class_function(gL.T, "get_priv", hlua_applet_tcp_get_priv);
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01008733 hlua_class_function(gL.T, "set_var", hlua_applet_tcp_set_var);
8734 hlua_class_function(gL.T, "unset_var", hlua_applet_tcp_unset_var);
8735 hlua_class_function(gL.T, "get_var", hlua_applet_tcp_get_var);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008736
8737 lua_settable(gL.T, -3);
8738
8739 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008740 class_applet_tcp_ref = hlua_register_metatable(gL.T, CLASS_APPLET_TCP);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008741
8742 /*
8743 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02008744 * Register class AppletHTTP
8745 *
8746 */
8747
8748 /* Create and fill the metatable. */
8749 lua_newtable(gL.T);
8750
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02008751 /* Create and fille the __index entry. */
8752 lua_pushstring(gL.T, "__index");
8753 lua_newtable(gL.T);
8754
8755 /* Register Lua functions. */
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01008756 hlua_class_function(gL.T, "set_priv", hlua_applet_http_set_priv);
8757 hlua_class_function(gL.T, "get_priv", hlua_applet_http_get_priv);
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01008758 hlua_class_function(gL.T, "set_var", hlua_applet_http_set_var);
8759 hlua_class_function(gL.T, "unset_var", hlua_applet_http_unset_var);
8760 hlua_class_function(gL.T, "get_var", hlua_applet_http_get_var);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02008761 hlua_class_function(gL.T, "getline", hlua_applet_http_getline);
8762 hlua_class_function(gL.T, "receive", hlua_applet_http_recv);
8763 hlua_class_function(gL.T, "send", hlua_applet_http_send);
8764 hlua_class_function(gL.T, "add_header", hlua_applet_http_addheader);
8765 hlua_class_function(gL.T, "set_status", hlua_applet_http_status);
8766 hlua_class_function(gL.T, "start_response", hlua_applet_http_start_response);
8767
8768 lua_settable(gL.T, -3);
8769
8770 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008771 class_applet_http_ref = hlua_register_metatable(gL.T, CLASS_APPLET_HTTP);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02008772
8773 /*
8774 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008775 * Register class TXN
8776 *
8777 */
8778
8779 /* Create and fill the metatable. */
8780 lua_newtable(gL.T);
8781
8782 /* Create and fille the __index entry. */
8783 lua_pushstring(gL.T, "__index");
8784 lua_newtable(gL.T);
8785
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01008786 /* Register Lua functions. */
Patrick Hemmer268a7072018-05-11 12:52:31 -04008787 hlua_class_function(gL.T, "set_priv", hlua_set_priv);
8788 hlua_class_function(gL.T, "get_priv", hlua_get_priv);
8789 hlua_class_function(gL.T, "set_var", hlua_set_var);
8790 hlua_class_function(gL.T, "unset_var", hlua_unset_var);
8791 hlua_class_function(gL.T, "get_var", hlua_get_var);
8792 hlua_class_function(gL.T, "done", hlua_txn_done);
8793 hlua_class_function(gL.T, "set_loglevel", hlua_txn_set_loglevel);
8794 hlua_class_function(gL.T, "set_tos", hlua_txn_set_tos);
8795 hlua_class_function(gL.T, "set_mark", hlua_txn_set_mark);
8796 hlua_class_function(gL.T, "set_priority_class", hlua_txn_set_priority_class);
8797 hlua_class_function(gL.T, "set_priority_offset", hlua_txn_set_priority_offset);
8798 hlua_class_function(gL.T, "deflog", hlua_txn_deflog);
8799 hlua_class_function(gL.T, "log", hlua_txn_log);
8800 hlua_class_function(gL.T, "Debug", hlua_txn_log_debug);
8801 hlua_class_function(gL.T, "Info", hlua_txn_log_info);
8802 hlua_class_function(gL.T, "Warning", hlua_txn_log_warning);
8803 hlua_class_function(gL.T, "Alert", hlua_txn_log_alert);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01008804
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008805 lua_rawset(gL.T, -3);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008806
8807 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008808 class_txn_ref = hlua_register_metatable(gL.T, CLASS_TXN);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008809
8810 /*
8811 *
8812 * Register class Socket
8813 *
8814 */
8815
8816 /* Create and fill the metatable. */
8817 lua_newtable(gL.T);
8818
8819 /* Create and fille the __index entry. */
8820 lua_pushstring(gL.T, "__index");
8821 lua_newtable(gL.T);
8822
Baptiste Assmann84bb4932015-03-02 21:40:06 +01008823#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008824 hlua_class_function(gL.T, "connect_ssl", hlua_socket_connect_ssl);
Baptiste Assmann84bb4932015-03-02 21:40:06 +01008825#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008826 hlua_class_function(gL.T, "connect", hlua_socket_connect);
8827 hlua_class_function(gL.T, "send", hlua_socket_send);
8828 hlua_class_function(gL.T, "receive", hlua_socket_receive);
8829 hlua_class_function(gL.T, "close", hlua_socket_close);
8830 hlua_class_function(gL.T, "getpeername", hlua_socket_getpeername);
8831 hlua_class_function(gL.T, "getsockname", hlua_socket_getsockname);
8832 hlua_class_function(gL.T, "setoption", hlua_socket_setoption);
8833 hlua_class_function(gL.T, "settimeout", hlua_socket_settimeout);
8834
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008835 lua_rawset(gL.T, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008836
8837 /* Register the garbage collector entry. */
8838 lua_pushstring(gL.T, "__gc");
8839 lua_pushcclosure(gL.T, hlua_socket_gc, 0);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008840 lua_rawset(gL.T, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008841
8842 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008843 class_socket_ref = hlua_register_metatable(gL.T, CLASS_SOCKET);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008844
8845 /* Proxy and server configuration initialisation. */
8846 memset(&socket_proxy, 0, sizeof(socket_proxy));
8847 init_new_proxy(&socket_proxy);
8848 socket_proxy.parent = NULL;
8849 socket_proxy.last_change = now.tv_sec;
8850 socket_proxy.id = "LUA-SOCKET";
8851 socket_proxy.cap = PR_CAP_FE | PR_CAP_BE;
8852 socket_proxy.maxconn = 0;
8853 socket_proxy.accept = NULL;
8854 socket_proxy.options2 |= PR_O2_INDEPSTR;
8855 socket_proxy.srv = NULL;
8856 socket_proxy.conn_retries = 0;
8857 socket_proxy.timeout.connect = 5000; /* By default the timeout connection is 5s. */
8858
8859 /* Init TCP server: unchanged parameters */
8860 memset(&socket_tcp, 0, sizeof(socket_tcp));
8861 socket_tcp.next = NULL;
8862 socket_tcp.proxy = &socket_proxy;
8863 socket_tcp.obj_type = OBJ_TYPE_SERVER;
8864 LIST_INIT(&socket_tcp.actconns);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04008865 socket_tcp.pendconns = EB_ROOT;
Christopher Faulet40a007c2017-07-03 15:41:01 +02008866 socket_tcp.priv_conns = NULL;
8867 socket_tcp.idle_conns = NULL;
8868 socket_tcp.safe_conns = NULL;
Emeric Brun52a91d32017-08-31 14:41:55 +02008869 socket_tcp.next_state = SRV_ST_RUNNING; /* early server setup */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008870 socket_tcp.last_change = 0;
8871 socket_tcp.id = "LUA-TCP-CONN";
8872 socket_tcp.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8873 socket_tcp.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8874 socket_tcp.pp_opts = 0; /* Remove proxy protocol. */
8875
8876 /* XXX: Copy default parameter from default server,
8877 * but the default server is not initialized.
8878 */
8879 socket_tcp.maxqueue = socket_proxy.defsrv.maxqueue;
8880 socket_tcp.minconn = socket_proxy.defsrv.minconn;
8881 socket_tcp.maxconn = socket_proxy.defsrv.maxconn;
8882 socket_tcp.slowstart = socket_proxy.defsrv.slowstart;
8883 socket_tcp.onerror = socket_proxy.defsrv.onerror;
8884 socket_tcp.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
8885 socket_tcp.onmarkedup = socket_proxy.defsrv.onmarkedup;
8886 socket_tcp.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
8887 socket_tcp.uweight = socket_proxy.defsrv.iweight;
8888 socket_tcp.iweight = socket_proxy.defsrv.iweight;
8889
8890 socket_tcp.check.status = HCHK_STATUS_INI;
8891 socket_tcp.check.rise = socket_proxy.defsrv.check.rise;
8892 socket_tcp.check.fall = socket_proxy.defsrv.check.fall;
8893 socket_tcp.check.health = socket_tcp.check.rise; /* socket, but will fall down at first failure */
8894 socket_tcp.check.server = &socket_tcp;
8895
8896 socket_tcp.agent.status = HCHK_STATUS_INI;
8897 socket_tcp.agent.rise = socket_proxy.defsrv.agent.rise;
8898 socket_tcp.agent.fall = socket_proxy.defsrv.agent.fall;
8899 socket_tcp.agent.health = socket_tcp.agent.rise; /* socket, but will fall down at first failure */
8900 socket_tcp.agent.server = &socket_tcp;
8901
Willy Tarreaua261e9b2016-12-22 20:44:00 +01008902 socket_tcp.xprt = xprt_get(XPRT_RAW);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008903
8904#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008905 /* Init TCP server: unchanged parameters */
8906 memset(&socket_ssl, 0, sizeof(socket_ssl));
8907 socket_ssl.next = NULL;
8908 socket_ssl.proxy = &socket_proxy;
8909 socket_ssl.obj_type = OBJ_TYPE_SERVER;
8910 LIST_INIT(&socket_ssl.actconns);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04008911 socket_ssl.pendconns = EB_ROOT;
Willy Tarreaub784b352019-02-07 14:48:24 +01008912 socket_ssl.priv_conns = NULL;
8913 socket_ssl.idle_conns = NULL;
8914 socket_ssl.safe_conns = NULL;
Emeric Brun52a91d32017-08-31 14:41:55 +02008915 socket_ssl.next_state = SRV_ST_RUNNING; /* early server setup */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008916 socket_ssl.last_change = 0;
8917 socket_ssl.id = "LUA-SSL-CONN";
8918 socket_ssl.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8919 socket_ssl.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8920 socket_ssl.pp_opts = 0; /* Remove proxy protocol. */
8921
8922 /* XXX: Copy default parameter from default server,
8923 * but the default server is not initialized.
8924 */
8925 socket_ssl.maxqueue = socket_proxy.defsrv.maxqueue;
8926 socket_ssl.minconn = socket_proxy.defsrv.minconn;
8927 socket_ssl.maxconn = socket_proxy.defsrv.maxconn;
8928 socket_ssl.slowstart = socket_proxy.defsrv.slowstart;
8929 socket_ssl.onerror = socket_proxy.defsrv.onerror;
8930 socket_ssl.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
8931 socket_ssl.onmarkedup = socket_proxy.defsrv.onmarkedup;
8932 socket_ssl.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
8933 socket_ssl.uweight = socket_proxy.defsrv.iweight;
8934 socket_ssl.iweight = socket_proxy.defsrv.iweight;
8935
8936 socket_ssl.check.status = HCHK_STATUS_INI;
8937 socket_ssl.check.rise = socket_proxy.defsrv.check.rise;
8938 socket_ssl.check.fall = socket_proxy.defsrv.check.fall;
8939 socket_ssl.check.health = socket_ssl.check.rise; /* socket, but will fall down at first failure */
8940 socket_ssl.check.server = &socket_ssl;
8941
8942 socket_ssl.agent.status = HCHK_STATUS_INI;
8943 socket_ssl.agent.rise = socket_proxy.defsrv.agent.rise;
8944 socket_ssl.agent.fall = socket_proxy.defsrv.agent.fall;
8945 socket_ssl.agent.health = socket_ssl.agent.rise; /* socket, but will fall down at first failure */
8946 socket_ssl.agent.server = &socket_ssl;
8947
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008948 socket_ssl.use_ssl = 1;
Willy Tarreaua261e9b2016-12-22 20:44:00 +01008949 socket_ssl.xprt = xprt_get(XPRT_SSL);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008950
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008951 for (idx = 0; args[idx] != NULL; idx++) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008952 if ((kw = srv_find_kw(args[idx])) != NULL) { /* Maybe it's registered server keyword */
8953 /*
8954 *
8955 * If the keyword is not known, we can search in the registered
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08008956 * server keywords. This is useful to configure special SSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008957 * features like client certificates and ssl_verify.
8958 *
8959 */
8960 tmp_error = kw->parse(args, &idx, &socket_proxy, &socket_ssl, &error);
8961 if (tmp_error != 0) {
8962 fprintf(stderr, "INTERNAL ERROR: %s\n", error);
8963 abort(); /* This must be never arrives because the command line
8964 not editable by the user. */
8965 }
8966 idx += kw->skip;
8967 }
8968 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008969#endif
Thierry Fournier75933d42016-01-21 09:30:18 +01008970
8971 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01008972}
Willy Tarreaubb57d942016-12-21 19:04:56 +01008973
Willy Tarreau80713382018-11-26 10:19:54 +01008974static void hlua_register_build_options(void)
8975{
Willy Tarreaubb57d942016-12-21 19:04:56 +01008976 char *ptr = NULL;
Willy Tarreau80713382018-11-26 10:19:54 +01008977
Willy Tarreaubb57d942016-12-21 19:04:56 +01008978 memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE);
8979 hap_register_build_opts(ptr, 1);
8980}
Willy Tarreau80713382018-11-26 10:19:54 +01008981
8982INITCALL0(STG_REGISTER, hlua_register_build_options);