blob: 0b5eaafb98d4f454fa9139eae0c38d7943927c48 [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:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200757 memcpy(trash.area, argp[idx].data.str.area,
758 argp[idx].data.str.data);
759 trash.area[argp[idx].data.str.data] = 0;
760 if (inet_pton(AF_INET, trash.area, &argp[idx].data.ipv4))
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100761 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 address"));
762 argp[idx].type = ARGT_IPV4;
763 break;
764
765 case ARGT_MSK4:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200766 memcpy(trash.area, argp[idx].data.str.area,
767 argp[idx].data.str.data);
768 trash.area[argp[idx].data.str.data] = 0;
769 if (!str2mask(trash.area, &argp[idx].data.ipv4))
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100770 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 mask"));
771 argp[idx].type = ARGT_MSK4;
772 break;
773
774 case ARGT_IPV6:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200775 memcpy(trash.area, argp[idx].data.str.area,
776 argp[idx].data.str.data);
777 trash.area[argp[idx].data.str.data] = 0;
778 if (inet_pton(AF_INET6, trash.area, &argp[idx].data.ipv6))
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100779 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 address"));
780 argp[idx].type = ARGT_IPV6;
781 break;
782
783 case ARGT_MSK6:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200784 memcpy(trash.area, argp[idx].data.str.area,
785 argp[idx].data.str.data);
786 trash.area[argp[idx].data.str.data] = 0;
787 if (!str2mask6(trash.area, &argp[idx].data.ipv6))
Tim Duesterhusb814da62018-01-25 16:24:50 +0100788 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 mask"));
789 argp[idx].type = ARGT_MSK6;
790 break;
791
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100792 case ARGT_MAP:
793 case ARGT_REG:
794 case ARGT_USR:
795 WILL_LJMP(luaL_argerror(L, first + idx, "type not yet supported"));
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100796 break;
797 }
798
799 /* Check for type of argument. */
800 if ((mask & ARGT_MASK) != argp[idx].type) {
801 const char *msg = lua_pushfstring(L, "'%s' expected, got '%s'",
802 arg_type_names[(mask & ARGT_MASK)],
803 arg_type_names[argp[idx].type & ARGT_MASK]);
804 WILL_LJMP(luaL_argerror(L, first + idx, msg));
805 }
806
807 /* Next argument. */
808 mask >>= ARGT_BITS;
809 idx++;
810 }
811}
812
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100813/*
814 * The following functions are used to make correspondance between the the
815 * executed lua pointer and the "struct hlua *" that contain the context.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100816 *
817 * - hlua_gethlua : return the hlua context associated with an lua_State.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100818 * - hlua_sethlua : create the association between hlua context and lua_state.
819 */
820static inline struct hlua *hlua_gethlua(lua_State *L)
821{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +0100822 struct hlua **hlua = lua_getextraspace(L);
823 return *hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100824}
825static inline void hlua_sethlua(struct hlua *hlua)
826{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +0100827 struct hlua **hlua_store = lua_getextraspace(hlua->T);
828 *hlua_store = hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100829}
830
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100831/* This function is used to send logs. It try to send on screen (stderr)
832 * and on the default syslog server.
833 */
834static inline void hlua_sendlog(struct proxy *px, int level, const char *msg)
835{
836 struct tm tm;
837 char *p;
838
839 /* Cleanup the log message. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200840 p = trash.area;
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100841 for (; *msg != '\0'; msg++, p++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200842 if (p >= trash.area + trash.size - 1) {
Thierry FOURNIERccf00632015-09-16 12:47:03 +0200843 /* Break the message if exceed the buffer size. */
844 *(p-4) = ' ';
845 *(p-3) = '.';
846 *(p-2) = '.';
847 *(p-1) = '.';
848 break;
849 }
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100850 if (isprint(*msg))
851 *p = *msg;
852 else
853 *p = '.';
854 }
855 *p = '\0';
856
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200857 send_log(px, level, "%s\n", trash.area);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100858 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
Willy Tarreaua678b432015-08-28 10:14:59 +0200859 get_localtime(date.tv_sec, &tm);
860 fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n",
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100861 log_levels[level], tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec,
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200862 (int)getpid(), trash.area);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100863 fflush(stderr);
864 }
865}
866
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100867/* This function just ensure that the yield will be always
868 * returned with a timeout and permit to set some flags
869 */
870__LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx,
Thierry FOURNIERf90838b2015-03-06 13:48:32 +0100871 lua_KFunction k, int timeout, unsigned int flags)
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100872{
873 struct hlua *hlua = hlua_gethlua(L);
874
875 /* Set the wake timeout. If timeout is required, we set
876 * the expiration time.
877 */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +0200878 hlua->wake_time = timeout;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100879
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +0100880 hlua->flags |= flags;
881
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100882 /* Process the yield. */
Willy Tarreau9635e032018-10-16 17:52:55 +0200883 MAY_LJMP(lua_yieldk(L, nresults, ctx, k));
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100884}
885
Willy Tarreau87b09662015-04-03 00:22:06 +0200886/* This function initialises the Lua environment stored in the stream.
887 * It must be called at the start of the stream. This function creates
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100888 * an LUA coroutine. It can not be use to crete the main LUA context.
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200889 *
890 * This function is particular. it initialises a new Lua thread. If the
891 * initialisation fails (example: out of memory error), the lua function
892 * throws an error (longjmp).
893 *
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100894 * In some case (at least one), this function can be called from safe
895 * environement, so we must not initialise it. While the support of
896 * threads appear, the safe environment set a lock to ensure only one
897 * Lua execution at a time. If we initialize safe environment in another
898 * safe environmenet, we have a dead lock.
899 *
900 * set "already_safe" true if the context is initialized form safe
901 * Lua fonction.
902 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800903 * This function manipulates two Lua stacks: the main and the thread. Only
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200904 * the main stack can fail. The thread is not manipulated. This function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800905 * MUST NOT manipulate the created thread stack state, because it is not
906 * proctected against errors thrown by the thread stack.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100907 */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100908int hlua_ctx_init(struct hlua *lua, struct task *task, int already_safe)
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100909{
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100910 if (!already_safe) {
911 if (!SET_SAFE_LJMP(gL.T)) {
912 lua->Tref = LUA_REFNIL;
913 return 0;
914 }
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200915 }
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100916 lua->Mref = LUA_REFNIL;
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +0100917 lua->flags = 0;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100918 LIST_INIT(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100919 lua->T = lua_newthread(gL.T);
920 if (!lua->T) {
921 lua->Tref = LUA_REFNIL;
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100922 if (!already_safe)
923 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100924 return 0;
925 }
926 hlua_sethlua(lua);
927 lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
928 lua->task = task;
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100929 if (!already_safe)
930 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100931 return 1;
932}
933
Willy Tarreau87b09662015-04-03 00:22:06 +0200934/* Used to destroy the Lua coroutine when the attached stream or task
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100935 * is destroyed. The destroy also the memory context. The struct "lua"
936 * is not freed.
937 */
938void hlua_ctx_destroy(struct hlua *lua)
939{
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100940 if (!lua)
Thierry FOURNIERa718b292015-03-04 16:48:34 +0100941 return;
942
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100943 if (!lua->T)
944 goto end;
945
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100946 /* Purge all the pending signals. */
Thierry FOURNIERd6975962017-07-12 14:31:10 +0200947 notification_purge(&lua->com);
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100948
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200949 if (!SET_SAFE_LJMP(lua->T))
950 return;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100951 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200952 RESET_SAFE_LJMP(lua->T);
Thierry FOURNIER5a50a852015-09-23 16:59:28 +0200953
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200954 if (!SET_SAFE_LJMP(gL.T))
955 return;
956 luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref);
957 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER5a50a852015-09-23 16:59:28 +0200958 /* Forces a garbage collecting process. If the Lua program is finished
959 * without error, we run the GC on the thread pointer. Its freed all
960 * the unused memory.
961 * If the thread is finnish with an error or is currently yielded,
962 * it seems that the GC applied on the thread doesn't clean anything,
963 * so e run the GC on the main thread.
964 * NOTE: maybe this action locks all the Lua threads untiml the en of
965 * the garbage collection.
966 */
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +0200967 if (lua->flags & HLUA_MUST_GC) {
Thierry FOURNIER7bd10d52017-07-17 00:44:40 +0200968 if (!SET_SAFE_LJMP(gL.T))
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200969 return;
Thierry FOURNIER7bd10d52017-07-17 00:44:40 +0200970 lua_gc(gL.T, LUA_GCCOLLECT, 0);
971 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +0200972 }
Thierry FOURNIER5a50a852015-09-23 16:59:28 +0200973
Thierry FOURNIERa7b536b2015-09-21 22:50:24 +0200974 lua->T = NULL;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100975
976end:
Willy Tarreaubafbe012017-11-24 17:34:44 +0100977 pool_free(pool_head_hlua, lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100978}
979
980/* This function is used to restore the Lua context when a coroutine
981 * fails. This function copy the common memory between old coroutine
982 * and the new coroutine. The old coroutine is destroyed, and its
983 * replaced by the new coroutine.
984 * If the flag "keep_msg" is set, the last entry of the old is assumed
985 * as string error message and it is copied in the new stack.
986 */
987static int hlua_ctx_renew(struct hlua *lua, int keep_msg)
988{
989 lua_State *T;
990 int new_ref;
991
992 /* Renew the main LUA stack doesn't have sense. */
993 if (lua == &gL)
994 return 0;
995
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100996 /* New Lua coroutine. */
997 T = lua_newthread(gL.T);
998 if (!T)
999 return 0;
1000
1001 /* Copy last error message. */
1002 if (keep_msg)
1003 lua_xmove(lua->T, T, 1);
1004
1005 /* Copy data between the coroutines. */
1006 lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref);
1007 lua_xmove(lua->T, T, 1);
1008 new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Valur poped. */
1009
1010 /* Destroy old data. */
1011 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
1012
1013 /* The thread is garbage collected by Lua. */
1014 luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref);
1015
1016 /* Fill the struct with the new coroutine values. */
1017 lua->Mref = new_ref;
1018 lua->T = T;
1019 lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
1020
1021 /* Set context. */
1022 hlua_sethlua(lua);
1023
1024 return 1;
1025}
1026
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001027void hlua_hook(lua_State *L, lua_Debug *ar)
1028{
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001029 struct hlua *hlua = hlua_gethlua(L);
1030
1031 /* Lua cannot yield when its returning from a function,
1032 * so, we can fix the interrupt hook to 1 instruction,
1033 * expecting that the function is finnished.
1034 */
1035 if (lua_gethookmask(L) & LUA_MASKRET) {
1036 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1);
1037 return;
1038 }
1039
1040 /* restore the interrupt condition. */
1041 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
1042
1043 /* If we interrupt the Lua processing in yieldable state, we yield.
1044 * If the state is not yieldable, trying yield causes an error.
1045 */
1046 if (lua_isyieldable(L))
Willy Tarreau9635e032018-10-16 17:52:55 +02001047 MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD));
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001048
Thierry FOURNIERa85cfb12015-03-13 14:50:06 +01001049 /* If we cannot yield, update the clock and check the timeout. */
1050 tv_update_date(0, 1);
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001051 hlua->run_time += now_ms - hlua->start_time;
1052 if (hlua->max_time && hlua->run_time >= hlua->max_time) {
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001053 lua_pushfstring(L, "execution timeout");
1054 WILL_LJMP(lua_error(L));
1055 }
1056
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001057 /* Update the start time. */
1058 hlua->start_time = now_ms;
1059
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001060 /* Try to interrupt the process at the end of the current
1061 * unyieldable function.
1062 */
1063 lua_sethook(hlua->T, hlua_hook, LUA_MASKRET|LUA_MASKCOUNT, hlua_nb_instruction);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001064}
1065
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001066/* This function start or resumes the Lua stack execution. If the flag
1067 * "yield_allowed" if no set and the LUA stack execution returns a yield
1068 * The function return an error.
1069 *
1070 * The function can returns 4 values:
1071 * - HLUA_E_OK : The execution is terminated without any errors.
1072 * - HLUA_E_AGAIN : The execution must continue at the next associated
1073 * task wakeup.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001074 * - HLUA_E_ERRMSG : An error has occurred, an error message is set in
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001075 * the top of the stack.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001076 * - HLUA_E_ERR : An error has occurred without error message.
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001077 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001078 * If an error occurred, the stack is renewed and it is ready to run new
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001079 * LUA code.
1080 */
1081static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed)
1082{
1083 int ret;
1084 const char *msg;
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001085 const char *trace;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001086
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001087 /* Initialise run time counter. */
1088 if (!HLUA_IS_RUNNING(lua))
1089 lua->run_time = 0;
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001090
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001091 /* Lock the whole Lua execution. This lock must be before the
1092 * label "resume_execution".
1093 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001094 HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock);
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001095
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001096resume_execution:
1097
1098 /* This hook interrupts the Lua processing each 'hlua_nb_instruction'
1099 * instructions. it is used for preventing infinite loops.
1100 */
1101 lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
1102
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +01001103 /* Remove all flags except the running flags. */
Thierry FOURNIER2f3867f2015-09-28 01:02:01 +02001104 HLUA_SET_RUN(lua);
1105 HLUA_CLR_CTRLYIELD(lua);
1106 HLUA_CLR_WAKERESWR(lua);
1107 HLUA_CLR_WAKEREQWR(lua);
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +01001108
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001109 /* Update the start time. */
1110 lua->start_time = now_ms;
1111
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001112 /* Call the function. */
1113 ret = lua_resume(lua->T, gL.T, lua->nargs);
1114 switch (ret) {
1115
1116 case LUA_OK:
1117 ret = HLUA_E_OK;
1118 break;
1119
1120 case LUA_YIELD:
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001121 /* Check if the execution timeout is expired. It it is the case, we
1122 * break the Lua execution.
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001123 */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001124 tv_update_date(0, 1);
1125 lua->run_time += now_ms - lua->start_time;
1126 if (lua->max_time && lua->run_time > lua->max_time) {
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001127 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001128 ret = HLUA_E_ETMOUT;
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001129 break;
1130 }
1131 /* Process the forced yield. if the general yield is not allowed or
1132 * if no task were associated this the current Lua execution
1133 * coroutine, we resume the execution. Else we want to return in the
1134 * scheduler and we want to be waked up again, to continue the
1135 * current Lua execution. So we schedule our own task.
1136 */
1137 if (HLUA_IS_CTRLYIELDING(lua)) {
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001138 if (!yield_allowed || !lua->task)
1139 goto resume_execution;
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001140 task_wakeup(lua->task, TASK_WOKEN_MSG);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001141 }
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001142 if (!yield_allowed) {
1143 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001144 ret = HLUA_E_YIELD;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001145 break;
1146 }
1147 ret = HLUA_E_AGAIN;
1148 break;
1149
1150 case LUA_ERRRUN:
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001151
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001152 /* Special exit case. The traditional exit is returned as an error
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001153 * because the errors ares the only one mean to return immediately
1154 * from and lua execution.
1155 */
1156 if (lua->flags & HLUA_EXIT) {
1157 ret = HLUA_E_OK;
Thierry FOURNIERe1587b32015-08-28 09:54:13 +02001158 hlua_ctx_renew(lua, 0);
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001159 break;
1160 }
1161
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001162 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001163 if (!lua_checkstack(lua->T, 1)) {
1164 ret = HLUA_E_ERR;
1165 break;
1166 }
1167 msg = lua_tostring(lua->T, -1);
1168 lua_settop(lua->T, 0); /* Empty the stack. */
1169 lua_pop(lua->T, 1);
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001170 trace = hlua_traceback(lua->T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001171 if (msg)
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001172 lua_pushfstring(lua->T, "runtime error: %s from %s", msg, trace);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001173 else
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001174 lua_pushfstring(lua->T, "unknown runtime error from %s", trace);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001175 ret = HLUA_E_ERRMSG;
1176 break;
1177
1178 case LUA_ERRMEM:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001179 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001180 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001181 ret = HLUA_E_NOMEM;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001182 break;
1183
1184 case LUA_ERRERR:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001185 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001186 if (!lua_checkstack(lua->T, 1)) {
1187 ret = HLUA_E_ERR;
1188 break;
1189 }
1190 msg = lua_tostring(lua->T, -1);
1191 lua_settop(lua->T, 0); /* Empty the stack. */
1192 lua_pop(lua->T, 1);
1193 if (msg)
1194 lua_pushfstring(lua->T, "message handler error: %s", msg);
1195 else
1196 lua_pushfstring(lua->T, "message handler error");
1197 ret = HLUA_E_ERRMSG;
1198 break;
1199
1200 default:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001201 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001202 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001203 ret = HLUA_E_ERR;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001204 break;
1205 }
1206
Thierry FOURNIER6ab4d8e2015-09-27 22:17:19 +02001207 /* This GC permits to destroy some object when a Lua timeout strikes. */
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +02001208 if (lua->flags & HLUA_MUST_GC &&
1209 ret != HLUA_E_AGAIN)
Thierry FOURNIER6ab4d8e2015-09-27 22:17:19 +02001210 lua_gc(lua->T, LUA_GCCOLLECT, 0);
1211
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001212 switch (ret) {
1213 case HLUA_E_AGAIN:
1214 break;
1215
1216 case HLUA_E_ERRMSG:
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001217 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001218 hlua_ctx_renew(lua, 1);
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001219 HLUA_CLR_RUN(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001220 break;
1221
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001222 case HLUA_E_ETMOUT:
1223 case HLUA_E_NOMEM:
1224 case HLUA_E_YIELD:
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001225 case HLUA_E_ERR:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001226 HLUA_CLR_RUN(lua);
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001227 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001228 hlua_ctx_renew(lua, 0);
1229 break;
1230
1231 case HLUA_E_OK:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001232 HLUA_CLR_RUN(lua);
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001233 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001234 break;
1235 }
1236
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001237 /* This is the main exit point, remove the Lua lock. */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001238 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock);
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001239
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001240 return ret;
1241}
1242
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001243/* This function exit the current code. */
1244__LJMP static int hlua_done(lua_State *L)
1245{
1246 struct hlua *hlua = hlua_gethlua(L);
1247
1248 hlua->flags |= HLUA_EXIT;
1249 WILL_LJMP(lua_error(L));
1250
1251 return 0;
1252}
1253
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001254/* This function is an LUA binding. It provides a function
1255 * for deleting ACL from a referenced ACL file.
1256 */
1257__LJMP static int hlua_del_acl(lua_State *L)
1258{
1259 const char *name;
1260 const char *key;
1261 struct pat_ref *ref;
1262
1263 MAY_LJMP(check_args(L, 2, "del_acl"));
1264
1265 name = MAY_LJMP(luaL_checkstring(L, 1));
1266 key = MAY_LJMP(luaL_checkstring(L, 2));
1267
1268 ref = pat_ref_lookup(name);
1269 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001270 WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001271
Christopher Faulet9fa1f4e2020-06-03 18:39:16 +02001272 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001273 pat_ref_delete(ref, key);
Christopher Faulet9fa1f4e2020-06-03 18:39:16 +02001274 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001275 return 0;
1276}
1277
1278/* This function is an LUA binding. It provides a function
1279 * for deleting map entry from a referenced map file.
1280 */
1281static int hlua_del_map(lua_State *L)
1282{
1283 const char *name;
1284 const char *key;
1285 struct pat_ref *ref;
1286
1287 MAY_LJMP(check_args(L, 2, "del_map"));
1288
1289 name = MAY_LJMP(luaL_checkstring(L, 1));
1290 key = MAY_LJMP(luaL_checkstring(L, 2));
1291
1292 ref = pat_ref_lookup(name);
1293 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001294 WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001295
Christopher Faulet9fa1f4e2020-06-03 18:39:16 +02001296 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001297 pat_ref_delete(ref, key);
Christopher Faulet9fa1f4e2020-06-03 18:39:16 +02001298 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001299 return 0;
1300}
1301
1302/* This function is an LUA binding. It provides a function
1303 * for adding ACL pattern from a referenced ACL file.
1304 */
1305static int hlua_add_acl(lua_State *L)
1306{
1307 const char *name;
1308 const char *key;
1309 struct pat_ref *ref;
1310
1311 MAY_LJMP(check_args(L, 2, "add_acl"));
1312
1313 name = MAY_LJMP(luaL_checkstring(L, 1));
1314 key = MAY_LJMP(luaL_checkstring(L, 2));
1315
1316 ref = pat_ref_lookup(name);
1317 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001318 WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001319
Christopher Faulet9fa1f4e2020-06-03 18:39:16 +02001320 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001321 if (pat_ref_find_elt(ref, key) == NULL)
1322 pat_ref_add(ref, key, NULL, NULL);
Christopher Faulet9fa1f4e2020-06-03 18:39:16 +02001323 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001324 return 0;
1325}
1326
1327/* This function is an LUA binding. It provides a function
1328 * for setting map pattern and sample from a referenced map
1329 * file.
1330 */
1331static int hlua_set_map(lua_State *L)
1332{
1333 const char *name;
1334 const char *key;
1335 const char *value;
1336 struct pat_ref *ref;
1337
1338 MAY_LJMP(check_args(L, 3, "set_map"));
1339
1340 name = MAY_LJMP(luaL_checkstring(L, 1));
1341 key = MAY_LJMP(luaL_checkstring(L, 2));
1342 value = MAY_LJMP(luaL_checkstring(L, 3));
1343
1344 ref = pat_ref_lookup(name);
1345 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001346 WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001347
Christopher Faulet9fa1f4e2020-06-03 18:39:16 +02001348 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001349 if (pat_ref_find_elt(ref, key) != NULL)
1350 pat_ref_set(ref, key, value, NULL);
1351 else
1352 pat_ref_add(ref, key, value, NULL);
Christopher Faulet9fa1f4e2020-06-03 18:39:16 +02001353 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001354 return 0;
1355}
1356
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001357/* A class is a lot of memory that contain data. This data can be a table,
1358 * an integer or user data. This data is associated with a metatable. This
1359 * metatable have an original version registred in the global context with
1360 * the name of the object (_G[<name>] = <metable> ).
1361 *
1362 * A metable is a table that modify the standard behavior of a standard
1363 * access to the associated data. The entries of this new metatable are
1364 * defined as is:
1365 *
1366 * http://lua-users.org/wiki/MetatableEvents
1367 *
1368 * __index
1369 *
1370 * we access an absent field in a table, the result is nil. This is
1371 * true, but it is not the whole truth. Actually, such access triggers
1372 * the interpreter to look for an __index metamethod: If there is no
1373 * such method, as usually happens, then the access results in nil;
1374 * otherwise, the metamethod will provide the result.
1375 *
1376 * Control 'prototype' inheritance. When accessing "myTable[key]" and
1377 * the key does not appear in the table, but the metatable has an __index
1378 * property:
1379 *
1380 * - if the value is a function, the function is called, passing in the
1381 * table and the key; the return value of that function is returned as
1382 * the result.
1383 *
1384 * - if the value is another table, the value of the key in that table is
1385 * asked for and returned (and if it doesn't exist in that table, but that
1386 * table's metatable has an __index property, then it continues on up)
1387 *
1388 * - Use "rawget(myTable,key)" to skip this metamethod.
1389 *
1390 * http://www.lua.org/pil/13.4.1.html
1391 *
1392 * __newindex
1393 *
1394 * Like __index, but control property assignment.
1395 *
1396 * __mode - Control weak references. A string value with one or both
1397 * of the characters 'k' and 'v' which specifies that the the
1398 * keys and/or values in the table are weak references.
1399 *
1400 * __call - Treat a table like a function. When a table is followed by
1401 * parenthesis such as "myTable( 'foo' )" and the metatable has
1402 * a __call key pointing to a function, that function is invoked
1403 * (passing any specified arguments) and the return value is
1404 * returned.
1405 *
1406 * __metatable - Hide the metatable. When "getmetatable( myTable )" is
1407 * called, if the metatable for myTable has a __metatable
1408 * key, the value of that key is returned instead of the
1409 * actual metatable.
1410 *
1411 * __tostring - Control string representation. When the builtin
1412 * "tostring( myTable )" function is called, if the metatable
1413 * for myTable has a __tostring property set to a function,
1414 * that function is invoked (passing myTable to it) and the
1415 * return value is used as the string representation.
1416 *
1417 * __len - Control table length. When the table length is requested using
1418 * the length operator ( '#' ), if the metatable for myTable has
1419 * a __len key pointing to a function, that function is invoked
1420 * (passing myTable to it) and the return value used as the value
1421 * of "#myTable".
1422 *
1423 * __gc - Userdata finalizer code. When userdata is set to be garbage
1424 * collected, if the metatable has a __gc field pointing to a
1425 * function, that function is first invoked, passing the userdata
1426 * to it. The __gc metamethod is not called for tables.
1427 * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html)
1428 *
1429 * Special metamethods for redefining standard operators:
1430 * http://www.lua.org/pil/13.1.html
1431 *
1432 * __add "+"
1433 * __sub "-"
1434 * __mul "*"
1435 * __div "/"
1436 * __unm "!"
1437 * __pow "^"
1438 * __concat ".."
1439 *
1440 * Special methods for redfining standar relations
1441 * http://www.lua.org/pil/13.2.html
1442 *
1443 * __eq "=="
1444 * __lt "<"
1445 * __le "<="
1446 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001447
1448/*
1449 *
1450 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001451 * Class Map
1452 *
1453 *
1454 */
1455
1456/* Returns a struct hlua_map if the stack entry "ud" is
1457 * a class session, otherwise it throws an error.
1458 */
1459__LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud)
1460{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001461 return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001462}
1463
1464/* This function is the map constructor. It don't need
1465 * the class Map object. It creates and return a new Map
1466 * object. It must be called only during "body" or "init"
1467 * context because it process some filesystem accesses.
1468 */
1469__LJMP static int hlua_map_new(struct lua_State *L)
1470{
1471 const char *fn;
1472 int match = PAT_MATCH_STR;
1473 struct sample_conv conv;
1474 const char *file = "";
1475 int line = 0;
1476 lua_Debug ar;
1477 char *err = NULL;
1478 struct arg args[2];
1479
1480 if (lua_gettop(L) < 1 || lua_gettop(L) > 2)
1481 WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument."));
1482
1483 fn = MAY_LJMP(luaL_checkstring(L, 1));
1484
1485 if (lua_gettop(L) >= 2) {
1486 match = MAY_LJMP(luaL_checkinteger(L, 2));
1487 if (match < 0 || match >= PAT_MATCH_NUM)
1488 WILL_LJMP(luaL_error(L, "'new' needs a valid match method."));
1489 }
1490
1491 /* Get Lua filename and line number. */
1492 if (lua_getstack(L, 1, &ar)) { /* check function at level */
1493 lua_getinfo(L, "Sl", &ar); /* get info about it */
1494 if (ar.currentline > 0) { /* is there info? */
1495 file = ar.short_src;
1496 line = ar.currentline;
1497 }
1498 }
1499
1500 /* fill fake sample_conv struct. */
1501 conv.kw = ""; /* unused. */
1502 conv.process = NULL; /* unused. */
1503 conv.arg_mask = 0; /* unused. */
1504 conv.val_args = NULL; /* unused. */
1505 conv.out_type = SMP_T_STR;
1506 conv.private = (void *)(long)match;
1507 switch (match) {
1508 case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break;
1509 case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break;
1510 case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break;
1511 case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break;
1512 case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break;
1513 case PAT_MATCH_END: conv.in_type = SMP_T_STR; break;
1514 case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001515 case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001516 case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break;
1517 default:
1518 WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode."));
1519 }
1520
1521 /* fill fake args. */
1522 args[0].type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001523 args[0].data.str.area = (char *)fn;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001524 args[1].type = ARGT_STOP;
1525
1526 /* load the map. */
1527 if (!sample_load_map(args, &conv, file, line, &err)) {
1528 /* error case: we cant use luaL_error because we must
1529 * free the err variable.
1530 */
1531 luaL_where(L, 1);
1532 lua_pushfstring(L, "'new': %s.", err);
1533 lua_concat(L, 2);
1534 free(err);
1535 WILL_LJMP(lua_error(L));
1536 }
1537
1538 /* create the lua object. */
1539 lua_newtable(L);
1540 lua_pushlightuserdata(L, args[0].data.map);
1541 lua_rawseti(L, -2, 0);
1542
1543 /* Pop a class Map metatable and affect it to the userdata. */
1544 lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref);
1545 lua_setmetatable(L, -2);
1546
1547
1548 return 1;
1549}
1550
1551__LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str)
1552{
1553 struct map_descriptor *desc;
1554 struct pattern *pat;
1555 struct sample smp;
1556
1557 MAY_LJMP(check_args(L, 2, "lookup"));
1558 desc = MAY_LJMP(hlua_checkmap(L, 1));
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001559 if (desc->pat.expect_type == SMP_T_SINT) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001560 smp.data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001561 smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001562 }
1563 else {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001564 smp.data.type = SMP_T_STR;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001565 smp.flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001566 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 +02001567 }
1568
1569 pat = pattern_exec_match(&desc->pat, &smp, 1);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001570 if (!pat || !pat->data) {
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001571 if (str)
1572 lua_pushstring(L, "");
1573 else
1574 lua_pushnil(L);
1575 return 1;
1576 }
1577
1578 /* The Lua pattern must return a string, so we can't check the returned type */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001579 lua_pushlstring(L, pat->data->u.str.area, pat->data->u.str.data);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001580 return 1;
1581}
1582
1583__LJMP static int hlua_map_lookup(struct lua_State *L)
1584{
1585 return _hlua_map_lookup(L, 0);
1586}
1587
1588__LJMP static int hlua_map_slookup(struct lua_State *L)
1589{
1590 return _hlua_map_lookup(L, 1);
1591}
1592
1593/*
1594 *
1595 *
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001596 * Class Socket
1597 *
1598 *
1599 */
1600
1601__LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud)
1602{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001603 return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001604}
1605
1606/* This function is the handler called for each I/O on the established
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001607 * connection. It is used for notify space available to send or data
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001608 * received.
1609 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001610static void hlua_socket_handler(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001611{
Willy Tarreau00a37f02015-04-13 12:05:19 +02001612 struct stream_interface *si = appctx->owner;
Olivier Houchard9aaf7782017-09-13 18:30:23 +02001613 struct connection *c = cs_conn(objt_cs(si_opposite(si)->end));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001614
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001615 if (appctx->ctx.hlua_cosocket.die) {
1616 si_shutw(si);
1617 si_shutr(si);
1618 si_ic(si)->flags |= CF_READ_NULL;
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001619 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1620 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001621 stream_shutdown(si_strm(si), SF_ERR_KILLED);
1622 }
1623
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001624 /* If the connection object is not available, close all the
1625 * streams and wakeup everything waiting for.
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001626 */
1627 if (!c) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001628 si_shutw(si);
1629 si_shutr(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01001630 si_ic(si)->flags |= CF_READ_NULL;
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001631 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1632 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Willy Tarreaud4da1962015-04-20 01:31:23 +02001633 return;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001634 }
1635
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001636 /* If we cant write, wakeup the pending write signals. */
1637 if (channel_output_closed(si_ic(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001638 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001639
1640 /* If we cant read, wakeup the pending read signals. */
1641 if (channel_input_closed(si_oc(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001642 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001643
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001644 /* if the connection is not estabkished, inform the stream that we want
1645 * to be notified whenever the connection completes.
1646 */
1647 if (!(c->flags & CO_FL_CONNECTED)) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01001648 si_cant_get(si);
Willy Tarreau12c24232018-12-06 15:29:50 +01001649 si_rx_conn_blk(si);
Willy Tarreau3367d412018-11-15 10:57:41 +01001650 si_rx_endp_more(si);
Willy Tarreaud4da1962015-04-20 01:31:23 +02001651 return;
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001652 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001653
1654 /* This function is called after the connect. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01001655 appctx->ctx.hlua_cosocket.connected = 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001656
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001657 /* Wake the tasks which wants to write if the buffer have available space. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001658 if (channel_may_recv(si_ic(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001659 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001660
1661 /* Wake the tasks which wants to read if the buffer contains data. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001662 if (!channel_is_empty(si_oc(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001663 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001664
1665 /* Some data were injected in the buffer, notify the stream
1666 * interface.
1667 */
1668 if (!channel_is_empty(si_ic(si)))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01001669 si_update(si);
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001670
1671 /* If write notifications are registered, we considers we want
Willy Tarreau3367d412018-11-15 10:57:41 +01001672 * to write, so we clear the blocking flag.
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001673 */
1674 if (notification_registered(&appctx->ctx.hlua_cosocket.wake_on_write))
Willy Tarreau3367d412018-11-15 10:57:41 +01001675 si_rx_endp_more(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001676}
1677
Willy Tarreau87b09662015-04-03 00:22:06 +02001678/* This function is called when the "struct stream" is destroyed.
1679 * Remove the link from the object to this stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001680 * Wake all the pending signals.
1681 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001682static void hlua_socket_release(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001683{
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001684 struct xref *peer;
1685
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001686 /* Remove my link in the original object. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001687 peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref);
1688 if (peer)
1689 xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001690
1691 /* Wake all the task waiting for me. */
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001692 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1693 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001694}
1695
1696/* If the garbage collectio of the object is launch, nobody
Willy Tarreau87b09662015-04-03 00:22:06 +02001697 * uses this object. If the stream does not exists, just quit.
1698 * Send the shutdown signal to the stream. In some cases,
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001699 * pending signal can rest in the read and write lists. destroy
1700 * it.
1701 */
1702__LJMP static int hlua_socket_gc(lua_State *L)
1703{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001704 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001705 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001706 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001707
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001708 MAY_LJMP(check_args(L, 1, "__gc"));
1709
1710 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001711 peer = xref_get_peer_and_lock(&socket->xref);
1712 if (!peer)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001713 return 0;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001714 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001715
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001716 /* Set the flag which destroy the session. */
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001717 appctx->ctx.hlua_cosocket.die = 1;
1718 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001719
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001720 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001721 xref_disconnect(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001722 return 0;
1723}
1724
1725/* The close function send shutdown signal and break the
Willy Tarreau87b09662015-04-03 00:22:06 +02001726 * links between the stream and the object.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001727 */
sada05ed3302018-05-11 11:48:18 -07001728__LJMP static int hlua_socket_close_helper(lua_State *L)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001729{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001730 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001731 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001732 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001733
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001734 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001735
1736 /* Check if we run on the same thread than the xreator thread.
1737 * We cannot access to the socket if the thread is different.
1738 */
1739 if (socket->tid != tid)
1740 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1741
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001742 peer = xref_get_peer_and_lock(&socket->xref);
1743 if (!peer)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001744 return 0;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001745 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001746
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001747 /* Set the flag which destroy the session. */
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001748 appctx->ctx.hlua_cosocket.die = 1;
1749 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001750
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001751 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001752 xref_disconnect(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001753 return 0;
1754}
1755
sada05ed3302018-05-11 11:48:18 -07001756/* The close function calls close_helper.
1757 */
1758__LJMP static int hlua_socket_close(lua_State *L)
1759{
1760 MAY_LJMP(check_args(L, 1, "close"));
1761 return hlua_socket_close_helper(L);
1762}
1763
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001764/* This Lua function assumes that the stack contain three parameters.
1765 * 1 - USERDATA containing a struct socket
1766 * 2 - INTEGER with values of the macro defined below
1767 * If the integer is -1, we must read at most one line.
1768 * If the integer is -2, we ust read all the data until the
1769 * end of the stream.
1770 * If the integer is positive value, we must read a number of
1771 * bytes corresponding to this value.
1772 */
1773#define HLSR_READ_LINE (-1)
1774#define HLSR_READ_ALL (-2)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001775__LJMP static int hlua_socket_receive_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001776{
1777 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
1778 int wanted = lua_tointeger(L, 2);
1779 struct hlua *hlua = hlua_gethlua(L);
1780 struct appctx *appctx;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001781 size_t len;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001782 int nblk;
Willy Tarreau206ba832018-06-14 15:27:31 +02001783 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001784 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02001785 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001786 size_t len2;
Thierry FOURNIER00543922015-03-09 18:35:06 +01001787 int skip_at_end = 0;
Willy Tarreau81389672015-03-10 12:03:52 +01001788 struct channel *oc;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001789 struct stream_interface *si;
1790 struct stream *s;
1791 struct xref *peer;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001792 int missing_bytes;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001793
1794 /* Check if this lua stack is schedulable. */
1795 if (!hlua || !hlua->task)
1796 WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in "
1797 "'frontend', 'backend' or 'task'"));
1798
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001799 /* Check if we run on the same thread than the xreator thread.
1800 * We cannot access to the socket if the thread is different.
1801 */
1802 if (socket->tid != tid)
1803 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1804
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001805 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001806 peer = xref_get_peer_and_lock(&socket->xref);
1807 if (!peer)
1808 goto no_peer;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001809 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
1810 si = appctx->owner;
1811 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001812
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001813 oc = &s->res;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001814 if (wanted == HLSR_READ_LINE) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001815 /* Read line. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001816 nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001817 if (nblk < 0) /* Connection close. */
1818 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001819 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001820 goto connection_empty;
Thierry FOURNIER00543922015-03-09 18:35:06 +01001821
1822 /* remove final \r\n. */
1823 if (nblk == 1) {
1824 if (blk1[len1-1] == '\n') {
1825 len1--;
1826 skip_at_end++;
1827 if (blk1[len1-1] == '\r') {
1828 len1--;
1829 skip_at_end++;
1830 }
1831 }
1832 }
1833 else {
1834 if (blk2[len2-1] == '\n') {
1835 len2--;
1836 skip_at_end++;
1837 if (blk2[len2-1] == '\r') {
1838 len2--;
1839 skip_at_end++;
1840 }
1841 }
1842 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001843 }
1844
1845 else if (wanted == HLSR_READ_ALL) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001846 /* Read all the available data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001847 nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001848 if (nblk < 0) /* Connection close. */
1849 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001850 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001851 goto connection_empty;
1852 }
1853
1854 else {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001855 /* Read a block of data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001856 nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001857 if (nblk < 0) /* Connection close. */
1858 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001859 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001860 goto connection_empty;
1861
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001862 missing_bytes = wanted - socket->b.n;
1863 if (len1 > missing_bytes) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001864 nblk = 1;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001865 len1 = missing_bytes;
1866 } if (nblk == 2 && len1 + len2 > missing_bytes)
1867 len2 = missing_bytes - len1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001868 }
1869
1870 len = len1;
1871
1872 luaL_addlstring(&socket->b, blk1, len1);
1873 if (nblk == 2) {
1874 len += len2;
1875 luaL_addlstring(&socket->b, blk2, len2);
1876 }
1877
1878 /* Consume data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001879 co_skip(oc, len + skip_at_end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001880
1881 /* Don't wait anything. */
Thierry FOURNIER7e4ee472018-05-25 15:03:50 +02001882 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001883
1884 /* If the pattern reclaim to read all the data
1885 * in the connection, got out.
1886 */
1887 if (wanted == HLSR_READ_ALL)
1888 goto connection_empty;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001889 else if (wanted >= 0 && socket->b.n < wanted)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001890 goto connection_empty;
1891
1892 /* Return result. */
1893 luaL_pushresult(&socket->b);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001894 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001895 return 1;
1896
1897connection_closed:
1898
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001899 xref_unlock(&socket->xref, peer);
1900
1901no_peer:
1902
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001903 /* If the buffer containds data. */
1904 if (socket->b.n > 0) {
1905 luaL_pushresult(&socket->b);
1906 return 1;
1907 }
1908 lua_pushnil(L);
1909 lua_pushstring(L, "connection closed.");
1910 return 2;
1911
1912connection_empty:
1913
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001914 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) {
1915 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001916 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001917 }
1918 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02001919 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_receive_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001920 return 0;
1921}
1922
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001923/* This Lua function gets two parameters. The first one can be string
1924 * or a number. If the string is "*l", the user requires one line. If
1925 * the string is "*a", the user requires all the contents of the stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001926 * If the value is a number, the user require a number of bytes equal
1927 * to the value. The default value is "*l" (a line).
1928 *
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001929 * This parameter with a variable type is converted in integer. This
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001930 * integer takes this values:
1931 * -1 : read a line
1932 * -2 : read all the stream
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001933 * >0 : amount of bytes.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001934 *
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001935 * The second parameter is optional. It contains a string that must be
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001936 * concatenated with the read data.
1937 */
1938__LJMP static int hlua_socket_receive(struct lua_State *L)
1939{
1940 int wanted = HLSR_READ_LINE;
1941 const char *pattern;
1942 int type;
1943 char *error;
1944 size_t len;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001945 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001946
1947 if (lua_gettop(L) < 1 || lua_gettop(L) > 3)
1948 WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments."));
1949
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001950 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001951
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001952 /* Check if we run on the same thread than the xreator thread.
1953 * We cannot access to the socket if the thread is different.
1954 */
1955 if (socket->tid != tid)
1956 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1957
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001958 /* check for pattern. */
1959 if (lua_gettop(L) >= 2) {
1960 type = lua_type(L, 2);
1961 if (type == LUA_TSTRING) {
1962 pattern = lua_tostring(L, 2);
1963 if (strcmp(pattern, "*a") == 0)
1964 wanted = HLSR_READ_ALL;
1965 else if (strcmp(pattern, "*l") == 0)
1966 wanted = HLSR_READ_LINE;
1967 else {
1968 wanted = strtoll(pattern, &error, 10);
1969 if (*error != '\0')
1970 WILL_LJMP(luaL_error(L, "Unsupported pattern."));
1971 }
1972 }
1973 else if (type == LUA_TNUMBER) {
1974 wanted = lua_tointeger(L, 2);
1975 if (wanted < 0)
1976 WILL_LJMP(luaL_error(L, "Unsupported size."));
1977 }
1978 }
1979
1980 /* Set pattern. */
1981 lua_pushinteger(L, wanted);
Tim Duesterhusc6e377e2018-01-04 19:32:13 +01001982
1983 /* Check if we would replace the top by itself. */
1984 if (lua_gettop(L) != 2)
1985 lua_replace(L, 2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001986
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001987 /* init buffer, and fill it with prefix. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001988 luaL_buffinit(L, &socket->b);
1989
1990 /* Check prefix. */
1991 if (lua_gettop(L) >= 3) {
1992 if (lua_type(L, 3) != LUA_TSTRING)
1993 WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix"));
1994 pattern = lua_tolstring(L, 3, &len);
1995 luaL_addlstring(&socket->b, pattern, len);
1996 }
1997
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001998 return __LJMP(hlua_socket_receive_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001999}
2000
2001/* Write the Lua input string in the output buffer.
Mark Lakes22154b42018-01-29 14:38:40 -08002002 * This function returns a yield if no space is available.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002003 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002004static int hlua_socket_write_yield(struct lua_State *L,int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002005{
2006 struct hlua_socket *socket;
2007 struct hlua *hlua = hlua_gethlua(L);
2008 struct appctx *appctx;
2009 size_t buf_len;
2010 const char *buf;
2011 int len;
2012 int send_len;
2013 int sent;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002014 struct xref *peer;
2015 struct stream_interface *si;
2016 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002017
2018 /* Check if this lua stack is schedulable. */
2019 if (!hlua || !hlua->task)
2020 WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in "
2021 "'frontend', 'backend' or 'task'"));
2022
2023 /* Get object */
2024 socket = MAY_LJMP(hlua_checksocket(L, 1));
2025 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002026 sent = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002027
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002028 /* Check if we run on the same thread than the xreator thread.
2029 * We cannot access to the socket if the thread is different.
2030 */
2031 if (socket->tid != tid)
2032 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2033
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002034 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002035 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002036 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002037 lua_pushinteger(L, -1);
2038 return 1;
2039 }
2040 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2041 si = appctx->owner;
2042 s = si_strm(si);
2043
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002044 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002045 if (channel_output_closed(&s->req)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002046 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002047 lua_pushinteger(L, -1);
2048 return 1;
2049 }
2050
2051 /* Update the input buffer data. */
2052 buf += sent;
2053 send_len = buf_len - sent;
2054
2055 /* All the data are sent. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002056 if (sent >= buf_len) {
2057 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002058 return 1; /* Implicitly return the length sent. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002059 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002060
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002061 /* Check if the buffer is available because HAProxy doesn't allocate
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01002062 * the request buffer if its not required.
2063 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002064 if (s->req.buf.size == 0) {
Willy Tarreau581abd32018-10-25 10:21:41 +02002065 if (!si_alloc_ibuf(si, &appctx->buffer_wait))
Christopher Faulet33834b12016-12-19 09:29:06 +01002066 goto hlua_socket_write_yield_return;
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01002067 }
2068
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002069 /* Check for available space. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002070 len = b_room(&s->req.buf);
Christopher Faulet33834b12016-12-19 09:29:06 +01002071 if (len <= 0) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002072 goto hlua_socket_write_yield_return;
Christopher Faulet33834b12016-12-19 09:29:06 +01002073 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002074
2075 /* send data */
2076 if (len < send_len)
2077 send_len = len;
Thierry FOURNIER66b89192018-05-27 01:14:47 +02002078 len = ci_putblk(&s->req, buf, send_len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002079
2080 /* "Not enough space" (-1), "Buffer too little to contain
2081 * the data" (-2) are not expected because the available length
2082 * is tested.
2083 * Other unknown error are also not expected.
2084 */
2085 if (len <= 0) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01002086 if (len == -1)
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002087 s->req.flags |= CF_WAKE_WRITE;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002088
sada05ed3302018-05-11 11:48:18 -07002089 MAY_LJMP(hlua_socket_close_helper(L));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002090 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002091 lua_pushinteger(L, -1);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002092 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002093 return 1;
2094 }
2095
2096 /* update buffers. */
Thierry FOURNIER101b9762018-05-27 01:27:40 +02002097 appctx_wakeup(appctx);
Willy Tarreaude70fa12015-09-26 11:25:05 +02002098
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002099 s->req.rex = TICK_ETERNITY;
2100 s->res.wex = TICK_ETERNITY;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002101
2102 /* Update length sent. */
2103 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002104 lua_pushinteger(L, sent + len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002105
2106 /* All the data buffer is sent ? */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002107 if (sent + len >= buf_len) {
2108 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002109 return 1;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002110 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002111
2112hlua_socket_write_yield_return:
Thierry FOURNIERba42fcd2018-05-27 00:59:48 +02002113 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2114 xref_unlock(&socket->xref, peer);
2115 WILL_LJMP(luaL_error(L, "out of memory"));
2116 }
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002117 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002118 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_write_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002119 return 0;
2120}
2121
2122/* This function initiate the send of data. It just check the input
2123 * parameters and push an integer in the Lua stack that contain the
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002124 * amount of data written to the buffer. This is used by the function
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002125 * "hlua_socket_write_yield" that can yield.
2126 *
2127 * The Lua function gets between 3 and 4 parameters. The first one is
2128 * the associated object. The second is a string buffer. The third is
2129 * a facultative integer that represents where is the buffer position
2130 * of the start of the data that can send. The first byte is the
2131 * position "1". The default value is "1". The fourth argument is a
2132 * facultative integer that represents where is the buffer position
2133 * of the end of the data that can send. The default is the last byte.
2134 */
2135static int hlua_socket_send(struct lua_State *L)
2136{
2137 int i;
2138 int j;
2139 const char *buf;
2140 size_t buf_len;
2141
2142 /* Check number of arguments. */
2143 if (lua_gettop(L) < 2 || lua_gettop(L) > 4)
2144 WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments"));
2145
2146 /* Get the string. */
2147 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
2148
2149 /* Get and check j. */
2150 if (lua_gettop(L) == 4) {
2151 j = MAY_LJMP(luaL_checkinteger(L, 4));
2152 if (j < 0)
2153 j = buf_len + j + 1;
2154 if (j > buf_len)
2155 j = buf_len + 1;
2156 lua_pop(L, 1);
2157 }
2158 else
2159 j = buf_len;
2160
2161 /* Get and check i. */
2162 if (lua_gettop(L) == 3) {
2163 i = MAY_LJMP(luaL_checkinteger(L, 3));
2164 if (i < 0)
2165 i = buf_len + i + 1;
2166 if (i > buf_len)
2167 i = buf_len + 1;
2168 lua_pop(L, 1);
2169 } else
2170 i = 1;
2171
2172 /* Check bth i and j. */
2173 if (i > j) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002174 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002175 return 1;
2176 }
2177 if (i == 0 && j == 0) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002178 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002179 return 1;
2180 }
2181 if (i == 0)
2182 i = 1;
2183 if (j == 0)
2184 j = 1;
2185
2186 /* Pop the string. */
2187 lua_pop(L, 1);
2188
2189 /* Update the buffer length. */
2190 buf += i - 1;
2191 buf_len = j - i + 1;
2192 lua_pushlstring(L, buf, buf_len);
2193
2194 /* This unsigned is used to remember the amount of sent data. */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002195 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002196
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002197 return MAY_LJMP(hlua_socket_write_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002198}
2199
Willy Tarreau22b0a682015-06-17 19:43:49 +02002200#define SOCKET_INFO_MAX_LEN sizeof("[0000:0000:0000:0000:0000:0000:0000:0000]:12345")
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002201__LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr)
2202{
2203 static char buffer[SOCKET_INFO_MAX_LEN];
2204 int ret;
2205 int len;
2206 char *p;
2207
2208 ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1);
2209 if (ret <= 0) {
2210 lua_pushnil(L);
2211 return 1;
2212 }
2213
2214 if (ret == AF_UNIX) {
2215 lua_pushstring(L, buffer+1);
2216 return 1;
2217 }
2218 else if (ret == AF_INET6) {
2219 buffer[0] = '[';
2220 len = strlen(buffer);
2221 buffer[len] = ']';
2222 len++;
2223 buffer[len] = ':';
2224 len++;
2225 p = buffer;
2226 }
2227 else if (ret == AF_INET) {
2228 p = buffer + 1;
2229 len = strlen(p);
2230 p[len] = ':';
2231 len++;
2232 }
2233 else {
2234 lua_pushnil(L);
2235 return 1;
2236 }
2237
2238 if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) {
2239 lua_pushnil(L);
2240 return 1;
2241 }
2242
2243 lua_pushstring(L, p);
2244 return 1;
2245}
2246
2247/* Returns information about the peer of the connection. */
2248__LJMP static int hlua_socket_getpeername(struct lua_State *L)
2249{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002250 struct hlua_socket *socket;
2251 struct connection *conn;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002252 struct xref *peer;
2253 struct appctx *appctx;
2254 struct stream_interface *si;
2255 struct stream *s;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002256 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002257
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002258 MAY_LJMP(check_args(L, 1, "getpeername"));
2259
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002260 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002261
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002262 /* Check if we run on the same thread than the xreator thread.
2263 * We cannot access to the socket if the thread is different.
2264 */
2265 if (socket->tid != tid)
2266 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2267
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002268 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002269 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002270 if (!peer) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002271 lua_pushnil(L);
2272 return 1;
2273 }
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002274 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2275 si = appctx->owner;
2276 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002277
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002278 conn = cs_conn(objt_cs(s->si[1].end));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002279 if (!conn) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002280 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002281 lua_pushnil(L);
2282 return 1;
2283 }
2284
Willy Tarreaua71f6422016-11-16 17:00:14 +01002285 conn_get_to_addr(conn);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002286 if (!(conn->flags & CO_FL_ADDR_TO_SET)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002287 xref_unlock(&socket->xref, peer);
Willy Tarreaua71f6422016-11-16 17:00:14 +01002288 lua_pushnil(L);
2289 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002290 }
2291
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002292 ret = MAY_LJMP(hlua_socket_info(L, &conn->addr.to));
2293 xref_unlock(&socket->xref, peer);
2294 return ret;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002295}
2296
2297/* Returns information about my connection side. */
2298static int hlua_socket_getsockname(struct lua_State *L)
2299{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002300 struct hlua_socket *socket;
2301 struct connection *conn;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002302 struct appctx *appctx;
2303 struct xref *peer;
2304 struct stream_interface *si;
2305 struct stream *s;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002306 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002307
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002308 MAY_LJMP(check_args(L, 1, "getsockname"));
2309
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002310 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002311
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002312 /* Check if we run on the same thread than the xreator thread.
2313 * We cannot access to the socket if the thread is different.
2314 */
2315 if (socket->tid != tid)
2316 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2317
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002318 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002319 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002320 if (!peer) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002321 lua_pushnil(L);
2322 return 1;
2323 }
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002324 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2325 si = appctx->owner;
2326 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002327
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002328 conn = cs_conn(objt_cs(s->si[1].end));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002329 if (!conn) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002330 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002331 lua_pushnil(L);
2332 return 1;
2333 }
2334
Willy Tarreaua71f6422016-11-16 17:00:14 +01002335 conn_get_from_addr(conn);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002336 if (!(conn->flags & CO_FL_ADDR_FROM_SET)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002337 xref_unlock(&socket->xref, peer);
Willy Tarreaua71f6422016-11-16 17:00:14 +01002338 lua_pushnil(L);
2339 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002340 }
2341
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002342 ret = hlua_socket_info(L, &conn->addr.from);
2343 xref_unlock(&socket->xref, peer);
2344 return ret;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002345}
2346
2347/* This struct define the applet. */
Willy Tarreau30576452015-04-13 13:50:30 +02002348static struct applet update_applet = {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002349 .obj_type = OBJ_TYPE_APPLET,
2350 .name = "<LUA_TCP>",
2351 .fct = hlua_socket_handler,
2352 .release = hlua_socket_release,
2353};
2354
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002355__LJMP static int hlua_socket_connect_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002356{
2357 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
2358 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002359 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002360 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002361 struct stream_interface *si;
2362 struct stream *s;
2363
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002364 /* Check if we run on the same thread than the xreator thread.
2365 * We cannot access to the socket if the thread is different.
2366 */
2367 if (socket->tid != tid)
2368 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2369
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002370 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002371 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002372 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002373 lua_pushnil(L);
2374 lua_pushstring(L, "Can't connect");
2375 return 2;
2376 }
2377 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2378 si = appctx->owner;
2379 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002380
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002381 /* Check if we run on the same thread than the xreator thread.
2382 * We cannot access to the socket if the thread is different.
2383 */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002384 if (socket->tid != tid) {
2385 xref_unlock(&socket->xref, peer);
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002386 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002387 }
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002388
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002389 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002390 if (!hlua || channel_output_closed(&s->req)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002391 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002392 lua_pushnil(L);
2393 lua_pushstring(L, "Can't connect");
2394 return 2;
2395 }
2396
Willy Tarreaue09101e2018-10-16 17:37:12 +02002397 appctx = __objt_appctx(s->si[0].end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002398
2399 /* Check for connection established. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002400 if (appctx->ctx.hlua_cosocket.connected) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002401 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002402 lua_pushinteger(L, 1);
2403 return 1;
2404 }
2405
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002406 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2407 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002408 WILL_LJMP(luaL_error(L, "out of memory error"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002409 }
2410 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002411 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002412 return 0;
2413}
2414
2415/* This function fail or initite the connection. */
2416__LJMP static int hlua_socket_connect(struct lua_State *L)
2417{
2418 struct hlua_socket *socket;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002419 int port = -1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002420 const char *ip;
2421 struct connection *conn;
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002422 struct hlua *hlua;
2423 struct appctx *appctx;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002424 int low, high;
2425 struct sockaddr_storage *addr;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002426 struct xref *peer;
2427 struct stream_interface *si;
2428 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002429
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002430 if (lua_gettop(L) < 2)
2431 WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments"));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002432
2433 /* Get args. */
2434 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002435
2436 /* Check if we run on the same thread than the xreator thread.
2437 * We cannot access to the socket if the thread is different.
2438 */
2439 if (socket->tid != tid)
2440 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2441
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002442 ip = MAY_LJMP(luaL_checkstring(L, 2));
Tim Duesterhus6edab862018-01-06 19:04:45 +01002443 if (lua_gettop(L) >= 3) {
2444 luaL_Buffer b;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002445 port = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002446
Tim Duesterhus6edab862018-01-06 19:04:45 +01002447 /* Force the ip to end with a colon, to support IPv6 addresses
2448 * that are not enclosed within square brackets.
2449 */
2450 if (port > 0) {
2451 luaL_buffinit(L, &b);
2452 luaL_addstring(&b, ip);
2453 luaL_addchar(&b, ':');
2454 luaL_pushresult(&b);
2455 ip = lua_tolstring(L, lua_gettop(L), NULL);
2456 }
2457 }
2458
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002459 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002460 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002461 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002462 lua_pushnil(L);
2463 return 1;
2464 }
2465 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2466 si = appctx->owner;
2467 s = si_strm(si);
2468
2469 /* Initialise connection. */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002470 conn = cs_conn(si_alloc_cs(&s->si[1], NULL));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002471 if (!conn) {
2472 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002473 WILL_LJMP(luaL_error(L, "connect: internal error"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002474 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002475
Willy Tarreau3adac082015-09-26 17:51:09 +02002476 /* needed for the connection not to be closed */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002477 conn->target = s->target;
Willy Tarreau3adac082015-09-26 17:51:09 +02002478
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002479 /* Parse ip address. */
Willy Tarreau48ef4c92017-01-06 18:32:38 +01002480 addr = str2sa_range(ip, NULL, &low, &high, NULL, NULL, NULL, 0);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002481 if (!addr) {
2482 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002483 WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002484 }
2485 if (low != high) {
2486 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002487 WILL_LJMP(luaL_error(L, "connect: port ranges not supported : address '%s'", ip));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002488 }
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002489 memcpy(&conn->addr.to, addr, sizeof(struct sockaddr_storage));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002490
2491 /* Set port. */
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002492 if (low == 0) {
2493 if (conn->addr.to.ss_family == AF_INET) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002494 if (port == -1) {
2495 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002496 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002497 }
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002498 ((struct sockaddr_in *)&conn->addr.to)->sin_port = htons(port);
2499 } else if (conn->addr.to.ss_family == AF_INET6) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002500 if (port == -1) {
2501 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002502 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002503 }
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002504 ((struct sockaddr_in6 *)&conn->addr.to)->sin6_port = htons(port);
2505 }
2506 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002507
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002508 hlua = hlua_gethlua(L);
Willy Tarreaue09101e2018-10-16 17:37:12 +02002509 appctx = __objt_appctx(s->si[0].end);
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002510
2511 /* inform the stream that we want to be notified whenever the
2512 * connection completes.
2513 */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01002514 si_cant_get(&s->si[0]);
Willy Tarreau3367d412018-11-15 10:57:41 +01002515 si_rx_endp_more(&s->si[0]);
Thierry FOURNIER8c8fbbe2015-09-26 17:02:35 +02002516 appctx_wakeup(appctx);
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002517
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +02002518 hlua->flags |= HLUA_MUST_GC;
2519
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002520 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2521 xref_unlock(&socket->xref, peer);
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002522 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002523 }
2524 xref_unlock(&socket->xref, peer);
PiBa-NL706d5ee2018-05-05 23:51:42 +02002525
2526 task_wakeup(s->task, TASK_WOKEN_INIT);
2527 /* Return yield waiting for connection. */
2528
Willy Tarreau9635e032018-10-16 17:52:55 +02002529 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002530
2531 return 0;
2532}
2533
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002534#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002535__LJMP static int hlua_socket_connect_ssl(struct lua_State *L)
2536{
2537 struct hlua_socket *socket;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002538 struct xref *peer;
2539 struct appctx *appctx;
2540 struct stream_interface *si;
2541 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002542
2543 MAY_LJMP(check_args(L, 3, "connect_ssl"));
2544 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002545
2546 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002547 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002548 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002549 lua_pushnil(L);
2550 return 1;
2551 }
2552 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2553 si = appctx->owner;
2554 s = si_strm(si);
2555
2556 s->target = &socket_ssl.obj_type;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002557 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002558 return MAY_LJMP(hlua_socket_connect(L));
2559}
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002560#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002561
2562__LJMP static int hlua_socket_setoption(struct lua_State *L)
2563{
2564 return 0;
2565}
2566
2567__LJMP static int hlua_socket_settimeout(struct lua_State *L)
2568{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002569 struct hlua_socket *socket;
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002570 int tmout;
Mark Lakes56cc1252018-03-27 09:48:06 +02002571 double dtmout;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002572 struct xref *peer;
2573 struct appctx *appctx;
2574 struct stream_interface *si;
2575 struct stream *s;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002576
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002577 MAY_LJMP(check_args(L, 2, "settimeout"));
2578
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002579 socket = MAY_LJMP(hlua_checksocket(L, 1));
Mark Lakes56cc1252018-03-27 09:48:06 +02002580
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002581 /* convert the timeout to millis */
2582 dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002583
Thierry Fournier17a921b2018-03-08 09:59:02 +01002584 /* Check for negative values */
Mark Lakes56cc1252018-03-27 09:48:06 +02002585 if (dtmout < 0)
Thierry Fournier17a921b2018-03-08 09:59:02 +01002586 WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values"));
2587
Mark Lakes56cc1252018-03-27 09:48:06 +02002588 if (dtmout > INT_MAX) /* overflow check */
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002589 WILL_LJMP(luaL_error(L, "settimeout: cannot set values larger than %d ms", INT_MAX));
Mark Lakes56cc1252018-03-27 09:48:06 +02002590
2591 tmout = MS_TO_TICKS((int)dtmout);
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002592 if (tmout == 0)
2593 tmout++; /* very small timeouts are adjusted to a minium of 1ms */
Mark Lakes56cc1252018-03-27 09:48:06 +02002594
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002595 /* Check if we run on the same thread than the xreator thread.
2596 * We cannot access to the socket if the thread is different.
2597 */
2598 if (socket->tid != tid)
2599 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2600
Mark Lakes56cc1252018-03-27 09:48:06 +02002601 /* check for connection break. If some data were read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002602 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002603 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002604 hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts.");
2605 WILL_LJMP(lua_error(L));
2606 return 0;
2607 }
2608 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2609 si = appctx->owner;
2610 s = si_strm(si);
2611
Cyril Bonté7bb63452018-08-17 23:51:02 +02002612 s->sess->fe->timeout.connect = tmout;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002613 s->req.rto = tmout;
2614 s->req.wto = tmout;
2615 s->res.rto = tmout;
2616 s->res.wto = tmout;
Cyril Bonté7bb63452018-08-17 23:51:02 +02002617 s->req.rex = tick_add_ifset(now_ms, tmout);
2618 s->req.wex = tick_add_ifset(now_ms, tmout);
2619 s->res.rex = tick_add_ifset(now_ms, tmout);
2620 s->res.wex = tick_add_ifset(now_ms, tmout);
2621
2622 s->task->expire = tick_add_ifset(now_ms, tmout);
2623 task_queue(s->task);
2624
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002625 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002626
Thierry Fourniere9636f12018-03-08 09:54:32 +01002627 lua_pushinteger(L, 1);
Tim Duesterhus119a5f12018-01-06 19:16:25 +01002628 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002629}
2630
2631__LJMP static int hlua_socket_new(lua_State *L)
2632{
2633 struct hlua_socket *socket;
2634 struct appctx *appctx;
Willy Tarreau15b5e142015-04-04 14:38:25 +02002635 struct session *sess;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002636 struct stream *strm;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002637
2638 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002639 if (!lua_checkstack(L, 3)) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002640 hlua_pusherror(L, "socket: full stack");
2641 goto out_fail_conf;
2642 }
2643
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002644 /* Create the object: obj[0] = userdata. */
2645 lua_newtable(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002646 socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket)));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002647 lua_rawseti(L, -2, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002648 memset(socket, 0, sizeof(*socket));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002649 socket->tid = tid;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002650
Thierry FOURNIER4a6170c2015-03-09 17:07:10 +01002651 /* Check if the various memory pools are intialized. */
Willy Tarreaubafbe012017-11-24 17:34:44 +01002652 if (!pool_head_stream || !pool_head_buffer) {
Thierry FOURNIER4a6170c2015-03-09 17:07:10 +01002653 hlua_pusherror(L, "socket: uninitialized pools.");
2654 goto out_fail_conf;
2655 }
2656
Willy Tarreau87b09662015-04-03 00:22:06 +02002657 /* Pop a class stream metatable and affect it to the userdata. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002658 lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref);
2659 lua_setmetatable(L, -2);
2660
Willy Tarreaud420a972015-04-06 00:39:18 +02002661 /* Create the applet context */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01002662 appctx = appctx_new(&update_applet, tid_bit);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002663 if (!appctx) {
2664 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaufeb76402015-04-03 14:10:06 +02002665 goto out_fail_conf;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002666 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002667
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002668 appctx->ctx.hlua_cosocket.connected = 0;
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02002669 appctx->ctx.hlua_cosocket.die = 0;
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002670 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write);
2671 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read);
Willy Tarreaub2bf8332015-04-04 15:58:58 +02002672
Willy Tarreaud420a972015-04-06 00:39:18 +02002673 /* Now create a session, task and stream for this applet */
2674 sess = session_new(&socket_proxy, NULL, &appctx->obj_type);
2675 if (!sess) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002676 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002677 goto out_fail_sess;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002678 }
2679
Willy Tarreau87787ac2017-08-28 16:22:54 +02002680 strm = stream_new(sess, &appctx->obj_type);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002681 if (!strm) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002682 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002683 goto out_fail_stream;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002684 }
2685
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002686 /* Initialise cross reference between stream and Lua socket object. */
2687 xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002688
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002689 /* Configure "right" stream interface. this "si" is used to connect
2690 * and retrieve data from the server. The connection is initialized
2691 * with the "struct server".
2692 */
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002693 si_set_state(&strm->si[1], SI_ST_ASS);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002694
2695 /* Force destination server. */
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002696 strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET | SF_BE_ASSIGNED;
2697 strm->target = &socket_tcp.obj_type;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002698
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002699 return 1;
2700
Willy Tarreaud420a972015-04-06 00:39:18 +02002701 out_fail_stream:
Willy Tarreau11c36242015-04-04 15:54:03 +02002702 session_free(sess);
Willy Tarreaud420a972015-04-06 00:39:18 +02002703 out_fail_sess:
2704 appctx_free(appctx);
2705 out_fail_conf:
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002706 WILL_LJMP(lua_error(L));
2707 return 0;
2708}
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01002709
2710/*
2711 *
2712 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002713 * Class Channel
2714 *
2715 *
2716 */
2717
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002718/* This function is called before the Lua execution. It stores
2719 * the differents parsers state before executing some Lua code.
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002720 */
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002721static inline void consistency_set(struct stream *stream, int opt, struct hlua_consistency *c)
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002722{
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002723 c->mode = stream->be->mode;
2724 switch (c->mode) {
2725 case PR_MODE_HTTP:
2726 c->data.http.dir = opt & SMP_OPT_DIR;
2727 if (c->data.http.dir == SMP_OPT_DIR_REQ)
2728 c->data.http.state = stream->txn->req.msg_state;
2729 else
2730 c->data.http.state = stream->txn->rsp.msg_state;
2731 break;
2732 default:
2733 break;
2734 }
2735}
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002736
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002737/* This function is called after the Lua execution. it
2738 * returns true if the parser state is consistent, otherwise,
2739 * it return false.
2740 *
2741 * In HTTP mode, the parser state must be in the same state
2742 * or greater when we exit the function. Even if we do a
2743 * control yield. This prevent to break the HTTP message
2744 * from the Lua code.
2745 */
2746static inline int consistency_check(struct stream *stream, int opt, struct hlua_consistency *c)
2747{
2748 if (c->mode != stream->be->mode)
2749 return 0;
2750
2751 switch (c->mode) {
2752 case PR_MODE_HTTP:
2753 if (c->data.http.dir != (opt & SMP_OPT_DIR))
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002754 return 0;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002755 if (c->data.http.dir == SMP_OPT_DIR_REQ)
2756 return stream->txn->req.msg_state >= c->data.http.state;
2757 else
2758 return stream->txn->rsp.msg_state >= c->data.http.state;
2759 default:
2760 return 1;
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002761 }
2762 return 1;
2763}
2764
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002765/* Returns the struct hlua_channel join to the class channel in the
2766 * stack entry "ud" or throws an argument error.
2767 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002768__LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002769{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02002770 return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002771}
2772
Willy Tarreau47860ed2015-03-10 14:07:50 +01002773/* Pushes the channel onto the top of the stack. If the stask does not have a
2774 * free slots, the function fails and returns 0;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002775 */
Willy Tarreau2a71af42015-03-10 13:51:50 +01002776static int hlua_channel_new(lua_State *L, struct channel *channel)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002777{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002778 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002779 if (!lua_checkstack(L, 3))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002780 return 0;
2781
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002782 lua_newtable(L);
Willy Tarreau47860ed2015-03-10 14:07:50 +01002783 lua_pushlightuserdata(L, channel);
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002784 lua_rawseti(L, -2, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002785
2786 /* Pop a class sesison metatable and affect it to the userdata. */
2787 lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref);
2788 lua_setmetatable(L, -2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002789 return 1;
2790}
2791
2792/* Duplicate all the data present in the input channel and put it
2793 * in a string LUA variables. Returns -1 and push a nil value in
2794 * the stack if the channel is closed and all the data are consumed,
2795 * returns 0 if no data are available, otherwise it returns the length
2796 * of the builded string.
2797 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002798static inline int _hlua_channel_dup(struct channel *chn, lua_State *L)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002799{
2800 char *blk1;
2801 char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02002802 size_t len1;
2803 size_t len2;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002804 int ret;
2805 luaL_Buffer b;
2806
Willy Tarreau06d80a92017-10-19 14:32:15 +02002807 ret = ci_getblk_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002808 if (unlikely(ret == 0))
2809 return 0;
2810
2811 if (unlikely(ret < 0)) {
2812 lua_pushnil(L);
2813 return -1;
2814 }
2815
2816 luaL_buffinit(L, &b);
2817 luaL_addlstring(&b, blk1, len1);
2818 if (unlikely(ret == 2))
2819 luaL_addlstring(&b, blk2, len2);
2820 luaL_pushresult(&b);
2821
2822 if (unlikely(ret == 2))
2823 return len1 + len2;
2824 return len1;
2825}
2826
2827/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2828 * a yield. This function keep the data in the buffer.
2829 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002830__LJMP static int hlua_channel_dup_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002831{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002832 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002833
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002834 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2835
Christopher Faulet3f829a42018-12-13 21:56:45 +01002836 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2837 WILL_LJMP(lua_error(L));
2838
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002839 if (_hlua_channel_dup(chn, L) == 0)
Willy Tarreau9635e032018-10-16 17:52:55 +02002840 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_dup_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002841 return 1;
2842}
2843
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002844/* Check arguments for the function "hlua_channel_dup_yield". */
2845__LJMP static int hlua_channel_dup(lua_State *L)
2846{
2847 MAY_LJMP(check_args(L, 1, "dup"));
2848 MAY_LJMP(hlua_checkchannel(L, 1));
2849 return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0));
2850}
2851
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002852/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2853 * a yield. This function consumes the data in the buffer. It returns
2854 * a string containing the data or a nil pointer if no data are available
2855 * and the channel is closed.
2856 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002857__LJMP static int hlua_channel_get_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002858{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002859 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002860 int ret;
2861
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002862 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002863
Christopher Faulet3f829a42018-12-13 21:56:45 +01002864 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2865 WILL_LJMP(lua_error(L));
2866
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002867 ret = _hlua_channel_dup(chn, L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002868 if (unlikely(ret == 0))
Willy Tarreau9635e032018-10-16 17:52:55 +02002869 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002870
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002871 if (unlikely(ret == -1))
2872 return 1;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002873
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002874 b_sub(&chn->buf, ret);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002875 return 1;
2876}
2877
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002878/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002879__LJMP static int hlua_channel_get(lua_State *L)
2880{
2881 MAY_LJMP(check_args(L, 1, "get"));
2882 MAY_LJMP(hlua_checkchannel(L, 1));
2883 return MAY_LJMP(hlua_channel_get_yield(L, 0, 0));
2884}
2885
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002886/* This functions consumes and returns one line. If the channel is closed,
2887 * and the last data does not contains a final '\n', the data are returned
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002888 * without the final '\n'. When no more data are available, it returns nil
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002889 * value.
2890 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002891__LJMP static int hlua_channel_getline_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002892{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002893 char *blk1;
2894 char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02002895 size_t len1;
2896 size_t len2;
2897 size_t len;
Willy Tarreau47860ed2015-03-10 14:07:50 +01002898 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002899 int ret;
2900 luaL_Buffer b;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002901
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002902 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2903
Christopher Faulet3f829a42018-12-13 21:56:45 +01002904 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2905 WILL_LJMP(lua_error(L));
2906
Willy Tarreau06d80a92017-10-19 14:32:15 +02002907 ret = ci_getline_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002908 if (ret == 0)
Willy Tarreau9635e032018-10-16 17:52:55 +02002909 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_getline_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002910
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002911 if (ret == -1) {
2912 lua_pushnil(L);
2913 return 1;
2914 }
2915
2916 luaL_buffinit(L, &b);
2917 luaL_addlstring(&b, blk1, len1);
2918 len = len1;
2919 if (unlikely(ret == 2)) {
2920 luaL_addlstring(&b, blk2, len2);
2921 len += len2;
2922 }
2923 luaL_pushresult(&b);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002924 b_rep_blk(&chn->buf, ci_head(chn), ci_head(chn) + len, NULL, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002925 return 1;
2926}
2927
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002928/* Check arguments for the function "hlua_channel_getline_yield". */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002929__LJMP static int hlua_channel_getline(lua_State *L)
2930{
2931 MAY_LJMP(check_args(L, 1, "getline"));
2932 MAY_LJMP(hlua_checkchannel(L, 1));
2933 return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0));
2934}
2935
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002936/* This function takes a string as input, and append it at the
2937 * input side of channel. If the data is too big, but a space
2938 * is probably available after sending some data, the function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002939 * yields. If the data is bigger than the buffer, or if the
2940 * channel is closed, it returns -1. Otherwise, it returns the
2941 * amount of data written.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002942 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002943__LJMP static int hlua_channel_append_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002944{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002945 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002946 size_t len;
2947 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
2948 int l = MAY_LJMP(luaL_checkinteger(L, 3));
2949 int ret;
2950 int max;
2951
Christopher Faulet3f829a42018-12-13 21:56:45 +01002952 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2953 WILL_LJMP(lua_error(L));
2954
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002955 /* Check if the buffer is available because HAProxy doesn't allocate
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002956 * the request buffer if its not required.
2957 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002958 if (chn->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +01002959 si_rx_buff_blk(chn_prod(chn));
Willy Tarreau9635e032018-10-16 17:52:55 +02002960 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002961 }
2962
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002963 max = channel_recv_limit(chn) - b_data(&chn->buf);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002964 if (max > len - l)
2965 max = len - l;
2966
Willy Tarreau06d80a92017-10-19 14:32:15 +02002967 ret = ci_putblk(chn, str + l, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002968 if (ret == -2 || ret == -3) {
2969 lua_pushinteger(L, -1);
2970 return 1;
2971 }
Willy Tarreaubc18da12015-03-13 14:00:47 +01002972 if (ret == -1) {
2973 chn->flags |= CF_WAKE_WRITE;
Willy Tarreau9635e032018-10-16 17:52:55 +02002974 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Willy Tarreaubc18da12015-03-13 14:00:47 +01002975 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002976 l += ret;
2977 lua_pop(L, 1);
2978 lua_pushinteger(L, l);
2979
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002980 max = channel_recv_limit(chn) - b_data(&chn->buf);
Willy Tarreaua79021a2018-06-15 18:07:57 +02002981 if (max == 0 && co_data(chn) == 0) {
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002982 /* There are no space available, and the output buffer is empty.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002983 * in this case, we cannot add more data, so we cannot yield,
2984 * we return the amount of copyied data.
2985 */
2986 return 1;
2987 }
2988 if (l < len)
Willy Tarreau9635e032018-10-16 17:52:55 +02002989 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002990 return 1;
2991}
2992
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002993/* Just a wrapper of "hlua_channel_append_yield". It returns the length
2994 * of the written string, or -1 if the channel is closed or if the
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002995 * buffer size is too little for the data.
2996 */
2997__LJMP static int hlua_channel_append(lua_State *L)
2998{
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002999 size_t len;
3000
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003001 MAY_LJMP(check_args(L, 2, "append"));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003002 MAY_LJMP(hlua_checkchannel(L, 1));
3003 MAY_LJMP(luaL_checklstring(L, 2, &len));
3004 MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003005 lua_pushinteger(L, 0);
3006
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003007 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003008}
3009
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003010/* Just a wrapper of "hlua_channel_append_yield". This wrapper starts
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003011 * his process by cleaning the buffer. The result is a replacement
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003012 * of the current data. It returns the length of the written string,
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003013 * or -1 if the channel is closed or if the buffer size is too
3014 * little for the data.
3015 */
3016__LJMP static int hlua_channel_set(lua_State *L)
3017{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003018 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003019
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003020 MAY_LJMP(check_args(L, 2, "set"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003021 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003022 lua_pushinteger(L, 0);
3023
Christopher Faulet3f829a42018-12-13 21:56:45 +01003024 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
3025 WILL_LJMP(lua_error(L));
3026
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003027 b_set_data(&chn->buf, co_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003028
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003029 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003030}
3031
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003032/* Append data in the output side of the buffer. This data is immediately
3033 * sent. The function returns the amount of data written. If the buffer
3034 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003035 * if the channel is closed.
3036 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003037__LJMP static int hlua_channel_send_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003038{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003039 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003040 size_t len;
3041 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
3042 int l = MAY_LJMP(luaL_checkinteger(L, 3));
3043 int max;
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003044 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003045
Christopher Faulet3f829a42018-12-13 21:56:45 +01003046 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
3047 WILL_LJMP(lua_error(L));
3048
Willy Tarreau47860ed2015-03-10 14:07:50 +01003049 if (unlikely(channel_output_closed(chn))) {
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003050 lua_pushinteger(L, -1);
3051 return 1;
3052 }
3053
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003054 /* Check if the buffer is available because HAProxy doesn't allocate
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01003055 * the request buffer if its not required.
3056 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003057 if (chn->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +01003058 si_rx_buff_blk(chn_prod(chn));
Willy Tarreau9635e032018-10-16 17:52:55 +02003059 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01003060 }
3061
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003062 /* The written data will be immediately sent, so we can check
3063 * the available space without taking in account the reserve.
3064 * The reserve is guaranteed for the processing of incoming
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003065 * data, because the buffer will be flushed.
3066 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003067 max = b_room(&chn->buf);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003068
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003069 /* If there is no space available, and the output buffer is empty.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003070 * in this case, we cannot add more data, so we cannot yield,
3071 * we return the amount of copyied data.
3072 */
Willy Tarreaua79021a2018-06-15 18:07:57 +02003073 if (max == 0 && co_data(chn) == 0)
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003074 return 1;
3075
3076 /* Adjust the real required length. */
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003077 if (max > len - l)
3078 max = len - l;
3079
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003080 /* The buffer available size may be not contiguous. This test
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003081 * detects a non contiguous buffer and realign it.
3082 */
Willy Tarreau3f679992018-06-15 15:06:42 +02003083 if (ci_space_for_replace(chn) < max)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003084 channel_slow_realign(chn, trash.area);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003085
3086 /* Copy input data in the buffer. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003087 max = b_rep_blk(&chn->buf, ci_head(chn), ci_head(chn), str + l, max);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003088
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003089 /* buffer replace considers that the input part is filled.
3090 * so, I must forward these new data in the output part.
3091 */
Willy Tarreaubcbd3932018-06-06 07:13:22 +02003092 c_adv(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003093
3094 l += max;
3095 lua_pop(L, 1);
3096 lua_pushinteger(L, l);
3097
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003098 /* If there is no space available, and the output buffer is empty.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003099 * in this case, we cannot add more data, so we cannot yield,
3100 * we return the amount of copyied data.
3101 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003102 max = b_room(&chn->buf);
Willy Tarreaua79021a2018-06-15 18:07:57 +02003103 if (max == 0 && co_data(chn) == 0)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003104 return 1;
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003105
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003106 if (l < len) {
3107 /* If we are waiting for space in the response buffer, we
3108 * must set the flag WAKERESWR. This flag required the task
3109 * wake up if any activity is detected on the response buffer.
3110 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003111 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003112 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01003113 else
3114 HLUA_SET_WAKEREQWR(hlua);
Willy Tarreau9635e032018-10-16 17:52:55 +02003115 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003116 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003117
3118 return 1;
3119}
3120
3121/* Just a wraper of "_hlua_channel_send". This wrapper permits
3122 * yield the LUA process, and resume it without checking the
3123 * input arguments.
3124 */
3125__LJMP static int hlua_channel_send(lua_State *L)
3126{
3127 MAY_LJMP(check_args(L, 2, "send"));
3128 lua_pushinteger(L, 0);
3129
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003130 return MAY_LJMP(hlua_channel_send_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003131}
3132
3133/* This function forward and amount of butes. The data pass from
3134 * the input side of the buffer to the output side, and can be
3135 * forwarded. This function never fails.
3136 *
3137 * The Lua function takes an amount of bytes to be forwarded in
3138 * imput. It returns the number of bytes forwarded.
3139 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003140__LJMP static int hlua_channel_forward_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003141{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003142 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003143 int len;
3144 int l;
3145 int max;
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003146 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003147
3148 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Faulet3f829a42018-12-13 21:56:45 +01003149
3150 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
3151 WILL_LJMP(lua_error(L));
3152
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003153 len = MAY_LJMP(luaL_checkinteger(L, 2));
3154 l = MAY_LJMP(luaL_checkinteger(L, -1));
3155
3156 max = len - l;
Willy Tarreaua79021a2018-06-15 18:07:57 +02003157 if (max > ci_data(chn))
3158 max = ci_data(chn);
Willy Tarreau47860ed2015-03-10 14:07:50 +01003159 channel_forward(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003160 l += max;
3161
3162 lua_pop(L, 1);
3163 lua_pushinteger(L, l);
3164
3165 /* Check if it miss bytes to forward. */
3166 if (l < len) {
3167 /* The the input channel or the output channel are closed, we
3168 * must return the amount of data forwarded.
3169 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003170 if (channel_input_closed(chn) || channel_output_closed(chn))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003171 return 1;
3172
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003173 /* If we are waiting for space data in the response buffer, we
3174 * must set the flag WAKERESWR. This flag required the task
3175 * wake up if any activity is detected on the response buffer.
3176 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003177 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003178 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01003179 else
3180 HLUA_SET_WAKEREQWR(hlua);
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003181
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003182 /* Otherwise, we can yield waiting for new data in the inpout side. */
Willy Tarreau9635e032018-10-16 17:52:55 +02003183 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_forward_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003184 }
3185
3186 return 1;
3187}
3188
3189/* Just check the input and prepare the stack for the previous
3190 * function "hlua_channel_forward_yield"
3191 */
3192__LJMP static int hlua_channel_forward(lua_State *L)
3193{
3194 MAY_LJMP(check_args(L, 2, "forward"));
3195 MAY_LJMP(hlua_checkchannel(L, 1));
3196 MAY_LJMP(luaL_checkinteger(L, 2));
3197
3198 lua_pushinteger(L, 0);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003199 return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003200}
3201
3202/* Just returns the number of bytes available in the input
3203 * side of the buffer. This function never fails.
3204 */
3205__LJMP static int hlua_channel_get_in_len(lua_State *L)
3206{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003207 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003208
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003209 MAY_LJMP(check_args(L, 1, "get_in_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003210 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Fauleta3ceac12018-12-14 13:39:09 +01003211 if (IS_HTX_STRM(chn_strm(chn))) {
3212 struct htx *htx = htxbuf(&chn->buf);
3213 lua_pushinteger(L, htx->data - co_data(chn));
3214 }
3215 else
3216 lua_pushinteger(L, ci_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003217 return 1;
3218}
3219
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003220/* Returns true if the channel is full. */
3221__LJMP static int hlua_channel_is_full(lua_State *L)
3222{
3223 struct channel *chn;
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003224
3225 MAY_LJMP(check_args(L, 1, "is_full"));
3226 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Faulet0136ebb2020-02-26 11:59:19 +01003227 /* ignore the reserve, we are not on a producer side (ie in an
3228 * applet).
3229 */
3230 lua_pushboolean(L, channel_full(chn, 0));
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003231 return 1;
3232}
3233
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003234/* Just returns the number of bytes available in the output
3235 * side of the buffer. This function never fails.
3236 */
3237__LJMP static int hlua_channel_get_out_len(lua_State *L)
3238{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003239 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003240
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003241 MAY_LJMP(check_args(L, 1, "get_out_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003242 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Willy Tarreaua79021a2018-06-15 18:07:57 +02003243 lua_pushinteger(L, co_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003244 return 1;
3245}
3246
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003247/*
3248 *
3249 *
3250 * Class Fetches
3251 *
3252 *
3253 */
3254
3255/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003256 * a class stream, otherwise it throws an error.
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003257 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003258__LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003259{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003260 return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003261}
3262
3263/* This function creates and push in the stack a fetch object according
3264 * with a current TXN.
3265 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003266static int hlua_fetches_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003267{
Willy Tarreau7073c472015-04-06 11:15:40 +02003268 struct hlua_smp *hsmp;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003269
3270 /* Check stack size. */
3271 if (!lua_checkstack(L, 3))
3272 return 0;
3273
3274 /* Create the object: obj[0] = userdata.
3275 * Note that the base of the Fetches object is the
3276 * transaction object.
3277 */
3278 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003279 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003280 lua_rawseti(L, -2, 0);
3281
Willy Tarreau7073c472015-04-06 11:15:40 +02003282 hsmp->s = txn->s;
3283 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003284 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003285 hsmp->flags = flags;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003286
3287 /* Pop a class sesison metatable and affect it to the userdata. */
3288 lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref);
3289 lua_setmetatable(L, -2);
3290
3291 return 1;
3292}
3293
3294/* This function is an LUA binding. It is called with each sample-fetch.
3295 * It uses closure argument to store the associated sample-fetch. It
3296 * returns only one argument or throws an error. An error is thrown
3297 * only if an error is encountered during the argument parsing. If
3298 * the "sample-fetch" function fails, nil is returned.
3299 */
3300__LJMP static int hlua_run_sample_fetch(lua_State *L)
3301{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003302 struct hlua_smp *hsmp;
Willy Tarreau2ec22742015-03-10 14:27:20 +01003303 struct sample_fetch *f;
Frédéric Lécaillef874a832018-06-15 13:56:04 +02003304 struct arg args[ARGM_NBARGS + 1] = {{0}};
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003305 int i;
3306 struct sample smp;
3307
3308 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003309 f = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003310
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003311 /* Get traditional arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003312 hsmp = MAY_LJMP(hlua_checkfetches(L, 1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003313
Thierry FOURNIERca988662015-12-20 18:43:03 +01003314 /* Check execution authorization. */
3315 if (f->use & SMP_USE_HTTP_ANY &&
3316 !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) {
3317 lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which "
3318 "is not available in Lua services", f->kw);
3319 WILL_LJMP(lua_error(L));
3320 }
3321
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003322 /* Get extra arguments. */
3323 for (i = 0; i < lua_gettop(L) - 1; i++) {
3324 if (i >= ARGM_NBARGS)
3325 break;
3326 hlua_lua2arg(L, i + 2, &args[i]);
3327 }
3328 args[i].type = ARGT_STOP;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003329 args[i].data.str.area = NULL;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003330
3331 /* Check arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003332 MAY_LJMP(hlua_lua2arg_check(L, 2, args, f->arg_mask, hsmp->p));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003333
3334 /* Run the special args checker. */
Willy Tarreau2ec22742015-03-10 14:27:20 +01003335 if (f->val_args && !f->val_args(args, NULL)) {
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003336 lua_pushfstring(L, "error in arguments");
3337 WILL_LJMP(lua_error(L));
3338 }
3339
3340 /* Initialise the sample. */
3341 memset(&smp, 0, sizeof(smp));
3342
3343 /* Run the sample fetch process. */
Willy Tarreau1777ea62016-03-10 16:15:46 +01003344 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003345 if (!f->process(args, &smp, f->kw, f->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003346 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003347 lua_pushstring(L, "");
3348 else
3349 lua_pushnil(L);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003350 return 1;
3351 }
3352
3353 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003354 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003355 hlua_smp2lua_str(L, &smp);
3356 else
3357 hlua_smp2lua(L, &smp);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003358 return 1;
3359}
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003360
3361/*
3362 *
3363 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003364 * Class Converters
3365 *
3366 *
3367 */
3368
3369/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003370 * a class stream, otherwise it throws an error.
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003371 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003372__LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003373{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003374 return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003375}
3376
3377/* This function creates and push in the stack a Converters object
3378 * according with a current TXN.
3379 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003380static int hlua_converters_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003381{
Willy Tarreau7073c472015-04-06 11:15:40 +02003382 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003383
3384 /* Check stack size. */
3385 if (!lua_checkstack(L, 3))
3386 return 0;
3387
3388 /* Create the object: obj[0] = userdata.
3389 * Note that the base of the Converters object is the
3390 * same than the TXN object.
3391 */
3392 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003393 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003394 lua_rawseti(L, -2, 0);
3395
Willy Tarreau7073c472015-04-06 11:15:40 +02003396 hsmp->s = txn->s;
3397 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003398 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003399 hsmp->flags = flags;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003400
Willy Tarreau87b09662015-04-03 00:22:06 +02003401 /* Pop a class stream metatable and affect it to the table. */
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003402 lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref);
3403 lua_setmetatable(L, -2);
3404
3405 return 1;
3406}
3407
3408/* This function is an LUA binding. It is called with each converter.
3409 * It uses closure argument to store the associated converter. It
3410 * returns only one argument or throws an error. An error is thrown
3411 * only if an error is encountered during the argument parsing. If
3412 * the converter function function fails, nil is returned.
3413 */
3414__LJMP static int hlua_run_sample_conv(lua_State *L)
3415{
Willy Tarreauda5f1082015-04-06 11:17:13 +02003416 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003417 struct sample_conv *conv;
Frédéric Lécaillef874a832018-06-15 13:56:04 +02003418 struct arg args[ARGM_NBARGS + 1] = {{0}};
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003419 int i;
3420 struct sample smp;
3421
3422 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003423 conv = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003424
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003425 /* Get traditional arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003426 hsmp = MAY_LJMP(hlua_checkconverters(L, 1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003427
3428 /* Get extra arguments. */
3429 for (i = 0; i < lua_gettop(L) - 2; i++) {
3430 if (i >= ARGM_NBARGS)
3431 break;
3432 hlua_lua2arg(L, i + 3, &args[i]);
3433 }
3434 args[i].type = ARGT_STOP;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003435 args[i].data.str.area = NULL;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003436
3437 /* Check arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003438 MAY_LJMP(hlua_lua2arg_check(L, 3, args, conv->arg_mask, hsmp->p));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003439
3440 /* Run the special args checker. */
3441 if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) {
3442 hlua_pusherror(L, "error in arguments");
3443 WILL_LJMP(lua_error(L));
3444 }
3445
3446 /* Initialise the sample. */
3447 if (!hlua_lua2smp(L, 2, &smp)) {
3448 hlua_pusherror(L, "error in the input argument");
3449 WILL_LJMP(lua_error(L));
3450 }
3451
Willy Tarreau1777ea62016-03-10 16:15:46 +01003452 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
3453
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003454 /* Apply expected cast. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003455 if (!sample_casts[smp.data.type][conv->in_type]) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003456 hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'",
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003457 smp_to_type[smp.data.type], smp_to_type[conv->in_type]);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003458 WILL_LJMP(lua_error(L));
3459 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003460 if (sample_casts[smp.data.type][conv->in_type] != c_none &&
3461 !sample_casts[smp.data.type][conv->in_type](&smp)) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003462 hlua_pusherror(L, "error during the input argument casting");
3463 WILL_LJMP(lua_error(L));
3464 }
3465
3466 /* Run the sample conversion process. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003467 if (!conv->process(args, &smp, conv->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003468 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003469 lua_pushstring(L, "");
3470 else
Willy Tarreaua678b432015-08-28 10:14:59 +02003471 lua_pushnil(L);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003472 return 1;
3473 }
3474
3475 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003476 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003477 hlua_smp2lua_str(L, &smp);
3478 else
3479 hlua_smp2lua(L, &smp);
Willy Tarreaua678b432015-08-28 10:14:59 +02003480 return 1;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003481}
3482
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003483/*
3484 *
3485 *
3486 * Class AppletTCP
3487 *
3488 *
3489 */
3490
3491/* Returns a struct hlua_txn if the stack entry "ud" is
3492 * a class stream, otherwise it throws an error.
3493 */
3494__LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud)
3495{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003496 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003497}
3498
3499/* This function creates and push in the stack an Applet object
3500 * according with a current TXN.
3501 */
3502static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx)
3503{
3504 struct hlua_appctx *appctx;
3505 struct stream_interface *si = ctx->owner;
3506 struct stream *s = si_strm(si);
3507 struct proxy *p = s->be;
3508
3509 /* Check stack size. */
3510 if (!lua_checkstack(L, 3))
3511 return 0;
3512
3513 /* Create the object: obj[0] = userdata.
3514 * Note that the base of the Converters object is the
3515 * same than the TXN object.
3516 */
3517 lua_newtable(L);
3518 appctx = lua_newuserdata(L, sizeof(*appctx));
3519 lua_rawseti(L, -2, 0);
3520 appctx->appctx = ctx;
3521 appctx->htxn.s = s;
3522 appctx->htxn.p = p;
3523
3524 /* Create the "f" field that contains a list of fetches. */
3525 lua_pushstring(L, "f");
3526 if (!hlua_fetches_new(L, &appctx->htxn, 0))
3527 return 0;
3528 lua_settable(L, -3);
3529
3530 /* Create the "sf" field that contains a list of stringsafe fetches. */
3531 lua_pushstring(L, "sf");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003532 if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003533 return 0;
3534 lua_settable(L, -3);
3535
3536 /* Create the "c" field that contains a list of converters. */
3537 lua_pushstring(L, "c");
3538 if (!hlua_converters_new(L, &appctx->htxn, 0))
3539 return 0;
3540 lua_settable(L, -3);
3541
3542 /* Create the "sc" field that contains a list of stringsafe converters. */
3543 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003544 if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003545 return 0;
3546 lua_settable(L, -3);
3547
3548 /* Pop a class stream metatable and affect it to the table. */
3549 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref);
3550 lua_setmetatable(L, -2);
3551
3552 return 1;
3553}
3554
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003555__LJMP static int hlua_applet_tcp_set_var(lua_State *L)
3556{
3557 struct hlua_appctx *appctx;
3558 struct stream *s;
3559 const char *name;
3560 size_t len;
3561 struct sample smp;
3562
3563 MAY_LJMP(check_args(L, 3, "set_var"));
3564
3565 /* It is useles to retrieve the stream, but this function
3566 * runs only in a stream context.
3567 */
3568 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3569 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3570 s = appctx->htxn.s;
3571
3572 /* Converts the third argument in a sample. */
3573 hlua_lua2smp(L, 3, &smp);
3574
3575 /* Store the sample in a variable. */
3576 smp_set_owner(&smp, s->be, s->sess, s, 0);
3577 vars_set_by_name(name, len, &smp);
3578 return 0;
3579}
3580
3581__LJMP static int hlua_applet_tcp_unset_var(lua_State *L)
3582{
3583 struct hlua_appctx *appctx;
3584 struct stream *s;
3585 const char *name;
3586 size_t len;
3587 struct sample smp;
3588
3589 MAY_LJMP(check_args(L, 2, "unset_var"));
3590
3591 /* It is useles to retrieve the stream, but this function
3592 * runs only in a stream context.
3593 */
3594 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3595 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3596 s = appctx->htxn.s;
3597
3598 /* Unset the variable. */
3599 smp_set_owner(&smp, s->be, s->sess, s, 0);
3600 vars_unset_by_name(name, len, &smp);
3601 return 0;
3602}
3603
3604__LJMP static int hlua_applet_tcp_get_var(lua_State *L)
3605{
3606 struct hlua_appctx *appctx;
3607 struct stream *s;
3608 const char *name;
3609 size_t len;
3610 struct sample smp;
3611
3612 MAY_LJMP(check_args(L, 2, "get_var"));
3613
3614 /* It is useles to retrieve the stream, but this function
3615 * runs only in a stream context.
3616 */
3617 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3618 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3619 s = appctx->htxn.s;
3620
3621 smp_set_owner(&smp, s->be, s->sess, s, 0);
3622 if (!vars_get_by_name(name, len, &smp)) {
3623 lua_pushnil(L);
3624 return 1;
3625 }
3626
3627 return hlua_smp2lua(L, &smp);
3628}
3629
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003630__LJMP static int hlua_applet_tcp_set_priv(lua_State *L)
3631{
3632 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3633 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003634 struct hlua *hlua;
3635
3636 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003637 if (!s->hlua)
3638 return 0;
3639 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003640
3641 MAY_LJMP(check_args(L, 2, "set_priv"));
3642
3643 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02003644 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003645
3646 /* Get and store new value. */
3647 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
3648 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
3649
3650 return 0;
3651}
3652
3653__LJMP static int hlua_applet_tcp_get_priv(lua_State *L)
3654{
3655 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3656 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003657 struct hlua *hlua;
3658
3659 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003660 if (!s->hlua) {
3661 lua_pushnil(L);
3662 return 1;
3663 }
3664 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003665
3666 /* Push configuration index in the stack. */
3667 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
3668
3669 return 1;
3670}
3671
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003672/* If expected data not yet available, it returns a yield. This function
3673 * consumes the data in the buffer. It returns a string containing the
3674 * data. This string can be empty.
3675 */
3676__LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx)
3677{
3678 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3679 struct stream_interface *si = appctx->appctx->owner;
3680 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02003681 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003682 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02003683 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003684 size_t len2;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003685
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003686 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003687 ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003688
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003689 /* Data not yet available. return yield. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003690 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003691 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003692 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_getline_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003693 }
3694
3695 /* End of data: commit the total strings and return. */
3696 if (ret < 0) {
3697 luaL_pushresult(&appctx->b);
3698 return 1;
3699 }
3700
3701 /* Ensure that the block 2 length is usable. */
3702 if (ret == 1)
3703 len2 = 0;
3704
3705 /* dont check the max length read and dont check. */
3706 luaL_addlstring(&appctx->b, blk1, len1);
3707 luaL_addlstring(&appctx->b, blk2, len2);
3708
3709 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003710 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003711 luaL_pushresult(&appctx->b);
3712 return 1;
3713}
3714
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003715/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003716__LJMP static int hlua_applet_tcp_getline(lua_State *L)
3717{
3718 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3719
3720 /* Initialise the string catenation. */
3721 luaL_buffinit(L, &appctx->b);
3722
3723 return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0));
3724}
3725
3726/* If expected data not yet available, it returns a yield. This function
3727 * consumes the data in the buffer. It returns a string containing the
3728 * data. This string can be empty.
3729 */
3730__LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx)
3731{
3732 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3733 struct stream_interface *si = appctx->appctx->owner;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003734 size_t len = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003735 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02003736 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003737 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02003738 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003739 size_t len2;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003740
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003741 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003742 ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003743
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003744 /* Data not yet available. return yield. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003745 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003746 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003747 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003748 }
3749
3750 /* End of data: commit the total strings and return. */
3751 if (ret < 0) {
3752 luaL_pushresult(&appctx->b);
3753 return 1;
3754 }
3755
3756 /* Ensure that the block 2 length is usable. */
3757 if (ret == 1)
3758 len2 = 0;
3759
3760 if (len == -1) {
3761
3762 /* If len == -1, catenate all the data avalaile and
3763 * yield because we want to get all the data until
3764 * the end of data stream.
3765 */
3766 luaL_addlstring(&appctx->b, blk1, len1);
3767 luaL_addlstring(&appctx->b, blk2, len2);
Willy Tarreau06d80a92017-10-19 14:32:15 +02003768 co_skip(si_oc(si), len1 + len2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003769 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003770 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003771
3772 } else {
3773
3774 /* Copy the fisrt block caping to the length required. */
3775 if (len1 > len)
3776 len1 = len;
3777 luaL_addlstring(&appctx->b, blk1, len1);
3778 len -= len1;
3779
3780 /* Copy the second block. */
3781 if (len2 > len)
3782 len2 = len;
3783 luaL_addlstring(&appctx->b, blk2, len2);
3784 len -= len2;
3785
3786 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003787 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003788
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003789 /* If there is no other data available, yield waiting for new data. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003790 if (len > 0) {
3791 lua_pushinteger(L, len);
3792 lua_replace(L, 2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003793 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003794 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003795 }
3796
3797 /* return the result. */
3798 luaL_pushresult(&appctx->b);
3799 return 1;
3800 }
3801
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003802 /* we never execute this */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003803 hlua_pusherror(L, "Lua: internal error");
3804 WILL_LJMP(lua_error(L));
3805 return 0;
3806}
3807
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003808/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003809__LJMP static int hlua_applet_tcp_recv(lua_State *L)
3810{
3811 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3812 int len = -1;
3813
3814 if (lua_gettop(L) > 2)
3815 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
3816 if (lua_gettop(L) >= 2) {
3817 len = MAY_LJMP(luaL_checkinteger(L, 2));
3818 lua_pop(L, 1);
3819 }
3820
3821 /* Confirm or set the required length */
3822 lua_pushinteger(L, len);
3823
3824 /* Initialise the string catenation. */
3825 luaL_buffinit(L, &appctx->b);
3826
3827 return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0));
3828}
3829
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003830/* Append data in the output side of the buffer. This data is immediately
3831 * sent. The function returns the amount of data written. If the buffer
3832 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003833 * if the channel is closed.
3834 */
3835__LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx)
3836{
3837 size_t len;
3838 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3839 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
3840 int l = MAY_LJMP(luaL_checkinteger(L, 3));
3841 struct stream_interface *si = appctx->appctx->owner;
3842 struct channel *chn = si_ic(si);
3843 int max;
3844
3845 /* Get the max amount of data which can write as input in the channel. */
3846 max = channel_recv_max(chn);
3847 if (max > (len - l))
3848 max = len - l;
3849
3850 /* Copy data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003851 ci_putblk(chn, str + l, max);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003852
3853 /* update counters. */
3854 l += max;
3855 lua_pop(L, 1);
3856 lua_pushinteger(L, l);
3857
3858 /* If some data is not send, declares the situation to the
3859 * applet, and returns a yield.
3860 */
3861 if (l < len) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003862 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003863 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003864 }
3865
3866 return 1;
3867}
3868
3869/* Just a wraper of "hlua_applet_tcp_send_yield". This wrapper permits
3870 * yield the LUA process, and resume it without checking the
3871 * input arguments.
3872 */
3873__LJMP static int hlua_applet_tcp_send(lua_State *L)
3874{
3875 MAY_LJMP(check_args(L, 2, "send"));
3876 lua_pushinteger(L, 0);
3877
3878 return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0));
3879}
3880
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003881/*
3882 *
3883 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003884 * Class AppletHTTP
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003885 *
3886 *
3887 */
3888
3889/* Returns a struct hlua_txn if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003890 * a class stream, otherwise it throws an error.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003891 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003892__LJMP static struct hlua_appctx *hlua_checkapplet_http(lua_State *L, int ud)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003893{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003894 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003895}
3896
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003897/* This function creates and push in the stack an Applet object
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003898 * according with a current TXN.
3899 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003900static int hlua_applet_http_new(lua_State *L, struct appctx *ctx)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003901{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003902 struct hlua_appctx *appctx;
Thierry FOURNIER841475e2015-12-11 17:10:09 +01003903 struct hlua_txn htxn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003904 struct stream_interface *si = ctx->owner;
3905 struct stream *s = si_strm(si);
3906 struct proxy *px = s->be;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003907
3908 /* Check stack size. */
3909 if (!lua_checkstack(L, 3))
3910 return 0;
3911
3912 /* Create the object: obj[0] = userdata.
3913 * Note that the base of the Converters object is the
3914 * same than the TXN object.
3915 */
3916 lua_newtable(L);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003917 appctx = lua_newuserdata(L, sizeof(*appctx));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003918 lua_rawseti(L, -2, 0);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003919 appctx->appctx = ctx;
3920 appctx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */
Robin H. Johnson52f5db22017-01-01 13:10:52 -08003921 appctx->appctx->ctx.hlua_apphttp.reason = NULL; /* Use default reason based on status */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003922 appctx->htxn.s = s;
3923 appctx->htxn.p = px;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003924
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003925 /* Create the "f" field that contains a list of fetches. */
3926 lua_pushstring(L, "f");
3927 if (!hlua_fetches_new(L, &appctx->htxn, 0))
3928 return 0;
3929 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003930
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003931 /* Create the "sf" field that contains a list of stringsafe fetches. */
3932 lua_pushstring(L, "sf");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003933 if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003934 return 0;
3935 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003936
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003937 /* Create the "c" field that contains a list of converters. */
3938 lua_pushstring(L, "c");
3939 if (!hlua_converters_new(L, &appctx->htxn, 0))
3940 return 0;
3941 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003942
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003943 /* Create the "sc" field that contains a list of stringsafe converters. */
3944 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003945 if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003946 return 0;
3947 lua_settable(L, -3);
Willy Tarreaueee5b512015-04-03 23:46:31 +02003948
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003949 if (IS_HTX_STRM(s)) {
3950 /* HTX version */
3951 struct htx *htx = htxbuf(&s->req.buf);
Christopher Faulet29f17582019-05-23 11:03:26 +02003952 struct htx_blk *blk;
3953 struct htx_sl *sl;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003954 struct ist path;
3955 unsigned long long len = 0;
3956 int32_t pos;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003957
Christopher Faulet29f17582019-05-23 11:03:26 +02003958 blk = htx_get_first_blk(htx);
Christopher Faulet43a686d2019-11-18 15:50:25 +01003959 BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL);
Christopher Faulet29f17582019-05-23 11:03:26 +02003960 sl = htx_get_blk_ptr(htx, blk);
3961
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003962 /* Stores the request method. */
3963 lua_pushstring(L, "method");
3964 lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl));
3965 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003966
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003967 /* Stores the http version. */
3968 lua_pushstring(L, "version");
3969 lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl));
3970 lua_settable(L, -3);
Thierry FOURNIER841475e2015-12-11 17:10:09 +01003971
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003972 /* creates an array of headers. hlua_http_get_headers() crates and push
3973 * the array on the top of the stack.
3974 */
3975 lua_pushstring(L, "headers");
3976 htxn.s = s;
3977 htxn.p = px;
3978 htxn.dir = SMP_OPT_DIR_REQ;
3979 if (!hlua_http_get_headers(L, &htxn, &htxn.s->txn->req))
3980 return 0;
3981 lua_settable(L, -3);
3982
3983 path = http_get_path(htx_sl_req_uri(sl));
3984 if (path.ptr) {
3985 char *p, *q, *end;
3986
3987 p = path.ptr;
3988 end = path.ptr + path.len;
3989 q = p;
3990 while (q < end && *q != '?')
3991 q++;
3992
3993 /* Stores the request path. */
3994 lua_pushstring(L, "path");
3995 lua_pushlstring(L, p, q - p);
3996 lua_settable(L, -3);
3997
3998 /* Stores the query string. */
3999 lua_pushstring(L, "qs");
4000 if (*q == '?')
4001 q++;
4002 lua_pushlstring(L, q, end - q);
4003 lua_settable(L, -3);
4004 }
4005
Christopher Fauleta3f15502019-05-13 15:27:23 +02004006 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004007 struct htx_blk *blk = htx_get_blk(htx, pos);
4008 enum htx_blk_type type = htx_get_blk_type(blk);
4009
Christopher Faulet54b5e212019-06-04 10:08:28 +02004010 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004011 break;
4012 if (type == HTX_BLK_DATA)
4013 len += htx_get_blksz(blk);
4014 }
4015 if (htx->extra != ULLONG_MAX)
4016 len += htx->extra;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004017
Thierry FOURNIER7d388632017-02-22 02:06:16 +01004018 /* Stores the request path. */
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004019 lua_pushstring(L, "length");
4020 lua_pushinteger(L, len);
4021 lua_settable(L, -3);
4022 }
4023 else {
4024 /* Legacy HTTP version */
4025 struct http_txn *txn = s->txn;
4026 const char *path;
4027 const char *end;
4028 const char *p;
4029
4030 /* Stores the request method. */
4031 lua_pushstring(L, "method");
4032 lua_pushlstring(L, ci_head(txn->req.chn), txn->req.sl.rq.m_l);
Thierry FOURNIER7d388632017-02-22 02:06:16 +01004033 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004034
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004035 /* Stores the http version. */
4036 lua_pushstring(L, "version");
4037 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 +01004038 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01004039
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004040 /* creates an array of headers. hlua_http_get_headers() crates and push
4041 * the array on the top of the stack.
4042 */
4043 lua_pushstring(L, "headers");
4044 htxn.s = s;
4045 htxn.p = px;
4046 htxn.dir = SMP_OPT_DIR_REQ;
4047 if (!hlua_http_get_headers(L, &htxn, &htxn.s->txn->req))
4048 return 0;
4049 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01004050
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004051 /* Get path and qs */
4052 path = http_txn_get_path(txn);
4053 if (path) {
4054 end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
4055 p = path;
4056 while (p < end && *p != '?')
4057 p++;
4058
4059 /* Stores the request path. */
4060 lua_pushstring(L, "path");
4061 lua_pushlstring(L, path, p - path);
4062 lua_settable(L, -3);
4063
4064 /* Stores the query string. */
4065 lua_pushstring(L, "qs");
4066 if (*p == '?')
4067 p++;
4068 lua_pushlstring(L, p, end - p);
4069 lua_settable(L, -3);
4070 }
4071
4072 /* Stores the request path. */
4073 lua_pushstring(L, "length");
4074 lua_pushinteger(L, txn->req.body_len);
4075 lua_settable(L, -3);
4076 }
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01004077
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004078 /* Create an empty array of HTTP request headers. */
4079 lua_pushstring(L, "response");
4080 lua_newtable(L);
4081 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01004082
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004083 /* Pop a class stream metatable and affect it to the table. */
4084 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref);
4085 lua_setmetatable(L, -2);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004086
4087 return 1;
4088}
4089
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004090__LJMP static int hlua_applet_http_set_var(lua_State *L)
4091{
4092 struct hlua_appctx *appctx;
4093 struct stream *s;
4094 const char *name;
4095 size_t len;
4096 struct sample smp;
4097
4098 MAY_LJMP(check_args(L, 3, "set_var"));
4099
4100 /* It is useles to retrieve the stream, but this function
4101 * runs only in a stream context.
4102 */
4103 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4104 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4105 s = appctx->htxn.s;
4106
4107 /* Converts the third argument in a sample. */
4108 hlua_lua2smp(L, 3, &smp);
4109
4110 /* Store the sample in a variable. */
4111 smp_set_owner(&smp, s->be, s->sess, s, 0);
4112 vars_set_by_name(name, len, &smp);
4113 return 0;
4114}
4115
4116__LJMP static int hlua_applet_http_unset_var(lua_State *L)
4117{
4118 struct hlua_appctx *appctx;
4119 struct stream *s;
4120 const char *name;
4121 size_t len;
4122 struct sample smp;
4123
4124 MAY_LJMP(check_args(L, 2, "unset_var"));
4125
4126 /* It is useles to retrieve the stream, but this function
4127 * runs only in a stream context.
4128 */
4129 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4130 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4131 s = appctx->htxn.s;
4132
4133 /* Unset the variable. */
4134 smp_set_owner(&smp, s->be, s->sess, s, 0);
4135 vars_unset_by_name(name, len, &smp);
4136 return 0;
4137}
4138
4139__LJMP static int hlua_applet_http_get_var(lua_State *L)
4140{
4141 struct hlua_appctx *appctx;
4142 struct stream *s;
4143 const char *name;
4144 size_t len;
4145 struct sample smp;
4146
4147 MAY_LJMP(check_args(L, 2, "get_var"));
4148
4149 /* It is useles to retrieve the stream, but this function
4150 * runs only in a stream context.
4151 */
4152 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4153 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4154 s = appctx->htxn.s;
4155
4156 smp_set_owner(&smp, s->be, s->sess, s, 0);
4157 if (!vars_get_by_name(name, len, &smp)) {
4158 lua_pushnil(L);
4159 return 1;
4160 }
4161
4162 return hlua_smp2lua(L, &smp);
4163}
4164
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004165__LJMP static int hlua_applet_http_set_priv(lua_State *L)
4166{
4167 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4168 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004169 struct hlua *hlua;
4170
4171 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004172 if (!s->hlua)
4173 return 0;
4174 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004175
4176 MAY_LJMP(check_args(L, 2, "set_priv"));
4177
4178 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02004179 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004180
4181 /* Get and store new value. */
4182 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
4183 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
4184
4185 return 0;
4186}
4187
4188__LJMP static int hlua_applet_http_get_priv(lua_State *L)
4189{
4190 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4191 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004192 struct hlua *hlua;
4193
4194 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004195 if (!s->hlua) {
4196 lua_pushnil(L);
4197 return 1;
4198 }
4199 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004200
4201 /* Push configuration index in the stack. */
4202 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
4203
4204 return 1;
4205}
4206
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004207/* If expected data not yet available, it returns a yield. This function
4208 * consumes the data in the buffer. It returns a string containing the
4209 * data. This string can be empty.
4210 */
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004211__LJMP static int hlua_applet_htx_getline_yield(lua_State *L, int status, lua_KContext ctx)
4212{
4213 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4214 struct stream_interface *si = appctx->appctx->owner;
4215 struct channel *req = si_oc(si);
4216 struct htx *htx;
4217 struct htx_blk *blk;
4218 size_t count;
4219 int stop = 0;
4220
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004221 htx = htx_from_buf(&req->buf);
4222 count = co_data(req);
Christopher Fauleta3f15502019-05-13 15:27:23 +02004223 blk = htx_get_first_blk(htx);
Christopher Fauletcc26b132018-12-18 21:20:57 +01004224
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004225 while (count && !stop && blk) {
4226 enum htx_blk_type type = htx_get_blk_type(blk);
4227 uint32_t sz = htx_get_blksz(blk);
4228 struct ist v;
4229 uint32_t vlen;
4230 char *nl;
4231
Christopher Faulete461e342018-12-18 16:43:35 +01004232 if (type == HTX_BLK_EOM) {
4233 stop = 1;
4234 break;
4235 }
4236
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004237 vlen = sz;
4238 if (vlen > count) {
4239 if (type != HTX_BLK_DATA)
4240 break;
4241 vlen = count;
4242 }
4243
4244 switch (type) {
4245 case HTX_BLK_UNUSED:
4246 break;
4247
4248 case HTX_BLK_DATA:
4249 v = htx_get_blk_value(htx, blk);
4250 v.len = vlen;
4251 nl = istchr(v, '\n');
4252 if (nl != NULL) {
4253 stop = 1;
4254 vlen = nl - v.ptr + 1;
4255 }
4256 luaL_addlstring(&appctx->b, v.ptr, vlen);
4257 break;
4258
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004259 case HTX_BLK_TLR:
4260 case HTX_BLK_EOM:
4261 stop = 1;
4262 break;
4263
4264 default:
4265 break;
4266 }
4267
4268 co_set_data(req, co_data(req) - vlen);
4269 count -= vlen;
4270 if (sz == vlen)
4271 blk = htx_remove_blk(htx, blk);
4272 else {
4273 htx_cut_data_blk(htx, blk, vlen);
4274 break;
4275 }
4276 }
4277
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004278 htx_to_buf(htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004279 if (!stop) {
4280 si_cant_get(si);
4281 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_htx_getline_yield, TICK_ETERNITY, 0));
4282 }
4283
4284 /* return the result. */
4285 luaL_pushresult(&appctx->b);
4286 return 1;
4287}
4288
4289
4290/* If expected data not yet available, it returns a yield. This function
4291 * consumes the data in the buffer. It returns a string containing the
4292 * data. This string can be empty.
4293 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004294__LJMP static int hlua_applet_http_getline_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004295{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004296 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4297 struct stream_interface *si = appctx->appctx->owner;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004298 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02004299 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02004300 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02004301 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02004302 size_t len2;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004303
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004304 /* Check for the end of the data. */
4305 if (appctx->appctx->ctx.hlua_apphttp.left_bytes <= 0) {
4306 luaL_pushresult(&appctx->b);
4307 return 1;
4308 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004309
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004310 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004311 ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004312
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004313 /* Data not yet available. return yield. */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004314 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01004315 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004316 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_getline_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004317 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004318
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004319 /* End of data: commit the total strings and return. */
4320 if (ret < 0) {
4321 luaL_pushresult(&appctx->b);
4322 return 1;
4323 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004324
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004325 /* Ensure that the block 2 length is usable. */
4326 if (ret == 1)
4327 len2 = 0;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004328
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004329 /* Copy the fisrt block caping to the length required. */
4330 if (len1 > appctx->appctx->ctx.hlua_apphttp.left_bytes)
4331 len1 = appctx->appctx->ctx.hlua_apphttp.left_bytes;
4332 luaL_addlstring(&appctx->b, blk1, len1);
4333 appctx->appctx->ctx.hlua_apphttp.left_bytes -= len1;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004334
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004335 /* Copy the second block. */
4336 if (len2 > appctx->appctx->ctx.hlua_apphttp.left_bytes)
4337 len2 = appctx->appctx->ctx.hlua_apphttp.left_bytes;
4338 luaL_addlstring(&appctx->b, blk2, len2);
4339 appctx->appctx->ctx.hlua_apphttp.left_bytes -= len2;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004340
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004341 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004342 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004343 luaL_pushresult(&appctx->b);
4344 return 1;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004345}
4346
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004347/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004348__LJMP static int hlua_applet_http_getline(lua_State *L)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004349{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004350 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004351
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004352 /* Initialise the string catenation. */
4353 luaL_buffinit(L, &appctx->b);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004354
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004355 if (IS_HTX_STRM(si_strm(appctx->appctx->owner)))
4356 return MAY_LJMP(hlua_applet_htx_getline_yield(L, 0, 0));
4357 else
4358 return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004359}
4360
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004361/* If expected data not yet available, it returns a yield. This function
4362 * consumes the data in the buffer. It returns a string containing the
4363 * data. This string can be empty.
4364 */
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004365__LJMP static int hlua_applet_htx_recv_yield(lua_State *L, int status, lua_KContext ctx)
4366{
4367 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4368 struct stream_interface *si = appctx->appctx->owner;
4369 struct channel *req = si_oc(si);
4370 struct htx *htx;
4371 struct htx_blk *blk;
4372 size_t count;
4373 int len;
4374
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004375 htx = htx_from_buf(&req->buf);
4376 len = MAY_LJMP(luaL_checkinteger(L, 2));
4377 count = co_data(req);
Christopher Faulet29f17582019-05-23 11:03:26 +02004378 blk = htx_get_head_blk(htx);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004379 while (count && len && blk) {
4380 enum htx_blk_type type = htx_get_blk_type(blk);
4381 uint32_t sz = htx_get_blksz(blk);
4382 struct ist v;
4383 uint32_t vlen;
4384
Christopher Faulete461e342018-12-18 16:43:35 +01004385 if (type == HTX_BLK_EOM) {
4386 len = 0;
4387 break;
4388 }
4389
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004390 vlen = sz;
4391 if (len > 0 && vlen > len)
4392 vlen = len;
4393 if (vlen > count) {
4394 if (type != HTX_BLK_DATA)
4395 break;
4396 vlen = count;
4397 }
4398
4399 switch (type) {
4400 case HTX_BLK_UNUSED:
4401 break;
4402
4403 case HTX_BLK_DATA:
4404 v = htx_get_blk_value(htx, blk);
4405 luaL_addlstring(&appctx->b, v.ptr, vlen);
4406 break;
4407
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004408 case HTX_BLK_TLR:
4409 case HTX_BLK_EOM:
4410 len = 0;
4411 break;
4412
4413 default:
4414 break;
4415 }
4416
4417 co_set_data(req, co_data(req) - vlen);
4418 count -= vlen;
4419 if (len > 0)
4420 len -= vlen;
4421 if (sz == vlen)
4422 blk = htx_remove_blk(htx, blk);
4423 else {
4424 htx_cut_data_blk(htx, blk, vlen);
4425 break;
4426 }
4427 }
4428
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004429 htx_to_buf(htx, &req->buf);
4430
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004431 /* If we are no other data available, yield waiting for new data. */
4432 if (len) {
4433 if (len > 0) {
4434 lua_pushinteger(L, len);
4435 lua_replace(L, 2);
4436 }
4437 si_cant_get(si);
4438 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_htx_recv_yield, TICK_ETERNITY, 0));
4439 }
4440
4441 /* return the result. */
4442 luaL_pushresult(&appctx->b);
4443 return 1;
4444}
4445
4446/* If expected data not yet available, it returns a yield. This function
4447 * consumes the data in the buffer. It returns a string containing the
4448 * data. This string can be empty.
4449 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004450__LJMP static int hlua_applet_http_recv_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004451{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004452 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4453 struct stream_interface *si = appctx->appctx->owner;
4454 int len = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004455 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02004456 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02004457 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02004458 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02004459 size_t len2;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004460
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004461 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004462 ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004463
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004464 /* Data not yet available. return yield. */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004465 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01004466 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004467 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004468 }
4469
4470 /* End of data: commit the total strings and return. */
4471 if (ret < 0) {
4472 luaL_pushresult(&appctx->b);
4473 return 1;
4474 }
4475
4476 /* Ensure that the block 2 length is usable. */
4477 if (ret == 1)
4478 len2 = 0;
4479
4480 /* Copy the fisrt block caping to the length required. */
4481 if (len1 > len)
4482 len1 = len;
4483 luaL_addlstring(&appctx->b, blk1, len1);
4484 len -= len1;
4485
4486 /* Copy the second block. */
4487 if (len2 > len)
4488 len2 = len;
4489 luaL_addlstring(&appctx->b, blk2, len2);
4490 len -= len2;
4491
4492 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004493 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004494 if (appctx->appctx->ctx.hlua_apphttp.left_bytes != -1)
4495 appctx->appctx->ctx.hlua_apphttp.left_bytes -= len;
4496
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004497 /* If we are no other data available, yield waiting for new data. */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004498 if (len > 0) {
4499 lua_pushinteger(L, len);
4500 lua_replace(L, 2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01004501 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004502 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004503 }
4504
4505 /* return the result. */
4506 luaL_pushresult(&appctx->b);
4507 return 1;
4508}
4509
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004510/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004511__LJMP static int hlua_applet_http_recv(lua_State *L)
4512{
4513 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4514 int len = -1;
4515
4516 /* Check arguments. */
4517 if (lua_gettop(L) > 2)
4518 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
4519 if (lua_gettop(L) >= 2) {
4520 len = MAY_LJMP(luaL_checkinteger(L, 2));
4521 lua_pop(L, 1);
4522 }
4523
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004524 if (IS_HTX_STRM(si_strm(appctx->appctx->owner))) {
4525 /* HTX version */
4526 lua_pushinteger(L, len);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004527
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004528 /* Initialise the string catenation. */
4529 luaL_buffinit(L, &appctx->b);
4530
4531 return MAY_LJMP(hlua_applet_htx_recv_yield(L, 0, 0));
4532 }
4533 else {
4534 /* Legacy HTTP version */
4535 /* Check the required length */
4536 if (len == -1 || len > appctx->appctx->ctx.hlua_apphttp.left_bytes)
4537 len = appctx->appctx->ctx.hlua_apphttp.left_bytes;
4538 lua_pushinteger(L, len);
4539
4540 /* Initialise the string catenation. */
4541 luaL_buffinit(L, &appctx->b);
4542
4543 return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0));
4544 }
4545}
4546
4547/* Append data in the output side of the buffer. This data is immediately
4548 * sent. The function returns the amount of data written. If the buffer
4549 * cannot contain the data, the function yields. The function returns -1
4550 * if the channel is closed.
4551 */
4552__LJMP static int hlua_applet_htx_send_yield(lua_State *L, int status, lua_KContext ctx)
4553{
4554 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4555 struct stream_interface *si = appctx->appctx->owner;
4556 struct channel *res = si_ic(si);
4557 struct htx *htx = htx_from_buf(&res->buf);
4558 const char *data;
4559 size_t len;
4560 int l = MAY_LJMP(luaL_checkinteger(L, 3));
4561 int max;
4562
Christopher Faulet4e963012019-07-03 11:39:30 +02004563 max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx));
Christopher Faulet4b0e9b22019-01-09 12:16:58 +01004564 if (!max)
4565 goto snd_yield;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004566
4567 data = MAY_LJMP(luaL_checklstring(L, 2, &len));
4568
4569 /* Get the max amount of data which can write as input in the channel. */
4570 if (max > (len - l))
4571 max = len - l;
4572
4573 /* Copy data. */
Willy Tarreau0a7ef022019-05-28 10:30:11 +02004574 max = htx_add_data(htx, ist2(data + l, max));
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01004575 channel_add_input(res, max);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004576
4577 /* update counters. */
4578 l += max;
4579 lua_pop(L, 1);
4580 lua_pushinteger(L, l);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004581
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004582 /* If some data is not send, declares the situation to the
4583 * applet, and returns a yield.
4584 */
4585 if (l < len) {
Christopher Faulet4b0e9b22019-01-09 12:16:58 +01004586 snd_yield:
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004587 htx_to_buf(htx, &res->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004588 si_rx_room_blk(si);
4589 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_htx_send_yield, TICK_ETERNITY, 0));
4590 }
4591
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004592 htx_to_buf(htx, &res->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004593 return 1;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004594}
4595
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004596/* Append data in the output side of the buffer. This data is immediately
4597 * sent. The function returns the amount of data written. If the buffer
4598 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004599 * if the channel is closed.
4600 */
4601__LJMP static int hlua_applet_http_send_yield(lua_State *L, int status, lua_KContext ctx)
4602{
4603 size_t len;
4604 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4605 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
4606 int l = MAY_LJMP(luaL_checkinteger(L, 3));
4607 struct stream_interface *si = appctx->appctx->owner;
4608 struct channel *chn = si_ic(si);
4609 int max;
4610
4611 /* Get the max amount of data which can write as input in the channel. */
4612 max = channel_recv_max(chn);
4613 if (max > (len - l))
4614 max = len - l;
4615
4616 /* Copy data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004617 ci_putblk(chn, str + l, max);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004618
4619 /* update counters. */
4620 l += max;
4621 lua_pop(L, 1);
4622 lua_pushinteger(L, l);
4623
4624 /* If some data is not send, declares the situation to the
4625 * applet, and returns a yield.
4626 */
4627 if (l < len) {
Willy Tarreaudb398432018-11-15 11:08:52 +01004628 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004629 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004630 }
4631
4632 return 1;
4633}
4634
4635/* Just a wraper of "hlua_applet_send_yield". This wrapper permits
4636 * yield the LUA process, and resume it without checking the
4637 * input arguments.
4638 */
4639__LJMP static int hlua_applet_http_send(lua_State *L)
4640{
4641 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004642
4643 /* We want to send some data. Headers must be sent. */
4644 if (!(appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
4645 hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data.");
4646 WILL_LJMP(lua_error(L));
4647 }
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004648
4649 if (IS_HTX_STRM(si_strm(appctx->appctx->owner))) {
4650 /* HTX version */
4651 /* This interger is used for followinf the amount of data sent. */
4652 lua_pushinteger(L, 0);
4653
4654 return MAY_LJMP(hlua_applet_htx_send_yield(L, 0, 0));
4655 }
4656 else {
4657 /* Legacy HTTP version */
4658 size_t len;
4659 char hex[10];
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004660
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004661 MAY_LJMP(luaL_checklstring(L, 2, &len));
4662
4663 /* If transfer encoding chunked is selected, we surround the data
4664 * by chunk data.
4665 */
4666 if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_CHUNKED) {
4667 snprintf(hex, 9, "%x", (unsigned int)len);
4668 lua_pushfstring(L, "%s\r\n", hex);
4669 lua_insert(L, 2); /* swap the last 2 entries. */
4670 lua_pushstring(L, "\r\n");
4671 lua_concat(L, 3);
4672 }
4673
4674 /* This interger is used for followinf the amount of data sent. */
4675 lua_pushinteger(L, 0);
4676
4677 return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0));
4678 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004679}
4680
4681__LJMP static int hlua_applet_http_addheader(lua_State *L)
4682{
4683 const char *name;
4684 int ret;
4685
4686 MAY_LJMP(hlua_checkapplet_http(L, 1));
4687 name = MAY_LJMP(luaL_checkstring(L, 2));
4688 MAY_LJMP(luaL_checkstring(L, 3));
4689
4690 /* Push in the stack the "response" entry. */
4691 ret = lua_getfield(L, 1, "response");
4692 if (ret != LUA_TTABLE) {
4693 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] "
4694 "is expected as an array. %s found", lua_typename(L, ret));
4695 WILL_LJMP(lua_error(L));
4696 }
4697
4698 /* check if the header is already registered if it is not
4699 * the case, register it.
4700 */
4701 ret = lua_getfield(L, -1, name);
4702 if (ret == LUA_TNIL) {
4703
4704 /* Entry not found. */
4705 lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */
4706
4707 /* Insert the new header name in the array in the top of the stack.
4708 * It left the new array in the top of the stack.
4709 */
4710 lua_newtable(L);
4711 lua_pushvalue(L, 2);
4712 lua_pushvalue(L, -2);
4713 lua_settable(L, -4);
4714
4715 } else if (ret != LUA_TTABLE) {
4716
4717 /* corruption error. */
4718 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] "
4719 "is expected as an array. %s found", name, lua_typename(L, ret));
4720 WILL_LJMP(lua_error(L));
4721 }
4722
4723 /* Now the top od thestack is an array of values. We push
4724 * the header value as new entry.
4725 */
4726 lua_pushvalue(L, 3);
4727 ret = lua_rawlen(L, -2);
4728 lua_rawseti(L, -2, ret + 1);
4729 lua_pushboolean(L, 1);
4730 return 1;
4731}
4732
4733__LJMP static int hlua_applet_http_status(lua_State *L)
4734{
4735 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4736 int status = MAY_LJMP(luaL_checkinteger(L, 2));
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004737 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004738
4739 if (status < 100 || status > 599) {
4740 lua_pushboolean(L, 0);
4741 return 1;
4742 }
4743
4744 appctx->appctx->ctx.hlua_apphttp.status = status;
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004745 appctx->appctx->ctx.hlua_apphttp.reason = reason;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004746 lua_pushboolean(L, 1);
4747 return 1;
4748}
4749
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004750
4751__LJMP static int hlua_applet_htx_send_response(lua_State *L)
4752{
4753 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4754 struct stream_interface *si = appctx->appctx->owner;
4755 struct channel *res = si_ic(si);
4756 struct htx *htx;
4757 struct htx_sl *sl;
4758 struct h1m h1m;
4759 const char *status, *reason;
4760 const char *name, *value;
4761 size_t nlen, vlen;
4762 unsigned int flags;
4763
4764 /* Send the message at once. */
4765 htx = htx_from_buf(&res->buf);
4766 h1m_init_res(&h1m);
4767
4768 /* Use the same http version than the request. */
4769 status = ultoa_r(appctx->appctx->ctx.hlua_apphttp.status, trash.area, trash.size);
4770 reason = appctx->appctx->ctx.hlua_apphttp.reason;
4771 if (reason == NULL)
4772 reason = http_get_reason(appctx->appctx->ctx.hlua_apphttp.status);
4773 if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) {
4774 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11);
4775 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason));
4776 }
4777 else {
4778 flags = HTX_SL_F_IS_RESP;
4779 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason));
4780 }
4781 if (!sl) {
4782 hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n",
4783 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4784 WILL_LJMP(lua_error(L));
4785 }
4786 sl->info.res.status = appctx->appctx->ctx.hlua_apphttp.status;
4787
4788 /* Get the array associated to the field "response" in the object AppletHTTP. */
4789 lua_pushvalue(L, 0);
4790 if (lua_getfield(L, 1, "response") != LUA_TTABLE) {
4791 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n",
4792 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4793 WILL_LJMP(lua_error(L));
4794 }
4795
4796 /* Browse the list of headers. */
4797 lua_pushnil(L);
4798 while(lua_next(L, -2) != 0) {
4799 /* We expect a string as -2. */
4800 if (lua_type(L, -2) != LUA_TSTRING) {
4801 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n",
4802 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4803 lua_typename(L, lua_type(L, -2)));
4804 WILL_LJMP(lua_error(L));
4805 }
4806 name = lua_tolstring(L, -2, &nlen);
4807
4808 /* We expect an array as -1. */
4809 if (lua_type(L, -1) != LUA_TTABLE) {
4810 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n",
4811 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4812 name,
4813 lua_typename(L, lua_type(L, -1)));
4814 WILL_LJMP(lua_error(L));
4815 }
4816
4817 /* Browse the table who is on the top of the stack. */
4818 lua_pushnil(L);
4819 while(lua_next(L, -2) != 0) {
4820 int id;
4821
4822 /* We expect a number as -2. */
4823 if (lua_type(L, -2) != LUA_TNUMBER) {
4824 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n",
4825 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4826 name,
4827 lua_typename(L, lua_type(L, -2)));
4828 WILL_LJMP(lua_error(L));
4829 }
4830 id = lua_tointeger(L, -2);
4831
4832 /* We expect a string as -2. */
4833 if (lua_type(L, -1) != LUA_TSTRING) {
4834 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n",
4835 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4836 name, id,
4837 lua_typename(L, lua_type(L, -1)));
4838 WILL_LJMP(lua_error(L));
4839 }
4840 value = lua_tolstring(L, -1, &vlen);
4841
4842 /* Simple Protocol checks. */
4843 if (isteqi(ist2(name, nlen), ist("transfer-encoding")))
4844 h1_parse_xfer_enc_header(&h1m, ist2(name, nlen));
4845 else if (isteqi(ist2(name, nlen), ist("content-length"))) {
4846 struct ist v = ist2(value, vlen);
4847 int ret;
4848
4849 ret = h1_parse_cont_len_header(&h1m, &v);
4850 if (ret < 0) {
4851 hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n",
4852 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4853 name);
4854 WILL_LJMP(lua_error(L));
4855 }
4856 else if (ret == 0)
4857 goto next; /* Skip it */
4858 }
4859
4860 /* Add a new header */
4861 if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) {
4862 hlua_pusherror(L, "Lua applet http '%s': Failed to add header '%s' in the response.\n",
4863 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4864 name);
4865 WILL_LJMP(lua_error(L));
4866 }
4867 next:
4868 /* Remove the array from the stack, and get next element with a remaining string. */
4869 lua_pop(L, 1);
4870 }
4871
4872 /* Remove the array from the stack, and get next element with a remaining string. */
4873 lua_pop(L, 1);
4874 }
4875
4876 if (h1m.flags & H1_MF_CHNK)
4877 h1m.flags &= ~H1_MF_CLEN;
4878 if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK))
4879 h1m.flags |= H1_MF_XFER_LEN;
4880
4881 /* Uset HTX start-line flags */
4882 if (h1m.flags & H1_MF_XFER_ENC)
4883 flags |= HTX_SL_F_XFER_ENC;
4884 if (h1m.flags & H1_MF_XFER_LEN) {
4885 flags |= HTX_SL_F_XFER_LEN;
4886 if (h1m.flags & H1_MF_CHNK)
4887 flags |= HTX_SL_F_CHNK;
4888 else if (h1m.flags & H1_MF_CLEN)
4889 flags |= HTX_SL_F_CLEN;
4890 if (h1m.body_len == 0)
4891 flags |= HTX_SL_F_BODYLESS;
4892 }
4893 sl->flags |= flags;
4894
4895 /* If we dont have a content-length set, and the HTTP version is 1.1
4896 * and the status code implies the presence of a message body, we must
4897 * announce a transfer encoding chunked. This is required by haproxy
4898 * for the keepalive compliance. If the applet annouces a transfer-encoding
4899 * chunked itslef, don't do anything.
4900 */
4901 if ((flags & (HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN)) == HTX_SL_F_VER_11 &&
4902 appctx->appctx->ctx.hlua_apphttp.status >= 200 &&
4903 appctx->appctx->ctx.hlua_apphttp.status != 204 &&
4904 appctx->appctx->ctx.hlua_apphttp.status != 304) {
4905 /* Add a new header */
4906 sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN);
4907 if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) {
4908 hlua_pusherror(L, "Lua applet http '%s': Failed to add header 'transfer-encoding' in the response.\n",
4909 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4910 WILL_LJMP(lua_error(L));
4911 }
4912 }
4913
4914 /* Finalize headers. */
4915 if (!htx_add_endof(htx, HTX_BLK_EOH)) {
4916 hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n",
4917 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4918 WILL_LJMP(lua_error(L));
4919 }
4920
4921 if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) {
4922 b_reset(&res->buf);
4923 hlua_pusherror(L, "Lua: 'start_response': response header block too big");
4924 WILL_LJMP(lua_error(L));
4925 }
4926
4927 htx_to_buf(htx, &res->buf);
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01004928 channel_add_input(res, htx->data);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004929
4930 /* Headers sent, set the flag. */
4931 appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT;
4932 return 0;
4933
4934}
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004935/* We will build the status line and the headers of the HTTP response.
4936 * We will try send at once if its not possible, we give back the hand
4937 * waiting for more room.
4938 */
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004939__LJMP static int hlua_applet_htx_start_response_yield(lua_State *L, int status, lua_KContext ctx)
4940{
4941 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4942 struct stream_interface *si = appctx->appctx->owner;
4943 struct channel *res = si_ic(si);
4944
4945 if (co_data(res)) {
4946 si_rx_room_blk(si);
4947 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_htx_start_response_yield, TICK_ETERNITY, 0));
4948 }
4949 return MAY_LJMP(hlua_applet_htx_send_response(L));
4950}
4951
4952
4953__LJMP static int hlua_applet_htx_start_response(lua_State *L)
4954{
4955 return MAY_LJMP(hlua_applet_htx_start_response_yield(L, 0, 0));
4956}
4957
4958/* We will build the status line and the headers of the HTTP response.
4959 * We will try send at once if its not possible, we give back the hand
4960 * waiting for more room.
4961 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004962__LJMP static int hlua_applet_http_start_response_yield(lua_State *L, int status, lua_KContext ctx)
4963{
4964 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4965 struct stream_interface *si = appctx->appctx->owner;
4966 struct channel *chn = si_ic(si);
4967 int ret;
4968 size_t len;
4969 const char *msg;
4970
4971 /* Get the message as the first argument on the stack. */
4972 msg = MAY_LJMP(luaL_checklstring(L, 2, &len));
4973
4974 /* Send the message at once. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004975 ret = ci_putblk(chn, msg, len);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004976
4977 /* if ret == -2 or -3 the channel closed or the message si too
4978 * big for the buffers.
4979 */
4980 if (ret == -2 || ret == -3) {
4981 hlua_pusherror(L, "Lua: 'start_response': response header block too big");
4982 WILL_LJMP(lua_error(L));
4983 }
4984
4985 /* If ret is -1, we dont have room in the buffer, so we yield. */
4986 if (ret == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01004987 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004988 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_start_response_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004989 }
4990
4991 /* Headers sent, set the flag. */
4992 appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT;
4993 return 0;
4994}
4995
4996__LJMP static int hlua_applet_http_start_response(lua_State *L)
4997{
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004998 struct buffer *tmp;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004999 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005000 const char *name;
Willy Tarreaua3294632017-08-23 11:24:47 +02005001 size_t name_len;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005002 const char *value;
Willy Tarreaua3294632017-08-23 11:24:47 +02005003 size_t value_len;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005004 int id;
Willy Tarreauc9f4ea02017-08-23 09:32:06 +02005005 long long hdr_contentlength = -1;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005006 int hdr_chunked = 0;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005007 const char *reason;
5008
5009 if (IS_HTX_STRM(si_strm(appctx->appctx->owner)))
5010 return MAY_LJMP(hlua_applet_htx_start_response(L));
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005011
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005012 reason = appctx->appctx->ctx.hlua_apphttp.reason;
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005013 if (reason == NULL)
Willy Tarreau04f1e2d2018-09-10 18:04:24 +02005014 reason = http_get_reason(appctx->appctx->ctx.hlua_apphttp.status);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005015
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005016 tmp = get_trash_chunk();
5017
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005018 /* Use the same http version than the request. */
5019 chunk_appendf(tmp, "HTTP/1.%c %d %s\r\n",
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +01005020 appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11 ? '1' : '0',
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005021 appctx->appctx->ctx.hlua_apphttp.status,
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005022 reason);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005023
5024 /* Get the array associated to the field "response" in the object AppletHTTP. */
5025 lua_pushvalue(L, 0);
5026 if (lua_getfield(L, 1, "response") != LUA_TTABLE) {
5027 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n",
5028 appctx->appctx->rule->arg.hlua_rule->fcn.name);
5029 WILL_LJMP(lua_error(L));
5030 }
5031
5032 /* Browse the list of headers. */
5033 lua_pushnil(L);
5034 while(lua_next(L, -2) != 0) {
5035
5036 /* We expect a string as -2. */
5037 if (lua_type(L, -2) != LUA_TSTRING) {
5038 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n",
5039 appctx->appctx->rule->arg.hlua_rule->fcn.name,
5040 lua_typename(L, lua_type(L, -2)));
5041 WILL_LJMP(lua_error(L));
5042 }
Willy Tarreaua3294632017-08-23 11:24:47 +02005043 name = lua_tolstring(L, -2, &name_len);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005044
5045 /* We expect an array as -1. */
5046 if (lua_type(L, -1) != LUA_TTABLE) {
5047 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n",
5048 appctx->appctx->rule->arg.hlua_rule->fcn.name,
5049 name,
5050 lua_typename(L, lua_type(L, -1)));
5051 WILL_LJMP(lua_error(L));
5052 }
5053
5054 /* Browse the table who is on the top of the stack. */
5055 lua_pushnil(L);
5056 while(lua_next(L, -2) != 0) {
5057
5058 /* We expect a number as -2. */
5059 if (lua_type(L, -2) != LUA_TNUMBER) {
5060 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n",
5061 appctx->appctx->rule->arg.hlua_rule->fcn.name,
5062 name,
5063 lua_typename(L, lua_type(L, -2)));
5064 WILL_LJMP(lua_error(L));
5065 }
5066 id = lua_tointeger(L, -2);
5067
5068 /* We expect a string as -2. */
5069 if (lua_type(L, -1) != LUA_TSTRING) {
5070 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n",
5071 appctx->appctx->rule->arg.hlua_rule->fcn.name,
5072 name, id,
5073 lua_typename(L, lua_type(L, -1)));
5074 WILL_LJMP(lua_error(L));
5075 }
Willy Tarreaua3294632017-08-23 11:24:47 +02005076 value = lua_tolstring(L, -1, &value_len);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005077
5078 /* Catenate a new header. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005079 if (tmp->data + name_len + 2 + value_len + 2 < tmp->size) {
5080 memcpy(tmp->area + tmp->data, name, name_len);
5081 tmp->data += name_len;
5082 tmp->area[tmp->data++] = ':';
5083 tmp->area[tmp->data++] = ' ';
Willy Tarreaua3294632017-08-23 11:24:47 +02005084
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005085 memcpy(tmp->area + tmp->data, value,
5086 value_len);
5087 tmp->data += value_len;
5088 tmp->area[tmp->data++] = '\r';
5089 tmp->area[tmp->data++] = '\n';
Willy Tarreaua3294632017-08-23 11:24:47 +02005090 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005091
5092 /* Protocol checks. */
5093
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005094 /* Copy the header content length. The length conversion
Willy Tarreauc9f4ea02017-08-23 09:32:06 +02005095 * is done without control. If it contains a bad value,
5096 * the content-length remains negative so that we can
5097 * switch to either chunked encoding or close.
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005098 */
Willy Tarreaua3294632017-08-23 11:24:47 +02005099 if (name_len == 14 && strcasecmp("content-length", name) == 0)
Willy Tarreauc9f4ea02017-08-23 09:32:06 +02005100 strl2llrc(value, strlen(value), &hdr_contentlength);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005101
5102 /* Check if the client annouces a transfer-encoding chunked it self. */
Willy Tarreaua3294632017-08-23 11:24:47 +02005103 if (name_len == 17 && value_len == 7 &&
5104 strcasecmp("transfer-encoding", name) == 0 &&
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005105 strcasecmp("chunked", value) == 0)
5106 hdr_chunked = 1;
5107
5108 /* Remove the array from the stack, and get next element with a remaining string. */
5109 lua_pop(L, 1);
5110 }
5111
5112 /* Remove the array from the stack, and get next element with a remaining string. */
5113 lua_pop(L, 1);
5114 }
5115
Willy Tarreau06c75fe2017-08-23 09:10:38 +02005116 /* If we dont have a content-length set, and the HTTP version is 1.1
5117 * and the status code implies the presence of a message body, we must
5118 * announce a transfer encoding chunked. This is required by haproxy
5119 * for the keepalive compliance. If the applet annouces a transfer-encoding
5120 * chunked itslef, don't do anything.
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005121 */
Willy Tarreauc9f4ea02017-08-23 09:32:06 +02005122 if (hdr_contentlength < 0 && hdr_chunked == 0 &&
Willy Tarreau06c75fe2017-08-23 09:10:38 +02005123 (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) &&
5124 appctx->appctx->ctx.hlua_apphttp.status >= 200 &&
5125 appctx->appctx->ctx.hlua_apphttp.status != 204 &&
5126 appctx->appctx->ctx.hlua_apphttp.status != 304) {
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005127 chunk_appendf(tmp, "Transfer-encoding: chunked\r\n");
5128 appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_CHUNKED;
5129 }
5130
5131 /* Finalize headers. */
5132 chunk_appendf(tmp, "\r\n");
5133
5134 /* Remove the last entry and the array of headers */
5135 lua_pop(L, 2);
5136
5137 /* Push the headers block. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005138 lua_pushlstring(L, tmp->area, tmp->data);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005139
5140 return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0));
5141}
5142
5143/*
5144 *
5145 *
5146 * Class HTTP
5147 *
5148 *
5149 */
5150
5151/* Returns a struct hlua_txn if the stack entry "ud" is
5152 * a class stream, otherwise it throws an error.
5153 */
5154__LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud)
5155{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02005156 return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005157}
5158
5159/* This function creates and push in the stack a HTTP object
5160 * according with a current TXN.
5161 */
5162static int hlua_http_new(lua_State *L, struct hlua_txn *txn)
5163{
5164 struct hlua_txn *htxn;
5165
5166 /* Check stack size. */
5167 if (!lua_checkstack(L, 3))
5168 return 0;
5169
5170 /* Create the object: obj[0] = userdata.
5171 * Note that the base of the Converters object is the
5172 * same than the TXN object.
5173 */
5174 lua_newtable(L);
5175 htxn = lua_newuserdata(L, sizeof(*htxn));
5176 lua_rawseti(L, -2, 0);
5177
5178 htxn->s = txn->s;
5179 htxn->p = txn->p;
Christopher Faulet256b69a2019-05-23 11:14:21 +02005180 htxn->dir = txn->dir;
5181 htxn->flags = txn->flags;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005182
5183 /* Pop a class stream metatable and affect it to the table. */
5184 lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref);
5185 lua_setmetatable(L, -2);
5186
5187 return 1;
5188}
5189
5190/* This function creates ans returns an array of HTTP headers.
5191 * This function does not fails. It is used as wrapper with the
5192 * 2 following functions.
5193 */
5194__LJMP static int hlua_http_get_headers(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg)
5195{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005196 /* Create the table. */
5197 lua_newtable(L);
5198
5199 if (!htxn->s->txn)
5200 return 1;
5201
Christopher Faulet724a12c2018-12-13 22:12:15 +01005202 if (IS_HTX_STRM(htxn->s)) {
5203 /* HTX version */
5204 struct htx *htx = htxbuf(&msg->chn->buf);
5205 int32_t pos;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005206
Christopher Fauleta3f15502019-05-13 15:27:23 +02005207 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet724a12c2018-12-13 22:12:15 +01005208 struct htx_blk *blk = htx_get_blk(htx, pos);
5209 enum htx_blk_type type = htx_get_blk_type(blk);
5210 struct ist n, v;
5211 int len;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005212
Christopher Faulet724a12c2018-12-13 22:12:15 +01005213 if (type == HTX_BLK_HDR) {
5214 n = htx_get_blk_name(htx,blk);
5215 v = htx_get_blk_value(htx, blk);
5216 }
5217 else if (type == HTX_BLK_EOH)
5218 break;
5219 else
5220 continue;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005221
Christopher Faulet724a12c2018-12-13 22:12:15 +01005222 /* Check for existing entry:
5223 * assume that the table is on the top of the stack, and
5224 * push the key in the stack, the function lua_gettable()
5225 * perform the lookup.
5226 */
5227 lua_pushlstring(L, n.ptr, n.len);
5228 lua_gettable(L, -2);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005229
Christopher Faulet724a12c2018-12-13 22:12:15 +01005230 switch (lua_type(L, -1)) {
5231 case LUA_TNIL:
5232 /* Table not found, create it. */
5233 lua_pop(L, 1); /* remove the nil value. */
5234 lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */
5235 lua_newtable(L); /* create and push empty table. */
5236 lua_pushlstring(L, v.ptr, v.len); /* push header value. */
5237 lua_rawseti(L, -2, 0); /* index header value (pop it). */
5238 lua_rawset(L, -3); /* index new table with header name (pop the values). */
5239 break;
5240
5241 case LUA_TTABLE:
5242 /* Entry found: push the value in the table. */
5243 len = lua_rawlen(L, -1);
5244 lua_pushlstring(L, v.ptr, v.len); /* push header value. */
5245 lua_rawseti(L, -2, len+1); /* index header value (pop it). */
5246 lua_pop(L, 1); /* remove the table (it is stored in the main table). */
5247 break;
5248
5249 default:
5250 /* Other cases are errors. */
5251 hlua_pusherror(L, "internal error during the parsing of headers.");
5252 WILL_LJMP(lua_error(L));
5253 }
5254 }
5255 }
5256 else {
5257 /* Legacy HTTP version */
5258 const char *cur_ptr, *cur_next, *p;
5259 int old_idx, cur_idx;
5260 struct hdr_idx_elem *cur_hdr;
5261 const char *hn, *hv;
5262 int hnl, hvl;
5263 const char *in;
5264 char *out;
5265 int len;
5266
5267 /* Build array of headers. */
5268 old_idx = 0;
5269 cur_next = ci_head(msg->chn) + hdr_idx_first_pos(&htxn->s->txn->hdr_idx);
5270
5271 while (1) {
5272 cur_idx = htxn->s->txn->hdr_idx.v[old_idx].next;
5273 if (!cur_idx)
5274 break;
5275 old_idx = cur_idx;
5276
5277 cur_hdr = &htxn->s->txn->hdr_idx.v[cur_idx];
5278 cur_ptr = cur_next;
5279 cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1;
5280
5281 /* Now we have one full header at cur_ptr of len cur_hdr->len,
5282 * and the next header starts at cur_next. We'll check
5283 * this header in the list as well as against the default
5284 * rule.
5285 */
5286
5287 /* look for ': *'. */
5288 hn = cur_ptr;
5289 for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++);
5290 if (p >= cur_ptr+cur_hdr->len)
5291 continue;
5292 hnl = p - hn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005293 p++;
Christopher Faulet724a12c2018-12-13 22:12:15 +01005294 while (p < cur_ptr+cur_hdr->len && ( *p == ' ' || *p == '\t' ))
5295 p++;
5296 if (p >= cur_ptr+cur_hdr->len)
5297 continue;
5298 hv = p;
5299 hvl = cur_ptr+cur_hdr->len-p;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005300
Christopher Faulet724a12c2018-12-13 22:12:15 +01005301 /* Lowercase the key. Don't check the size of trash, it have
5302 * the size of one buffer and the input data contains in one
5303 * buffer.
5304 */
5305 out = trash.area;
5306 for (in=hn; in<hn+hnl; in++, out++)
5307 *out = tolower(*in);
5308 *out = '\0';
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005309
Christopher Faulet724a12c2018-12-13 22:12:15 +01005310 /* Check for existing entry:
5311 * assume that the table is on the top of the stack, and
5312 * push the key in the stack, the function lua_gettable()
5313 * perform the lookup.
5314 */
5315 lua_pushlstring(L, trash.area, hnl);
5316 lua_gettable(L, -2);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005317
Christopher Faulet724a12c2018-12-13 22:12:15 +01005318 switch (lua_type(L, -1)) {
5319 case LUA_TNIL:
5320 /* Table not found, create it. */
5321 lua_pop(L, 1); /* remove the nil value. */
5322 lua_pushlstring(L, trash.area, hnl); /* push the header name as key. */
5323 lua_newtable(L); /* create and push empty table. */
5324 lua_pushlstring(L, hv, hvl); /* push header value. */
5325 lua_rawseti(L, -2, 0); /* index header value (pop it). */
5326 lua_rawset(L, -3); /* index new table with header name (pop the values). */
5327 break;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005328
Christopher Faulet724a12c2018-12-13 22:12:15 +01005329 case LUA_TTABLE:
5330 /* Entry found: push the value in the table. */
5331 len = lua_rawlen(L, -1);
5332 lua_pushlstring(L, hv, hvl); /* push header value. */
5333 lua_rawseti(L, -2, len+1); /* index header value (pop it). */
5334 lua_pop(L, 1); /* remove the table (it is stored in the main table). */
5335 break;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005336
Christopher Faulet724a12c2018-12-13 22:12:15 +01005337 default:
5338 /* Other cases are errors. */
5339 hlua_pusherror(L, "internal error during the parsing of headers.");
5340 WILL_LJMP(lua_error(L));
5341 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005342 }
5343 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005344 return 1;
5345}
5346
5347__LJMP static int hlua_http_req_get_headers(lua_State *L)
5348{
5349 struct hlua_txn *htxn;
5350
5351 MAY_LJMP(check_args(L, 1, "req_get_headers"));
5352 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5353
Christopher Faulet2351ca22019-07-26 16:31:34 +02005354 if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005355 WILL_LJMP(lua_error(L));
5356
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005357 return hlua_http_get_headers(L, htxn, &htxn->s->txn->req);
5358}
5359
5360__LJMP static int hlua_http_res_get_headers(lua_State *L)
5361{
5362 struct hlua_txn *htxn;
5363
5364 MAY_LJMP(check_args(L, 1, "res_get_headers"));
5365 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5366
Christopher Faulet2351ca22019-07-26 16:31:34 +02005367 if (htxn->dir != SMP_OPT_DIR_RES || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005368 WILL_LJMP(lua_error(L));
5369
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005370 return hlua_http_get_headers(L, htxn, &htxn->s->txn->rsp);
5371}
5372
5373/* This function replace full header, or just a value in
5374 * the request or in the response. It is a wrapper fir the
5375 * 4 following functions.
5376 */
5377__LJMP static inline int hlua_http_rep_hdr(lua_State *L, struct hlua_txn *htxn,
5378 struct http_msg *msg, int action)
5379{
5380 size_t name_len;
5381 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
5382 const char *reg = MAY_LJMP(luaL_checkstring(L, 3));
5383 const char *value = MAY_LJMP(luaL_checkstring(L, 4));
Dragan Dosen26743032019-04-30 15:54:36 +02005384 struct my_regex *re;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005385
Dragan Dosen26743032019-04-30 15:54:36 +02005386 if (!(re = regex_comp(reg, 1, 1, NULL)))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005387 WILL_LJMP(luaL_argerror(L, 3, "invalid regex"));
5388
Christopher Faulet5a8549e2019-06-17 13:36:06 +02005389 if (IS_HTX_STRM(htxn->s)) {
5390 struct htx *htx = htxbuf(&msg->chn->buf);
5391
5392 htx_transform_header_str(htxn->s, msg->chn, htx, ist2(name, name_len), value, re, action);
5393 }
5394 else
5395 http_transform_header_str(htxn->s, msg, name, name_len, value, re, action);
Dragan Dosen26743032019-04-30 15:54:36 +02005396 regex_free(re);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005397 return 0;
5398}
5399
5400__LJMP static int hlua_http_req_rep_hdr(lua_State *L)
5401{
5402 struct hlua_txn *htxn;
5403
5404 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
5405 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5406
Christopher Faulet2351ca22019-07-26 16:31:34 +02005407 if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005408 WILL_LJMP(lua_error(L));
5409
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005410 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_HDR));
5411}
5412
5413__LJMP static int hlua_http_res_rep_hdr(lua_State *L)
5414{
5415 struct hlua_txn *htxn;
5416
5417 MAY_LJMP(check_args(L, 4, "res_rep_hdr"));
5418 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5419
Christopher Faulet2351ca22019-07-26 16:31:34 +02005420 if (htxn->dir != SMP_OPT_DIR_RES || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005421 WILL_LJMP(lua_error(L));
5422
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005423 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->rsp, ACT_HTTP_REPLACE_HDR));
5424}
5425
5426__LJMP static int hlua_http_req_rep_val(lua_State *L)
5427{
5428 struct hlua_txn *htxn;
5429
5430 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
5431 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5432
Christopher Faulet2351ca22019-07-26 16:31:34 +02005433 if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005434 WILL_LJMP(lua_error(L));
5435
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005436 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_VAL));
5437}
5438
5439__LJMP static int hlua_http_res_rep_val(lua_State *L)
5440{
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005441 struct hlua_txn *htxn;
5442
5443 MAY_LJMP(check_args(L, 4, "res_rep_val"));
5444 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5445
Christopher Faulet2351ca22019-07-26 16:31:34 +02005446 if (htxn->dir != SMP_OPT_DIR_RES || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005447 WILL_LJMP(lua_error(L));
5448
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02005449 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->rsp, ACT_HTTP_REPLACE_VAL));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005450}
5451
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08005452/* This function deletes all the occurrences of an header.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005453 * It is a wrapper for the 2 following functions.
5454 */
5455__LJMP static inline int hlua_http_del_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg)
5456{
5457 size_t len;
5458 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005459
Christopher Faulet724a12c2018-12-13 22:12:15 +01005460 if (IS_HTX_STRM(htxn->s)) {
5461 /* HTX version */
5462 struct htx *htx = htxbuf(&msg->chn->buf);
5463 struct http_hdr_ctx ctx;
5464
5465 ctx.blk = NULL;
5466 while (http_find_header(htx, ist2(name, len), &ctx, 1))
5467 http_remove_header(htx, &ctx);
5468 }
5469 else {
5470 /* Legacy HTTP version */
5471 struct hdr_ctx ctx;
5472 struct http_txn *txn = htxn->s->txn;
5473
5474 ctx.idx = 0;
5475 while (http_find_header2(name, len, ci_head(msg->chn), &txn->hdr_idx, &ctx))
5476 http_remove_header2(msg, &txn->hdr_idx, &ctx);
5477 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005478 return 0;
5479}
5480
5481__LJMP static int hlua_http_req_del_hdr(lua_State *L)
5482{
5483 struct hlua_txn *htxn;
5484
5485 MAY_LJMP(check_args(L, 2, "req_del_hdr"));
5486 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5487
Christopher Faulet2351ca22019-07-26 16:31:34 +02005488 if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005489 WILL_LJMP(lua_error(L));
5490
Willy Tarreaueee5b512015-04-03 23:46:31 +02005491 return hlua_http_del_hdr(L, htxn, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005492}
5493
5494__LJMP static int hlua_http_res_del_hdr(lua_State *L)
5495{
5496 struct hlua_txn *htxn;
5497
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005498 MAY_LJMP(check_args(L, 2, "res_del_hdr"));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005499 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5500
Christopher Faulet2351ca22019-07-26 16:31:34 +02005501 if (htxn->dir != SMP_OPT_DIR_RES || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005502 WILL_LJMP(lua_error(L));
5503
Willy Tarreaueee5b512015-04-03 23:46:31 +02005504 return hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005505}
5506
5507/* This function adds an header. It is a wrapper used by
5508 * the 2 following functions.
5509 */
5510__LJMP static inline int hlua_http_add_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg)
5511{
5512 size_t name_len;
5513 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
5514 size_t value_len;
5515 const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len));
5516 char *p;
5517
Christopher Faulet724a12c2018-12-13 22:12:15 +01005518 if (IS_HTX_STRM(htxn->s)) {
5519 /* HTX version */
5520 struct htx *htx = htxbuf(&msg->chn->buf);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005521
Christopher Faulet724a12c2018-12-13 22:12:15 +01005522 lua_pushboolean(L, http_add_header(htx, ist2(name, name_len),
5523 ist2(value, value_len)));
5524 }
5525 else {
5526 /* Legacy HTTP version */
5527 /* Check length. */
5528 trash.data = value_len + name_len + 2;
5529 if (trash.data > trash.size)
5530 return 0;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005531
Christopher Faulet724a12c2018-12-13 22:12:15 +01005532 /* Creates the header string. */
5533 p = trash.area;
5534 memcpy(p, name, name_len);
5535 p += name_len;
5536 *p = ':';
5537 p++;
5538 *p = ' ';
5539 p++;
5540 memcpy(p, value, value_len);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005541
Christopher Faulet724a12c2018-12-13 22:12:15 +01005542 lua_pushboolean(L, http_header_add_tail2(msg, &htxn->s->txn->hdr_idx,
5543 trash.area, trash.data) != 0);
5544 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005545 return 0;
5546}
5547
5548__LJMP static int hlua_http_req_add_hdr(lua_State *L)
5549{
5550 struct hlua_txn *htxn;
5551
5552 MAY_LJMP(check_args(L, 3, "req_add_hdr"));
5553 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5554
Christopher Faulet2351ca22019-07-26 16:31:34 +02005555 if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005556 WILL_LJMP(lua_error(L));
5557
Willy Tarreaueee5b512015-04-03 23:46:31 +02005558 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005559}
5560
5561__LJMP static int hlua_http_res_add_hdr(lua_State *L)
5562{
5563 struct hlua_txn *htxn;
5564
5565 MAY_LJMP(check_args(L, 3, "res_add_hdr"));
5566 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5567
Christopher Faulet2351ca22019-07-26 16:31:34 +02005568 if (htxn->dir != SMP_OPT_DIR_RES || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005569 WILL_LJMP(lua_error(L));
5570
Willy Tarreaueee5b512015-04-03 23:46:31 +02005571 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005572}
5573
5574static int hlua_http_req_set_hdr(lua_State *L)
5575{
5576 struct hlua_txn *htxn;
5577
5578 MAY_LJMP(check_args(L, 3, "req_set_hdr"));
5579 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5580
Christopher Faulet2351ca22019-07-26 16:31:34 +02005581 if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005582 WILL_LJMP(lua_error(L));
5583
Willy Tarreaueee5b512015-04-03 23:46:31 +02005584 hlua_http_del_hdr(L, htxn, &htxn->s->txn->req);
5585 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005586}
5587
5588static int hlua_http_res_set_hdr(lua_State *L)
5589{
5590 struct hlua_txn *htxn;
5591
5592 MAY_LJMP(check_args(L, 3, "res_set_hdr"));
5593 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5594
Christopher Faulet2351ca22019-07-26 16:31:34 +02005595 if (htxn->dir != SMP_OPT_DIR_RES || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005596 WILL_LJMP(lua_error(L));
5597
Willy Tarreaueee5b512015-04-03 23:46:31 +02005598 hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp);
5599 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005600}
5601
5602/* This function set the method. */
5603static int hlua_http_req_set_meth(lua_State *L)
5604{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005605 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005606 size_t name_len;
5607 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005608
Christopher Faulet2351ca22019-07-26 16:31:34 +02005609 if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005610 WILL_LJMP(lua_error(L));
5611
Willy Tarreau987e3fb2015-04-04 01:09:08 +02005612 lua_pushboolean(L, http_replace_req_line(0, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005613 return 1;
5614}
5615
5616/* This function set the method. */
5617static int hlua_http_req_set_path(lua_State *L)
5618{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005619 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005620 size_t name_len;
5621 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02005622
Christopher Faulet2351ca22019-07-26 16:31:34 +02005623 if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005624 WILL_LJMP(lua_error(L));
5625
Willy Tarreau987e3fb2015-04-04 01:09:08 +02005626 lua_pushboolean(L, http_replace_req_line(1, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005627 return 1;
5628}
5629
5630/* This function set the query-string. */
5631static int hlua_http_req_set_query(lua_State *L)
5632{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005633 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005634 size_t name_len;
5635 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005636
Christopher Faulet2351ca22019-07-26 16:31:34 +02005637 if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005638 WILL_LJMP(lua_error(L));
5639
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005640 /* Check length. */
5641 if (name_len > trash.size - 1) {
5642 lua_pushboolean(L, 0);
5643 return 1;
5644 }
5645
5646 /* Add the mark question as prefix. */
5647 chunk_reset(&trash);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005648 trash.area[trash.data++] = '?';
5649 memcpy(trash.area + trash.data, name, name_len);
5650 trash.data += name_len;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005651
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005652 lua_pushboolean(L,
5653 http_replace_req_line(2, trash.area, trash.data, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005654 return 1;
5655}
5656
5657/* This function set the uri. */
5658static int hlua_http_req_set_uri(lua_State *L)
5659{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005660 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005661 size_t name_len;
5662 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005663
Christopher Faulet2351ca22019-07-26 16:31:34 +02005664 if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005665 WILL_LJMP(lua_error(L));
5666
Willy Tarreau987e3fb2015-04-04 01:09:08 +02005667 lua_pushboolean(L, http_replace_req_line(3, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005668 return 1;
5669}
5670
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005671/* This function set the response code & optionally reason. */
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005672static int hlua_http_res_set_status(lua_State *L)
5673{
5674 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5675 unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2));
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005676 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005677
Christopher Faulet2351ca22019-07-26 16:31:34 +02005678 if (htxn->dir != SMP_OPT_DIR_RES || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005679 WILL_LJMP(lua_error(L));
5680
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005681 http_set_status(code, reason, htxn->s);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005682 return 0;
5683}
5684
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005685/*
5686 *
5687 *
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005688 * Class TXN
5689 *
5690 *
5691 */
5692
5693/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02005694 * a class stream, otherwise it throws an error.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005695 */
5696__LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud)
5697{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02005698 return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref));
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005699}
5700
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005701__LJMP static int hlua_set_var(lua_State *L)
5702{
5703 struct hlua_txn *htxn;
5704 const char *name;
5705 size_t len;
5706 struct sample smp;
5707
5708 MAY_LJMP(check_args(L, 3, "set_var"));
5709
5710 /* It is useles to retrieve the stream, but this function
5711 * runs only in a stream context.
5712 */
5713 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5714 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5715
5716 /* Converts the third argument in a sample. */
5717 hlua_lua2smp(L, 3, &smp);
5718
5719 /* Store the sample in a variable. */
Willy Tarreau7560dd42016-03-10 16:28:58 +01005720 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Willy Tarreau6204cd92016-03-10 16:33:04 +01005721 vars_set_by_name(name, len, &smp);
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005722 return 0;
5723}
5724
Christopher Faulet85d79c92016-11-09 16:54:56 +01005725__LJMP static int hlua_unset_var(lua_State *L)
5726{
5727 struct hlua_txn *htxn;
5728 const char *name;
5729 size_t len;
5730 struct sample smp;
5731
5732 MAY_LJMP(check_args(L, 2, "unset_var"));
5733
5734 /* It is useles to retrieve the stream, but this function
5735 * runs only in a stream context.
5736 */
5737 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5738 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5739
5740 /* Unset the variable. */
5741 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
5742 vars_unset_by_name(name, len, &smp);
5743 return 0;
5744}
5745
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005746__LJMP static int hlua_get_var(lua_State *L)
5747{
5748 struct hlua_txn *htxn;
5749 const char *name;
5750 size_t len;
5751 struct sample smp;
5752
5753 MAY_LJMP(check_args(L, 2, "get_var"));
5754
5755 /* It is useles to retrieve the stream, but this function
5756 * runs only in a stream context.
5757 */
5758 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5759 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5760
Willy Tarreau7560dd42016-03-10 16:28:58 +01005761 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Willy Tarreau6204cd92016-03-10 16:33:04 +01005762 if (!vars_get_by_name(name, len, &smp)) {
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005763 lua_pushnil(L);
5764 return 1;
5765 }
5766
5767 return hlua_smp2lua(L, &smp);
5768}
5769
Willy Tarreau59551662015-03-10 14:23:13 +01005770__LJMP static int hlua_set_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005771{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005772 struct hlua *hlua;
5773
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005774 MAY_LJMP(check_args(L, 2, "set_priv"));
5775
Willy Tarreau87b09662015-04-03 00:22:06 +02005776 /* It is useles to retrieve the stream, but this function
5777 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005778 */
5779 MAY_LJMP(hlua_checktxn(L, 1));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005780 hlua = hlua_gethlua(L);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005781
5782 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02005783 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005784
5785 /* Get and store new value. */
5786 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
5787 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
5788
5789 return 0;
5790}
5791
Willy Tarreau59551662015-03-10 14:23:13 +01005792__LJMP static int hlua_get_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005793{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005794 struct hlua *hlua;
5795
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005796 MAY_LJMP(check_args(L, 1, "get_priv"));
5797
Willy Tarreau87b09662015-04-03 00:22:06 +02005798 /* It is useles to retrieve the stream, but this function
5799 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005800 */
5801 MAY_LJMP(hlua_checktxn(L, 1));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005802 hlua = hlua_gethlua(L);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005803
5804 /* Push configuration index in the stack. */
5805 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
5806
5807 return 1;
5808}
5809
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005810/* Create stack entry containing a class TXN. This function
5811 * return 0 if the stack does not contains free slots,
5812 * otherwise it returns 1.
5813 */
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005814static int hlua_txn_new(lua_State *L, struct stream *s, struct proxy *p, int dir, int flags)
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005815{
Willy Tarreaude491382015-04-06 11:04:28 +02005816 struct hlua_txn *htxn;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005817
5818 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005819 if (!lua_checkstack(L, 3))
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005820 return 0;
5821
5822 /* NOTE: The allocation never fails. The failure
5823 * throw an error, and the function never returns.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08005824 * if the throw is not available, the process is aborted.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005825 */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005826 /* Create the object: obj[0] = userdata. */
5827 lua_newtable(L);
Willy Tarreaude491382015-04-06 11:04:28 +02005828 htxn = lua_newuserdata(L, sizeof(*htxn));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005829 lua_rawseti(L, -2, 0);
5830
Willy Tarreaude491382015-04-06 11:04:28 +02005831 htxn->s = s;
5832 htxn->p = p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01005833 htxn->dir = dir;
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005834 htxn->flags = flags;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005835
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005836 /* Create the "f" field that contains a list of fetches. */
5837 lua_pushstring(L, "f");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005838 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005839 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005840 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005841
5842 /* Create the "sf" field that contains a list of stringsafe fetches. */
5843 lua_pushstring(L, "sf");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005844 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP | HLUA_F_AS_STRING))
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005845 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005846 lua_rawset(L, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005847
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005848 /* Create the "c" field that contains a list of converters. */
5849 lua_pushstring(L, "c");
Willy Tarreaude491382015-04-06 11:04:28 +02005850 if (!hlua_converters_new(L, htxn, 0))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005851 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005852 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005853
5854 /* Create the "sc" field that contains a list of stringsafe converters. */
5855 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01005856 if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING))
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005857 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005858 lua_rawset(L, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005859
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005860 /* Create the "req" field that contains the request channel object. */
5861 lua_pushstring(L, "req");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005862 if (!hlua_channel_new(L, &s->req))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005863 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005864 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005865
5866 /* Create the "res" field that contains the response channel object. */
5867 lua_pushstring(L, "res");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005868 if (!hlua_channel_new(L, &s->res))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005869 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005870 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005871
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005872 /* Creates the HTTP object is the current proxy allows http. */
5873 lua_pushstring(L, "http");
5874 if (p->mode == PR_MODE_HTTP) {
Willy Tarreaude491382015-04-06 11:04:28 +02005875 if (!hlua_http_new(L, htxn))
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005876 return 0;
5877 }
5878 else
5879 lua_pushnil(L);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005880 lua_rawset(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005881
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005882 /* Pop a class sesison metatable and affect it to the userdata. */
5883 lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref);
5884 lua_setmetatable(L, -2);
5885
5886 return 1;
5887}
5888
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005889__LJMP static int hlua_txn_deflog(lua_State *L)
5890{
5891 const char *msg;
5892 struct hlua_txn *htxn;
5893
5894 MAY_LJMP(check_args(L, 2, "deflog"));
5895 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5896 msg = MAY_LJMP(luaL_checkstring(L, 2));
5897
5898 hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg);
5899 return 0;
5900}
5901
5902__LJMP static int hlua_txn_log(lua_State *L)
5903{
5904 int level;
5905 const char *msg;
5906 struct hlua_txn *htxn;
5907
5908 MAY_LJMP(check_args(L, 3, "log"));
5909 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5910 level = MAY_LJMP(luaL_checkinteger(L, 2));
5911 msg = MAY_LJMP(luaL_checkstring(L, 3));
5912
5913 if (level < 0 || level >= NB_LOG_LEVELS)
5914 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
5915
5916 hlua_sendlog(htxn->s->be, level, msg);
5917 return 0;
5918}
5919
5920__LJMP static int hlua_txn_log_debug(lua_State *L)
5921{
5922 const char *msg;
5923 struct hlua_txn *htxn;
5924
5925 MAY_LJMP(check_args(L, 2, "Debug"));
5926 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5927 msg = MAY_LJMP(luaL_checkstring(L, 2));
5928 hlua_sendlog(htxn->s->be, LOG_DEBUG, msg);
5929 return 0;
5930}
5931
5932__LJMP static int hlua_txn_log_info(lua_State *L)
5933{
5934 const char *msg;
5935 struct hlua_txn *htxn;
5936
5937 MAY_LJMP(check_args(L, 2, "Info"));
5938 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5939 msg = MAY_LJMP(luaL_checkstring(L, 2));
5940 hlua_sendlog(htxn->s->be, LOG_INFO, msg);
5941 return 0;
5942}
5943
5944__LJMP static int hlua_txn_log_warning(lua_State *L)
5945{
5946 const char *msg;
5947 struct hlua_txn *htxn;
5948
5949 MAY_LJMP(check_args(L, 2, "Warning"));
5950 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5951 msg = MAY_LJMP(luaL_checkstring(L, 2));
5952 hlua_sendlog(htxn->s->be, LOG_WARNING, msg);
5953 return 0;
5954}
5955
5956__LJMP static int hlua_txn_log_alert(lua_State *L)
5957{
5958 const char *msg;
5959 struct hlua_txn *htxn;
5960
5961 MAY_LJMP(check_args(L, 2, "Alert"));
5962 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5963 msg = MAY_LJMP(luaL_checkstring(L, 2));
5964 hlua_sendlog(htxn->s->be, LOG_ALERT, msg);
5965 return 0;
5966}
5967
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005968__LJMP static int hlua_txn_set_loglevel(lua_State *L)
5969{
5970 struct hlua_txn *htxn;
5971 int ll;
5972
5973 MAY_LJMP(check_args(L, 2, "set_loglevel"));
5974 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5975 ll = MAY_LJMP(luaL_checkinteger(L, 2));
5976
5977 if (ll < 0 || ll > 7)
5978 WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7"));
5979
5980 htxn->s->logs.level = ll;
5981 return 0;
5982}
5983
5984__LJMP static int hlua_txn_set_tos(lua_State *L)
5985{
5986 struct hlua_txn *htxn;
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005987 int tos;
5988
5989 MAY_LJMP(check_args(L, 2, "set_tos"));
5990 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5991 tos = MAY_LJMP(luaL_checkinteger(L, 2));
5992
Willy Tarreau1a18b542018-12-11 16:37:42 +01005993 conn_set_tos(objt_conn(htxn->s->sess->origin), tos);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005994 return 0;
5995}
5996
5997__LJMP static int hlua_txn_set_mark(lua_State *L)
5998{
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005999 struct hlua_txn *htxn;
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01006000 int mark;
6001
6002 MAY_LJMP(check_args(L, 2, "set_mark"));
6003 htxn = MAY_LJMP(hlua_checktxn(L, 1));
6004 mark = MAY_LJMP(luaL_checkinteger(L, 2));
6005
Lukas Tribusb59291a2019-08-11 18:03:45 +02006006 conn_set_mark(objt_conn(htxn->s->sess->origin), mark);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01006007 return 0;
6008}
6009
Patrick Hemmer268a7072018-05-11 12:52:31 -04006010__LJMP static int hlua_txn_set_priority_class(lua_State *L)
6011{
6012 struct hlua_txn *htxn;
6013
6014 MAY_LJMP(check_args(L, 2, "set_priority_class"));
6015 htxn = MAY_LJMP(hlua_checktxn(L, 1));
6016 htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2)));
6017 return 0;
6018}
6019
6020__LJMP static int hlua_txn_set_priority_offset(lua_State *L)
6021{
6022 struct hlua_txn *htxn;
6023
6024 MAY_LJMP(check_args(L, 2, "set_priority_offset"));
6025 htxn = MAY_LJMP(hlua_checktxn(L, 1));
6026 htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2)));
6027 return 0;
6028}
6029
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01006030/* This function is an Lua binding that send pending data
6031 * to the client, and close the stream interface.
6032 */
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02006033__LJMP static int hlua_txn_done(lua_State *L)
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01006034{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02006035 struct hlua_txn *htxn;
Thierry FOURNIER9bd52d42016-07-14 11:45:33 +02006036 struct hlua *hlua;
Willy Tarreau81389672015-03-10 12:03:52 +01006037 struct channel *ic, *oc;
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01006038
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006039 MAY_LJMP(check_args(L, 1, "close"));
Willy Tarreaub2ccb562015-04-06 11:11:15 +02006040 htxn = MAY_LJMP(hlua_checktxn(L, 1));
Thierry FOURNIER9bd52d42016-07-14 11:45:33 +02006041 hlua = hlua_gethlua(L);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01006042
Thierry FOURNIERab00df62016-07-14 11:42:37 +02006043 /* If the flags NOTERM is set, we cannot terminate the http
6044 * session, so we just end the execution of the current
6045 * lua code.
6046 */
6047 if (htxn->flags & HLUA_TXN_NOTERM) {
6048 WILL_LJMP(hlua_done(L));
6049 return 0;
6050 }
6051
Willy Tarreaub2ccb562015-04-06 11:11:15 +02006052 ic = &htxn->s->req;
6053 oc = &htxn->s->res;
Willy Tarreau81389672015-03-10 12:03:52 +01006054
Christopher Faulet72c69272019-07-26 16:40:24 +02006055 if (IS_HTX_STRM(htxn->s)) {
6056 htxn->s->txn->status = 0;
6057 http_reply_and_close(htxn->s, 0, NULL);
6058 ic->analysers &= AN_REQ_FLT_END;
6059 oc->analysers &= AN_RES_FLT_END;
6060 }
Christopher Fauletb58fb792019-07-16 10:52:40 +02006061 else {
6062 if (htxn->s->txn) {
6063 /* HTTP mode, let's stay in sync with the stream */
6064 b_del(&ic->buf, htxn->s->txn->req.sov);
6065 htxn->s->txn->req.next -= htxn->s->txn->req.sov;
6066 htxn->s->txn->req.sov = 0;
6067
6068 ic->analysers &= AN_REQ_HTTP_XFER_BODY;
6069 oc->analysers = AN_RES_HTTP_XFER_BODY;
6070 htxn->s->txn->req.msg_state = HTTP_MSG_CLOSED;
6071 htxn->s->txn->rsp.msg_state = HTTP_MSG_DONE;
Willy Tarreau630ef452015-08-28 10:06:15 +02006072
Willy Tarreau630ef452015-08-28 10:06:15 +02006073 /* Note that if we want to support keep-alive, we need
6074 * to bypass the close/shutr_now calls below, but that
6075 * may only be done if the HTTP request was already
6076 * processed and the connection header is known (ie
6077 * not during TCP rules).
6078 */
Christopher Fauletb58fb792019-07-16 10:52:40 +02006079 }
Willy Tarreau630ef452015-08-28 10:06:15 +02006080
Christopher Fauletb58fb792019-07-16 10:52:40 +02006081 channel_auto_read(ic);
6082 channel_abort(ic);
6083 channel_auto_close(ic);
6084 channel_erase(ic);
Thierry FOURNIER10ec2142015-08-24 17:23:45 +02006085
Christopher Fauletb58fb792019-07-16 10:52:40 +02006086 oc->wex = tick_add_ifset(now_ms, oc->wto);
6087 channel_auto_read(oc);
6088 channel_auto_close(oc);
6089 channel_shutr_now(oc);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01006090
Christopher Fauletb58fb792019-07-16 10:52:40 +02006091 ic->analysers = 0;
6092 }
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02006093
Christopher Faulet72c69272019-07-26 16:40:24 +02006094 if (!(htxn->s->flags & SF_ERR_MASK)) // this is not really an error but it is
6095 htxn->s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
6096
Thierry FOURNIER9bd52d42016-07-14 11:45:33 +02006097 hlua->flags |= HLUA_STOP;
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02006098 WILL_LJMP(hlua_done(L));
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01006099 return 0;
6100}
6101
6102__LJMP static int hlua_log(lua_State *L)
6103{
6104 int level;
6105 const char *msg;
6106
6107 MAY_LJMP(check_args(L, 2, "log"));
6108 level = MAY_LJMP(luaL_checkinteger(L, 1));
6109 msg = MAY_LJMP(luaL_checkstring(L, 2));
6110
6111 if (level < 0 || level >= NB_LOG_LEVELS)
6112 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
6113
6114 hlua_sendlog(NULL, level, msg);
6115 return 0;
6116}
6117
6118__LJMP static int hlua_log_debug(lua_State *L)
6119{
6120 const char *msg;
6121
6122 MAY_LJMP(check_args(L, 1, "debug"));
6123 msg = MAY_LJMP(luaL_checkstring(L, 1));
6124 hlua_sendlog(NULL, LOG_DEBUG, msg);
6125 return 0;
6126}
6127
6128__LJMP static int hlua_log_info(lua_State *L)
6129{
6130 const char *msg;
6131
6132 MAY_LJMP(check_args(L, 1, "info"));
6133 msg = MAY_LJMP(luaL_checkstring(L, 1));
6134 hlua_sendlog(NULL, LOG_INFO, msg);
6135 return 0;
6136}
6137
6138__LJMP static int hlua_log_warning(lua_State *L)
6139{
6140 const char *msg;
6141
6142 MAY_LJMP(check_args(L, 1, "warning"));
6143 msg = MAY_LJMP(luaL_checkstring(L, 1));
6144 hlua_sendlog(NULL, LOG_WARNING, msg);
6145 return 0;
6146}
6147
6148__LJMP static int hlua_log_alert(lua_State *L)
6149{
6150 const char *msg;
6151
6152 MAY_LJMP(check_args(L, 1, "alert"));
6153 msg = MAY_LJMP(luaL_checkstring(L, 1));
6154 hlua_sendlog(NULL, LOG_ALERT, msg);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01006155 return 0;
6156}
6157
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006158__LJMP static int hlua_sleep_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006159{
6160 int wakeup_ms = lua_tointeger(L, -1);
6161 if (now_ms < wakeup_ms)
Willy Tarreau9635e032018-10-16 17:52:55 +02006162 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006163 return 0;
6164}
6165
6166__LJMP static int hlua_sleep(lua_State *L)
6167{
6168 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006169 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006170
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006171 MAY_LJMP(check_args(L, 1, "sleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006172
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006173 delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006174 wakeup_ms = tick_add(now_ms, delay);
6175 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006176
Willy Tarreau9635e032018-10-16 17:52:55 +02006177 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006178 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006179}
6180
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006181__LJMP static int hlua_msleep(lua_State *L)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006182{
6183 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006184 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006185
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006186 MAY_LJMP(check_args(L, 1, "msleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006187
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006188 delay = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006189 wakeup_ms = tick_add(now_ms, delay);
6190 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006191
Willy Tarreau9635e032018-10-16 17:52:55 +02006192 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006193 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006194}
6195
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01006196/* This functionis an LUA binding. it permits to give back
6197 * the hand at the HAProxy scheduler. It is used when the
6198 * LUA processing consumes a lot of time.
6199 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006200__LJMP static int hlua_yield_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006201{
6202 return 0;
6203}
6204
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01006205__LJMP static int hlua_yield(lua_State *L)
6206{
Willy Tarreau9635e032018-10-16 17:52:55 +02006207 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_yield_yield, TICK_ETERNITY, HLUA_CTRLYIELD));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006208 return 0;
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01006209}
6210
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006211/* This function change the nice of the currently executed
6212 * task. It is used set low or high priority at the current
6213 * task.
6214 */
Willy Tarreau59551662015-03-10 14:23:13 +01006215__LJMP static int hlua_set_nice(lua_State *L)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006216{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006217 struct hlua *hlua;
6218 int nice;
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006219
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006220 MAY_LJMP(check_args(L, 1, "set_nice"));
6221 hlua = hlua_gethlua(L);
6222 nice = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006223
6224 /* If he task is not set, I'm in a start mode. */
6225 if (!hlua || !hlua->task)
6226 return 0;
6227
6228 if (nice < -1024)
6229 nice = -1024;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006230 else if (nice > 1024)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006231 nice = 1024;
6232
6233 hlua->task->nice = nice;
6234 return 0;
6235}
6236
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006237/* This function is used as a callback of a task. It is called by the
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006238 * HAProxy task subsystem when the task is awaked. The LUA runtime can
6239 * return an E_AGAIN signal, the emmiter of this signal must set a
6240 * signal to wake the task.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006241 *
6242 * Task wrapper are longjmp safe because the only one Lua code
6243 * executed is the safe hlua_ctx_resume();
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006244 */
Willy Tarreau83a5ff42019-08-21 14:14:50 +02006245struct task *hlua_process_task(struct task *task, void *context, unsigned short state)
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006246{
Olivier Houchard9f6af332018-05-25 14:04:04 +02006247 struct hlua *hlua = context;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006248 enum hlua_exec status;
6249
Christopher Faulet5bc99722018-04-25 10:34:45 +02006250 if (task->thread_mask == MAX_THREADS_MASK)
6251 task_set_affinity(task, tid_bit);
6252
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006253 /* If it is the first call to the task, we must initialize the
6254 * execution timeouts.
6255 */
6256 if (!HLUA_IS_RUNNING(hlua))
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02006257 hlua->max_time = hlua_timeout_task;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006258
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006259 /* Execute the Lua code. */
6260 status = hlua_ctx_resume(hlua, 1);
6261
6262 switch (status) {
6263 /* finished or yield */
6264 case HLUA_E_OK:
6265 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006266 task_destroy(task);
Tim Duesterhuscd235c62018-04-24 13:56:01 +02006267 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006268 break;
6269
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006270 case HLUA_E_AGAIN: /* co process or timeout wake me later. */
Thierry FOURNIERcb146882017-12-10 17:10:57 +01006271 notification_gc(&hlua->com);
PiBa-NLfe971b32018-05-02 22:27:14 +02006272 task->expire = hlua->wake_time;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006273 break;
6274
6275 /* finished with error. */
6276 case HLUA_E_ERRMSG:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006277 SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006278 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006279 task_destroy(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02006280 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006281 break;
6282
6283 case HLUA_E_ERR:
6284 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006285 SEND_ERR(NULL, "Lua task: unknown error.\n");
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006286 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006287 task_destroy(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02006288 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006289 break;
6290 }
Emeric Brun253e53e2017-10-17 18:58:40 +02006291 return task;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006292}
6293
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006294/* This function is an LUA binding that register LUA function to be
6295 * executed after the HAProxy configuration parsing and before the
6296 * HAProxy scheduler starts. This function expect only one LUA
6297 * argument that is a function. This function returns nothing, but
6298 * throws if an error is encountered.
6299 */
6300__LJMP static int hlua_register_init(lua_State *L)
6301{
6302 struct hlua_init_function *init;
6303 int ref;
6304
6305 MAY_LJMP(check_args(L, 1, "register_init"));
6306
6307 ref = MAY_LJMP(hlua_checkfunction(L, 1));
6308
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006309 init = calloc(1, sizeof(*init));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006310 if (!init)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006311 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006312
6313 init->function_ref = ref;
6314 LIST_ADDQ(&hlua_init_functions, &init->l);
6315 return 0;
6316}
6317
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006318/* This functio is an LUA binding. It permits to register a task
6319 * executed in parallel of the main HAroxy activity. The task is
6320 * created and it is set in the HAProxy scheduler. It can be called
6321 * from the "init" section, "post init" or during the runtime.
6322 *
6323 * Lua prototype:
6324 *
6325 * <none> core.register_task(<function>)
6326 */
6327static int hlua_register_task(lua_State *L)
6328{
6329 struct hlua *hlua;
6330 struct task *task;
6331 int ref;
6332
6333 MAY_LJMP(check_args(L, 1, "register_task"));
6334
6335 ref = MAY_LJMP(hlua_checkfunction(L, 1));
6336
Willy Tarreaubafbe012017-11-24 17:34:44 +01006337 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006338 if (!hlua)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006339 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006340
Emeric Brunc60def82017-09-27 14:59:38 +02006341 task = task_new(MAX_THREADS_MASK);
Willy Tarreaue09101e2018-10-16 17:37:12 +02006342 if (!task)
6343 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
6344
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006345 task->context = hlua;
6346 task->process = hlua_process_task;
6347
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006348 if (!hlua_ctx_init(hlua, task, 1))
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006349 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006350
6351 /* Restore the function in the stack. */
6352 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref);
6353 hlua->nargs = 0;
6354
6355 /* Schedule task. */
6356 task_schedule(task, now_ms);
6357
6358 return 0;
6359}
6360
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006361/* Wrapper called by HAProxy to execute an LUA converter. This wrapper
6362 * doesn't allow "yield" functions because the HAProxy engine cannot
6363 * resume converters.
6364 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006365static int hlua_sample_conv_wrapper(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006366{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006367 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006368 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006369 const char *error;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006370
Willy Tarreaube508f12016-03-10 11:47:01 +01006371 if (!stream)
6372 return 0;
6373
Willy Tarreau87b09662015-04-03 00:22:06 +02006374 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006375 * Lua context can be not initialized. This behavior
6376 * permits to save performances because a systematic
6377 * Lua initialization cause 5% performances loss.
6378 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006379 if (!stream->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006380 stream->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006381 if (!stream->hlua) {
6382 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
6383 return 0;
6384 }
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006385 if (!hlua_ctx_init(stream->hlua, stream->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006386 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
6387 return 0;
6388 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006389 }
6390
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006391 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006392 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006393
6394 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006395 if (!SET_SAFE_LJMP(stream->hlua->T)) {
6396 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
6397 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006398 else
6399 error = "critical error";
6400 SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006401 return 0;
6402 }
6403
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006404 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006405 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006406 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006407 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006408 return 0;
6409 }
6410
6411 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006412 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006413
6414 /* convert input sample and pust-it in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006415 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006416 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006417 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006418 return 0;
6419 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006420 hlua_smp2lua(stream->hlua->T, smp);
6421 stream->hlua->nargs = 1;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006422
6423 /* push keywords in the stack. */
6424 if (arg_p) {
6425 for (; arg_p->type != ARGT_STOP; arg_p++) {
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_arg2lua(stream->hlua->T, arg_p);
6432 stream->hlua->nargs++;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006433 }
6434 }
6435
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006436 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006437 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006438
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006439 /* At this point the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006440 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006441 }
6442
6443 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006444 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006445 /* finished. */
6446 case HLUA_E_OK:
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02006447 /* If the stack is empty, the function fails. */
6448 if (lua_gettop(stream->hlua->T) <= 0)
6449 return 0;
6450
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006451 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006452 hlua_lua2smp(stream->hlua->T, -1, smp);
6453 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006454 return 1;
6455
6456 /* yield. */
6457 case HLUA_E_AGAIN:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006458 SEND_ERR(stream->be, "Lua converter '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006459 return 0;
6460
6461 /* finished with error. */
6462 case HLUA_E_ERRMSG:
6463 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006464 SEND_ERR(stream->be, "Lua converter '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006465 fcn->name, lua_tostring(stream->hlua->T, -1));
6466 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006467 return 0;
6468
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006469 case HLUA_E_ETMOUT:
6470 SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name);
6471 return 0;
6472
6473 case HLUA_E_NOMEM:
6474 SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name);
6475 return 0;
6476
6477 case HLUA_E_YIELD:
6478 SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name);
6479 return 0;
6480
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006481 case HLUA_E_ERR:
6482 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006483 SEND_ERR(stream->be, "Lua converter '%s' returns an unknown error.\n", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006484
6485 default:
6486 return 0;
6487 }
6488}
6489
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006490/* Wrapper called by HAProxy to execute a sample-fetch. this wrapper
6491 * doesn't allow "yield" functions because the HAProxy engine cannot
Willy Tarreaube508f12016-03-10 11:47:01 +01006492 * resume sample-fetches. This function will be called by the sample
6493 * fetch engine to call lua-based fetch operations.
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006494 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +02006495static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp,
6496 const char *kw, void *private)
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006497{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006498 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006499 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006500 const char *error;
Willy Tarreau83061a82018-07-13 11:56:34 +02006501 const struct buffer msg = { };
Christopher Fauletff96b8b2019-07-26 15:09:53 +02006502 unsigned int hflags = HLUA_TXN_NOTERM;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006503
Willy Tarreaube508f12016-03-10 11:47:01 +01006504 if (!stream)
6505 return 0;
Christopher Fauletafd8f102018-11-08 11:34:21 +01006506
Willy Tarreau87b09662015-04-03 00:22:06 +02006507 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006508 * Lua context can be not initialized. This behavior
6509 * permits to save performances because a systematic
6510 * Lua initialization cause 5% performances loss.
6511 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006512 if (!stream->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006513 stream->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006514 if (!stream->hlua) {
6515 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
6516 return 0;
6517 }
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006518 if (!hlua_ctx_init(stream->hlua, stream->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006519 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
6520 return 0;
6521 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006522 }
6523
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006524 consistency_set(stream, smp->opt, &stream->hlua->cons);
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006525
Christopher Fauletff96b8b2019-07-26 15:09:53 +02006526 if (stream->be->mode == PR_MODE_HTTP) {
6527 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
6528 hflags |= ((stream->txn->req.msg_state < HTTP_MSG_BODY) ? 0 : HLUA_TXN_HTTP_RDY);
6529 else
6530 hflags |= ((stream->txn->rsp.msg_state < HTTP_MSG_BODY) ? 0 : HLUA_TXN_HTTP_RDY);
6531 }
6532
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006533 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006534 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006535
6536 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006537 if (!SET_SAFE_LJMP(stream->hlua->T)) {
6538 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
6539 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006540 else
6541 error = "critical error";
6542 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006543 return 0;
6544 }
6545
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006546 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006547 if (!lua_checkstack(stream->hlua->T, 2)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006548 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006549 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006550 return 0;
6551 }
6552
6553 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006554 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006555
6556 /* push arguments in the stack. */
Christopher Fauletff96b8b2019-07-26 15:09:53 +02006557 if (!hlua_txn_new(stream->hlua->T, stream, smp->px, smp->opt & SMP_OPT_DIR, hflags)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006558 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006559 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006560 return 0;
6561 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006562 stream->hlua->nargs = 1;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006563
6564 /* push keywords in the stack. */
6565 for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) {
6566 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006567 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006568 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006569 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006570 return 0;
6571 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006572 hlua_arg2lua(stream->hlua->T, arg_p);
6573 stream->hlua->nargs++;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006574 }
6575
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006576 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006577 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006578
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006579 /* At this point the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006580 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006581 }
6582
6583 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006584 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006585 /* finished. */
6586 case HLUA_E_OK:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006587 if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) {
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006588 si_retnclose(&stream->si[0], &msg);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02006589 return 0;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006590 }
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02006591 /* If the stack is empty, the function fails. */
6592 if (lua_gettop(stream->hlua->T) <= 0)
6593 return 0;
6594
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006595 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006596 hlua_lua2smp(stream->hlua->T, -1, smp);
6597 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006598
6599 /* Set the end of execution flag. */
6600 smp->flags &= ~SMP_F_MAY_CHANGE;
6601 return 1;
6602
6603 /* yield. */
6604 case HLUA_E_AGAIN:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006605 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006606 si_retnclose(&stream->si[0], &msg);
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006607 SEND_ERR(smp->px, "Lua sample-fetch '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006608 return 0;
6609
6610 /* finished with error. */
6611 case HLUA_E_ERRMSG:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006612 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006613 si_retnclose(&stream->si[0], &msg);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006614 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006615 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006616 fcn->name, lua_tostring(stream->hlua->T, -1));
6617 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006618 return 0;
6619
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006620 case HLUA_E_ETMOUT:
6621 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006622 si_retnclose(&stream->si[0], &msg);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006623 SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name);
6624 return 0;
6625
6626 case HLUA_E_NOMEM:
6627 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006628 si_retnclose(&stream->si[0], &msg);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006629 SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name);
6630 return 0;
6631
6632 case HLUA_E_YIELD:
6633 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006634 si_retnclose(&stream->si[0], &msg);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006635 SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name);
6636 return 0;
6637
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006638 case HLUA_E_ERR:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006639 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006640 si_retnclose(&stream->si[0], &msg);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006641 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006642 SEND_ERR(smp->px, "Lua sample-fetch '%s' returns an unknown error.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006643
6644 default:
6645 return 0;
6646 }
6647}
6648
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006649/* This function is an LUA binding used for registering
6650 * "sample-conv" functions. It expects a converter name used
6651 * in the haproxy configuration file, and an LUA function.
6652 */
6653__LJMP static int hlua_register_converters(lua_State *L)
6654{
6655 struct sample_conv_kw_list *sck;
6656 const char *name;
6657 int ref;
6658 int len;
6659 struct hlua_function *fcn;
6660
6661 MAY_LJMP(check_args(L, 2, "register_converters"));
6662
6663 /* First argument : converter name. */
6664 name = MAY_LJMP(luaL_checkstring(L, 1));
6665
6666 /* Second argument : lua function. */
6667 ref = MAY_LJMP(hlua_checkfunction(L, 2));
6668
6669 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006670 sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006671 if (!sck)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006672 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006673 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006674 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006675 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006676
6677 /* Fill fcn. */
6678 fcn->name = strdup(name);
6679 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006680 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006681 fcn->function_ref = ref;
6682
6683 /* List head */
6684 sck->list.n = sck->list.p = NULL;
6685
6686 /* converter keyword. */
6687 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006688 sck->kw[0].kw = calloc(1, len);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006689 if (!sck->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006690 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006691
6692 snprintf((char *)sck->kw[0].kw, len, "lua.%s", name);
6693 sck->kw[0].process = hlua_sample_conv_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01006694 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 +01006695 sck->kw[0].val_args = NULL;
6696 sck->kw[0].in_type = SMP_T_STR;
6697 sck->kw[0].out_type = SMP_T_STR;
6698 sck->kw[0].private = fcn;
6699
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006700 /* Register this new converter */
6701 sample_register_convs(sck);
6702
6703 return 0;
6704}
6705
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006706/* This function is an LUA binding used for registering
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006707 * "sample-fetch" functions. It expects a converter name used
6708 * in the haproxy configuration file, and an LUA function.
6709 */
6710__LJMP static int hlua_register_fetches(lua_State *L)
6711{
6712 const char *name;
6713 int ref;
6714 int len;
6715 struct sample_fetch_kw_list *sfk;
6716 struct hlua_function *fcn;
6717
6718 MAY_LJMP(check_args(L, 2, "register_fetches"));
6719
6720 /* First argument : sample-fetch name. */
6721 name = MAY_LJMP(luaL_checkstring(L, 1));
6722
6723 /* Second argument : lua function. */
6724 ref = MAY_LJMP(hlua_checkfunction(L, 2));
6725
6726 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006727 sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006728 if (!sfk)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006729 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006730 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006731 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006732 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006733
6734 /* Fill fcn. */
6735 fcn->name = strdup(name);
6736 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006737 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006738 fcn->function_ref = ref;
6739
6740 /* List head */
6741 sfk->list.n = sfk->list.p = NULL;
6742
6743 /* sample-fetch keyword. */
6744 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006745 sfk->kw[0].kw = calloc(1, len);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006746 if (!sfk->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006747 return luaL_error(L, "Lua out of memory error.");
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006748
6749 snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name);
6750 sfk->kw[0].process = hlua_sample_fetch_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01006751 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 +01006752 sfk->kw[0].val_args = NULL;
6753 sfk->kw[0].out_type = SMP_T_STR;
6754 sfk->kw[0].use = SMP_USE_HTTP_ANY;
6755 sfk->kw[0].val = 0;
6756 sfk->kw[0].private = fcn;
6757
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006758 /* Register this new fetch. */
6759 sample_register_fetches(sfk);
6760
6761 return 0;
6762}
6763
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006764/* This function is a wrapper to execute each LUA function declared
6765 * as an action wrapper during the initialisation period. This function
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006766 * return ACT_RET_CONT if the processing is finished (with or without
6767 * error) and return ACT_RET_YIELD if the function must be called again
6768 * because the LUA returns a yield.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006769 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006770static enum act_return hlua_action(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +02006771 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006772{
6773 char **arg;
Christopher Fauletff96b8b2019-07-26 15:09:53 +02006774 unsigned int hflags = 0;
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02006775 int dir;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006776 const char *error;
Willy Tarreau83061a82018-07-13 11:56:34 +02006777 const struct buffer msg = { };
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006778
6779 switch (rule->from) {
Christopher Fauletff96b8b2019-07-26 15:09:53 +02006780 case ACT_F_TCP_REQ_CNT: ; dir = SMP_OPT_DIR_REQ; break;
6781 case ACT_F_TCP_RES_CNT: ; dir = SMP_OPT_DIR_RES; break;
6782 case ACT_F_HTTP_REQ: hflags = HLUA_TXN_HTTP_RDY ; dir = SMP_OPT_DIR_REQ; break;
6783 case ACT_F_HTTP_RES: hflags = HLUA_TXN_HTTP_RDY ; dir = SMP_OPT_DIR_RES; break;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006784 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006785 SEND_ERR(px, "Lua: internal error while execute action.\n");
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006786 return ACT_RET_CONT;
6787 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006788
Willy Tarreau87b09662015-04-03 00:22:06 +02006789 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006790 * Lua context can be not initialized. This behavior
6791 * permits to save performances because a systematic
6792 * Lua initialization cause 5% performances loss.
6793 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006794 if (!s->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006795 s->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006796 if (!s->hlua) {
6797 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
6798 rule->arg.hlua_rule->fcn.name);
6799 return ACT_RET_CONT;
6800 }
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006801 if (!hlua_ctx_init(s->hlua, s->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006802 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
6803 rule->arg.hlua_rule->fcn.name);
6804 return ACT_RET_CONT;
6805 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006806 }
6807
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006808 consistency_set(s, dir, &s->hlua->cons);
6809
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006810 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006811 if (!HLUA_IS_RUNNING(s->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006812
6813 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006814 if (!SET_SAFE_LJMP(s->hlua->T)) {
6815 if (lua_type(s->hlua->T, -1) == LUA_TSTRING)
6816 error = lua_tostring(s->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006817 else
6818 error = "critical error";
6819 SEND_ERR(px, "Lua function '%s': %s.\n",
6820 rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006821 return ACT_RET_CONT;
6822 }
6823
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006824 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006825 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006826 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006827 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006828 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006829 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006830 }
6831
6832 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006833 lua_rawgeti(s->hlua->T, LUA_REGISTRYINDEX, rule->arg.hlua_rule->fcn.function_ref);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006834
Willy Tarreau87b09662015-04-03 00:22:06 +02006835 /* Create and and push object stream in the stack. */
Christopher Fauletff96b8b2019-07-26 15:09:53 +02006836 if (!hlua_txn_new(s->hlua->T, s, px, dir, hflags)) {
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 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006842 s->hlua->nargs = 1;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006843
6844 /* push keywords in the stack. */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006845 for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006846 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006847 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006848 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006849 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006850 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006851 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006852 lua_pushstring(s->hlua->T, *arg);
6853 s->hlua->nargs++;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006854 }
6855
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006856 /* Now the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006857 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006858
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006859 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006860 s->hlua->max_time = hlua_timeout_session;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006861 }
6862
Christopher Faulet719ddc82020-06-02 18:46:07 +02006863 /* Always reset the analyse expiration timeout for the corresponding
6864 * channel in case the lua script yield, to be sure to not keep an
6865 * expired timeout.
6866 */
6867 if (dir == SMP_OPT_DIR_REQ)
6868 s->req.analyse_exp = TICK_ETERNITY;
6869 else
6870 s->res.analyse_exp = TICK_ETERNITY;
6871
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006872 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006873 switch (hlua_ctx_resume(s->hlua, !(flags & ACT_FLAG_FINAL))) {
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006874 /* finished. */
6875 case HLUA_E_OK:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006876 if (!consistency_check(s, dir, &s->hlua->cons)) {
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006877 si_retnclose(&s->si[0], &msg);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02006878 return ACT_RET_ERR;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006879 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006880 if (s->hlua->flags & HLUA_STOP)
Christopher Faulet4629d082019-07-04 11:27:15 +02006881 return ACT_RET_DONE;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006882 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006883
6884 /* yield. */
6885 case HLUA_E_AGAIN:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006886 /* Set timeout in the required channel. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006887 if (s->hlua->wake_time != TICK_ETERNITY) {
Christopher Fauletea0b0e22019-08-14 23:19:45 +02006888 if (dir == SMP_OPT_DIR_REQ)
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006889 s->req.analyse_exp = s->hlua->wake_time;
Christopher Fauletff96b8b2019-07-26 15:09:53 +02006890 else
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006891 s->res.analyse_exp = s->hlua->wake_time;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006892 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006893 /* Some actions can be wake up when a "write" event
6894 * is detected on a response channel. This is useful
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006895 * only for actions targeted on the requests.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006896 */
Christopher Fauletb22f6502019-07-26 14:54:52 +02006897 if (HLUA_IS_WAKERESWR(s->hlua))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006898 s->res.flags |= CF_WAKE_WRITE;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006899 if (HLUA_IS_WAKEREQWR(s->hlua))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006900 s->req.flags |= CF_WAKE_WRITE;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006901 /* We can quit the function without consistency check
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006902 * because HAProxy is not able to manipulate data, it
6903 * is only allowed to call me again. */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006904 return ACT_RET_YIELD;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006905
6906 /* finished with error. */
6907 case HLUA_E_ERRMSG:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006908 if (!consistency_check(s, dir, &s->hlua->cons)) {
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006909 si_retnclose(&s->si[0], &msg);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02006910 return ACT_RET_ERR;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006911 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006912 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006913 SEND_ERR(px, "Lua function '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006914 rule->arg.hlua_rule->fcn.name, lua_tostring(s->hlua->T, -1));
6915 lua_pop(s->hlua->T, 1);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006916 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006917
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006918 case HLUA_E_ETMOUT:
6919 if (!consistency_check(s, dir, &s->hlua->cons)) {
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006920 si_retnclose(&s->si[0], &msg);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006921 return ACT_RET_ERR;
6922 }
6923 SEND_ERR(px, "Lua function '%s': execution timeout.\n", rule->arg.hlua_rule->fcn.name);
6924 return 0;
6925
6926 case HLUA_E_NOMEM:
6927 if (!consistency_check(s, dir, &s->hlua->cons)) {
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006928 si_retnclose(&s->si[0], &msg);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006929 return ACT_RET_ERR;
6930 }
6931 SEND_ERR(px, "Lua function '%s': out of memory error.\n", rule->arg.hlua_rule->fcn.name);
6932 return 0;
6933
6934 case HLUA_E_YIELD:
6935 if (!consistency_check(s, dir, &s->hlua->cons)) {
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006936 si_retnclose(&s->si[0], &msg);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006937 return ACT_RET_ERR;
6938 }
6939 SEND_ERR(px, "Lua function '%s': aborting Lua processing on expired timeout.\n",
6940 rule->arg.hlua_rule->fcn.name);
6941 return 0;
6942
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006943 case HLUA_E_ERR:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006944 if (!consistency_check(s, dir, &s->hlua->cons)) {
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006945 si_retnclose(&s->si[0], &msg);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02006946 return ACT_RET_ERR;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006947 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006948 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006949 SEND_ERR(px, "Lua function '%s' return an unknown error.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006950 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006951
6952 default:
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006953 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006954 }
6955}
6956
Olivier Houchard9f6af332018-05-25 14:04:04 +02006957struct task *hlua_applet_wakeup(struct task *t, void *context, unsigned short state)
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006958{
Olivier Houchard9f6af332018-05-25 14:04:04 +02006959 struct appctx *ctx = context;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006960
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006961 appctx_wakeup(ctx);
Willy Tarreaud9587412017-08-23 16:07:33 +02006962 t->expire = TICK_ETERNITY;
Willy Tarreaud1aa41f2017-07-21 16:41:56 +02006963 return t;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006964}
6965
6966static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
6967{
6968 struct stream_interface *si = ctx->owner;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006969 struct hlua *hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006970 struct task *task;
6971 char **arg;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006972 const char *error;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006973
Willy Tarreaubafbe012017-11-24 17:34:44 +01006974 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006975 if (!hlua) {
6976 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
6977 ctx->rule->arg.hlua_rule->fcn.name);
6978 return 0;
6979 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006980 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006981 ctx->ctx.hlua_apptcp.hlua = hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006982 ctx->ctx.hlua_apptcp.flags = 0;
6983
6984 /* Create task used by signal to wakeup applets. */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01006985 task = task_new(tid_bit);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006986 if (!task) {
6987 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
6988 ctx->rule->arg.hlua_rule->fcn.name);
6989 return 0;
6990 }
6991 task->nice = 0;
6992 task->context = ctx;
6993 task->process = hlua_applet_wakeup;
6994 ctx->ctx.hlua_apptcp.task = task;
6995
6996 /* In the execution wrappers linked with a stream, the
6997 * Lua context can be not initialized. This behavior
6998 * permits to save performances because a systematic
6999 * Lua initialization cause 5% performances loss.
7000 */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01007001 if (!hlua_ctx_init(hlua, task, 0)) {
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007002 SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n",
7003 ctx->rule->arg.hlua_rule->fcn.name);
7004 return 0;
7005 }
7006
7007 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02007008 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007009
7010 /* The following Lua calls can fail. */
7011 if (!SET_SAFE_LJMP(hlua->T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01007012 if (lua_type(hlua->T, -1) == LUA_TSTRING)
7013 error = lua_tostring(hlua->T, -1);
7014 else
7015 error = "critical error";
7016 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
7017 ctx->rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007018 return 0;
7019 }
7020
7021 /* Check stack available size. */
7022 if (!lua_checkstack(hlua->T, 1)) {
7023 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
7024 ctx->rule->arg.hlua_rule->fcn.name);
7025 RESET_SAFE_LJMP(hlua->T);
7026 return 0;
7027 }
7028
7029 /* Restore the function in the stack. */
7030 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref);
7031
7032 /* Create and and push object stream in the stack. */
7033 if (!hlua_applet_tcp_new(hlua->T, ctx)) {
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 hlua->nargs = 1;
7040
7041 /* push keywords in the stack. */
7042 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
7043 if (!lua_checkstack(hlua->T, 1)) {
7044 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
7045 ctx->rule->arg.hlua_rule->fcn.name);
7046 RESET_SAFE_LJMP(hlua->T);
7047 return 0;
7048 }
7049 lua_pushstring(hlua->T, *arg);
7050 hlua->nargs++;
7051 }
7052
7053 RESET_SAFE_LJMP(hlua->T);
7054
7055 /* Wakeup the applet ASAP. */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01007056 si_cant_get(si);
Willy Tarreau3367d412018-11-15 10:57:41 +01007057 si_rx_endp_more(si);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007058
7059 return 1;
7060}
7061
Willy Tarreau83a5ff42019-08-21 14:14:50 +02007062void hlua_applet_tcp_fct(struct appctx *ctx)
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007063{
7064 struct stream_interface *si = ctx->owner;
7065 struct stream *strm = si_strm(si);
7066 struct channel *res = si_ic(si);
7067 struct act_rule *rule = ctx->rule;
7068 struct proxy *px = strm->be;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007069 struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007070
7071 /* The applet execution is already done. */
Olivier Houchard594c8c52018-08-28 14:41:31 +02007072 if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) {
7073 /* eat the whole request */
7074 co_skip(si_oc(si), co_data(si_oc(si)));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007075 return;
Olivier Houchard594c8c52018-08-28 14:41:31 +02007076 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007077
7078 /* If the stream is disconnect or closed, ldo nothing. */
7079 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
7080 return;
7081
7082 /* Execute the function. */
7083 switch (hlua_ctx_resume(hlua, 1)) {
7084 /* finished. */
7085 case HLUA_E_OK:
7086 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
7087
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007088 /* eat the whole request */
Willy Tarreaua79021a2018-06-15 18:07:57 +02007089 co_skip(si_oc(si), co_data(si_oc(si)));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007090 res->flags |= CF_READ_NULL;
7091 si_shutr(si);
7092 return;
7093
7094 /* yield. */
7095 case HLUA_E_AGAIN:
Thierry Fournier0164f202016-02-20 17:47:43 +01007096 if (hlua->wake_time != TICK_ETERNITY)
7097 task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007098 return;
7099
7100 /* finished with error. */
7101 case HLUA_E_ERRMSG:
7102 /* Display log. */
7103 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
7104 rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1));
7105 lua_pop(hlua->T, 1);
7106 goto error;
7107
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007108 case HLUA_E_ETMOUT:
7109 SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n",
7110 rule->arg.hlua_rule->fcn.name);
7111 goto error;
7112
7113 case HLUA_E_NOMEM:
7114 SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n",
7115 rule->arg.hlua_rule->fcn.name);
7116 goto error;
7117
7118 case HLUA_E_YIELD: /* unexpected */
7119 SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n",
7120 rule->arg.hlua_rule->fcn.name);
7121 goto error;
7122
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007123 case HLUA_E_ERR:
7124 /* Display log. */
7125 SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n",
7126 rule->arg.hlua_rule->fcn.name);
7127 goto error;
7128
7129 default:
7130 goto error;
7131 }
7132
7133error:
7134
7135 /* For all other cases, just close the stream. */
7136 si_shutw(si);
7137 si_shutr(si);
7138 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
7139}
7140
7141static void hlua_applet_tcp_release(struct appctx *ctx)
7142{
Olivier Houchard3f795f72019-04-17 22:51:06 +02007143 task_destroy(ctx->ctx.hlua_apptcp.task);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007144 ctx->ctx.hlua_apptcp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007145 hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007146 ctx->ctx.hlua_apptcp.hlua = NULL;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007147}
7148
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007149/* The function returns 1 if the initialisation is complete, 0 if
7150 * an errors occurs and -1 if more data are required for initializing
7151 * the applet.
7152 */
7153static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
7154{
7155 struct stream_interface *si = ctx->owner;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007156 struct http_txn *txn;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007157 struct hlua *hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007158 char **arg;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007159 struct task *task;
Thierry Fournierfd107a22016-02-19 19:57:23 +01007160 const char *error;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007161
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007162 txn = strm->txn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007163
Willy Tarreau0078bfc2015-10-07 20:20:28 +02007164 /* We want two things in HTTP mode :
7165 * - enforce server-close mode if we were in keep-alive, so that the
7166 * applet is released after each response ;
7167 * - enable request body transfer to the applet in order to resync
7168 * with the response body.
7169 */
7170 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)
7171 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreau0078bfc2015-10-07 20:20:28 +02007172
Willy Tarreaubafbe012017-11-24 17:34:44 +01007173 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007174 if (!hlua) {
7175 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
7176 ctx->rule->arg.hlua_rule->fcn.name);
7177 return 0;
7178 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007179 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007180 ctx->ctx.hlua_apphttp.hlua = hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007181 ctx->ctx.hlua_apphttp.left_bytes = -1;
7182 ctx->ctx.hlua_apphttp.flags = 0;
7183
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +01007184 if (txn->req.flags & HTTP_MSGF_VER_11)
7185 ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11;
7186
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007187 /* Create task used by signal to wakeup applets. */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01007188 task = task_new(tid_bit);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007189 if (!task) {
7190 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
7191 ctx->rule->arg.hlua_rule->fcn.name);
7192 return 0;
7193 }
7194 task->nice = 0;
7195 task->context = ctx;
7196 task->process = hlua_applet_wakeup;
7197 ctx->ctx.hlua_apphttp.task = task;
7198
7199 /* In the execution wrappers linked with a stream, the
7200 * Lua context can be not initialized. This behavior
7201 * permits to save performances because a systematic
7202 * Lua initialization cause 5% performances loss.
7203 */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01007204 if (!hlua_ctx_init(hlua, task, 0)) {
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007205 SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n",
7206 ctx->rule->arg.hlua_rule->fcn.name);
7207 return 0;
7208 }
7209
7210 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02007211 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007212
7213 /* The following Lua calls can fail. */
7214 if (!SET_SAFE_LJMP(hlua->T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01007215 if (lua_type(hlua->T, -1) == LUA_TSTRING)
7216 error = lua_tostring(hlua->T, -1);
7217 else
7218 error = "critical error";
7219 SEND_ERR(px, "Lua applet http '%s': %s.\n",
7220 ctx->rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007221 return 0;
7222 }
7223
7224 /* Check stack available size. */
7225 if (!lua_checkstack(hlua->T, 1)) {
7226 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
7227 ctx->rule->arg.hlua_rule->fcn.name);
7228 RESET_SAFE_LJMP(hlua->T);
7229 return 0;
7230 }
7231
7232 /* Restore the function in the stack. */
7233 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref);
7234
7235 /* Create and and push object stream in the stack. */
7236 if (!hlua_applet_http_new(hlua->T, ctx)) {
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 hlua->nargs = 1;
7243
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007244 /* push keywords in the stack. */
7245 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
7246 if (!lua_checkstack(hlua->T, 1)) {
7247 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
7248 ctx->rule->arg.hlua_rule->fcn.name);
7249 RESET_SAFE_LJMP(hlua->T);
7250 return 0;
7251 }
7252 lua_pushstring(hlua->T, *arg);
7253 hlua->nargs++;
7254 }
7255
7256 RESET_SAFE_LJMP(hlua->T);
7257
7258 /* Wakeup the applet when data is ready for read. */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01007259 si_cant_get(si);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007260
7261 return 1;
7262}
7263
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007264static void hlua_applet_htx_fct(struct appctx *ctx)
7265{
7266 struct stream_interface *si = ctx->owner;
7267 struct stream *strm = si_strm(si);
7268 struct channel *req = si_oc(si);
7269 struct channel *res = si_ic(si);
7270 struct act_rule *rule = ctx->rule;
7271 struct proxy *px = strm->be;
7272 struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua;
7273 struct htx *req_htx, *res_htx;
7274
7275 res_htx = htx_from_buf(&res->buf);
7276
7277 /* If the stream is disconnect or closed, ldo nothing. */
7278 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
7279 goto out;
7280
7281 /* Check if the input buffer is avalaible. */
7282 if (!b_size(&res->buf)) {
7283 si_rx_room_blk(si);
7284 goto out;
7285 }
7286 /* check that the output is not closed */
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007287 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007288 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7289
7290 /* Set the currently running flag. */
7291 if (!HLUA_IS_RUNNING(hlua) &&
7292 !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
7293 struct htx_blk *blk;
7294 size_t count = co_data(req);
7295
7296 if (!count) {
7297 si_cant_get(si);
7298 goto out;
7299 }
7300
7301 /* We need to flush the request header. This left the body for
7302 * the Lua.
7303 */
7304 req_htx = htx_from_buf(&req->buf);
Christopher Fauleta3f15502019-05-13 15:27:23 +02007305 blk = htx_get_first_blk(req_htx);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007306 while (count && blk) {
7307 enum htx_blk_type type = htx_get_blk_type(blk);
7308 uint32_t sz = htx_get_blksz(blk);
7309
7310 if (sz > count) {
7311 si_cant_get(si);
Christopher Faulet0ae79d02019-02-27 21:36:59 +01007312 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007313 goto out;
7314 }
7315
7316 count -= sz;
7317 co_set_data(req, co_data(req) - sz);
7318 blk = htx_remove_blk(req_htx, blk);
7319
7320 if (type == HTX_BLK_EOH)
7321 break;
7322 }
Christopher Faulet0ae79d02019-02-27 21:36:59 +01007323 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007324 }
7325
7326 /* Executes The applet if it is not done. */
7327 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
7328
7329 /* Execute the function. */
7330 switch (hlua_ctx_resume(hlua, 1)) {
7331 /* finished. */
7332 case HLUA_E_OK:
7333 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7334 break;
7335
7336 /* yield. */
7337 case HLUA_E_AGAIN:
7338 if (hlua->wake_time != TICK_ETERNITY)
7339 task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time);
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007340 goto out;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007341
7342 /* finished with error. */
7343 case HLUA_E_ERRMSG:
7344 /* Display log. */
7345 SEND_ERR(px, "Lua applet http '%s': %s.\n",
7346 rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1));
7347 lua_pop(hlua->T, 1);
7348 goto error;
7349
7350 case HLUA_E_ETMOUT:
7351 SEND_ERR(px, "Lua applet http '%s': execution timeout.\n",
7352 rule->arg.hlua_rule->fcn.name);
7353 goto error;
7354
7355 case HLUA_E_NOMEM:
7356 SEND_ERR(px, "Lua applet http '%s': out of memory error.\n",
7357 rule->arg.hlua_rule->fcn.name);
7358 goto error;
7359
7360 case HLUA_E_YIELD: /* unexpected */
7361 SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n",
7362 rule->arg.hlua_rule->fcn.name);
7363 goto error;
7364
7365 case HLUA_E_ERR:
7366 /* Display log. */
7367 SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n",
7368 rule->arg.hlua_rule->fcn.name);
7369 goto error;
7370
7371 default:
7372 goto error;
7373 }
7374 }
7375
7376 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007377 if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT)
7378 goto done;
7379
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007380 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT))
7381 goto error;
7382
Christopher Faulet54b5e212019-06-04 10:08:28 +02007383 /* Don't add TLR because mux-h1 will take care of it */
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007384 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
7385 si_rx_room_blk(si);
7386 goto out;
7387 }
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01007388 channel_add_input(res, 1);
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007389 strm->txn->status = ctx->ctx.hlua_apphttp.status;
7390 ctx->ctx.hlua_apphttp.flags |= APPLET_RSP_SENT;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007391 }
7392
7393 done:
7394 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007395 if (!(res->flags & CF_SHUTR)) {
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007396 res->flags |= CF_READ_NULL;
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007397 si_shutr(si);
7398 }
7399
7400 /* eat the whole request */
7401 if (co_data(req)) {
7402 req_htx = htx_from_buf(&req->buf);
7403 co_htx_skip(req, req_htx, co_data(req));
7404 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007405 }
7406 }
7407
7408 out:
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007409 htx_to_buf(res_htx, &res->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007410 return;
7411
7412 error:
7413
7414 /* If we are in HTTP mode, and we are not send any
7415 * data, return a 500 server error in best effort:
7416 * if there is no room available in the buffer,
7417 * just close the connection.
7418 */
7419 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
7420 struct buffer *err = &htx_err_chunks[HTTP_ERR_500];
7421
7422 channel_erase(res);
7423 res->buf.data = b_data(err);
7424 memcpy(res->buf.area, b_head(err), b_data(err));
7425 res_htx = htx_from_buf(&res->buf);
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01007426 channel_add_input(res, res_htx->data);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007427 }
7428 if (!(strm->flags & SF_ERR_MASK))
7429 strm->flags |= SF_ERR_RESOURCE;
7430 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7431 goto done;
7432}
7433
Willy Tarreau83a5ff42019-08-21 14:14:50 +02007434void hlua_applet_http_fct(struct appctx *ctx)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007435{
7436 struct stream_interface *si = ctx->owner;
7437 struct stream *strm = si_strm(si);
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007438 struct channel *req = si_oc(si);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007439 struct channel *res = si_ic(si);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007440 struct act_rule *rule = ctx->rule;
7441 struct proxy *px = strm->be;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007442 struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua;
Willy Tarreau206ba832018-06-14 15:27:31 +02007443 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02007444 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02007445 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02007446 size_t len2;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007447 int ret;
7448
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007449 if (IS_HTX_STRM(strm))
7450 return hlua_applet_htx_fct(ctx);
7451
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007452 /* If the stream is disconnect or closed, ldo nothing. */
7453 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007454 goto out;
7455
7456 /* Check if the input buffer is avalaible. */
7457 if (!b_size(&res->buf)) {
7458 si_rx_room_blk(si);
7459 goto out;
7460 }
7461 /* check that the output is not closed */
7462 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
7463 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007464
7465 /* Set the currently running flag. */
7466 if (!HLUA_IS_RUNNING(hlua) &&
7467 !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007468 /* Store the max amount of bytes that we can read. */
7469 ctx->ctx.hlua_apphttp.left_bytes = strm->txn->req.body_len;
7470
7471 /* We need to flush the request header. This left the body
7472 * for the Lua.
7473 */
7474
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007475 /* Read the maximum amount of data available. */
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007476 ret = co_getblk_nc(req, &blk1, &len1, &blk2, &len2);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007477 if (ret == -1)
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007478 goto out;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007479
7480 /* No data available, ask for more data. */
7481 if (ret == 1)
7482 len2 = 0;
7483 if (ret == 0)
7484 len1 = 0;
Thierry FOURNIER70d318c2018-06-30 10:37:33 +02007485 if (len1 + len2 < strm->txn->req.eoh + strm->txn->req.eol) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01007486 si_cant_get(si);
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007487 goto out;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007488 }
7489
7490 /* skip the requests bytes. */
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007491 co_skip(req, strm->txn->req.eoh + strm->txn->req.eol);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007492 }
7493
7494 /* Executes The applet if it is not done. */
7495 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
7496
7497 /* Execute the function. */
7498 switch (hlua_ctx_resume(hlua, 1)) {
7499 /* finished. */
7500 case HLUA_E_OK:
7501 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7502 break;
7503
7504 /* yield. */
7505 case HLUA_E_AGAIN:
Thierry Fournier0164f202016-02-20 17:47:43 +01007506 if (hlua->wake_time != TICK_ETERNITY)
7507 task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time);
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007508 goto out;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007509
7510 /* finished with error. */
7511 case HLUA_E_ERRMSG:
7512 /* Display log. */
7513 SEND_ERR(px, "Lua applet http '%s': %s.\n",
7514 rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1));
7515 lua_pop(hlua->T, 1);
7516 goto error;
7517
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007518 case HLUA_E_ETMOUT:
7519 SEND_ERR(px, "Lua applet http '%s': execution timeout.\n",
7520 rule->arg.hlua_rule->fcn.name);
7521 goto error;
7522
7523 case HLUA_E_NOMEM:
7524 SEND_ERR(px, "Lua applet http '%s': out of memory error.\n",
7525 rule->arg.hlua_rule->fcn.name);
7526 goto error;
7527
7528 case HLUA_E_YIELD: /* unexpected */
7529 SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n",
7530 rule->arg.hlua_rule->fcn.name);
7531 goto error;
7532
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007533 case HLUA_E_ERR:
7534 /* Display log. */
7535 SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n",
7536 rule->arg.hlua_rule->fcn.name);
7537 goto error;
7538
7539 default:
7540 goto error;
7541 }
7542 }
7543
7544 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007545 if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT)
7546 goto done;
7547
Christopher Fauletcc26b132018-12-18 21:20:57 +01007548 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT))
7549 goto error;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007550
7551 /* We must send the final chunk. */
7552 if (ctx->ctx.hlua_apphttp.flags & APPLET_CHUNKED &&
7553 !(ctx->ctx.hlua_apphttp.flags & APPLET_LAST_CHK)) {
7554
7555 /* sent last chunk at once. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02007556 ret = ci_putblk(res, "0\r\n\r\n", 5);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007557
7558 /* critical error. */
7559 if (ret == -2 || ret == -3) {
7560 SEND_ERR(px, "Lua applet http '%s'cannont send last chunk.\n",
7561 rule->arg.hlua_rule->fcn.name);
7562 goto error;
7563 }
7564
7565 /* no enough space error. */
7566 if (ret == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01007567 si_rx_room_blk(si);
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007568 goto out;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007569 }
7570
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007571 strm->txn->status = ctx->ctx.hlua_apphttp.status;
7572 ctx->ctx.hlua_apphttp.flags |= (APPLET_LAST_CHK|APPLET_RSP_SENT);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007573 }
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007574 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007575
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007576 done:
7577 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
7578 if (!(res->flags & CF_SHUTR)) {
7579 res->flags |= CF_READ_NULL;
7580 si_shutr(si);
7581 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007582
7583 /* eat the whole request */
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007584 if (co_data(req))
7585 co_skip(req, co_data(req));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007586 }
7587
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007588 out:
7589 return;
7590
7591 error:
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007592
7593 /* If we are in HTTP mode, and we are not send any
7594 * data, return a 500 server error in best effort:
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007595 * if there is no room available in the buffer,
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007596 * just close the connection.
7597 */
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007598 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
7599 channel_erase(res);
7600 ci_putblk(res, error_500, strlen(error_500));
7601 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007602 if (!(strm->flags & SF_ERR_MASK))
7603 strm->flags |= SF_ERR_RESOURCE;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007604 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007605 goto done;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007606}
7607
7608static void hlua_applet_http_release(struct appctx *ctx)
7609{
Olivier Houchard3f795f72019-04-17 22:51:06 +02007610 task_destroy(ctx->ctx.hlua_apphttp.task);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007611 ctx->ctx.hlua_apphttp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007612 hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007613 ctx->ctx.hlua_apphttp.hlua = NULL;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007614}
7615
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007616/* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in
7617 * succes case, else return ACT_RET_PRS_ERR.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007618 *
7619 * This function can fail with an abort() due to an Lua critical error.
7620 * We are in the configuration parsing process of HAProxy, this abort() is
7621 * tolerated.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007622 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007623static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px,
7624 struct act_rule *rule, char **err)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007625{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007626 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007627 int i;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007628
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007629 /* Memory for the rule. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007630 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007631 if (!rule->arg.hlua_rule) {
7632 memprintf(err, "out of memory error");
Thierry FOURNIERafa80492015-08-19 09:04:15 +02007633 return ACT_RET_PRS_ERR;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007634 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007635
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007636 /* Memory for arguments. */
7637 rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, sizeof(char *));
7638 if (!rule->arg.hlua_rule->args) {
7639 memprintf(err, "out of memory error");
7640 return ACT_RET_PRS_ERR;
7641 }
7642
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007643 /* Reference the Lua function and store the reference. */
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007644 rule->arg.hlua_rule->fcn = *fcn;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007645
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007646 /* Expect some arguments */
7647 for (i = 0; i < fcn->nargs; i++) {
Thierry FOURNIER1725c2e2019-01-06 19:38:49 +01007648 if (*args[*cur_arg] == '\0') {
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007649 memprintf(err, "expect %d arguments", fcn->nargs);
7650 return ACT_RET_PRS_ERR;
7651 }
Thierry FOURNIER1725c2e2019-01-06 19:38:49 +01007652 rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]);
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007653 if (!rule->arg.hlua_rule->args[i]) {
7654 memprintf(err, "out of memory error");
7655 return ACT_RET_PRS_ERR;
7656 }
7657 (*cur_arg)++;
7658 }
7659 rule->arg.hlua_rule->args[i] = NULL;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007660
Thierry FOURNIER42148732015-09-02 17:17:33 +02007661 rule->action = ACT_CUSTOM;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007662 rule->action_ptr = hlua_action;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02007663 return ACT_RET_PRS_OK;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007664}
7665
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007666static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px,
7667 struct act_rule *rule, char **err)
7668{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007669 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007670
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01007671 /* HTTP applets are forbidden in tcp-request rules.
7672 * HTTP applet request requires everything initilized by
7673 * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER).
7674 * The applet will be immediately initilized, but its before
7675 * the call of this analyzer.
7676 */
7677 if (rule->from != ACT_F_HTTP_REQ) {
7678 memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets");
7679 return ACT_RET_PRS_ERR;
7680 }
7681
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007682 /* Memory for the rule. */
7683 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
7684 if (!rule->arg.hlua_rule) {
7685 memprintf(err, "out of memory error");
7686 return ACT_RET_PRS_ERR;
7687 }
7688
7689 /* Reference the Lua function and store the reference. */
7690 rule->arg.hlua_rule->fcn = *fcn;
7691
7692 /* TODO: later accept arguments. */
7693 rule->arg.hlua_rule->args = NULL;
7694
7695 /* Add applet pointer in the rule. */
7696 rule->applet.obj_type = OBJ_TYPE_APPLET;
7697 rule->applet.name = fcn->name;
7698 rule->applet.init = hlua_applet_http_init;
7699 rule->applet.fct = hlua_applet_http_fct;
7700 rule->applet.release = hlua_applet_http_release;
7701 rule->applet.timeout = hlua_timeout_applet;
7702
7703 return ACT_RET_PRS_OK;
7704}
7705
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007706/* This function is an LUA binding used for registering
7707 * "sample-conv" functions. It expects a converter name used
7708 * in the haproxy configuration file, and an LUA function.
7709 */
7710__LJMP static int hlua_register_action(lua_State *L)
7711{
7712 struct action_kw_list *akl;
7713 const char *name;
7714 int ref;
7715 int len;
7716 struct hlua_function *fcn;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007717 int nargs;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007718
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007719 /* Initialise the number of expected arguments at 0. */
7720 nargs = 0;
7721
7722 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
7723 WILL_LJMP(luaL_error(L, "'register_action' needs between 3 and 4 arguments"));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007724
7725 /* First argument : converter name. */
7726 name = MAY_LJMP(luaL_checkstring(L, 1));
7727
7728 /* Second argument : environment. */
7729 if (lua_type(L, 2) != LUA_TTABLE)
7730 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
7731
7732 /* Third argument : lua function. */
7733 ref = MAY_LJMP(hlua_checkfunction(L, 3));
7734
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007735 /* Fourth argument : number of mandatory arguments expected on the configuration line. */
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007736 if (lua_gettop(L) >= 4)
7737 nargs = MAY_LJMP(luaL_checkinteger(L, 4));
7738
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007739 /* browse the second argument as an array. */
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007740 lua_pushnil(L);
7741 while (lua_next(L, 2) != 0) {
7742 if (lua_type(L, -1) != LUA_TSTRING)
7743 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
7744
7745 /* Check required environment. Only accepted "http" or "tcp". */
7746 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007747 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007748 if (!akl)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007749 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007750 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007751 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007752 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007753
7754 /* Fill fcn. */
7755 fcn->name = strdup(name);
7756 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007757 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007758 fcn->function_ref = ref;
7759
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007760 /* Set the expected number od arguments. */
7761 fcn->nargs = nargs;
7762
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007763 /* List head */
7764 akl->list.n = akl->list.p = NULL;
7765
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007766 /* action keyword. */
7767 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007768 akl->kw[0].kw = calloc(1, len);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007769 if (!akl->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007770 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007771
7772 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
7773
7774 akl->kw[0].match_pfx = 0;
7775 akl->kw[0].private = fcn;
7776 akl->kw[0].parse = action_register_lua;
7777
7778 /* select the action registering point. */
7779 if (strcmp(lua_tostring(L, -1), "tcp-req") == 0)
7780 tcp_req_cont_keywords_register(akl);
7781 else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0)
7782 tcp_res_cont_keywords_register(akl);
7783 else if (strcmp(lua_tostring(L, -1), "http-req") == 0)
7784 http_req_keywords_register(akl);
7785 else if (strcmp(lua_tostring(L, -1), "http-res") == 0)
7786 http_res_keywords_register(akl);
7787 else
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007788 WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. "
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007789 "'tcp-req', 'tcp-res', 'http-req' or 'http-res' "
7790 "are expected.", lua_tostring(L, -1)));
7791
7792 /* pop the environment string. */
7793 lua_pop(L, 1);
7794 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007795 return ACT_RET_PRS_OK;
7796}
7797
7798static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px,
7799 struct act_rule *rule, char **err)
7800{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007801 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007802
Christopher Fauleteb709802019-04-11 22:04:08 +02007803 if (px->mode == PR_MODE_HTTP && (px->options2 & PR_O2_USE_HTX)) {
Christopher Fauletafd8f102018-11-08 11:34:21 +01007804 memprintf(err, "Lua services cannot be used when the HTX internal representation is enabled");
7805 return ACT_RET_PRS_ERR;
7806 }
7807
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007808 /* Memory for the rule. */
7809 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
7810 if (!rule->arg.hlua_rule) {
7811 memprintf(err, "out of memory error");
7812 return ACT_RET_PRS_ERR;
7813 }
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007814
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007815 /* Reference the Lua function and store the reference. */
7816 rule->arg.hlua_rule->fcn = *fcn;
7817
7818 /* TODO: later accept arguments. */
7819 rule->arg.hlua_rule->args = NULL;
7820
7821 /* Add applet pointer in the rule. */
7822 rule->applet.obj_type = OBJ_TYPE_APPLET;
7823 rule->applet.name = fcn->name;
7824 rule->applet.init = hlua_applet_tcp_init;
7825 rule->applet.fct = hlua_applet_tcp_fct;
7826 rule->applet.release = hlua_applet_tcp_release;
7827 rule->applet.timeout = hlua_timeout_applet;
7828
7829 return 0;
7830}
7831
7832/* This function is an LUA binding used for registering
7833 * "sample-conv" functions. It expects a converter name used
7834 * in the haproxy configuration file, and an LUA function.
7835 */
7836__LJMP static int hlua_register_service(lua_State *L)
7837{
7838 struct action_kw_list *akl;
7839 const char *name;
7840 const char *env;
7841 int ref;
7842 int len;
7843 struct hlua_function *fcn;
7844
7845 MAY_LJMP(check_args(L, 3, "register_service"));
7846
7847 /* First argument : converter name. */
7848 name = MAY_LJMP(luaL_checkstring(L, 1));
7849
7850 /* Second argument : environment. */
7851 env = MAY_LJMP(luaL_checkstring(L, 2));
7852
7853 /* Third argument : lua function. */
7854 ref = MAY_LJMP(hlua_checkfunction(L, 3));
7855
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007856 /* Allocate and fill the sample fetch keyword struct. */
7857 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
7858 if (!akl)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007859 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007860 fcn = calloc(1, sizeof(*fcn));
7861 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007862 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007863
7864 /* Fill fcn. */
7865 len = strlen("<lua.>") + strlen(name) + 1;
7866 fcn->name = calloc(1, len);
7867 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007868 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007869 snprintf((char *)fcn->name, len, "<lua.%s>", name);
7870 fcn->function_ref = ref;
7871
7872 /* List head */
7873 akl->list.n = akl->list.p = NULL;
7874
7875 /* converter keyword. */
7876 len = strlen("lua.") + strlen(name) + 1;
7877 akl->kw[0].kw = calloc(1, len);
7878 if (!akl->kw[0].kw)
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
7881 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
7882
Thierry FOURNIER / OZON.IO02564fd2016-11-12 11:07:05 +01007883 /* Check required environment. Only accepted "http" or "tcp". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007884 if (strcmp(env, "tcp") == 0)
7885 akl->kw[0].parse = action_register_service_tcp;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007886 else if (strcmp(env, "http") == 0)
7887 akl->kw[0].parse = action_register_service_http;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007888 else
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007889 WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. "
Eric Salamafe7456f2017-12-21 14:30:07 +01007890 "'tcp' or 'http' are expected.", env));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007891
7892 akl->kw[0].match_pfx = 0;
7893 akl->kw[0].private = fcn;
7894
7895 /* End of array. */
7896 memset(&akl->kw[1], 0, sizeof(*akl->kw));
7897
7898 /* Register this new converter */
7899 service_keywords_register(akl);
7900
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007901 return 0;
7902}
7903
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007904/* This function initialises Lua cli handler. It copies the
7905 * arguments in the Lua stack and create channel IO objects.
7906 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02007907static int hlua_cli_parse_fct(char **args, char *payload, struct appctx *appctx, void *private)
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007908{
7909 struct hlua *hlua;
7910 struct hlua_function *fcn;
7911 int i;
7912 const char *error;
7913
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007914 fcn = private;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007915 appctx->ctx.hlua_cli.fcn = private;
7916
Willy Tarreaubafbe012017-11-24 17:34:44 +01007917 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007918 if (!hlua) {
7919 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007920 return 1;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007921 }
7922 HLUA_INIT(hlua);
7923 appctx->ctx.hlua_cli.hlua = hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007924
7925 /* Create task used by signal to wakeup applets.
7926 * We use the same wakeup fonction than the Lua applet_tcp and
7927 * applet_http. It is absolutely compatible.
7928 */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01007929 appctx->ctx.hlua_cli.task = task_new(tid_bit);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007930 if (!appctx->ctx.hlua_cli.task) {
Thierry FOURNIERffbf5692016-12-16 11:14:06 +01007931 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007932 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007933 }
7934 appctx->ctx.hlua_cli.task->nice = 0;
7935 appctx->ctx.hlua_cli.task->context = appctx;
7936 appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup;
7937
7938 /* Initialises the Lua context */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01007939 if (!hlua_ctx_init(hlua, appctx->ctx.hlua_cli.task, 0)) {
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007940 SEND_ERR(NULL, "Lua cli '%s': can't initialize Lua context.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007941 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007942 }
7943
7944 /* The following Lua calls can fail. */
7945 if (!SET_SAFE_LJMP(hlua->T)) {
7946 if (lua_type(hlua->T, -1) == LUA_TSTRING)
7947 error = lua_tostring(hlua->T, -1);
7948 else
7949 error = "critical error";
7950 SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error);
7951 goto error;
7952 }
7953
7954 /* Check stack available size. */
7955 if (!lua_checkstack(hlua->T, 2)) {
7956 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7957 goto error;
7958 }
7959
7960 /* Restore the function in the stack. */
7961 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
7962
7963 /* Once the arguments parsed, the CLI is like an AppletTCP,
7964 * so push AppletTCP in the stack.
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007965 */
7966 if (!hlua_applet_tcp_new(hlua->T, appctx)) {
7967 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7968 goto error;
7969 }
7970 hlua->nargs = 1;
7971
7972 /* push keywords in the stack. */
7973 for (i = 0; *args[i]; i++) {
7974 /* Check stack available size. */
7975 if (!lua_checkstack(hlua->T, 1)) {
7976 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7977 goto error;
7978 }
7979 lua_pushstring(hlua->T, args[i]);
7980 hlua->nargs++;
7981 }
7982
7983 /* We must initialize the execution timeouts. */
7984 hlua->max_time = hlua_timeout_session;
7985
7986 /* At this point the execution is safe. */
7987 RESET_SAFE_LJMP(hlua->T);
7988
7989 /* It's ok */
7990 return 0;
7991
7992 /* It's not ok. */
7993error:
7994 RESET_SAFE_LJMP(hlua->T);
7995 hlua_ctx_destroy(hlua);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007996 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007997 return 1;
7998}
7999
8000static int hlua_cli_io_handler_fct(struct appctx *appctx)
8001{
8002 struct hlua *hlua;
8003 struct stream_interface *si;
8004 struct hlua_function *fcn;
8005
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01008006 hlua = appctx->ctx.hlua_cli.hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008007 si = appctx->owner;
Willy Tarreau8ae4f752016-12-14 15:41:45 +01008008 fcn = appctx->ctx.hlua_cli.fcn;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008009
8010 /* If the stream is disconnect or closed, ldo nothing. */
8011 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
8012 return 1;
8013
8014 /* Execute the function. */
8015 switch (hlua_ctx_resume(hlua, 1)) {
8016
8017 /* finished. */
8018 case HLUA_E_OK:
8019 return 1;
8020
8021 /* yield. */
8022 case HLUA_E_AGAIN:
8023 /* We want write. */
8024 if (HLUA_IS_WAKERESWR(hlua))
Willy Tarreaudb398432018-11-15 11:08:52 +01008025 si_rx_room_blk(si);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008026 /* Set the timeout. */
8027 if (hlua->wake_time != TICK_ETERNITY)
8028 task_schedule(hlua->task, hlua->wake_time);
8029 return 0;
8030
8031 /* finished with error. */
8032 case HLUA_E_ERRMSG:
8033 /* Display log. */
8034 SEND_ERR(NULL, "Lua cli '%s': %s.\n",
8035 fcn->name, lua_tostring(hlua->T, -1));
8036 lua_pop(hlua->T, 1);
8037 return 1;
8038
Thierry Fournierd5b073c2018-05-21 19:42:47 +02008039 case HLUA_E_ETMOUT:
8040 SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n",
8041 fcn->name);
8042 return 1;
8043
8044 case HLUA_E_NOMEM:
8045 SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n",
8046 fcn->name);
8047 return 1;
8048
8049 case HLUA_E_YIELD: /* unexpected */
8050 SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n",
8051 fcn->name);
8052 return 1;
8053
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008054 case HLUA_E_ERR:
8055 /* Display log. */
8056 SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n",
8057 fcn->name);
8058 return 1;
8059
8060 default:
8061 return 1;
8062 }
8063
8064 return 1;
8065}
8066
8067static void hlua_cli_io_release_fct(struct appctx *appctx)
8068{
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01008069 hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01008070 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008071}
8072
8073/* This function is an LUA binding used for registering
8074 * new keywords in the cli. It expects a list of keywords
8075 * which are the "path". It is limited to 5 keywords. A
8076 * description of the command, a function to be executed
8077 * for the parsing and a function for io handlers.
8078 */
8079__LJMP static int hlua_register_cli(lua_State *L)
8080{
8081 struct cli_kw_list *cli_kws;
8082 const char *message;
8083 int ref_io;
8084 int len;
8085 struct hlua_function *fcn;
8086 int index;
8087 int i;
8088
8089 MAY_LJMP(check_args(L, 3, "register_cli"));
8090
8091 /* First argument : an array of maximum 5 keywords. */
8092 if (!lua_istable(L, 1))
8093 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table"));
8094
8095 /* Second argument : string with contextual message. */
8096 message = MAY_LJMP(luaL_checkstring(L, 2));
8097
8098 /* Third and fourth argument : lua function. */
8099 ref_io = MAY_LJMP(hlua_checkfunction(L, 3));
8100
8101 /* Allocate and fill the sample fetch keyword struct. */
8102 cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2);
8103 if (!cli_kws)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008104 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008105 fcn = calloc(1, sizeof(*fcn));
8106 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008107 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008108
8109 /* Fill path. */
8110 index = 0;
8111 lua_pushnil(L);
8112 while(lua_next(L, 1) != 0) {
8113 if (index >= 5)
8114 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries"));
8115 if (lua_type(L, -1) != LUA_TSTRING)
8116 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings"));
8117 cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1));
8118 if (!cli_kws->kw[0].str_kw[index])
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008119 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008120 index++;
8121 lua_pop(L, 1);
8122 }
8123
8124 /* Copy help message. */
8125 cli_kws->kw[0].usage = strdup(message);
8126 if (!cli_kws->kw[0].usage)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008127 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008128
8129 /* Fill fcn io handler. */
8130 len = strlen("<lua.cli>") + 1;
8131 for (i = 0; i < index; i++)
8132 len += strlen(cli_kws->kw[0].str_kw[i]) + 1;
8133 fcn->name = calloc(1, len);
8134 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008135 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008136 strncat((char *)fcn->name, "<lua.cli", len);
8137 for (i = 0; i < index; i++) {
8138 strncat((char *)fcn->name, ".", len);
8139 strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len);
8140 }
8141 strncat((char *)fcn->name, ">", len);
8142 fcn->function_ref = ref_io;
8143
8144 /* Fill last entries. */
8145 cli_kws->kw[0].private = fcn;
8146 cli_kws->kw[0].parse = hlua_cli_parse_fct;
8147 cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct;
8148 cli_kws->kw[0].io_release = hlua_cli_io_release_fct;
8149
8150 /* Register this new converter */
8151 cli_register_kw(cli_kws);
8152
8153 return 0;
8154}
8155
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008156static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx,
8157 struct proxy *defpx, const char *file, int line,
8158 char **err, unsigned int *timeout)
8159{
8160 const char *error;
8161
8162 error = parse_time_err(args[1], timeout, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02008163 if (error == PARSE_TIME_OVER) {
8164 memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)",
8165 args[1], args[0]);
8166 return -1;
8167 }
8168 else if (error == PARSE_TIME_UNDER) {
8169 memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)",
8170 args[1], args[0]);
8171 return -1;
8172 }
8173 else if (error) {
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008174 memprintf(err, "%s: invalid timeout", args[0]);
8175 return -1;
8176 }
8177 return 0;
8178}
8179
8180static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx,
8181 struct proxy *defpx, const char *file, int line,
8182 char **err)
8183{
8184 return hlua_read_timeout(args, section_type, curpx, defpx,
8185 file, line, err, &hlua_timeout_session);
8186}
8187
8188static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx,
8189 struct proxy *defpx, const char *file, int line,
8190 char **err)
8191{
8192 return hlua_read_timeout(args, section_type, curpx, defpx,
8193 file, line, err, &hlua_timeout_task);
8194}
8195
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008196static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx,
8197 struct proxy *defpx, const char *file, int line,
8198 char **err)
8199{
8200 return hlua_read_timeout(args, section_type, curpx, defpx,
8201 file, line, err, &hlua_timeout_applet);
8202}
8203
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01008204static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx,
8205 struct proxy *defpx, const char *file, int line,
8206 char **err)
8207{
8208 char *error;
8209
8210 hlua_nb_instruction = strtoll(args[1], &error, 10);
8211 if (*error != '\0') {
8212 memprintf(err, "%s: invalid number", args[0]);
8213 return -1;
8214 }
8215 return 0;
8216}
8217
Willy Tarreau32f61e22015-03-18 17:54:59 +01008218static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx,
8219 struct proxy *defpx, const char *file, int line,
8220 char **err)
8221{
8222 char *error;
8223
8224 if (*(args[1]) == 0) {
8225 memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]);
8226 return -1;
8227 }
8228 hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L;
8229 if (*error != '\0') {
8230 memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error);
8231 return -1;
8232 }
8233 return 0;
8234}
8235
8236
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008237/* This function is called by the main configuration key "lua-load". It loads and
8238 * execute an lua file during the parsing of the HAProxy configuration file. It is
8239 * the main lua entry point.
8240 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08008241 * This function runs with the HAProxy keywords API. It returns -1 if an error
8242 * occurs, otherwise it returns 0.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008243 *
8244 * In some error case, LUA set an error message in top of the stack. This function
8245 * returns this error message in the HAProxy logs and pop it from the stack.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008246 *
8247 * This function can fail with an abort() due to an Lua critical error.
8248 * We are in the configuration parsing process of HAProxy, this abort() is
8249 * tolerated.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008250 */
8251static int hlua_load(char **args, int section_type, struct proxy *curpx,
8252 struct proxy *defpx, const char *file, int line,
8253 char **err)
8254{
8255 int error;
8256
8257 /* Just load and compile the file. */
8258 error = luaL_loadfile(gL.T, args[1]);
8259 if (error) {
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008260 memprintf(err, "error in Lua file '%s': %s", args[1], lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008261 lua_pop(gL.T, 1);
8262 return -1;
8263 }
8264
8265 /* If no syntax error where detected, execute the code. */
8266 error = lua_pcall(gL.T, 0, LUA_MULTRET, 0);
8267 switch (error) {
8268 case LUA_OK:
8269 break;
8270 case LUA_ERRRUN:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008271 memprintf(err, "Lua runtime error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008272 lua_pop(gL.T, 1);
8273 return -1;
8274 case LUA_ERRMEM:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008275 memprintf(err, "Lua out of memory error.n");
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008276 return -1;
8277 case LUA_ERRERR:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008278 memprintf(err, "Lua message handler error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008279 lua_pop(gL.T, 1);
8280 return -1;
8281 case LUA_ERRGCMM:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008282 memprintf(err, "Lua garbage collector 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 default:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008286 memprintf(err, "Lua unknonwn error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008287 lua_pop(gL.T, 1);
8288 return -1;
8289 }
8290
8291 return 0;
8292}
8293
8294/* configuration keywords declaration */
8295static struct cfg_kw_list cfg_kws = {{ },{
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008296 { CFG_GLOBAL, "lua-load", hlua_load },
8297 { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout },
8298 { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout },
Thierry FOURNIER56da1012015-10-01 08:42:31 +02008299 { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout },
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01008300 { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield },
Willy Tarreau32f61e22015-03-18 17:54:59 +01008301 { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem },
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008302 { 0, NULL, NULL },
8303}};
8304
Willy Tarreau0108d902018-11-25 19:14:37 +01008305INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
8306
Christopher Fauletafd8f102018-11-08 11:34:21 +01008307
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008308/* This function can fail with an abort() due to an Lua critical error.
8309 * We are in the initialisation process of HAProxy, this abort() is
8310 * tolerated.
8311 */
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008312int hlua_post_init()
8313{
8314 struct hlua_init_function *init;
8315 const char *msg;
8316 enum hlua_exec ret;
Thierry Fournierfd107a22016-02-19 19:57:23 +01008317 const char *error;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008318
Thierry Fournier3d4a6752016-02-19 20:53:30 +01008319 /* Call post initialisation function in safe environement. */
8320 if (!SET_SAFE_LJMP(gL.T)) {
8321 if (lua_type(gL.T, -1) == LUA_TSTRING)
8322 error = lua_tostring(gL.T, -1);
8323 else
8324 error = "critical error";
8325 fprintf(stderr, "Lua post-init: %s.\n", error);
8326 exit(1);
8327 }
Frédéric Lécaille54f2bcf2018-08-29 13:46:24 +02008328
8329#if USE_OPENSSL
8330 /* Initialize SSL server. */
8331 if (socket_ssl.xprt->prepare_srv) {
8332 int saved_used_backed = global.ssl_used_backend;
8333 // don't affect maxconn automatic computation
8334 socket_ssl.xprt->prepare_srv(&socket_ssl);
8335 global.ssl_used_backend = saved_used_backed;
8336 }
8337#endif
8338
Thierry Fournier3d4a6752016-02-19 20:53:30 +01008339 hlua_fcn_post_init(gL.T);
8340 RESET_SAFE_LJMP(gL.T);
8341
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008342 list_for_each_entry(init, &hlua_init_functions, l) {
8343 lua_rawgeti(gL.T, LUA_REGISTRYINDEX, init->function_ref);
8344 ret = hlua_ctx_resume(&gL, 0);
8345 switch (ret) {
8346 case HLUA_E_OK:
8347 lua_pop(gL.T, -1);
8348 return 1;
8349 case HLUA_E_AGAIN:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008350 ha_alert("Lua init: yield not allowed.\n");
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008351 return 0;
8352 case HLUA_E_ERRMSG:
8353 msg = lua_tostring(gL.T, -1);
Christopher Faulet767a84b2017-11-24 16:50:31 +01008354 ha_alert("lua init: %s.\n", msg);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008355 return 0;
8356 case HLUA_E_ERR:
8357 default:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008358 ha_alert("Lua init: unknown runtime error.\n");
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008359 return 0;
8360 }
8361 }
8362 return 1;
8363}
8364
Willy Tarreau32f61e22015-03-18 17:54:59 +01008365/* The memory allocator used by the Lua stack. <ud> is a pointer to the
8366 * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize>
8367 * is the previously allocated size or the kind of object in case of a new
8368 * allocation. <nsize> is the requested new size.
8369 */
8370static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
8371{
8372 struct hlua_mem_allocator *zone = ud;
8373
8374 if (nsize == 0) {
8375 /* it's a free */
8376 if (ptr)
8377 zone->allocated -= osize;
8378 free(ptr);
8379 return NULL;
8380 }
8381
8382 if (!ptr) {
8383 /* it's a new allocation */
8384 if (zone->limit && zone->allocated + nsize > zone->limit)
8385 return NULL;
8386
8387 ptr = malloc(nsize);
8388 if (ptr)
8389 zone->allocated += nsize;
8390 return ptr;
8391 }
8392
8393 /* it's a realloc */
8394 if (zone->limit && zone->allocated + nsize - osize > zone->limit)
8395 return NULL;
8396
8397 ptr = realloc(ptr, nsize);
8398 if (ptr)
8399 zone->allocated += nsize - osize;
8400 return ptr;
8401}
8402
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008403/* Ithis function can fail with an abort() due to an Lua critical error.
8404 * We are in the initialisation process of HAProxy, this abort() is
8405 * tolerated.
8406 */
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01008407void hlua_init(void)
8408{
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008409 int i;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008410 int idx;
8411 struct sample_fetch *sf;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008412 struct sample_conv *sc;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008413 char *p;
Thierry Fournierfd107a22016-02-19 19:57:23 +01008414 const char *error_msg;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008415#ifdef USE_OPENSSL
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008416 struct srv_kw *kw;
8417 int tmp_error;
8418 char *error;
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008419 char *args[] = { /* SSL client configuration. */
8420 "ssl",
8421 "verify",
8422 "none",
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008423 NULL
8424 };
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008425#endif
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008426
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008427 /* Init main lua stack. */
8428 gL.Mref = LUA_REFNIL;
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01008429 gL.flags = 0;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01008430 LIST_INIT(&gL.com);
Willy Tarreau42ef75f2017-04-12 21:40:29 +02008431 gL.T = lua_newstate(hlua_alloc, &hlua_global_allocator);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008432 hlua_sethlua(&gL);
8433 gL.Tref = LUA_REFNIL;
8434 gL.task = NULL;
8435
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08008436 /* From this point, until the end of the initialisation function,
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008437 * the Lua function can fail with an abort. We are in the initialisation
8438 * process of HAProxy, this abort() is tolerated.
8439 */
8440
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008441 /* Initialise lua. */
8442 luaL_openlibs(gL.T);
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008443
Thierry Fournier75933d42016-01-21 09:30:18 +01008444 /* Set safe environment for the initialisation. */
8445 if (!SET_SAFE_LJMP(gL.T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01008446 if (lua_type(gL.T, -1) == LUA_TSTRING)
8447 error_msg = lua_tostring(gL.T, -1);
8448 else
8449 error_msg = "critical error";
8450 fprintf(stderr, "Lua init: %s.\n", error_msg);
Thierry Fournier75933d42016-01-21 09:30:18 +01008451 exit(1);
8452 }
8453
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008454 /*
8455 *
8456 * Create "core" object.
8457 *
8458 */
8459
Thierry FOURNIERa2d8c652015-03-11 17:29:39 +01008460 /* This table entry is the object "core" base. */
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008461 lua_newtable(gL.T);
8462
8463 /* Push the loglevel constants. */
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008464 for (i = 0; i < NB_LOG_LEVELS; i++)
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008465 hlua_class_const_int(gL.T, log_levels[i], i);
8466
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008467 /* Register special functions. */
8468 hlua_class_function(gL.T, "register_init", hlua_register_init);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01008469 hlua_class_function(gL.T, "register_task", hlua_register_task);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01008470 hlua_class_function(gL.T, "register_fetches", hlua_register_fetches);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01008471 hlua_class_function(gL.T, "register_converters", hlua_register_converters);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02008472 hlua_class_function(gL.T, "register_action", hlua_register_action);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008473 hlua_class_function(gL.T, "register_service", hlua_register_service);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008474 hlua_class_function(gL.T, "register_cli", hlua_register_cli);
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01008475 hlua_class_function(gL.T, "yield", hlua_yield);
Willy Tarreau59551662015-03-10 14:23:13 +01008476 hlua_class_function(gL.T, "set_nice", hlua_set_nice);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01008477 hlua_class_function(gL.T, "sleep", hlua_sleep);
8478 hlua_class_function(gL.T, "msleep", hlua_msleep);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01008479 hlua_class_function(gL.T, "add_acl", hlua_add_acl);
8480 hlua_class_function(gL.T, "del_acl", hlua_del_acl);
8481 hlua_class_function(gL.T, "set_map", hlua_set_map);
8482 hlua_class_function(gL.T, "del_map", hlua_del_map);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008483 hlua_class_function(gL.T, "tcp", hlua_socket_new);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01008484 hlua_class_function(gL.T, "log", hlua_log);
8485 hlua_class_function(gL.T, "Debug", hlua_log_debug);
8486 hlua_class_function(gL.T, "Info", hlua_log_info);
8487 hlua_class_function(gL.T, "Warning", hlua_log_warning);
8488 hlua_class_function(gL.T, "Alert", hlua_log_alert);
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02008489 hlua_class_function(gL.T, "done", hlua_done);
Thierry Fournierfb0b5462016-01-21 09:28:58 +01008490 hlua_fcn_reg_core_fcn(gL.T);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008491
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008492 lua_setglobal(gL.T, "core");
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008493
8494 /*
8495 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008496 * Register class Map
8497 *
8498 */
8499
8500 /* This table entry is the object "Map" base. */
8501 lua_newtable(gL.T);
8502
8503 /* register pattern types. */
8504 for (i=0; i<PAT_MATCH_NUM; i++)
8505 hlua_class_const_int(gL.T, pat_match_names[i], i);
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01008506 for (i=0; i<PAT_MATCH_NUM; i++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008507 snprintf(trash.area, trash.size, "_%s", pat_match_names[i]);
8508 hlua_class_const_int(gL.T, trash.area, i);
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01008509 }
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008510
8511 /* register constructor. */
8512 hlua_class_function(gL.T, "new", hlua_map_new);
8513
8514 /* Create and fill the metatable. */
8515 lua_newtable(gL.T);
8516
8517 /* Create and fille the __index entry. */
8518 lua_pushstring(gL.T, "__index");
8519 lua_newtable(gL.T);
8520
8521 /* Register . */
8522 hlua_class_function(gL.T, "lookup", hlua_map_lookup);
8523 hlua_class_function(gL.T, "slookup", hlua_map_slookup);
8524
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008525 lua_rawset(gL.T, -3);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008526
Thierry Fournier45e78d72016-02-19 18:34:46 +01008527 /* Register previous table in the registry with reference and named entry.
8528 * The function hlua_register_metatable() pops the stack, so we
8529 * previously create a copy of the table.
8530 */
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008531 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008532 class_map_ref = hlua_register_metatable(gL.T, CLASS_MAP);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008533
8534 /* Assign the metatable to the mai Map object. */
8535 lua_setmetatable(gL.T, -2);
8536
8537 /* Set a name to the table. */
8538 lua_setglobal(gL.T, "Map");
8539
8540 /*
8541 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008542 * Register class Channel
8543 *
8544 */
8545
8546 /* Create and fill the metatable. */
8547 lua_newtable(gL.T);
8548
8549 /* Create and fille the __index entry. */
8550 lua_pushstring(gL.T, "__index");
8551 lua_newtable(gL.T);
8552
8553 /* Register . */
8554 hlua_class_function(gL.T, "get", hlua_channel_get);
8555 hlua_class_function(gL.T, "dup", hlua_channel_dup);
8556 hlua_class_function(gL.T, "getline", hlua_channel_getline);
8557 hlua_class_function(gL.T, "set", hlua_channel_set);
8558 hlua_class_function(gL.T, "append", hlua_channel_append);
8559 hlua_class_function(gL.T, "send", hlua_channel_send);
8560 hlua_class_function(gL.T, "forward", hlua_channel_forward);
8561 hlua_class_function(gL.T, "get_in_len", hlua_channel_get_in_len);
8562 hlua_class_function(gL.T, "get_out_len", hlua_channel_get_out_len);
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01008563 hlua_class_function(gL.T, "is_full", hlua_channel_is_full);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008564
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008565 lua_rawset(gL.T, -3);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008566
8567 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008568 class_channel_ref = hlua_register_metatable(gL.T, CLASS_CHANNEL);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008569
8570 /*
8571 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008572 * Register class Fetches
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008573 *
8574 */
8575
8576 /* Create and fill the metatable. */
8577 lua_newtable(gL.T);
8578
8579 /* Create and fille the __index entry. */
8580 lua_pushstring(gL.T, "__index");
8581 lua_newtable(gL.T);
8582
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008583 /* Browse existing fetches and create the associated
8584 * object method.
8585 */
8586 sf = NULL;
8587 while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) {
8588
8589 /* Dont register the keywork if the arguments check function are
8590 * not safe during the runtime.
8591 */
8592 if ((sf->val_args != NULL) &&
8593 (sf->val_args != val_payload_lv) &&
8594 (sf->val_args != val_hdr))
8595 continue;
8596
8597 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
8598 * by an underscore.
8599 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008600 strncpy(trash.area, sf->kw, trash.size);
8601 trash.area[trash.size - 1] = '\0';
8602 for (p = trash.area; *p; p++)
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008603 if (*p == '.' || *p == '-' || *p == '+')
8604 *p = '_';
8605
8606 /* Register the function. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008607 lua_pushstring(gL.T, trash.area);
Willy Tarreau2ec22742015-03-10 14:27:20 +01008608 lua_pushlightuserdata(gL.T, sf);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008609 lua_pushcclosure(gL.T, hlua_run_sample_fetch, 1);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008610 lua_rawset(gL.T, -3);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008611 }
8612
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008613 lua_rawset(gL.T, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008614
8615 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008616 class_fetches_ref = hlua_register_metatable(gL.T, CLASS_FETCHES);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008617
8618 /*
8619 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008620 * Register class Converters
8621 *
8622 */
8623
8624 /* Create and fill the metatable. */
8625 lua_newtable(gL.T);
8626
8627 /* Create and fill the __index entry. */
8628 lua_pushstring(gL.T, "__index");
8629 lua_newtable(gL.T);
8630
8631 /* Browse existing converters and create the associated
8632 * object method.
8633 */
8634 sc = NULL;
8635 while ((sc = sample_conv_getnext(sc, &idx)) != NULL) {
8636 /* Dont register the keywork if the arguments check function are
8637 * not safe during the runtime.
8638 */
8639 if (sc->val_args != NULL)
8640 continue;
8641
8642 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
8643 * by an underscore.
8644 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008645 strncpy(trash.area, sc->kw, trash.size);
8646 trash.area[trash.size - 1] = '\0';
8647 for (p = trash.area; *p; p++)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008648 if (*p == '.' || *p == '-' || *p == '+')
8649 *p = '_';
8650
8651 /* Register the function. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008652 lua_pushstring(gL.T, trash.area);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008653 lua_pushlightuserdata(gL.T, sc);
8654 lua_pushcclosure(gL.T, hlua_run_sample_conv, 1);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008655 lua_rawset(gL.T, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008656 }
8657
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008658 lua_rawset(gL.T, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008659
8660 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008661 class_converters_ref = hlua_register_metatable(gL.T, CLASS_CONVERTERS);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008662
8663 /*
8664 *
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008665 * Register class HTTP
8666 *
8667 */
8668
8669 /* Create and fill the metatable. */
8670 lua_newtable(gL.T);
8671
8672 /* Create and fille the __index entry. */
8673 lua_pushstring(gL.T, "__index");
8674 lua_newtable(gL.T);
8675
8676 /* Register Lua functions. */
8677 hlua_class_function(gL.T, "req_get_headers",hlua_http_req_get_headers);
8678 hlua_class_function(gL.T, "req_del_header", hlua_http_req_del_hdr);
8679 hlua_class_function(gL.T, "req_rep_header", hlua_http_req_rep_hdr);
8680 hlua_class_function(gL.T, "req_rep_value", hlua_http_req_rep_val);
8681 hlua_class_function(gL.T, "req_add_header", hlua_http_req_add_hdr);
8682 hlua_class_function(gL.T, "req_set_header", hlua_http_req_set_hdr);
8683 hlua_class_function(gL.T, "req_set_method", hlua_http_req_set_meth);
8684 hlua_class_function(gL.T, "req_set_path", hlua_http_req_set_path);
8685 hlua_class_function(gL.T, "req_set_query", hlua_http_req_set_query);
8686 hlua_class_function(gL.T, "req_set_uri", hlua_http_req_set_uri);
8687
8688 hlua_class_function(gL.T, "res_get_headers",hlua_http_res_get_headers);
8689 hlua_class_function(gL.T, "res_del_header", hlua_http_res_del_hdr);
8690 hlua_class_function(gL.T, "res_rep_header", hlua_http_res_rep_hdr);
8691 hlua_class_function(gL.T, "res_rep_value", hlua_http_res_rep_val);
8692 hlua_class_function(gL.T, "res_add_header", hlua_http_res_add_hdr);
8693 hlua_class_function(gL.T, "res_set_header", hlua_http_res_set_hdr);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02008694 hlua_class_function(gL.T, "res_set_status", hlua_http_res_set_status);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008695
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008696 lua_rawset(gL.T, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008697
8698 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008699 class_http_ref = hlua_register_metatable(gL.T, CLASS_HTTP);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008700
8701 /*
8702 *
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008703 * Register class AppletTCP
8704 *
8705 */
8706
8707 /* Create and fill the metatable. */
8708 lua_newtable(gL.T);
8709
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008710 /* Create and fille the __index entry. */
8711 lua_pushstring(gL.T, "__index");
8712 lua_newtable(gL.T);
8713
8714 /* Register Lua functions. */
Thierry FOURNIER / OZON.IO3e1d7912016-12-12 12:29:34 +01008715 hlua_class_function(gL.T, "getline", hlua_applet_tcp_getline);
8716 hlua_class_function(gL.T, "receive", hlua_applet_tcp_recv);
8717 hlua_class_function(gL.T, "send", hlua_applet_tcp_send);
8718 hlua_class_function(gL.T, "set_priv", hlua_applet_tcp_set_priv);
8719 hlua_class_function(gL.T, "get_priv", hlua_applet_tcp_get_priv);
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01008720 hlua_class_function(gL.T, "set_var", hlua_applet_tcp_set_var);
8721 hlua_class_function(gL.T, "unset_var", hlua_applet_tcp_unset_var);
8722 hlua_class_function(gL.T, "get_var", hlua_applet_tcp_get_var);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008723
8724 lua_settable(gL.T, -3);
8725
8726 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008727 class_applet_tcp_ref = hlua_register_metatable(gL.T, CLASS_APPLET_TCP);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008728
8729 /*
8730 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02008731 * Register class AppletHTTP
8732 *
8733 */
8734
8735 /* Create and fill the metatable. */
8736 lua_newtable(gL.T);
8737
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02008738 /* Create and fille the __index entry. */
8739 lua_pushstring(gL.T, "__index");
8740 lua_newtable(gL.T);
8741
8742 /* Register Lua functions. */
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01008743 hlua_class_function(gL.T, "set_priv", hlua_applet_http_set_priv);
8744 hlua_class_function(gL.T, "get_priv", hlua_applet_http_get_priv);
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01008745 hlua_class_function(gL.T, "set_var", hlua_applet_http_set_var);
8746 hlua_class_function(gL.T, "unset_var", hlua_applet_http_unset_var);
8747 hlua_class_function(gL.T, "get_var", hlua_applet_http_get_var);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02008748 hlua_class_function(gL.T, "getline", hlua_applet_http_getline);
8749 hlua_class_function(gL.T, "receive", hlua_applet_http_recv);
8750 hlua_class_function(gL.T, "send", hlua_applet_http_send);
8751 hlua_class_function(gL.T, "add_header", hlua_applet_http_addheader);
8752 hlua_class_function(gL.T, "set_status", hlua_applet_http_status);
8753 hlua_class_function(gL.T, "start_response", hlua_applet_http_start_response);
8754
8755 lua_settable(gL.T, -3);
8756
8757 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008758 class_applet_http_ref = hlua_register_metatable(gL.T, CLASS_APPLET_HTTP);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02008759
8760 /*
8761 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008762 * Register class TXN
8763 *
8764 */
8765
8766 /* Create and fill the metatable. */
8767 lua_newtable(gL.T);
8768
8769 /* Create and fille the __index entry. */
8770 lua_pushstring(gL.T, "__index");
8771 lua_newtable(gL.T);
8772
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01008773 /* Register Lua functions. */
Patrick Hemmer268a7072018-05-11 12:52:31 -04008774 hlua_class_function(gL.T, "set_priv", hlua_set_priv);
8775 hlua_class_function(gL.T, "get_priv", hlua_get_priv);
8776 hlua_class_function(gL.T, "set_var", hlua_set_var);
8777 hlua_class_function(gL.T, "unset_var", hlua_unset_var);
8778 hlua_class_function(gL.T, "get_var", hlua_get_var);
8779 hlua_class_function(gL.T, "done", hlua_txn_done);
8780 hlua_class_function(gL.T, "set_loglevel", hlua_txn_set_loglevel);
8781 hlua_class_function(gL.T, "set_tos", hlua_txn_set_tos);
8782 hlua_class_function(gL.T, "set_mark", hlua_txn_set_mark);
8783 hlua_class_function(gL.T, "set_priority_class", hlua_txn_set_priority_class);
8784 hlua_class_function(gL.T, "set_priority_offset", hlua_txn_set_priority_offset);
8785 hlua_class_function(gL.T, "deflog", hlua_txn_deflog);
8786 hlua_class_function(gL.T, "log", hlua_txn_log);
8787 hlua_class_function(gL.T, "Debug", hlua_txn_log_debug);
8788 hlua_class_function(gL.T, "Info", hlua_txn_log_info);
8789 hlua_class_function(gL.T, "Warning", hlua_txn_log_warning);
8790 hlua_class_function(gL.T, "Alert", hlua_txn_log_alert);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01008791
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008792 lua_rawset(gL.T, -3);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008793
8794 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008795 class_txn_ref = hlua_register_metatable(gL.T, CLASS_TXN);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008796
8797 /*
8798 *
8799 * Register class Socket
8800 *
8801 */
8802
8803 /* Create and fill the metatable. */
8804 lua_newtable(gL.T);
8805
8806 /* Create and fille the __index entry. */
8807 lua_pushstring(gL.T, "__index");
8808 lua_newtable(gL.T);
8809
Baptiste Assmann84bb4932015-03-02 21:40:06 +01008810#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008811 hlua_class_function(gL.T, "connect_ssl", hlua_socket_connect_ssl);
Baptiste Assmann84bb4932015-03-02 21:40:06 +01008812#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008813 hlua_class_function(gL.T, "connect", hlua_socket_connect);
8814 hlua_class_function(gL.T, "send", hlua_socket_send);
8815 hlua_class_function(gL.T, "receive", hlua_socket_receive);
8816 hlua_class_function(gL.T, "close", hlua_socket_close);
8817 hlua_class_function(gL.T, "getpeername", hlua_socket_getpeername);
8818 hlua_class_function(gL.T, "getsockname", hlua_socket_getsockname);
8819 hlua_class_function(gL.T, "setoption", hlua_socket_setoption);
8820 hlua_class_function(gL.T, "settimeout", hlua_socket_settimeout);
8821
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008822 lua_rawset(gL.T, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008823
8824 /* Register the garbage collector entry. */
8825 lua_pushstring(gL.T, "__gc");
8826 lua_pushcclosure(gL.T, hlua_socket_gc, 0);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008827 lua_rawset(gL.T, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008828
8829 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008830 class_socket_ref = hlua_register_metatable(gL.T, CLASS_SOCKET);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008831
8832 /* Proxy and server configuration initialisation. */
8833 memset(&socket_proxy, 0, sizeof(socket_proxy));
8834 init_new_proxy(&socket_proxy);
8835 socket_proxy.parent = NULL;
8836 socket_proxy.last_change = now.tv_sec;
8837 socket_proxy.id = "LUA-SOCKET";
8838 socket_proxy.cap = PR_CAP_FE | PR_CAP_BE;
8839 socket_proxy.maxconn = 0;
8840 socket_proxy.accept = NULL;
8841 socket_proxy.options2 |= PR_O2_INDEPSTR;
8842 socket_proxy.srv = NULL;
8843 socket_proxy.conn_retries = 0;
8844 socket_proxy.timeout.connect = 5000; /* By default the timeout connection is 5s. */
8845
8846 /* Init TCP server: unchanged parameters */
8847 memset(&socket_tcp, 0, sizeof(socket_tcp));
8848 socket_tcp.next = NULL;
8849 socket_tcp.proxy = &socket_proxy;
8850 socket_tcp.obj_type = OBJ_TYPE_SERVER;
8851 LIST_INIT(&socket_tcp.actconns);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04008852 socket_tcp.pendconns = EB_ROOT;
Christopher Faulet40a007c2017-07-03 15:41:01 +02008853 socket_tcp.priv_conns = NULL;
8854 socket_tcp.idle_conns = NULL;
8855 socket_tcp.safe_conns = NULL;
Emeric Brun52a91d32017-08-31 14:41:55 +02008856 socket_tcp.next_state = SRV_ST_RUNNING; /* early server setup */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008857 socket_tcp.last_change = 0;
8858 socket_tcp.id = "LUA-TCP-CONN";
8859 socket_tcp.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8860 socket_tcp.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8861 socket_tcp.pp_opts = 0; /* Remove proxy protocol. */
8862
8863 /* XXX: Copy default parameter from default server,
8864 * but the default server is not initialized.
8865 */
8866 socket_tcp.maxqueue = socket_proxy.defsrv.maxqueue;
8867 socket_tcp.minconn = socket_proxy.defsrv.minconn;
8868 socket_tcp.maxconn = socket_proxy.defsrv.maxconn;
8869 socket_tcp.slowstart = socket_proxy.defsrv.slowstart;
8870 socket_tcp.onerror = socket_proxy.defsrv.onerror;
8871 socket_tcp.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
8872 socket_tcp.onmarkedup = socket_proxy.defsrv.onmarkedup;
8873 socket_tcp.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
8874 socket_tcp.uweight = socket_proxy.defsrv.iweight;
8875 socket_tcp.iweight = socket_proxy.defsrv.iweight;
8876
8877 socket_tcp.check.status = HCHK_STATUS_INI;
8878 socket_tcp.check.rise = socket_proxy.defsrv.check.rise;
8879 socket_tcp.check.fall = socket_proxy.defsrv.check.fall;
8880 socket_tcp.check.health = socket_tcp.check.rise; /* socket, but will fall down at first failure */
8881 socket_tcp.check.server = &socket_tcp;
8882
8883 socket_tcp.agent.status = HCHK_STATUS_INI;
8884 socket_tcp.agent.rise = socket_proxy.defsrv.agent.rise;
8885 socket_tcp.agent.fall = socket_proxy.defsrv.agent.fall;
8886 socket_tcp.agent.health = socket_tcp.agent.rise; /* socket, but will fall down at first failure */
8887 socket_tcp.agent.server = &socket_tcp;
8888
Willy Tarreaua261e9b2016-12-22 20:44:00 +01008889 socket_tcp.xprt = xprt_get(XPRT_RAW);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008890
8891#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008892 /* Init TCP server: unchanged parameters */
8893 memset(&socket_ssl, 0, sizeof(socket_ssl));
8894 socket_ssl.next = NULL;
8895 socket_ssl.proxy = &socket_proxy;
8896 socket_ssl.obj_type = OBJ_TYPE_SERVER;
8897 LIST_INIT(&socket_ssl.actconns);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04008898 socket_ssl.pendconns = EB_ROOT;
Willy Tarreaub784b352019-02-07 14:48:24 +01008899 socket_ssl.priv_conns = NULL;
8900 socket_ssl.idle_conns = NULL;
8901 socket_ssl.safe_conns = NULL;
Emeric Brun52a91d32017-08-31 14:41:55 +02008902 socket_ssl.next_state = SRV_ST_RUNNING; /* early server setup */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008903 socket_ssl.last_change = 0;
8904 socket_ssl.id = "LUA-SSL-CONN";
8905 socket_ssl.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8906 socket_ssl.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8907 socket_ssl.pp_opts = 0; /* Remove proxy protocol. */
8908
8909 /* XXX: Copy default parameter from default server,
8910 * but the default server is not initialized.
8911 */
8912 socket_ssl.maxqueue = socket_proxy.defsrv.maxqueue;
8913 socket_ssl.minconn = socket_proxy.defsrv.minconn;
8914 socket_ssl.maxconn = socket_proxy.defsrv.maxconn;
8915 socket_ssl.slowstart = socket_proxy.defsrv.slowstart;
8916 socket_ssl.onerror = socket_proxy.defsrv.onerror;
8917 socket_ssl.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
8918 socket_ssl.onmarkedup = socket_proxy.defsrv.onmarkedup;
8919 socket_ssl.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
8920 socket_ssl.uweight = socket_proxy.defsrv.iweight;
8921 socket_ssl.iweight = socket_proxy.defsrv.iweight;
8922
8923 socket_ssl.check.status = HCHK_STATUS_INI;
8924 socket_ssl.check.rise = socket_proxy.defsrv.check.rise;
8925 socket_ssl.check.fall = socket_proxy.defsrv.check.fall;
8926 socket_ssl.check.health = socket_ssl.check.rise; /* socket, but will fall down at first failure */
8927 socket_ssl.check.server = &socket_ssl;
8928
8929 socket_ssl.agent.status = HCHK_STATUS_INI;
8930 socket_ssl.agent.rise = socket_proxy.defsrv.agent.rise;
8931 socket_ssl.agent.fall = socket_proxy.defsrv.agent.fall;
8932 socket_ssl.agent.health = socket_ssl.agent.rise; /* socket, but will fall down at first failure */
8933 socket_ssl.agent.server = &socket_ssl;
8934
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008935 socket_ssl.use_ssl = 1;
Willy Tarreaua261e9b2016-12-22 20:44:00 +01008936 socket_ssl.xprt = xprt_get(XPRT_SSL);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008937
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008938 for (idx = 0; args[idx] != NULL; idx++) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008939 if ((kw = srv_find_kw(args[idx])) != NULL) { /* Maybe it's registered server keyword */
8940 /*
8941 *
8942 * If the keyword is not known, we can search in the registered
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08008943 * server keywords. This is useful to configure special SSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008944 * features like client certificates and ssl_verify.
8945 *
8946 */
8947 tmp_error = kw->parse(args, &idx, &socket_proxy, &socket_ssl, &error);
8948 if (tmp_error != 0) {
8949 fprintf(stderr, "INTERNAL ERROR: %s\n", error);
8950 abort(); /* This must be never arrives because the command line
8951 not editable by the user. */
8952 }
8953 idx += kw->skip;
8954 }
8955 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008956#endif
Thierry Fournier75933d42016-01-21 09:30:18 +01008957
8958 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01008959}
Willy Tarreaubb57d942016-12-21 19:04:56 +01008960
Willy Tarreau80713382018-11-26 10:19:54 +01008961static void hlua_register_build_options(void)
8962{
Willy Tarreaubb57d942016-12-21 19:04:56 +01008963 char *ptr = NULL;
Willy Tarreau80713382018-11-26 10:19:54 +01008964
Willy Tarreaubb57d942016-12-21 19:04:56 +01008965 memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE);
8966 hap_register_build_opts(ptr, 1);
8967}
Willy Tarreau80713382018-11-26 10:19:54 +01008968
8969INITCALL0(STG_REGISTER, hlua_register_build_options);