blob: 189bf475ad677da2c65663c7a479f883cd2dc1b2 [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>
Thierry FOURNIER380d0932015-01-23 14:27:52 +010032
William Lallemand9ed62032016-11-21 17:49:11 +010033#include <types/cli.h>
Thierry FOURNIER380d0932015-01-23 14:27:52 +010034#include <types/hlua.h>
35#include <types/proxy.h>
William Lallemand9ed62032016-11-21 17:49:11 +010036#include <types/stats.h>
Thierry FOURNIER380d0932015-01-23 14:27:52 +010037
Thierry FOURNIER55da1652015-01-23 11:36:30 +010038#include <proto/arg.h>
Willy Tarreau8a8d83b2015-04-13 13:24:54 +020039#include <proto/applet.h>
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010040#include <proto/channel.h>
William Lallemand9ed62032016-11-21 17:49:11 +010041#include <proto/cli.h>
Willy Tarreaua71f6422016-11-16 17:00:14 +010042#include <proto/connection.h>
William Lallemand9ed62032016-11-21 17:49:11 +010043#include <proto/stats.h>
Thierry FOURNIER9a819e72015-02-16 20:22:55 +010044#include <proto/hdr_idx.h>
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +010045#include <proto/hlua.h>
Thierry Fournierfb0b5462016-01-21 09:28:58 +010046#include <proto/hlua_fcn.h>
Willy Tarreau79e57332018-10-02 16:01:16 +020047#include <proto/http_fetch.h>
Willy Tarreau61c112a2018-10-02 16:43:32 +020048#include <proto/http_rules.h>
Thierry FOURNIER3def3932015-04-07 11:27:54 +020049#include <proto/map.h>
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010050#include <proto/obj_type.h>
Patrick Hemmer268a7072018-05-11 12:52:31 -040051#include <proto/queue.h>
Thierry FOURNIER83758bb2015-02-04 13:21:04 +010052#include <proto/pattern.h>
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +010053#include <proto/payload.h>
54#include <proto/proto_http.h>
55#include <proto/sample.h>
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010056#include <proto/server.h>
Willy Tarreaufeb76402015-04-03 14:10:06 +020057#include <proto/session.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020058#include <proto/stream.h>
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010059#include <proto/stream_interface.h>
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +010060#include <proto/task.h>
Willy Tarreau39713102016-11-25 15:49:32 +010061#include <proto/tcp_rules.h>
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +020062#include <proto/vars.h>
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +010063
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +010064/* Lua uses longjmp to perform yield or throwing errors. This
65 * macro is used only for identifying the function that can
66 * not return because a longjmp is executed.
67 * __LJMP marks a prototype of hlua file that can use longjmp.
68 * WILL_LJMP() marks an lua function that will use longjmp.
69 * MAY_LJMP() marks an lua function that may use longjmp.
70 */
71#define __LJMP
Willy Tarreau4e7cc332018-10-20 17:45:48 +020072#define WILL_LJMP(func) do { func; my_unreachable(); } while(0)
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +010073#define MAY_LJMP(func) func
74
Thierry FOURNIERbabae282015-09-17 11:36:37 +020075/* This couple of function executes securely some Lua calls outside of
76 * the lua runtime environment. Each Lua call can return a longjmp
77 * if it encounter a memory error.
78 *
79 * Lua documentation extract:
80 *
81 * If an error happens outside any protected environment, Lua calls
82 * a panic function (see lua_atpanic) and then calls abort, thus
83 * exiting the host application. Your panic function can avoid this
84 * exit by never returning (e.g., doing a long jump to your own
85 * recovery point outside Lua).
86 *
87 * The panic function runs as if it were a message handler (see
88 * §2.3); in particular, the error message is at the top of the
89 * stack. However, there is no guarantee about stack space. To push
90 * anything on the stack, the panic function must first check the
91 * available space (see §4.2).
92 *
93 * We must check all the Lua entry point. This includes:
94 * - The include/proto/hlua.h exported functions
95 * - the task wrapper function
96 * - The action wrapper function
97 * - The converters wrapper function
98 * - The sample-fetch wrapper functions
99 *
100 * It is tolerated that the initilisation function returns an abort.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800101 * Before each Lua abort, an error message is written on stderr.
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200102 *
103 * The macro SET_SAFE_LJMP initialise the longjmp. The Macro
104 * RESET_SAFE_LJMP reset the longjmp. These function must be macro
105 * because they must be exists in the program stack when the longjmp
106 * is called.
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +0200107 *
108 * Note that the Lua processing is not really thread safe. It provides
109 * heavy system which consists to add our own lock function in the Lua
110 * code and recompile the library. This system will probably not accepted
111 * by maintainers of various distribs.
112 *
113 * Our main excution point of the Lua is the function lua_resume(). A
114 * quick looking on the Lua sources displays a lua_lock() a the start
115 * of function and a lua_unlock() at the end of the function. So I
116 * conclude that the Lua thread safe mode just perform a mutex around
117 * all execution. So I prefer to do this in the HAProxy code, it will be
118 * easier for distro maintainers.
119 *
120 * Note that the HAProxy lua functions rounded by the macro SET_SAFE_LJMP
121 * and RESET_SAFE_LJMP manipulates the Lua stack, so it will be careful
122 * to set mutex around these functions.
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200123 */
Willy Tarreau86abe442018-11-25 20:12:18 +0100124__decl_spinlock(hlua_global_lock);
Thierry FOURNIERffbad792017-07-12 11:39:04 +0200125THREAD_LOCAL jmp_buf safe_ljmp_env;
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200126static int hlua_panic_safe(lua_State *L) { return 0; }
127static int hlua_panic_ljmp(lua_State *L) { longjmp(safe_ljmp_env, 1); }
128
129#define SET_SAFE_LJMP(__L) \
130 ({ \
131 int ret; \
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100132 HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200133 if (setjmp(safe_ljmp_env) != 0) { \
134 lua_atpanic(__L, hlua_panic_safe); \
135 ret = 0; \
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100136 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200137 } else { \
138 lua_atpanic(__L, hlua_panic_ljmp); \
139 ret = 1; \
140 } \
141 ret; \
142 })
143
144/* If we are the last function catching Lua errors, we
145 * must reset the panic function.
146 */
147#define RESET_SAFE_LJMP(__L) \
148 do { \
149 lua_atpanic(__L, hlua_panic_safe); \
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100150 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200151 } while(0)
152
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200153/* Applet status flags */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200154#define APPLET_DONE 0x01 /* applet processing is done. */
155#define APPLET_100C 0x02 /* 100 continue expected. */
156#define APPLET_HDR_SENT 0x04 /* Response header sent. */
157#define APPLET_CHUNKED 0x08 /* Use transfer encoding chunked. */
158#define APPLET_LAST_CHK 0x10 /* Last chunk sent. */
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +0100159#define APPLET_HTTP11 0x20 /* Last chunk sent. */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200160
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100161/* The main Lua execution context. */
162struct hlua gL;
163
Thierry FOURNIERebed6e92016-12-16 11:54:07 +0100164/* This is the memory pool containing struct lua for applets
165 * (including cli).
166 */
Willy Tarreau8ceae722018-11-26 11:58:30 +0100167DECLARE_STATIC_POOL(pool_head_hlua, "hlua", sizeof(struct hlua));
Thierry FOURNIERebed6e92016-12-16 11:54:07 +0100168
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100169/* Used for Socket connection. */
170static struct proxy socket_proxy;
171static struct server socket_tcp;
172#ifdef USE_OPENSSL
173static struct server socket_ssl;
174#endif
175
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +0100176/* List head of the function called at the initialisation time. */
177struct list hlua_init_functions = LIST_HEAD_INIT(hlua_init_functions);
178
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +0100179/* The following variables contains the reference of the different
180 * Lua classes. These references are useful for identify metadata
181 * associated with an object.
182 */
Thierry FOURNIER65f34c62015-02-16 20:11:43 +0100183static int class_txn_ref;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100184static int class_socket_ref;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +0100185static int class_channel_ref;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +0100186static int class_fetches_ref;
Thierry FOURNIER594afe72015-03-10 23:58:30 +0100187static int class_converters_ref;
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100188static int class_http_ref;
Thierry FOURNIER3def3932015-04-07 11:27:54 +0200189static int class_map_ref;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200190static int class_applet_tcp_ref;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200191static int class_applet_http_ref;
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +0100192
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100193/* Global Lua execution timeout. By default Lua, execution linked
Willy Tarreau87b09662015-04-03 00:22:06 +0200194 * with stream (actions, sample-fetches and converters) have a
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100195 * short timeout. Lua linked with tasks doesn't have a timeout
196 * because a task may remain alive during all the haproxy execution.
197 */
198static unsigned int hlua_timeout_session = 4000; /* session timeout. */
199static unsigned int hlua_timeout_task = TICK_ETERNITY; /* task timeout. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200200static unsigned int hlua_timeout_applet = 4000; /* applet timeout. */
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100201
Thierry FOURNIERee9f8022015-03-03 17:37:37 +0100202/* Interrupts the Lua processing each "hlua_nb_instruction" instructions.
203 * it is used for preventing infinite loops.
204 *
205 * I test the scheer with an infinite loop containing one incrementation
206 * and one test. I run this loop between 10 seconds, I raise a ceil of
207 * 710M loops from one interrupt each 9000 instructions, so I fix the value
208 * to one interrupt each 10 000 instructions.
209 *
210 * configured | Number of
211 * instructions | loops executed
212 * between two | in milions
213 * forced yields |
214 * ---------------+---------------
215 * 10 | 160
216 * 500 | 670
217 * 1000 | 680
218 * 5000 | 700
219 * 7000 | 700
220 * 8000 | 700
221 * 9000 | 710 <- ceil
222 * 10000 | 710
223 * 100000 | 710
224 * 1000000 | 710
225 *
226 */
227static unsigned int hlua_nb_instruction = 10000;
228
Willy Tarreau32f61e22015-03-18 17:54:59 +0100229/* Descriptor for the memory allocation state. If limit is not null, it will
230 * be enforced on any memory allocation.
231 */
232struct hlua_mem_allocator {
233 size_t allocated;
234 size_t limit;
235};
236
237static struct hlua_mem_allocator hlua_global_allocator;
238
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200239static const char error_500[] =
Jarno Huuskonen16ad94a2017-01-09 14:17:10 +0200240 "HTTP/1.0 500 Internal Server Error\r\n"
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200241 "Cache-Control: no-cache\r\n"
242 "Connection: close\r\n"
243 "Content-Type: text/html\r\n"
244 "\r\n"
Joseph Herlant7fe15772018-11-15 10:07:32 -0800245 "<html><body><h1>500 Internal Server Error</h1>\nAn internal server error occurred.\n</body></html>\n";
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200246
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100247/* These functions converts types between HAProxy internal args or
248 * sample and LUA types. Another function permits to check if the
249 * LUA stack contains arguments according with an required ARG_T
250 * format.
251 */
252static int hlua_arg2lua(lua_State *L, const struct arg *arg);
253static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100254__LJMP static int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp,
David Carlier0c437f42016-04-27 16:21:56 +0100255 uint64_t mask, struct proxy *p);
Willy Tarreau5eadada2015-03-10 17:28:54 +0100256static int hlua_smp2lua(lua_State *L, struct sample *smp);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100257static int hlua_smp2lua_str(lua_State *L, struct sample *smp);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100258static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp);
259
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200260__LJMP static int hlua_http_get_headers(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg);
261
Thierry FOURNIER23bc3752015-09-11 19:15:43 +0200262#define SEND_ERR(__be, __fmt, __args...) \
263 do { \
264 send_log(__be, LOG_ERR, __fmt, ## __args); \
265 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) \
Christopher Faulet767a84b2017-11-24 16:50:31 +0100266 ha_alert(__fmt, ## __args); \
Thierry FOURNIER23bc3752015-09-11 19:15:43 +0200267 } while (0)
268
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100269/* Used to check an Lua function type in the stack. It creates and
270 * returns a reference of the function. This function throws an
271 * error if the rgument is not a "function".
272 */
273__LJMP unsigned int hlua_checkfunction(lua_State *L, int argno)
274{
275 if (!lua_isfunction(L, argno)) {
Thierry FOURNIERfd1e9552018-02-23 18:41:18 +0100276 const char *msg = lua_pushfstring(L, "function expected, got %s", luaL_typename(L, argno));
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100277 WILL_LJMP(luaL_argerror(L, argno, msg));
278 }
279 lua_pushvalue(L, argno);
280 return luaL_ref(L, LUA_REGISTRYINDEX);
281}
282
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200283/* Return the string that is of the top of the stack. */
284const char *hlua_get_top_error_string(lua_State *L)
285{
286 if (lua_gettop(L) < 1)
287 return "unknown error";
288 if (lua_type(L, -1) != LUA_TSTRING)
289 return "unknown error";
290 return lua_tostring(L, -1);
291}
292
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200293__LJMP static const char *hlua_traceback(lua_State *L)
294{
295 lua_Debug ar;
296 int level = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +0200297 struct buffer *msg = get_trash_chunk();
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200298 int filled = 0;
299
300 while (lua_getstack(L, level++, &ar)) {
301
302 /* Add separator */
303 if (filled)
304 chunk_appendf(msg, ", ");
305 filled = 1;
306
307 /* Fill fields:
308 * 'S': fills in the fields source, short_src, linedefined, lastlinedefined, and what;
309 * 'l': fills in the field currentline;
310 * 'n': fills in the field name and namewhat;
311 * 't': fills in the field istailcall;
312 */
313 lua_getinfo(L, "Slnt", &ar);
314
315 /* Append code localisation */
316 if (ar.currentline > 0)
317 chunk_appendf(msg, "%s:%d ", ar.short_src, ar.currentline);
318 else
319 chunk_appendf(msg, "%s ", ar.short_src);
320
321 /*
322 * Get function name
323 *
324 * if namewhat is no empty, name is defined.
325 * what contains "Lua" for Lua function, "C" for C function,
326 * or "main" for main code.
327 */
328 if (*ar.namewhat != '\0' && ar.name != NULL) /* is there a name from code? */
329 chunk_appendf(msg, "%s '%s'", ar.namewhat, ar.name); /* use it */
330
331 else if (*ar.what == 'm') /* "main", the code is not executed in a function */
332 chunk_appendf(msg, "main chunk");
333
334 else if (*ar.what != 'C') /* for Lua functions, use <file:line> */
335 chunk_appendf(msg, "C function line %d", ar.linedefined);
336
337 else /* nothing left... */
338 chunk_appendf(msg, "?");
339
340
341 /* Display tailed call */
342 if (ar.istailcall)
343 chunk_appendf(msg, " ...");
344 }
345
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200346 return msg->area;
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200347}
348
349
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100350/* This function check the number of arguments available in the
351 * stack. If the number of arguments available is not the same
352 * then <nb> an error is throwed.
353 */
354__LJMP static inline void check_args(lua_State *L, int nb, char *fcn)
355{
356 if (lua_gettop(L) == nb)
357 return;
358 WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb));
359}
360
Mark Lakes22154b42018-01-29 14:38:40 -0800361/* This function pushes an error string prefixed by the file name
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100362 * and the line number where the error is encountered.
363 */
364static int hlua_pusherror(lua_State *L, const char *fmt, ...)
365{
366 va_list argp;
367 va_start(argp, fmt);
368 luaL_where(L, 1);
369 lua_pushvfstring(L, fmt, argp);
370 va_end(argp);
371 lua_concat(L, 2);
372 return 1;
373}
374
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100375/* This functions is used with sample fetch and converters. It
376 * converts the HAProxy configuration argument in a lua stack
377 * values.
378 *
379 * It takes an array of "arg", and each entry of the array is
380 * converted and pushed in the LUA stack.
381 */
382static int hlua_arg2lua(lua_State *L, const struct arg *arg)
383{
384 switch (arg->type) {
385 case ARGT_SINT:
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100386 case ARGT_TIME:
387 case ARGT_SIZE:
Thierry FOURNIERf90838b2015-03-06 13:48:32 +0100388 lua_pushinteger(L, arg->data.sint);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100389 break;
390
391 case ARGT_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200392 lua_pushlstring(L, arg->data.str.area, arg->data.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100393 break;
394
395 case ARGT_IPV4:
396 case ARGT_IPV6:
397 case ARGT_MSK4:
398 case ARGT_MSK6:
399 case ARGT_FE:
400 case ARGT_BE:
401 case ARGT_TAB:
402 case ARGT_SRV:
403 case ARGT_USR:
404 case ARGT_MAP:
405 default:
406 lua_pushnil(L);
407 break;
408 }
409 return 1;
410}
411
412/* This function take one entrie in an LUA stack at the index "ud",
413 * and try to convert it in an HAProxy argument entry. This is useful
414 * with sample fetch wrappers. The input arguments are gived to the
415 * lua wrapper and converted as arg list by thi function.
416 */
417static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg)
418{
419 switch (lua_type(L, ud)) {
420
421 case LUA_TNUMBER:
422 case LUA_TBOOLEAN:
423 arg->type = ARGT_SINT;
424 arg->data.sint = lua_tointeger(L, ud);
425 break;
426
427 case LUA_TSTRING:
428 arg->type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200429 arg->data.str.area = (char *)lua_tolstring(L, ud, (size_t *)&arg->data.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100430 break;
431
432 case LUA_TUSERDATA:
433 case LUA_TNIL:
434 case LUA_TTABLE:
435 case LUA_TFUNCTION:
436 case LUA_TTHREAD:
437 case LUA_TLIGHTUSERDATA:
438 arg->type = ARGT_SINT;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +0200439 arg->data.sint = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100440 break;
441 }
442 return 1;
443}
444
445/* the following functions are used to convert a struct sample
446 * in Lua type. This useful to convert the return of the
447 * fetchs or converters.
448 */
Willy Tarreau5eadada2015-03-10 17:28:54 +0100449static int hlua_smp2lua(lua_State *L, struct sample *smp)
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100450{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200451 switch (smp->data.type) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100452 case SMP_T_SINT:
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100453 case SMP_T_BOOL:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200454 lua_pushinteger(L, smp->data.u.sint);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100455 break;
456
457 case SMP_T_BIN:
458 case SMP_T_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200459 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100460 break;
461
462 case SMP_T_METH:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200463 switch (smp->data.u.meth.meth) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100464 case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break;
465 case HTTP_METH_GET: lua_pushstring(L, "GET"); break;
466 case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break;
467 case HTTP_METH_POST: lua_pushstring(L, "POST"); break;
468 case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break;
469 case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break;
470 case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break;
471 case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break;
472 case HTTP_METH_OTHER:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200473 lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100474 break;
475 default:
476 lua_pushnil(L);
477 break;
478 }
479 break;
480
481 case SMP_T_IPV4:
482 case SMP_T_IPV6:
483 case SMP_T_ADDR: /* This type is never used to qualify a sample. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200484 if (sample_casts[smp->data.type][SMP_T_STR] &&
485 sample_casts[smp->data.type][SMP_T_STR](smp))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200486 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Willy Tarreau5eadada2015-03-10 17:28:54 +0100487 else
488 lua_pushnil(L);
489 break;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100490 default:
491 lua_pushnil(L);
492 break;
493 }
494 return 1;
495}
496
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100497/* the following functions are used to convert a struct sample
498 * in Lua strings. This is useful to convert the return of the
499 * fetchs or converters.
500 */
501static int hlua_smp2lua_str(lua_State *L, struct sample *smp)
502{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200503 switch (smp->data.type) {
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100504
505 case SMP_T_BIN:
506 case SMP_T_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200507 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100508 break;
509
510 case SMP_T_METH:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200511 switch (smp->data.u.meth.meth) {
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100512 case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break;
513 case HTTP_METH_GET: lua_pushstring(L, "GET"); break;
514 case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break;
515 case HTTP_METH_POST: lua_pushstring(L, "POST"); break;
516 case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break;
517 case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break;
518 case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break;
519 case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break;
520 case HTTP_METH_OTHER:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200521 lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100522 break;
523 default:
524 lua_pushstring(L, "");
525 break;
526 }
527 break;
528
529 case SMP_T_SINT:
530 case SMP_T_BOOL:
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100531 case SMP_T_IPV4:
532 case SMP_T_IPV6:
533 case SMP_T_ADDR: /* This type is never used to qualify a sample. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200534 if (sample_casts[smp->data.type][SMP_T_STR] &&
535 sample_casts[smp->data.type][SMP_T_STR](smp))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200536 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100537 else
538 lua_pushstring(L, "");
539 break;
540 default:
541 lua_pushstring(L, "");
542 break;
543 }
544 return 1;
545}
546
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100547/* the following functions are used to convert an Lua type in a
548 * struct sample. This is useful to provide data from a converter
549 * to the LUA code.
550 */
551static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp)
552{
553 switch (lua_type(L, ud)) {
554
555 case LUA_TNUMBER:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200556 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200557 smp->data.u.sint = lua_tointeger(L, ud);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100558 break;
559
560
561 case LUA_TBOOLEAN:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200562 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200563 smp->data.u.sint = lua_toboolean(L, ud);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100564 break;
565
566 case LUA_TSTRING:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200567 smp->data.type = SMP_T_STR;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100568 smp->flags |= SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200569 smp->data.u.str.area = (char *)lua_tolstring(L, ud, (size_t *)&smp->data.u.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100570 break;
571
572 case LUA_TUSERDATA:
573 case LUA_TNIL:
574 case LUA_TTABLE:
575 case LUA_TFUNCTION:
576 case LUA_TTHREAD:
577 case LUA_TLIGHTUSERDATA:
Thierry FOURNIER93405e12015-08-26 14:19:03 +0200578 case LUA_TNONE:
579 default:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200580 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200581 smp->data.u.sint = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100582 break;
583 }
584 return 1;
585}
586
587/* This function check the "argp" builded by another conversion function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800588 * is in accord with the expected argp defined by the "mask". The function
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100589 * returns true or false. It can be adjust the types if there compatibles.
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100590 *
591 * This function assumes thant the argp argument contains ARGM_NBARGS + 1
592 * entries.
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100593 */
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100594__LJMP int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp,
David Carlier0c437f42016-04-27 16:21:56 +0100595 uint64_t mask, struct proxy *p)
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100596{
597 int min_arg;
598 int idx;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100599 struct proxy *px;
600 char *sname, *pname;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100601
602 idx = 0;
603 min_arg = ARGM(mask);
604 mask >>= ARGM_BITS;
605
606 while (1) {
607
608 /* Check oversize. */
609 if (idx >= ARGM_NBARGS && argp[idx].type != ARGT_STOP) {
Cyril Bonté577a36a2015-03-02 00:08:38 +0100610 WILL_LJMP(luaL_argerror(L, first + idx, "Malformed argument mask"));
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100611 }
612
613 /* Check for mandatory arguments. */
614 if (argp[idx].type == ARGT_STOP) {
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100615 if (idx < min_arg) {
616
617 /* If miss other argument than the first one, we return an error. */
618 if (idx > 0)
619 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
620
621 /* If first argument have a certain type, some default values
622 * may be used. See the function smp_resolve_args().
623 */
624 switch (mask & ARGT_MASK) {
625
626 case ARGT_FE:
627 if (!(p->cap & PR_CAP_FE))
628 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
629 argp[idx].data.prx = p;
630 argp[idx].type = ARGT_FE;
631 argp[idx+1].type = ARGT_STOP;
632 break;
633
634 case ARGT_BE:
635 if (!(p->cap & PR_CAP_BE))
636 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
637 argp[idx].data.prx = p;
638 argp[idx].type = ARGT_BE;
639 argp[idx+1].type = ARGT_STOP;
640 break;
641
642 case ARGT_TAB:
643 argp[idx].data.prx = p;
644 argp[idx].type = ARGT_TAB;
645 argp[idx+1].type = ARGT_STOP;
646 break;
647
648 default:
649 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
650 break;
651 }
652 }
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100653 return 0;
654 }
655
656 /* Check for exceed the number of requiered argument. */
657 if ((mask & ARGT_MASK) == ARGT_STOP &&
658 argp[idx].type != ARGT_STOP) {
659 WILL_LJMP(luaL_argerror(L, first + idx, "Last argument expected"));
660 }
661
662 if ((mask & ARGT_MASK) == ARGT_STOP &&
663 argp[idx].type == ARGT_STOP) {
664 return 0;
665 }
666
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100667 /* Convert some argument types. */
668 switch (mask & ARGT_MASK) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100669 case ARGT_SINT:
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100670 if (argp[idx].type != ARGT_SINT)
671 WILL_LJMP(luaL_argerror(L, first + idx, "integer expected"));
672 argp[idx].type = ARGT_SINT;
673 break;
674
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100675 case ARGT_TIME:
676 if (argp[idx].type != ARGT_SINT)
677 WILL_LJMP(luaL_argerror(L, first + idx, "integer expected"));
Thierry FOURNIER29176f32015-07-07 00:41:29 +0200678 argp[idx].type = ARGT_TIME;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100679 break;
680
681 case ARGT_SIZE:
682 if (argp[idx].type != ARGT_SINT)
683 WILL_LJMP(luaL_argerror(L, first + idx, "integer expected"));
Thierry FOURNIER29176f32015-07-07 00:41:29 +0200684 argp[idx].type = ARGT_SIZE;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100685 break;
686
687 case ARGT_FE:
688 if (argp[idx].type != ARGT_STR)
689 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200690 memcpy(trash.area, argp[idx].data.str.area,
691 argp[idx].data.str.data);
692 trash.area[argp[idx].data.str.data] = 0;
693 argp[idx].data.prx = proxy_fe_by_name(trash.area);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100694 if (!argp[idx].data.prx)
695 WILL_LJMP(luaL_argerror(L, first + idx, "frontend doesn't exist"));
696 argp[idx].type = ARGT_FE;
697 break;
698
699 case ARGT_BE:
700 if (argp[idx].type != ARGT_STR)
701 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200702 memcpy(trash.area, argp[idx].data.str.area,
703 argp[idx].data.str.data);
704 trash.area[argp[idx].data.str.data] = 0;
705 argp[idx].data.prx = proxy_be_by_name(trash.area);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100706 if (!argp[idx].data.prx)
707 WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist"));
708 argp[idx].type = ARGT_BE;
709 break;
710
711 case ARGT_TAB:
712 if (argp[idx].type != ARGT_STR)
713 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200714 memcpy(trash.area, argp[idx].data.str.area,
715 argp[idx].data.str.data);
716 trash.area[argp[idx].data.str.data] = 0;
717 argp[idx].data.prx = proxy_tbl_by_name(trash.area);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100718 if (!argp[idx].data.prx)
719 WILL_LJMP(luaL_argerror(L, first + idx, "table doesn't exist"));
720 argp[idx].type = ARGT_TAB;
721 break;
722
723 case ARGT_SRV:
724 if (argp[idx].type != ARGT_STR)
725 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200726 memcpy(trash.area, argp[idx].data.str.area,
727 argp[idx].data.str.data);
728 trash.area[argp[idx].data.str.data] = 0;
729 sname = strrchr(trash.area, '/');
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100730 if (sname) {
731 *sname++ = '\0';
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200732 pname = trash.area;
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200733 px = proxy_be_by_name(pname);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100734 if (!px)
735 WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist"));
736 }
737 else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200738 sname = trash.area;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100739 px = p;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100740 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100741 argp[idx].data.srv = findserver(px, sname);
742 if (!argp[idx].data.srv)
743 WILL_LJMP(luaL_argerror(L, first + idx, "server doesn't exist"));
744 argp[idx].type = ARGT_SRV;
745 break;
746
747 case ARGT_IPV4:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200748 memcpy(trash.area, argp[idx].data.str.area,
749 argp[idx].data.str.data);
750 trash.area[argp[idx].data.str.data] = 0;
751 if (inet_pton(AF_INET, trash.area, &argp[idx].data.ipv4))
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100752 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 address"));
753 argp[idx].type = ARGT_IPV4;
754 break;
755
756 case ARGT_MSK4:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200757 memcpy(trash.area, argp[idx].data.str.area,
758 argp[idx].data.str.data);
759 trash.area[argp[idx].data.str.data] = 0;
760 if (!str2mask(trash.area, &argp[idx].data.ipv4))
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100761 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 mask"));
762 argp[idx].type = ARGT_MSK4;
763 break;
764
765 case ARGT_IPV6:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200766 memcpy(trash.area, argp[idx].data.str.area,
767 argp[idx].data.str.data);
768 trash.area[argp[idx].data.str.data] = 0;
769 if (inet_pton(AF_INET6, trash.area, &argp[idx].data.ipv6))
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100770 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 address"));
771 argp[idx].type = ARGT_IPV6;
772 break;
773
774 case ARGT_MSK6:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200775 memcpy(trash.area, argp[idx].data.str.area,
776 argp[idx].data.str.data);
777 trash.area[argp[idx].data.str.data] = 0;
778 if (!str2mask6(trash.area, &argp[idx].data.ipv6))
Tim Duesterhusb814da62018-01-25 16:24:50 +0100779 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 mask"));
780 argp[idx].type = ARGT_MSK6;
781 break;
782
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100783 case ARGT_MAP:
784 case ARGT_REG:
785 case ARGT_USR:
786 WILL_LJMP(luaL_argerror(L, first + idx, "type not yet supported"));
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100787 break;
788 }
789
790 /* Check for type of argument. */
791 if ((mask & ARGT_MASK) != argp[idx].type) {
792 const char *msg = lua_pushfstring(L, "'%s' expected, got '%s'",
793 arg_type_names[(mask & ARGT_MASK)],
794 arg_type_names[argp[idx].type & ARGT_MASK]);
795 WILL_LJMP(luaL_argerror(L, first + idx, msg));
796 }
797
798 /* Next argument. */
799 mask >>= ARGT_BITS;
800 idx++;
801 }
802}
803
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100804/*
805 * The following functions are used to make correspondance between the the
806 * executed lua pointer and the "struct hlua *" that contain the context.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100807 *
808 * - hlua_gethlua : return the hlua context associated with an lua_State.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100809 * - hlua_sethlua : create the association between hlua context and lua_state.
810 */
811static inline struct hlua *hlua_gethlua(lua_State *L)
812{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +0100813 struct hlua **hlua = lua_getextraspace(L);
814 return *hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100815}
816static inline void hlua_sethlua(struct hlua *hlua)
817{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +0100818 struct hlua **hlua_store = lua_getextraspace(hlua->T);
819 *hlua_store = hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100820}
821
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100822/* This function is used to send logs. It try to send on screen (stderr)
823 * and on the default syslog server.
824 */
825static inline void hlua_sendlog(struct proxy *px, int level, const char *msg)
826{
827 struct tm tm;
828 char *p;
829
830 /* Cleanup the log message. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200831 p = trash.area;
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100832 for (; *msg != '\0'; msg++, p++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200833 if (p >= trash.area + trash.size - 1) {
Thierry FOURNIERccf00632015-09-16 12:47:03 +0200834 /* Break the message if exceed the buffer size. */
835 *(p-4) = ' ';
836 *(p-3) = '.';
837 *(p-2) = '.';
838 *(p-1) = '.';
839 break;
840 }
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100841 if (isprint(*msg))
842 *p = *msg;
843 else
844 *p = '.';
845 }
846 *p = '\0';
847
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200848 send_log(px, level, "%s\n", trash.area);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100849 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
Willy Tarreaua678b432015-08-28 10:14:59 +0200850 get_localtime(date.tv_sec, &tm);
851 fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n",
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100852 log_levels[level], tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec,
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200853 (int)getpid(), trash.area);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100854 fflush(stderr);
855 }
856}
857
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100858/* This function just ensure that the yield will be always
859 * returned with a timeout and permit to set some flags
860 */
861__LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx,
Thierry FOURNIERf90838b2015-03-06 13:48:32 +0100862 lua_KFunction k, int timeout, unsigned int flags)
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100863{
864 struct hlua *hlua = hlua_gethlua(L);
865
866 /* Set the wake timeout. If timeout is required, we set
867 * the expiration time.
868 */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +0200869 hlua->wake_time = timeout;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100870
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +0100871 hlua->flags |= flags;
872
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100873 /* Process the yield. */
Willy Tarreau9635e032018-10-16 17:52:55 +0200874 MAY_LJMP(lua_yieldk(L, nresults, ctx, k));
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100875}
876
Willy Tarreau87b09662015-04-03 00:22:06 +0200877/* This function initialises the Lua environment stored in the stream.
878 * It must be called at the start of the stream. This function creates
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100879 * an LUA coroutine. It can not be use to crete the main LUA context.
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200880 *
881 * This function is particular. it initialises a new Lua thread. If the
882 * initialisation fails (example: out of memory error), the lua function
883 * throws an error (longjmp).
884 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800885 * This function manipulates two Lua stacks: the main and the thread. Only
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200886 * the main stack can fail. The thread is not manipulated. This function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800887 * MUST NOT manipulate the created thread stack state, because it is not
888 * proctected against errors thrown by the thread stack.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100889 */
890int hlua_ctx_init(struct hlua *lua, struct task *task)
891{
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200892 if (!SET_SAFE_LJMP(gL.T)) {
893 lua->Tref = LUA_REFNIL;
894 return 0;
895 }
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100896 lua->Mref = LUA_REFNIL;
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +0100897 lua->flags = 0;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100898 LIST_INIT(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100899 lua->T = lua_newthread(gL.T);
900 if (!lua->T) {
901 lua->Tref = LUA_REFNIL;
Thierry FOURNIER0a976202017-07-12 11:18:00 +0200902 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100903 return 0;
904 }
905 hlua_sethlua(lua);
906 lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
907 lua->task = task;
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200908 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100909 return 1;
910}
911
Willy Tarreau87b09662015-04-03 00:22:06 +0200912/* Used to destroy the Lua coroutine when the attached stream or task
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100913 * is destroyed. The destroy also the memory context. The struct "lua"
914 * is not freed.
915 */
916void hlua_ctx_destroy(struct hlua *lua)
917{
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100918 if (!lua)
Thierry FOURNIERa718b292015-03-04 16:48:34 +0100919 return;
920
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100921 if (!lua->T)
922 goto end;
923
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100924 /* Purge all the pending signals. */
Thierry FOURNIERd6975962017-07-12 14:31:10 +0200925 notification_purge(&lua->com);
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100926
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200927 if (!SET_SAFE_LJMP(lua->T))
928 return;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100929 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200930 RESET_SAFE_LJMP(lua->T);
Thierry FOURNIER5a50a852015-09-23 16:59:28 +0200931
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200932 if (!SET_SAFE_LJMP(gL.T))
933 return;
934 luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref);
935 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER5a50a852015-09-23 16:59:28 +0200936 /* Forces a garbage collecting process. If the Lua program is finished
937 * without error, we run the GC on the thread pointer. Its freed all
938 * the unused memory.
939 * If the thread is finnish with an error or is currently yielded,
940 * it seems that the GC applied on the thread doesn't clean anything,
941 * so e run the GC on the main thread.
942 * NOTE: maybe this action locks all the Lua threads untiml the en of
943 * the garbage collection.
944 */
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +0200945 if (lua->flags & HLUA_MUST_GC) {
Thierry FOURNIER7bd10d52017-07-17 00:44:40 +0200946 if (!SET_SAFE_LJMP(gL.T))
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200947 return;
Thierry FOURNIER7bd10d52017-07-17 00:44:40 +0200948 lua_gc(gL.T, LUA_GCCOLLECT, 0);
949 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +0200950 }
Thierry FOURNIER5a50a852015-09-23 16:59:28 +0200951
Thierry FOURNIERa7b536b2015-09-21 22:50:24 +0200952 lua->T = NULL;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100953
954end:
Willy Tarreaubafbe012017-11-24 17:34:44 +0100955 pool_free(pool_head_hlua, lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100956}
957
958/* This function is used to restore the Lua context when a coroutine
959 * fails. This function copy the common memory between old coroutine
960 * and the new coroutine. The old coroutine is destroyed, and its
961 * replaced by the new coroutine.
962 * If the flag "keep_msg" is set, the last entry of the old is assumed
963 * as string error message and it is copied in the new stack.
964 */
965static int hlua_ctx_renew(struct hlua *lua, int keep_msg)
966{
967 lua_State *T;
968 int new_ref;
969
970 /* Renew the main LUA stack doesn't have sense. */
971 if (lua == &gL)
972 return 0;
973
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100974 /* New Lua coroutine. */
975 T = lua_newthread(gL.T);
976 if (!T)
977 return 0;
978
979 /* Copy last error message. */
980 if (keep_msg)
981 lua_xmove(lua->T, T, 1);
982
983 /* Copy data between the coroutines. */
984 lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref);
985 lua_xmove(lua->T, T, 1);
986 new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Valur poped. */
987
988 /* Destroy old data. */
989 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
990
991 /* The thread is garbage collected by Lua. */
992 luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref);
993
994 /* Fill the struct with the new coroutine values. */
995 lua->Mref = new_ref;
996 lua->T = T;
997 lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
998
999 /* Set context. */
1000 hlua_sethlua(lua);
1001
1002 return 1;
1003}
1004
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001005void hlua_hook(lua_State *L, lua_Debug *ar)
1006{
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001007 struct hlua *hlua = hlua_gethlua(L);
1008
1009 /* Lua cannot yield when its returning from a function,
1010 * so, we can fix the interrupt hook to 1 instruction,
1011 * expecting that the function is finnished.
1012 */
1013 if (lua_gethookmask(L) & LUA_MASKRET) {
1014 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1);
1015 return;
1016 }
1017
1018 /* restore the interrupt condition. */
1019 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
1020
1021 /* If we interrupt the Lua processing in yieldable state, we yield.
1022 * If the state is not yieldable, trying yield causes an error.
1023 */
1024 if (lua_isyieldable(L))
Willy Tarreau9635e032018-10-16 17:52:55 +02001025 MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD));
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001026
Thierry FOURNIERa85cfb12015-03-13 14:50:06 +01001027 /* If we cannot yield, update the clock and check the timeout. */
1028 tv_update_date(0, 1);
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001029 hlua->run_time += now_ms - hlua->start_time;
1030 if (hlua->max_time && hlua->run_time >= hlua->max_time) {
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001031 lua_pushfstring(L, "execution timeout");
1032 WILL_LJMP(lua_error(L));
1033 }
1034
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001035 /* Update the start time. */
1036 hlua->start_time = now_ms;
1037
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001038 /* Try to interrupt the process at the end of the current
1039 * unyieldable function.
1040 */
1041 lua_sethook(hlua->T, hlua_hook, LUA_MASKRET|LUA_MASKCOUNT, hlua_nb_instruction);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001042}
1043
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001044/* This function start or resumes the Lua stack execution. If the flag
1045 * "yield_allowed" if no set and the LUA stack execution returns a yield
1046 * The function return an error.
1047 *
1048 * The function can returns 4 values:
1049 * - HLUA_E_OK : The execution is terminated without any errors.
1050 * - HLUA_E_AGAIN : The execution must continue at the next associated
1051 * task wakeup.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001052 * - HLUA_E_ERRMSG : An error has occurred, an error message is set in
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001053 * the top of the stack.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001054 * - HLUA_E_ERR : An error has occurred without error message.
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001055 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001056 * If an error occurred, the stack is renewed and it is ready to run new
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001057 * LUA code.
1058 */
1059static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed)
1060{
1061 int ret;
1062 const char *msg;
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001063 const char *trace;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001064
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001065 /* Initialise run time counter. */
1066 if (!HLUA_IS_RUNNING(lua))
1067 lua->run_time = 0;
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001068
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001069 /* Lock the whole Lua execution. This lock must be before the
1070 * label "resume_execution".
1071 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001072 HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock);
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001073
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001074resume_execution:
1075
1076 /* This hook interrupts the Lua processing each 'hlua_nb_instruction'
1077 * instructions. it is used for preventing infinite loops.
1078 */
1079 lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
1080
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +01001081 /* Remove all flags except the running flags. */
Thierry FOURNIER2f3867f2015-09-28 01:02:01 +02001082 HLUA_SET_RUN(lua);
1083 HLUA_CLR_CTRLYIELD(lua);
1084 HLUA_CLR_WAKERESWR(lua);
1085 HLUA_CLR_WAKEREQWR(lua);
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +01001086
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001087 /* Update the start time. */
1088 lua->start_time = now_ms;
1089
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001090 /* Call the function. */
1091 ret = lua_resume(lua->T, gL.T, lua->nargs);
1092 switch (ret) {
1093
1094 case LUA_OK:
1095 ret = HLUA_E_OK;
1096 break;
1097
1098 case LUA_YIELD:
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001099 /* Check if the execution timeout is expired. It it is the case, we
1100 * break the Lua execution.
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001101 */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001102 tv_update_date(0, 1);
1103 lua->run_time += now_ms - lua->start_time;
1104 if (lua->max_time && lua->run_time > lua->max_time) {
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001105 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001106 ret = HLUA_E_ETMOUT;
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001107 break;
1108 }
1109 /* Process the forced yield. if the general yield is not allowed or
1110 * if no task were associated this the current Lua execution
1111 * coroutine, we resume the execution. Else we want to return in the
1112 * scheduler and we want to be waked up again, to continue the
1113 * current Lua execution. So we schedule our own task.
1114 */
1115 if (HLUA_IS_CTRLYIELDING(lua)) {
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001116 if (!yield_allowed || !lua->task)
1117 goto resume_execution;
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001118 task_wakeup(lua->task, TASK_WOKEN_MSG);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001119 }
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001120 if (!yield_allowed) {
1121 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001122 ret = HLUA_E_YIELD;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001123 break;
1124 }
1125 ret = HLUA_E_AGAIN;
1126 break;
1127
1128 case LUA_ERRRUN:
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001129
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001130 /* Special exit case. The traditional exit is returned as an error
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001131 * because the errors ares the only one mean to return immediately
1132 * from and lua execution.
1133 */
1134 if (lua->flags & HLUA_EXIT) {
1135 ret = HLUA_E_OK;
Thierry FOURNIERe1587b32015-08-28 09:54:13 +02001136 hlua_ctx_renew(lua, 0);
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001137 break;
1138 }
1139
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001140 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001141 if (!lua_checkstack(lua->T, 1)) {
1142 ret = HLUA_E_ERR;
1143 break;
1144 }
1145 msg = lua_tostring(lua->T, -1);
1146 lua_settop(lua->T, 0); /* Empty the stack. */
1147 lua_pop(lua->T, 1);
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001148 trace = hlua_traceback(lua->T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001149 if (msg)
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001150 lua_pushfstring(lua->T, "runtime error: %s from %s", msg, trace);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001151 else
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001152 lua_pushfstring(lua->T, "unknown runtime error from %s", trace);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001153 ret = HLUA_E_ERRMSG;
1154 break;
1155
1156 case LUA_ERRMEM:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001157 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001158 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001159 ret = HLUA_E_NOMEM;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001160 break;
1161
1162 case LUA_ERRERR:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001163 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001164 if (!lua_checkstack(lua->T, 1)) {
1165 ret = HLUA_E_ERR;
1166 break;
1167 }
1168 msg = lua_tostring(lua->T, -1);
1169 lua_settop(lua->T, 0); /* Empty the stack. */
1170 lua_pop(lua->T, 1);
1171 if (msg)
1172 lua_pushfstring(lua->T, "message handler error: %s", msg);
1173 else
1174 lua_pushfstring(lua->T, "message handler error");
1175 ret = HLUA_E_ERRMSG;
1176 break;
1177
1178 default:
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_ERR;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001182 break;
1183 }
1184
Thierry FOURNIER6ab4d8e2015-09-27 22:17:19 +02001185 /* This GC permits to destroy some object when a Lua timeout strikes. */
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +02001186 if (lua->flags & HLUA_MUST_GC &&
1187 ret != HLUA_E_AGAIN)
Thierry FOURNIER6ab4d8e2015-09-27 22:17:19 +02001188 lua_gc(lua->T, LUA_GCCOLLECT, 0);
1189
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001190 switch (ret) {
1191 case HLUA_E_AGAIN:
1192 break;
1193
1194 case HLUA_E_ERRMSG:
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001195 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001196 hlua_ctx_renew(lua, 1);
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001197 HLUA_CLR_RUN(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001198 break;
1199
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001200 case HLUA_E_ETMOUT:
1201 case HLUA_E_NOMEM:
1202 case HLUA_E_YIELD:
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001203 case HLUA_E_ERR:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001204 HLUA_CLR_RUN(lua);
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001205 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001206 hlua_ctx_renew(lua, 0);
1207 break;
1208
1209 case HLUA_E_OK:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001210 HLUA_CLR_RUN(lua);
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001211 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001212 break;
1213 }
1214
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001215 /* This is the main exit point, remove the Lua lock. */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001216 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock);
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001217
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001218 return ret;
1219}
1220
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001221/* This function exit the current code. */
1222__LJMP static int hlua_done(lua_State *L)
1223{
1224 struct hlua *hlua = hlua_gethlua(L);
1225
1226 hlua->flags |= HLUA_EXIT;
1227 WILL_LJMP(lua_error(L));
1228
1229 return 0;
1230}
1231
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001232/* This function is an LUA binding. It provides a function
1233 * for deleting ACL from a referenced ACL file.
1234 */
1235__LJMP static int hlua_del_acl(lua_State *L)
1236{
1237 const char *name;
1238 const char *key;
1239 struct pat_ref *ref;
1240
1241 MAY_LJMP(check_args(L, 2, "del_acl"));
1242
1243 name = MAY_LJMP(luaL_checkstring(L, 1));
1244 key = MAY_LJMP(luaL_checkstring(L, 2));
1245
1246 ref = pat_ref_lookup(name);
1247 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001248 WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001249
1250 pat_ref_delete(ref, key);
1251 return 0;
1252}
1253
1254/* This function is an LUA binding. It provides a function
1255 * for deleting map entry from a referenced map file.
1256 */
1257static int hlua_del_map(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_map"));
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_map': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001271
1272 pat_ref_delete(ref, key);
1273 return 0;
1274}
1275
1276/* This function is an LUA binding. It provides a function
1277 * for adding ACL pattern from a referenced ACL file.
1278 */
1279static int hlua_add_acl(lua_State *L)
1280{
1281 const char *name;
1282 const char *key;
1283 struct pat_ref *ref;
1284
1285 MAY_LJMP(check_args(L, 2, "add_acl"));
1286
1287 name = MAY_LJMP(luaL_checkstring(L, 1));
1288 key = MAY_LJMP(luaL_checkstring(L, 2));
1289
1290 ref = pat_ref_lookup(name);
1291 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001292 WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001293
1294 if (pat_ref_find_elt(ref, key) == NULL)
1295 pat_ref_add(ref, key, NULL, NULL);
1296 return 0;
1297}
1298
1299/* This function is an LUA binding. It provides a function
1300 * for setting map pattern and sample from a referenced map
1301 * file.
1302 */
1303static int hlua_set_map(lua_State *L)
1304{
1305 const char *name;
1306 const char *key;
1307 const char *value;
1308 struct pat_ref *ref;
1309
1310 MAY_LJMP(check_args(L, 3, "set_map"));
1311
1312 name = MAY_LJMP(luaL_checkstring(L, 1));
1313 key = MAY_LJMP(luaL_checkstring(L, 2));
1314 value = MAY_LJMP(luaL_checkstring(L, 3));
1315
1316 ref = pat_ref_lookup(name);
1317 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001318 WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001319
1320 if (pat_ref_find_elt(ref, key) != NULL)
1321 pat_ref_set(ref, key, value, NULL);
1322 else
1323 pat_ref_add(ref, key, value, NULL);
1324 return 0;
1325}
1326
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001327/* A class is a lot of memory that contain data. This data can be a table,
1328 * an integer or user data. This data is associated with a metatable. This
1329 * metatable have an original version registred in the global context with
1330 * the name of the object (_G[<name>] = <metable> ).
1331 *
1332 * A metable is a table that modify the standard behavior of a standard
1333 * access to the associated data. The entries of this new metatable are
1334 * defined as is:
1335 *
1336 * http://lua-users.org/wiki/MetatableEvents
1337 *
1338 * __index
1339 *
1340 * we access an absent field in a table, the result is nil. This is
1341 * true, but it is not the whole truth. Actually, such access triggers
1342 * the interpreter to look for an __index metamethod: If there is no
1343 * such method, as usually happens, then the access results in nil;
1344 * otherwise, the metamethod will provide the result.
1345 *
1346 * Control 'prototype' inheritance. When accessing "myTable[key]" and
1347 * the key does not appear in the table, but the metatable has an __index
1348 * property:
1349 *
1350 * - if the value is a function, the function is called, passing in the
1351 * table and the key; the return value of that function is returned as
1352 * the result.
1353 *
1354 * - if the value is another table, the value of the key in that table is
1355 * asked for and returned (and if it doesn't exist in that table, but that
1356 * table's metatable has an __index property, then it continues on up)
1357 *
1358 * - Use "rawget(myTable,key)" to skip this metamethod.
1359 *
1360 * http://www.lua.org/pil/13.4.1.html
1361 *
1362 * __newindex
1363 *
1364 * Like __index, but control property assignment.
1365 *
1366 * __mode - Control weak references. A string value with one or both
1367 * of the characters 'k' and 'v' which specifies that the the
1368 * keys and/or values in the table are weak references.
1369 *
1370 * __call - Treat a table like a function. When a table is followed by
1371 * parenthesis such as "myTable( 'foo' )" and the metatable has
1372 * a __call key pointing to a function, that function is invoked
1373 * (passing any specified arguments) and the return value is
1374 * returned.
1375 *
1376 * __metatable - Hide the metatable. When "getmetatable( myTable )" is
1377 * called, if the metatable for myTable has a __metatable
1378 * key, the value of that key is returned instead of the
1379 * actual metatable.
1380 *
1381 * __tostring - Control string representation. When the builtin
1382 * "tostring( myTable )" function is called, if the metatable
1383 * for myTable has a __tostring property set to a function,
1384 * that function is invoked (passing myTable to it) and the
1385 * return value is used as the string representation.
1386 *
1387 * __len - Control table length. When the table length is requested using
1388 * the length operator ( '#' ), if the metatable for myTable has
1389 * a __len key pointing to a function, that function is invoked
1390 * (passing myTable to it) and the return value used as the value
1391 * of "#myTable".
1392 *
1393 * __gc - Userdata finalizer code. When userdata is set to be garbage
1394 * collected, if the metatable has a __gc field pointing to a
1395 * function, that function is first invoked, passing the userdata
1396 * to it. The __gc metamethod is not called for tables.
1397 * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html)
1398 *
1399 * Special metamethods for redefining standard operators:
1400 * http://www.lua.org/pil/13.1.html
1401 *
1402 * __add "+"
1403 * __sub "-"
1404 * __mul "*"
1405 * __div "/"
1406 * __unm "!"
1407 * __pow "^"
1408 * __concat ".."
1409 *
1410 * Special methods for redfining standar relations
1411 * http://www.lua.org/pil/13.2.html
1412 *
1413 * __eq "=="
1414 * __lt "<"
1415 * __le "<="
1416 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001417
1418/*
1419 *
1420 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001421 * Class Map
1422 *
1423 *
1424 */
1425
1426/* Returns a struct hlua_map if the stack entry "ud" is
1427 * a class session, otherwise it throws an error.
1428 */
1429__LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud)
1430{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001431 return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001432}
1433
1434/* This function is the map constructor. It don't need
1435 * the class Map object. It creates and return a new Map
1436 * object. It must be called only during "body" or "init"
1437 * context because it process some filesystem accesses.
1438 */
1439__LJMP static int hlua_map_new(struct lua_State *L)
1440{
1441 const char *fn;
1442 int match = PAT_MATCH_STR;
1443 struct sample_conv conv;
1444 const char *file = "";
1445 int line = 0;
1446 lua_Debug ar;
1447 char *err = NULL;
1448 struct arg args[2];
1449
1450 if (lua_gettop(L) < 1 || lua_gettop(L) > 2)
1451 WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument."));
1452
1453 fn = MAY_LJMP(luaL_checkstring(L, 1));
1454
1455 if (lua_gettop(L) >= 2) {
1456 match = MAY_LJMP(luaL_checkinteger(L, 2));
1457 if (match < 0 || match >= PAT_MATCH_NUM)
1458 WILL_LJMP(luaL_error(L, "'new' needs a valid match method."));
1459 }
1460
1461 /* Get Lua filename and line number. */
1462 if (lua_getstack(L, 1, &ar)) { /* check function at level */
1463 lua_getinfo(L, "Sl", &ar); /* get info about it */
1464 if (ar.currentline > 0) { /* is there info? */
1465 file = ar.short_src;
1466 line = ar.currentline;
1467 }
1468 }
1469
1470 /* fill fake sample_conv struct. */
1471 conv.kw = ""; /* unused. */
1472 conv.process = NULL; /* unused. */
1473 conv.arg_mask = 0; /* unused. */
1474 conv.val_args = NULL; /* unused. */
1475 conv.out_type = SMP_T_STR;
1476 conv.private = (void *)(long)match;
1477 switch (match) {
1478 case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break;
1479 case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break;
1480 case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break;
1481 case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break;
1482 case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break;
1483 case PAT_MATCH_END: conv.in_type = SMP_T_STR; break;
1484 case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001485 case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001486 case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break;
1487 default:
1488 WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode."));
1489 }
1490
1491 /* fill fake args. */
1492 args[0].type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001493 args[0].data.str.area = (char *)fn;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001494 args[1].type = ARGT_STOP;
1495
1496 /* load the map. */
1497 if (!sample_load_map(args, &conv, file, line, &err)) {
1498 /* error case: we cant use luaL_error because we must
1499 * free the err variable.
1500 */
1501 luaL_where(L, 1);
1502 lua_pushfstring(L, "'new': %s.", err);
1503 lua_concat(L, 2);
1504 free(err);
1505 WILL_LJMP(lua_error(L));
1506 }
1507
1508 /* create the lua object. */
1509 lua_newtable(L);
1510 lua_pushlightuserdata(L, args[0].data.map);
1511 lua_rawseti(L, -2, 0);
1512
1513 /* Pop a class Map metatable and affect it to the userdata. */
1514 lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref);
1515 lua_setmetatable(L, -2);
1516
1517
1518 return 1;
1519}
1520
1521__LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str)
1522{
1523 struct map_descriptor *desc;
1524 struct pattern *pat;
1525 struct sample smp;
1526
1527 MAY_LJMP(check_args(L, 2, "lookup"));
1528 desc = MAY_LJMP(hlua_checkmap(L, 1));
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001529 if (desc->pat.expect_type == SMP_T_SINT) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001530 smp.data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001531 smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001532 }
1533 else {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001534 smp.data.type = SMP_T_STR;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001535 smp.flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001536 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 +02001537 }
1538
1539 pat = pattern_exec_match(&desc->pat, &smp, 1);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001540 if (!pat || !pat->data) {
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001541 if (str)
1542 lua_pushstring(L, "");
1543 else
1544 lua_pushnil(L);
1545 return 1;
1546 }
1547
1548 /* The Lua pattern must return a string, so we can't check the returned type */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001549 lua_pushlstring(L, pat->data->u.str.area, pat->data->u.str.data);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001550 return 1;
1551}
1552
1553__LJMP static int hlua_map_lookup(struct lua_State *L)
1554{
1555 return _hlua_map_lookup(L, 0);
1556}
1557
1558__LJMP static int hlua_map_slookup(struct lua_State *L)
1559{
1560 return _hlua_map_lookup(L, 1);
1561}
1562
1563/*
1564 *
1565 *
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001566 * Class Socket
1567 *
1568 *
1569 */
1570
1571__LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud)
1572{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001573 return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001574}
1575
1576/* This function is the handler called for each I/O on the established
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001577 * connection. It is used for notify space available to send or data
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001578 * received.
1579 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001580static void hlua_socket_handler(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001581{
Willy Tarreau00a37f02015-04-13 12:05:19 +02001582 struct stream_interface *si = appctx->owner;
Olivier Houchard9aaf7782017-09-13 18:30:23 +02001583 struct connection *c = cs_conn(objt_cs(si_opposite(si)->end));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001584
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001585 if (appctx->ctx.hlua_cosocket.die) {
1586 si_shutw(si);
1587 si_shutr(si);
1588 si_ic(si)->flags |= CF_READ_NULL;
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001589 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1590 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001591 stream_shutdown(si_strm(si), SF_ERR_KILLED);
1592 }
1593
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001594 /* If the connection object is not available, close all the
1595 * streams and wakeup everything waiting for.
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001596 */
1597 if (!c) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001598 si_shutw(si);
1599 si_shutr(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01001600 si_ic(si)->flags |= CF_READ_NULL;
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001601 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1602 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Willy Tarreaud4da1962015-04-20 01:31:23 +02001603 return;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001604 }
1605
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001606 /* If we cant write, wakeup the pending write signals. */
1607 if (channel_output_closed(si_ic(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001608 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001609
1610 /* If we cant read, wakeup the pending read signals. */
1611 if (channel_input_closed(si_oc(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001612 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001613
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001614 /* if the connection is not estabkished, inform the stream that we want
1615 * to be notified whenever the connection completes.
1616 */
1617 if (!(c->flags & CO_FL_CONNECTED)) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01001618 si_cant_get(si);
Willy Tarreau12c24232018-12-06 15:29:50 +01001619 si_rx_conn_blk(si);
Willy Tarreau3367d412018-11-15 10:57:41 +01001620 si_rx_endp_more(si);
Willy Tarreaud4da1962015-04-20 01:31:23 +02001621 return;
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001622 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001623
1624 /* This function is called after the connect. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01001625 appctx->ctx.hlua_cosocket.connected = 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001626
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001627 /* Wake the tasks which wants to write if the buffer have available space. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001628 if (channel_may_recv(si_ic(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001629 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001630
1631 /* Wake the tasks which wants to read if the buffer contains data. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001632 if (!channel_is_empty(si_oc(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001633 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001634
1635 /* Some data were injected in the buffer, notify the stream
1636 * interface.
1637 */
1638 if (!channel_is_empty(si_ic(si)))
1639 stream_int_update(si);
1640
1641 /* If write notifications are registered, we considers we want
Willy Tarreau3367d412018-11-15 10:57:41 +01001642 * to write, so we clear the blocking flag.
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001643 */
1644 if (notification_registered(&appctx->ctx.hlua_cosocket.wake_on_write))
Willy Tarreau3367d412018-11-15 10:57:41 +01001645 si_rx_endp_more(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001646}
1647
Willy Tarreau87b09662015-04-03 00:22:06 +02001648/* This function is called when the "struct stream" is destroyed.
1649 * Remove the link from the object to this stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001650 * Wake all the pending signals.
1651 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001652static void hlua_socket_release(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001653{
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001654 struct xref *peer;
1655
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001656 /* Remove my link in the original object. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001657 peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref);
1658 if (peer)
1659 xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001660
1661 /* Wake all the task waiting for me. */
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001662 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1663 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001664}
1665
1666/* If the garbage collectio of the object is launch, nobody
Willy Tarreau87b09662015-04-03 00:22:06 +02001667 * uses this object. If the stream does not exists, just quit.
1668 * Send the shutdown signal to the stream. In some cases,
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001669 * pending signal can rest in the read and write lists. destroy
1670 * it.
1671 */
1672__LJMP static int hlua_socket_gc(lua_State *L)
1673{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001674 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001675 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001676 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001677
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001678 MAY_LJMP(check_args(L, 1, "__gc"));
1679
1680 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001681 peer = xref_get_peer_and_lock(&socket->xref);
1682 if (!peer)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001683 return 0;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001684 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001685
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001686 /* Set the flag which destroy the session. */
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001687 appctx->ctx.hlua_cosocket.die = 1;
1688 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001689
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001690 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001691 xref_disconnect(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001692 return 0;
1693}
1694
1695/* The close function send shutdown signal and break the
Willy Tarreau87b09662015-04-03 00:22:06 +02001696 * links between the stream and the object.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001697 */
sada05ed3302018-05-11 11:48:18 -07001698__LJMP static int hlua_socket_close_helper(lua_State *L)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001699{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001700 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001701 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001702 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001703
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001704 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001705
1706 /* Check if we run on the same thread than the xreator thread.
1707 * We cannot access to the socket if the thread is different.
1708 */
1709 if (socket->tid != tid)
1710 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1711
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001712 peer = xref_get_peer_and_lock(&socket->xref);
1713 if (!peer)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001714 return 0;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001715 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001716
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001717 /* Set the flag which destroy the session. */
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001718 appctx->ctx.hlua_cosocket.die = 1;
1719 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001720
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001721 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001722 xref_disconnect(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001723 return 0;
1724}
1725
sada05ed3302018-05-11 11:48:18 -07001726/* The close function calls close_helper.
1727 */
1728__LJMP static int hlua_socket_close(lua_State *L)
1729{
1730 MAY_LJMP(check_args(L, 1, "close"));
1731 return hlua_socket_close_helper(L);
1732}
1733
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001734/* This Lua function assumes that the stack contain three parameters.
1735 * 1 - USERDATA containing a struct socket
1736 * 2 - INTEGER with values of the macro defined below
1737 * If the integer is -1, we must read at most one line.
1738 * If the integer is -2, we ust read all the data until the
1739 * end of the stream.
1740 * If the integer is positive value, we must read a number of
1741 * bytes corresponding to this value.
1742 */
1743#define HLSR_READ_LINE (-1)
1744#define HLSR_READ_ALL (-2)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001745__LJMP static int hlua_socket_receive_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001746{
1747 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
1748 int wanted = lua_tointeger(L, 2);
1749 struct hlua *hlua = hlua_gethlua(L);
1750 struct appctx *appctx;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001751 size_t len;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001752 int nblk;
Willy Tarreau206ba832018-06-14 15:27:31 +02001753 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001754 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02001755 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001756 size_t len2;
Thierry FOURNIER00543922015-03-09 18:35:06 +01001757 int skip_at_end = 0;
Willy Tarreau81389672015-03-10 12:03:52 +01001758 struct channel *oc;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001759 struct stream_interface *si;
1760 struct stream *s;
1761 struct xref *peer;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001762 int missing_bytes;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001763
1764 /* Check if this lua stack is schedulable. */
1765 if (!hlua || !hlua->task)
1766 WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in "
1767 "'frontend', 'backend' or 'task'"));
1768
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001769 /* Check if we run on the same thread than the xreator thread.
1770 * We cannot access to the socket if the thread is different.
1771 */
1772 if (socket->tid != tid)
1773 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1774
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001775 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001776 peer = xref_get_peer_and_lock(&socket->xref);
1777 if (!peer)
1778 goto no_peer;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001779 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
1780 si = appctx->owner;
1781 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001782
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001783 oc = &s->res;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001784 if (wanted == HLSR_READ_LINE) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001785 /* Read line. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001786 nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001787 if (nblk < 0) /* Connection close. */
1788 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001789 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001790 goto connection_empty;
Thierry FOURNIER00543922015-03-09 18:35:06 +01001791
1792 /* remove final \r\n. */
1793 if (nblk == 1) {
1794 if (blk1[len1-1] == '\n') {
1795 len1--;
1796 skip_at_end++;
1797 if (blk1[len1-1] == '\r') {
1798 len1--;
1799 skip_at_end++;
1800 }
1801 }
1802 }
1803 else {
1804 if (blk2[len2-1] == '\n') {
1805 len2--;
1806 skip_at_end++;
1807 if (blk2[len2-1] == '\r') {
1808 len2--;
1809 skip_at_end++;
1810 }
1811 }
1812 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001813 }
1814
1815 else if (wanted == HLSR_READ_ALL) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001816 /* Read all the available data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001817 nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001818 if (nblk < 0) /* Connection close. */
1819 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001820 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001821 goto connection_empty;
1822 }
1823
1824 else {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001825 /* Read a block of data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001826 nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001827 if (nblk < 0) /* Connection close. */
1828 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001829 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001830 goto connection_empty;
1831
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001832 missing_bytes = wanted - socket->b.n;
1833 if (len1 > missing_bytes) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001834 nblk = 1;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001835 len1 = missing_bytes;
1836 } if (nblk == 2 && len1 + len2 > missing_bytes)
1837 len2 = missing_bytes - len1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001838 }
1839
1840 len = len1;
1841
1842 luaL_addlstring(&socket->b, blk1, len1);
1843 if (nblk == 2) {
1844 len += len2;
1845 luaL_addlstring(&socket->b, blk2, len2);
1846 }
1847
1848 /* Consume data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001849 co_skip(oc, len + skip_at_end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001850
1851 /* Don't wait anything. */
Thierry FOURNIER7e4ee472018-05-25 15:03:50 +02001852 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001853
1854 /* If the pattern reclaim to read all the data
1855 * in the connection, got out.
1856 */
1857 if (wanted == HLSR_READ_ALL)
1858 goto connection_empty;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001859 else if (wanted >= 0 && socket->b.n < wanted)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001860 goto connection_empty;
1861
1862 /* Return result. */
1863 luaL_pushresult(&socket->b);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001864 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001865 return 1;
1866
1867connection_closed:
1868
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001869 xref_unlock(&socket->xref, peer);
1870
1871no_peer:
1872
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001873 /* If the buffer containds data. */
1874 if (socket->b.n > 0) {
1875 luaL_pushresult(&socket->b);
1876 return 1;
1877 }
1878 lua_pushnil(L);
1879 lua_pushstring(L, "connection closed.");
1880 return 2;
1881
1882connection_empty:
1883
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001884 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) {
1885 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001886 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001887 }
1888 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02001889 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_receive_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001890 return 0;
1891}
1892
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001893/* This Lua function gets two parameters. The first one can be string
1894 * or a number. If the string is "*l", the user requires one line. If
1895 * the string is "*a", the user requires all the contents of the stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001896 * If the value is a number, the user require a number of bytes equal
1897 * to the value. The default value is "*l" (a line).
1898 *
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001899 * This parameter with a variable type is converted in integer. This
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001900 * integer takes this values:
1901 * -1 : read a line
1902 * -2 : read all the stream
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001903 * >0 : amount of bytes.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001904 *
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001905 * The second parameter is optional. It contains a string that must be
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001906 * concatenated with the read data.
1907 */
1908__LJMP static int hlua_socket_receive(struct lua_State *L)
1909{
1910 int wanted = HLSR_READ_LINE;
1911 const char *pattern;
1912 int type;
1913 char *error;
1914 size_t len;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001915 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001916
1917 if (lua_gettop(L) < 1 || lua_gettop(L) > 3)
1918 WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments."));
1919
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001920 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001921
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001922 /* Check if we run on the same thread than the xreator thread.
1923 * We cannot access to the socket if the thread is different.
1924 */
1925 if (socket->tid != tid)
1926 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1927
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001928 /* check for pattern. */
1929 if (lua_gettop(L) >= 2) {
1930 type = lua_type(L, 2);
1931 if (type == LUA_TSTRING) {
1932 pattern = lua_tostring(L, 2);
1933 if (strcmp(pattern, "*a") == 0)
1934 wanted = HLSR_READ_ALL;
1935 else if (strcmp(pattern, "*l") == 0)
1936 wanted = HLSR_READ_LINE;
1937 else {
1938 wanted = strtoll(pattern, &error, 10);
1939 if (*error != '\0')
1940 WILL_LJMP(luaL_error(L, "Unsupported pattern."));
1941 }
1942 }
1943 else if (type == LUA_TNUMBER) {
1944 wanted = lua_tointeger(L, 2);
1945 if (wanted < 0)
1946 WILL_LJMP(luaL_error(L, "Unsupported size."));
1947 }
1948 }
1949
1950 /* Set pattern. */
1951 lua_pushinteger(L, wanted);
Tim Duesterhusc6e377e2018-01-04 19:32:13 +01001952
1953 /* Check if we would replace the top by itself. */
1954 if (lua_gettop(L) != 2)
1955 lua_replace(L, 2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001956
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001957 /* init buffer, and fill it with prefix. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001958 luaL_buffinit(L, &socket->b);
1959
1960 /* Check prefix. */
1961 if (lua_gettop(L) >= 3) {
1962 if (lua_type(L, 3) != LUA_TSTRING)
1963 WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix"));
1964 pattern = lua_tolstring(L, 3, &len);
1965 luaL_addlstring(&socket->b, pattern, len);
1966 }
1967
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001968 return __LJMP(hlua_socket_receive_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001969}
1970
1971/* Write the Lua input string in the output buffer.
Mark Lakes22154b42018-01-29 14:38:40 -08001972 * This function returns a yield if no space is available.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001973 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001974static int hlua_socket_write_yield(struct lua_State *L,int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001975{
1976 struct hlua_socket *socket;
1977 struct hlua *hlua = hlua_gethlua(L);
1978 struct appctx *appctx;
1979 size_t buf_len;
1980 const char *buf;
1981 int len;
1982 int send_len;
1983 int sent;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001984 struct xref *peer;
1985 struct stream_interface *si;
1986 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001987
1988 /* Check if this lua stack is schedulable. */
1989 if (!hlua || !hlua->task)
1990 WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in "
1991 "'frontend', 'backend' or 'task'"));
1992
1993 /* Get object */
1994 socket = MAY_LJMP(hlua_checksocket(L, 1));
1995 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001996 sent = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001997
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001998 /* Check if we run on the same thread than the xreator thread.
1999 * We cannot access to the socket if the thread is different.
2000 */
2001 if (socket->tid != tid)
2002 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2003
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002004 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002005 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002006 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002007 lua_pushinteger(L, -1);
2008 return 1;
2009 }
2010 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2011 si = appctx->owner;
2012 s = si_strm(si);
2013
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002014 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002015 if (channel_output_closed(&s->req)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002016 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002017 lua_pushinteger(L, -1);
2018 return 1;
2019 }
2020
2021 /* Update the input buffer data. */
2022 buf += sent;
2023 send_len = buf_len - sent;
2024
2025 /* All the data are sent. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002026 if (sent >= buf_len) {
2027 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002028 return 1; /* Implicitly return the length sent. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002029 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002030
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002031 /* Check if the buffer is available because HAProxy doesn't allocate
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01002032 * the request buffer if its not required.
2033 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002034 if (s->req.buf.size == 0) {
Willy Tarreau581abd32018-10-25 10:21:41 +02002035 if (!si_alloc_ibuf(si, &appctx->buffer_wait))
Christopher Faulet33834b12016-12-19 09:29:06 +01002036 goto hlua_socket_write_yield_return;
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01002037 }
2038
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002039 /* Check for available space. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002040 len = b_room(&s->req.buf);
Christopher Faulet33834b12016-12-19 09:29:06 +01002041 if (len <= 0) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002042 goto hlua_socket_write_yield_return;
Christopher Faulet33834b12016-12-19 09:29:06 +01002043 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002044
2045 /* send data */
2046 if (len < send_len)
2047 send_len = len;
Thierry FOURNIER66b89192018-05-27 01:14:47 +02002048 len = ci_putblk(&s->req, buf, send_len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002049
2050 /* "Not enough space" (-1), "Buffer too little to contain
2051 * the data" (-2) are not expected because the available length
2052 * is tested.
2053 * Other unknown error are also not expected.
2054 */
2055 if (len <= 0) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01002056 if (len == -1)
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002057 s->req.flags |= CF_WAKE_WRITE;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002058
sada05ed3302018-05-11 11:48:18 -07002059 MAY_LJMP(hlua_socket_close_helper(L));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002060 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002061 lua_pushinteger(L, -1);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002062 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002063 return 1;
2064 }
2065
2066 /* update buffers. */
Thierry FOURNIER101b9762018-05-27 01:27:40 +02002067 appctx_wakeup(appctx);
Willy Tarreaude70fa12015-09-26 11:25:05 +02002068
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002069 s->req.rex = TICK_ETERNITY;
2070 s->res.wex = TICK_ETERNITY;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002071
2072 /* Update length sent. */
2073 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002074 lua_pushinteger(L, sent + len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002075
2076 /* All the data buffer is sent ? */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002077 if (sent + len >= buf_len) {
2078 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002079 return 1;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002080 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002081
2082hlua_socket_write_yield_return:
Thierry FOURNIERba42fcd2018-05-27 00:59:48 +02002083 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2084 xref_unlock(&socket->xref, peer);
2085 WILL_LJMP(luaL_error(L, "out of memory"));
2086 }
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002087 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002088 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_write_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002089 return 0;
2090}
2091
2092/* This function initiate the send of data. It just check the input
2093 * parameters and push an integer in the Lua stack that contain the
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002094 * amount of data written to the buffer. This is used by the function
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002095 * "hlua_socket_write_yield" that can yield.
2096 *
2097 * The Lua function gets between 3 and 4 parameters. The first one is
2098 * the associated object. The second is a string buffer. The third is
2099 * a facultative integer that represents where is the buffer position
2100 * of the start of the data that can send. The first byte is the
2101 * position "1". The default value is "1". The fourth argument is a
2102 * facultative integer that represents where is the buffer position
2103 * of the end of the data that can send. The default is the last byte.
2104 */
2105static int hlua_socket_send(struct lua_State *L)
2106{
2107 int i;
2108 int j;
2109 const char *buf;
2110 size_t buf_len;
2111
2112 /* Check number of arguments. */
2113 if (lua_gettop(L) < 2 || lua_gettop(L) > 4)
2114 WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments"));
2115
2116 /* Get the string. */
2117 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
2118
2119 /* Get and check j. */
2120 if (lua_gettop(L) == 4) {
2121 j = MAY_LJMP(luaL_checkinteger(L, 4));
2122 if (j < 0)
2123 j = buf_len + j + 1;
2124 if (j > buf_len)
2125 j = buf_len + 1;
2126 lua_pop(L, 1);
2127 }
2128 else
2129 j = buf_len;
2130
2131 /* Get and check i. */
2132 if (lua_gettop(L) == 3) {
2133 i = MAY_LJMP(luaL_checkinteger(L, 3));
2134 if (i < 0)
2135 i = buf_len + i + 1;
2136 if (i > buf_len)
2137 i = buf_len + 1;
2138 lua_pop(L, 1);
2139 } else
2140 i = 1;
2141
2142 /* Check bth i and j. */
2143 if (i > j) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002144 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002145 return 1;
2146 }
2147 if (i == 0 && j == 0) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002148 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002149 return 1;
2150 }
2151 if (i == 0)
2152 i = 1;
2153 if (j == 0)
2154 j = 1;
2155
2156 /* Pop the string. */
2157 lua_pop(L, 1);
2158
2159 /* Update the buffer length. */
2160 buf += i - 1;
2161 buf_len = j - i + 1;
2162 lua_pushlstring(L, buf, buf_len);
2163
2164 /* This unsigned is used to remember the amount of sent data. */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002165 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002166
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002167 return MAY_LJMP(hlua_socket_write_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002168}
2169
Willy Tarreau22b0a682015-06-17 19:43:49 +02002170#define SOCKET_INFO_MAX_LEN sizeof("[0000:0000:0000:0000:0000:0000:0000:0000]:12345")
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002171__LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr)
2172{
2173 static char buffer[SOCKET_INFO_MAX_LEN];
2174 int ret;
2175 int len;
2176 char *p;
2177
2178 ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1);
2179 if (ret <= 0) {
2180 lua_pushnil(L);
2181 return 1;
2182 }
2183
2184 if (ret == AF_UNIX) {
2185 lua_pushstring(L, buffer+1);
2186 return 1;
2187 }
2188 else if (ret == AF_INET6) {
2189 buffer[0] = '[';
2190 len = strlen(buffer);
2191 buffer[len] = ']';
2192 len++;
2193 buffer[len] = ':';
2194 len++;
2195 p = buffer;
2196 }
2197 else if (ret == AF_INET) {
2198 p = buffer + 1;
2199 len = strlen(p);
2200 p[len] = ':';
2201 len++;
2202 }
2203 else {
2204 lua_pushnil(L);
2205 return 1;
2206 }
2207
2208 if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) {
2209 lua_pushnil(L);
2210 return 1;
2211 }
2212
2213 lua_pushstring(L, p);
2214 return 1;
2215}
2216
2217/* Returns information about the peer of the connection. */
2218__LJMP static int hlua_socket_getpeername(struct lua_State *L)
2219{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002220 struct hlua_socket *socket;
2221 struct connection *conn;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002222 struct xref *peer;
2223 struct appctx *appctx;
2224 struct stream_interface *si;
2225 struct stream *s;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002226 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002227
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002228 MAY_LJMP(check_args(L, 1, "getpeername"));
2229
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002230 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002231
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002232 /* Check if we run on the same thread than the xreator thread.
2233 * We cannot access to the socket if the thread is different.
2234 */
2235 if (socket->tid != tid)
2236 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2237
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002238 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002239 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002240 if (!peer) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002241 lua_pushnil(L);
2242 return 1;
2243 }
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002244 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2245 si = appctx->owner;
2246 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002247
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002248 conn = cs_conn(objt_cs(s->si[1].end));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002249 if (!conn) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002250 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002251 lua_pushnil(L);
2252 return 1;
2253 }
2254
Willy Tarreaua71f6422016-11-16 17:00:14 +01002255 conn_get_to_addr(conn);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002256 if (!(conn->flags & CO_FL_ADDR_TO_SET)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002257 xref_unlock(&socket->xref, peer);
Willy Tarreaua71f6422016-11-16 17:00:14 +01002258 lua_pushnil(L);
2259 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002260 }
2261
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002262 ret = MAY_LJMP(hlua_socket_info(L, &conn->addr.to));
2263 xref_unlock(&socket->xref, peer);
2264 return ret;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002265}
2266
2267/* Returns information about my connection side. */
2268static int hlua_socket_getsockname(struct lua_State *L)
2269{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002270 struct hlua_socket *socket;
2271 struct connection *conn;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002272 struct appctx *appctx;
2273 struct xref *peer;
2274 struct stream_interface *si;
2275 struct stream *s;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002276 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002277
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002278 MAY_LJMP(check_args(L, 1, "getsockname"));
2279
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002280 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002281
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002282 /* Check if we run on the same thread than the xreator thread.
2283 * We cannot access to the socket if the thread is different.
2284 */
2285 if (socket->tid != tid)
2286 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2287
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002288 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002289 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002290 if (!peer) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002291 lua_pushnil(L);
2292 return 1;
2293 }
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002294 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2295 si = appctx->owner;
2296 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002297
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002298 conn = cs_conn(objt_cs(s->si[1].end));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002299 if (!conn) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002300 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002301 lua_pushnil(L);
2302 return 1;
2303 }
2304
Willy Tarreaua71f6422016-11-16 17:00:14 +01002305 conn_get_from_addr(conn);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002306 if (!(conn->flags & CO_FL_ADDR_FROM_SET)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002307 xref_unlock(&socket->xref, peer);
Willy Tarreaua71f6422016-11-16 17:00:14 +01002308 lua_pushnil(L);
2309 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002310 }
2311
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002312 ret = hlua_socket_info(L, &conn->addr.from);
2313 xref_unlock(&socket->xref, peer);
2314 return ret;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002315}
2316
2317/* This struct define the applet. */
Willy Tarreau30576452015-04-13 13:50:30 +02002318static struct applet update_applet = {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002319 .obj_type = OBJ_TYPE_APPLET,
2320 .name = "<LUA_TCP>",
2321 .fct = hlua_socket_handler,
2322 .release = hlua_socket_release,
2323};
2324
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002325__LJMP static int hlua_socket_connect_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002326{
2327 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
2328 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002329 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002330 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002331 struct stream_interface *si;
2332 struct stream *s;
2333
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002334 /* Check if we run on the same thread than the xreator thread.
2335 * We cannot access to the socket if the thread is different.
2336 */
2337 if (socket->tid != tid)
2338 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2339
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002340 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002341 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002342 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002343 lua_pushnil(L);
2344 lua_pushstring(L, "Can't connect");
2345 return 2;
2346 }
2347 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2348 si = appctx->owner;
2349 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002350
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002351 /* Check if we run on the same thread than the xreator thread.
2352 * We cannot access to the socket if the thread is different.
2353 */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002354 if (socket->tid != tid) {
2355 xref_unlock(&socket->xref, peer);
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002356 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002357 }
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002358
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002359 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002360 if (!hlua || channel_output_closed(&s->req)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002361 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002362 lua_pushnil(L);
2363 lua_pushstring(L, "Can't connect");
2364 return 2;
2365 }
2366
Willy Tarreaue09101e2018-10-16 17:37:12 +02002367 appctx = __objt_appctx(s->si[0].end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002368
2369 /* Check for connection established. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002370 if (appctx->ctx.hlua_cosocket.connected) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002371 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002372 lua_pushinteger(L, 1);
2373 return 1;
2374 }
2375
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002376 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2377 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002378 WILL_LJMP(luaL_error(L, "out of memory error"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002379 }
2380 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002381 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002382 return 0;
2383}
2384
2385/* This function fail or initite the connection. */
2386__LJMP static int hlua_socket_connect(struct lua_State *L)
2387{
2388 struct hlua_socket *socket;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002389 int port = -1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002390 const char *ip;
2391 struct connection *conn;
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002392 struct hlua *hlua;
2393 struct appctx *appctx;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002394 int low, high;
2395 struct sockaddr_storage *addr;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002396 struct xref *peer;
2397 struct stream_interface *si;
2398 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002399
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002400 if (lua_gettop(L) < 2)
2401 WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments"));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002402
2403 /* Get args. */
2404 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002405
2406 /* Check if we run on the same thread than the xreator thread.
2407 * We cannot access to the socket if the thread is different.
2408 */
2409 if (socket->tid != tid)
2410 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2411
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002412 ip = MAY_LJMP(luaL_checkstring(L, 2));
Tim Duesterhus6edab862018-01-06 19:04:45 +01002413 if (lua_gettop(L) >= 3) {
2414 luaL_Buffer b;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002415 port = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002416
Tim Duesterhus6edab862018-01-06 19:04:45 +01002417 /* Force the ip to end with a colon, to support IPv6 addresses
2418 * that are not enclosed within square brackets.
2419 */
2420 if (port > 0) {
2421 luaL_buffinit(L, &b);
2422 luaL_addstring(&b, ip);
2423 luaL_addchar(&b, ':');
2424 luaL_pushresult(&b);
2425 ip = lua_tolstring(L, lua_gettop(L), NULL);
2426 }
2427 }
2428
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002429 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002430 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002431 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002432 lua_pushnil(L);
2433 return 1;
2434 }
2435 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2436 si = appctx->owner;
2437 s = si_strm(si);
2438
2439 /* Initialise connection. */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002440 conn = cs_conn(si_alloc_cs(&s->si[1], NULL));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002441 if (!conn) {
2442 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002443 WILL_LJMP(luaL_error(L, "connect: internal error"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002444 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002445
Willy Tarreau3adac082015-09-26 17:51:09 +02002446 /* needed for the connection not to be closed */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002447 conn->target = s->target;
Willy Tarreau3adac082015-09-26 17:51:09 +02002448
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002449 /* Parse ip address. */
Willy Tarreau48ef4c92017-01-06 18:32:38 +01002450 addr = str2sa_range(ip, NULL, &low, &high, NULL, NULL, NULL, 0);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002451 if (!addr) {
2452 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002453 WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002454 }
2455 if (low != high) {
2456 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002457 WILL_LJMP(luaL_error(L, "connect: port ranges not supported : address '%s'", ip));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002458 }
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002459 memcpy(&conn->addr.to, addr, sizeof(struct sockaddr_storage));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002460
2461 /* Set port. */
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002462 if (low == 0) {
2463 if (conn->addr.to.ss_family == AF_INET) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002464 if (port == -1) {
2465 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002466 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002467 }
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002468 ((struct sockaddr_in *)&conn->addr.to)->sin_port = htons(port);
2469 } else if (conn->addr.to.ss_family == AF_INET6) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002470 if (port == -1) {
2471 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002472 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002473 }
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002474 ((struct sockaddr_in6 *)&conn->addr.to)->sin6_port = htons(port);
2475 }
2476 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002477
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002478 hlua = hlua_gethlua(L);
Willy Tarreaue09101e2018-10-16 17:37:12 +02002479 appctx = __objt_appctx(s->si[0].end);
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002480
2481 /* inform the stream that we want to be notified whenever the
2482 * connection completes.
2483 */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01002484 si_cant_get(&s->si[0]);
Willy Tarreau3367d412018-11-15 10:57:41 +01002485 si_rx_endp_more(&s->si[0]);
Thierry FOURNIER8c8fbbe2015-09-26 17:02:35 +02002486 appctx_wakeup(appctx);
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002487
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +02002488 hlua->flags |= HLUA_MUST_GC;
2489
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002490 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2491 xref_unlock(&socket->xref, peer);
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002492 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002493 }
2494 xref_unlock(&socket->xref, peer);
PiBa-NL706d5ee2018-05-05 23:51:42 +02002495
2496 task_wakeup(s->task, TASK_WOKEN_INIT);
2497 /* Return yield waiting for connection. */
2498
Willy Tarreau9635e032018-10-16 17:52:55 +02002499 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002500
2501 return 0;
2502}
2503
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002504#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002505__LJMP static int hlua_socket_connect_ssl(struct lua_State *L)
2506{
2507 struct hlua_socket *socket;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002508 struct xref *peer;
2509 struct appctx *appctx;
2510 struct stream_interface *si;
2511 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002512
2513 MAY_LJMP(check_args(L, 3, "connect_ssl"));
2514 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002515
2516 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002517 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002518 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002519 lua_pushnil(L);
2520 return 1;
2521 }
2522 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2523 si = appctx->owner;
2524 s = si_strm(si);
2525
2526 s->target = &socket_ssl.obj_type;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002527 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002528 return MAY_LJMP(hlua_socket_connect(L));
2529}
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002530#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002531
2532__LJMP static int hlua_socket_setoption(struct lua_State *L)
2533{
2534 return 0;
2535}
2536
2537__LJMP static int hlua_socket_settimeout(struct lua_State *L)
2538{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002539 struct hlua_socket *socket;
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002540 int tmout;
Mark Lakes56cc1252018-03-27 09:48:06 +02002541 double dtmout;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002542 struct xref *peer;
2543 struct appctx *appctx;
2544 struct stream_interface *si;
2545 struct stream *s;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002546
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002547 MAY_LJMP(check_args(L, 2, "settimeout"));
2548
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002549 socket = MAY_LJMP(hlua_checksocket(L, 1));
Mark Lakes56cc1252018-03-27 09:48:06 +02002550
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002551 /* convert the timeout to millis */
2552 dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002553
Thierry Fournier17a921b2018-03-08 09:59:02 +01002554 /* Check for negative values */
Mark Lakes56cc1252018-03-27 09:48:06 +02002555 if (dtmout < 0)
Thierry Fournier17a921b2018-03-08 09:59:02 +01002556 WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values"));
2557
Mark Lakes56cc1252018-03-27 09:48:06 +02002558 if (dtmout > INT_MAX) /* overflow check */
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002559 WILL_LJMP(luaL_error(L, "settimeout: cannot set values larger than %d ms", INT_MAX));
Mark Lakes56cc1252018-03-27 09:48:06 +02002560
2561 tmout = MS_TO_TICKS((int)dtmout);
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002562 if (tmout == 0)
2563 tmout++; /* very small timeouts are adjusted to a minium of 1ms */
Mark Lakes56cc1252018-03-27 09:48:06 +02002564
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002565 /* Check if we run on the same thread than the xreator thread.
2566 * We cannot access to the socket if the thread is different.
2567 */
2568 if (socket->tid != tid)
2569 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2570
Mark Lakes56cc1252018-03-27 09:48:06 +02002571 /* check for connection break. If some data were read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002572 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002573 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002574 hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts.");
2575 WILL_LJMP(lua_error(L));
2576 return 0;
2577 }
2578 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2579 si = appctx->owner;
2580 s = si_strm(si);
2581
Cyril Bonté7bb63452018-08-17 23:51:02 +02002582 s->sess->fe->timeout.connect = tmout;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002583 s->req.rto = tmout;
2584 s->req.wto = tmout;
2585 s->res.rto = tmout;
2586 s->res.wto = tmout;
Cyril Bonté7bb63452018-08-17 23:51:02 +02002587 s->req.rex = tick_add_ifset(now_ms, tmout);
2588 s->req.wex = tick_add_ifset(now_ms, tmout);
2589 s->res.rex = tick_add_ifset(now_ms, tmout);
2590 s->res.wex = tick_add_ifset(now_ms, tmout);
2591
2592 s->task->expire = tick_add_ifset(now_ms, tmout);
2593 task_queue(s->task);
2594
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002595 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002596
Thierry Fourniere9636f12018-03-08 09:54:32 +01002597 lua_pushinteger(L, 1);
Tim Duesterhus119a5f12018-01-06 19:16:25 +01002598 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002599}
2600
2601__LJMP static int hlua_socket_new(lua_State *L)
2602{
2603 struct hlua_socket *socket;
2604 struct appctx *appctx;
Willy Tarreau15b5e142015-04-04 14:38:25 +02002605 struct session *sess;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002606 struct stream *strm;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002607
2608 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002609 if (!lua_checkstack(L, 3)) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002610 hlua_pusherror(L, "socket: full stack");
2611 goto out_fail_conf;
2612 }
2613
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002614 /* Create the object: obj[0] = userdata. */
2615 lua_newtable(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002616 socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket)));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002617 lua_rawseti(L, -2, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002618 memset(socket, 0, sizeof(*socket));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002619 socket->tid = tid;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002620
Thierry FOURNIER4a6170c2015-03-09 17:07:10 +01002621 /* Check if the various memory pools are intialized. */
Willy Tarreaubafbe012017-11-24 17:34:44 +01002622 if (!pool_head_stream || !pool_head_buffer) {
Thierry FOURNIER4a6170c2015-03-09 17:07:10 +01002623 hlua_pusherror(L, "socket: uninitialized pools.");
2624 goto out_fail_conf;
2625 }
2626
Willy Tarreau87b09662015-04-03 00:22:06 +02002627 /* Pop a class stream metatable and affect it to the userdata. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002628 lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref);
2629 lua_setmetatable(L, -2);
2630
Willy Tarreaud420a972015-04-06 00:39:18 +02002631 /* Create the applet context */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01002632 appctx = appctx_new(&update_applet, tid_bit);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002633 if (!appctx) {
2634 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaufeb76402015-04-03 14:10:06 +02002635 goto out_fail_conf;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002636 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002637
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002638 appctx->ctx.hlua_cosocket.connected = 0;
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02002639 appctx->ctx.hlua_cosocket.die = 0;
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002640 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write);
2641 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read);
Willy Tarreaub2bf8332015-04-04 15:58:58 +02002642
Willy Tarreaud420a972015-04-06 00:39:18 +02002643 /* Now create a session, task and stream for this applet */
2644 sess = session_new(&socket_proxy, NULL, &appctx->obj_type);
2645 if (!sess) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002646 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002647 goto out_fail_sess;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002648 }
2649
Willy Tarreau87787ac2017-08-28 16:22:54 +02002650 strm = stream_new(sess, &appctx->obj_type);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002651 if (!strm) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002652 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002653 goto out_fail_stream;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002654 }
2655
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002656 /* Initialise cross reference between stream and Lua socket object. */
2657 xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002658
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002659 /* Configure "right" stream interface. this "si" is used to connect
2660 * and retrieve data from the server. The connection is initialized
2661 * with the "struct server".
2662 */
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002663 si_set_state(&strm->si[1], SI_ST_ASS);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002664
2665 /* Force destination server. */
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002666 strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET | SF_BE_ASSIGNED;
2667 strm->target = &socket_tcp.obj_type;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002668
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002669 return 1;
2670
Willy Tarreaud420a972015-04-06 00:39:18 +02002671 out_fail_stream:
Willy Tarreau11c36242015-04-04 15:54:03 +02002672 session_free(sess);
Willy Tarreaud420a972015-04-06 00:39:18 +02002673 out_fail_sess:
2674 appctx_free(appctx);
2675 out_fail_conf:
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002676 WILL_LJMP(lua_error(L));
2677 return 0;
2678}
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01002679
2680/*
2681 *
2682 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002683 * Class Channel
2684 *
2685 *
2686 */
2687
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002688/* The state between the channel data and the HTTP parser state can be
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002689 * inconsistent, so reset the parser and call it again. Warning, this
2690 * action does not revalidate the request and does not send a 400 if the modified
2691 * request is not valid.
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002692 *
Thierry FOURNIER6e01f382015-11-02 09:52:54 +01002693 * This function never fails. The direction is set using dir, which equals
2694 * either SMP_OPT_DIR_REQ or SMP_OPT_DIR_RES.
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002695 */
2696static void hlua_resynchonize_proto(struct stream *stream, int dir)
2697{
2698 /* Protocol HTTP. */
2699 if (stream->be->mode == PR_MODE_HTTP) {
2700
Thierry FOURNIER6e01f382015-11-02 09:52:54 +01002701 if (dir == SMP_OPT_DIR_REQ)
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002702 http_txn_reset_req(stream->txn);
Thierry FOURNIER6e01f382015-11-02 09:52:54 +01002703 else if (dir == SMP_OPT_DIR_RES)
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002704 http_txn_reset_res(stream->txn);
2705
2706 if (stream->txn->hdr_idx.v)
2707 hdr_idx_init(&stream->txn->hdr_idx);
2708
Thierry FOURNIER6e01f382015-11-02 09:52:54 +01002709 if (dir == SMP_OPT_DIR_REQ)
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002710 http_msg_analyzer(&stream->txn->req, &stream->txn->hdr_idx);
Thierry FOURNIER6e01f382015-11-02 09:52:54 +01002711 else if (dir == SMP_OPT_DIR_RES)
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002712 http_msg_analyzer(&stream->txn->rsp, &stream->txn->hdr_idx);
2713 }
2714}
2715
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002716/* This function is called before the Lua execution. It stores
2717 * the differents parsers state before executing some Lua code.
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002718 */
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002719static inline void consistency_set(struct stream *stream, int opt, struct hlua_consistency *c)
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002720{
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002721 c->mode = stream->be->mode;
2722 switch (c->mode) {
2723 case PR_MODE_HTTP:
2724 c->data.http.dir = opt & SMP_OPT_DIR;
2725 if (c->data.http.dir == SMP_OPT_DIR_REQ)
2726 c->data.http.state = stream->txn->req.msg_state;
2727 else
2728 c->data.http.state = stream->txn->rsp.msg_state;
2729 break;
2730 default:
2731 break;
2732 }
2733}
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002734
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002735/* This function is called after the Lua execution. it
2736 * returns true if the parser state is consistent, otherwise,
2737 * it return false.
2738 *
2739 * In HTTP mode, the parser state must be in the same state
2740 * or greater when we exit the function. Even if we do a
2741 * control yield. This prevent to break the HTTP message
2742 * from the Lua code.
2743 */
2744static inline int consistency_check(struct stream *stream, int opt, struct hlua_consistency *c)
2745{
2746 if (c->mode != stream->be->mode)
2747 return 0;
2748
2749 switch (c->mode) {
2750 case PR_MODE_HTTP:
2751 if (c->data.http.dir != (opt & SMP_OPT_DIR))
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002752 return 0;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002753 if (c->data.http.dir == SMP_OPT_DIR_REQ)
2754 return stream->txn->req.msg_state >= c->data.http.state;
2755 else
2756 return stream->txn->rsp.msg_state >= c->data.http.state;
2757 default:
2758 return 1;
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002759 }
2760 return 1;
2761}
2762
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002763/* Returns the struct hlua_channel join to the class channel in the
2764 * stack entry "ud" or throws an argument error.
2765 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002766__LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002767{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02002768 return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002769}
2770
Willy Tarreau47860ed2015-03-10 14:07:50 +01002771/* Pushes the channel onto the top of the stack. If the stask does not have a
2772 * free slots, the function fails and returns 0;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002773 */
Willy Tarreau2a71af42015-03-10 13:51:50 +01002774static int hlua_channel_new(lua_State *L, struct channel *channel)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002775{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002776 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002777 if (!lua_checkstack(L, 3))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002778 return 0;
2779
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002780 lua_newtable(L);
Willy Tarreau47860ed2015-03-10 14:07:50 +01002781 lua_pushlightuserdata(L, channel);
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002782 lua_rawseti(L, -2, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002783
2784 /* Pop a class sesison metatable and affect it to the userdata. */
2785 lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref);
2786 lua_setmetatable(L, -2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002787 return 1;
2788}
2789
2790/* Duplicate all the data present in the input channel and put it
2791 * in a string LUA variables. Returns -1 and push a nil value in
2792 * the stack if the channel is closed and all the data are consumed,
2793 * returns 0 if no data are available, otherwise it returns the length
2794 * of the builded string.
2795 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002796static inline int _hlua_channel_dup(struct channel *chn, lua_State *L)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002797{
2798 char *blk1;
2799 char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02002800 size_t len1;
2801 size_t len2;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002802 int ret;
2803 luaL_Buffer b;
2804
Willy Tarreau06d80a92017-10-19 14:32:15 +02002805 ret = ci_getblk_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002806 if (unlikely(ret == 0))
2807 return 0;
2808
2809 if (unlikely(ret < 0)) {
2810 lua_pushnil(L);
2811 return -1;
2812 }
2813
2814 luaL_buffinit(L, &b);
2815 luaL_addlstring(&b, blk1, len1);
2816 if (unlikely(ret == 2))
2817 luaL_addlstring(&b, blk2, len2);
2818 luaL_pushresult(&b);
2819
2820 if (unlikely(ret == 2))
2821 return len1 + len2;
2822 return len1;
2823}
2824
2825/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2826 * a yield. This function keep the data in the buffer.
2827 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002828__LJMP static int hlua_channel_dup_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002829{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002830 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002831
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002832 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2833
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002834 if (_hlua_channel_dup(chn, L) == 0)
Willy Tarreau9635e032018-10-16 17:52:55 +02002835 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_dup_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002836 return 1;
2837}
2838
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002839/* Check arguments for the function "hlua_channel_dup_yield". */
2840__LJMP static int hlua_channel_dup(lua_State *L)
2841{
2842 MAY_LJMP(check_args(L, 1, "dup"));
2843 MAY_LJMP(hlua_checkchannel(L, 1));
2844 return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0));
2845}
2846
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002847/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2848 * a yield. This function consumes the data in the buffer. It returns
2849 * a string containing the data or a nil pointer if no data are available
2850 * and the channel is closed.
2851 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002852__LJMP static int hlua_channel_get_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002853{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002854 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002855 int ret;
2856
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002857 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002858
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002859 ret = _hlua_channel_dup(chn, L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002860 if (unlikely(ret == 0))
Willy Tarreau9635e032018-10-16 17:52:55 +02002861 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002862
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002863 if (unlikely(ret == -1))
2864 return 1;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002865
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002866 b_sub(&chn->buf, ret);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002867 hlua_resynchonize_proto(chn_strm(chn), !!(chn->flags & CF_ISRESP));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002868 return 1;
2869}
2870
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002871/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002872__LJMP static int hlua_channel_get(lua_State *L)
2873{
2874 MAY_LJMP(check_args(L, 1, "get"));
2875 MAY_LJMP(hlua_checkchannel(L, 1));
2876 return MAY_LJMP(hlua_channel_get_yield(L, 0, 0));
2877}
2878
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002879/* This functions consumes and returns one line. If the channel is closed,
2880 * and the last data does not contains a final '\n', the data are returned
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002881 * without the final '\n'. When no more data are available, it returns nil
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002882 * value.
2883 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002884__LJMP static int hlua_channel_getline_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002885{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002886 char *blk1;
2887 char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02002888 size_t len1;
2889 size_t len2;
2890 size_t len;
Willy Tarreau47860ed2015-03-10 14:07:50 +01002891 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002892 int ret;
2893 luaL_Buffer b;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002894
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002895 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2896
Willy Tarreau06d80a92017-10-19 14:32:15 +02002897 ret = ci_getline_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002898 if (ret == 0)
Willy Tarreau9635e032018-10-16 17:52:55 +02002899 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_getline_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002900
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002901 if (ret == -1) {
2902 lua_pushnil(L);
2903 return 1;
2904 }
2905
2906 luaL_buffinit(L, &b);
2907 luaL_addlstring(&b, blk1, len1);
2908 len = len1;
2909 if (unlikely(ret == 2)) {
2910 luaL_addlstring(&b, blk2, len2);
2911 len += len2;
2912 }
2913 luaL_pushresult(&b);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002914 b_rep_blk(&chn->buf, ci_head(chn), ci_head(chn) + len, NULL, 0);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002915 hlua_resynchonize_proto(chn_strm(chn), !!(chn->flags & CF_ISRESP));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002916 return 1;
2917}
2918
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002919/* Check arguments for the function "hlua_channel_getline_yield". */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002920__LJMP static int hlua_channel_getline(lua_State *L)
2921{
2922 MAY_LJMP(check_args(L, 1, "getline"));
2923 MAY_LJMP(hlua_checkchannel(L, 1));
2924 return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0));
2925}
2926
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002927/* This function takes a string as input, and append it at the
2928 * input side of channel. If the data is too big, but a space
2929 * is probably available after sending some data, the function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002930 * yields. If the data is bigger than the buffer, or if the
2931 * channel is closed, it returns -1. Otherwise, it returns the
2932 * amount of data written.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002933 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002934__LJMP static int hlua_channel_append_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002935{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002936 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002937 size_t len;
2938 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
2939 int l = MAY_LJMP(luaL_checkinteger(L, 3));
2940 int ret;
2941 int max;
2942
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002943 /* Check if the buffer is available because HAProxy doesn't allocate
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002944 * the request buffer if its not required.
2945 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002946 if (chn->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +01002947 si_rx_buff_blk(chn_prod(chn));
Willy Tarreau9635e032018-10-16 17:52:55 +02002948 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002949 }
2950
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002951 max = channel_recv_limit(chn) - b_data(&chn->buf);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002952 if (max > len - l)
2953 max = len - l;
2954
Willy Tarreau06d80a92017-10-19 14:32:15 +02002955 ret = ci_putblk(chn, str + l, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002956 if (ret == -2 || ret == -3) {
2957 lua_pushinteger(L, -1);
2958 return 1;
2959 }
Willy Tarreaubc18da12015-03-13 14:00:47 +01002960 if (ret == -1) {
2961 chn->flags |= CF_WAKE_WRITE;
Willy Tarreau9635e032018-10-16 17:52:55 +02002962 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Willy Tarreaubc18da12015-03-13 14:00:47 +01002963 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002964 l += ret;
2965 lua_pop(L, 1);
2966 lua_pushinteger(L, l);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002967 hlua_resynchonize_proto(chn_strm(chn), !!(chn->flags & CF_ISRESP));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002968
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002969 max = channel_recv_limit(chn) - b_data(&chn->buf);
Willy Tarreaua79021a2018-06-15 18:07:57 +02002970 if (max == 0 && co_data(chn) == 0) {
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002971 /* There are no space available, and the output buffer is empty.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002972 * in this case, we cannot add more data, so we cannot yield,
2973 * we return the amount of copyied data.
2974 */
2975 return 1;
2976 }
2977 if (l < len)
Willy Tarreau9635e032018-10-16 17:52:55 +02002978 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002979 return 1;
2980}
2981
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002982/* Just a wrapper of "hlua_channel_append_yield". It returns the length
2983 * of the written string, or -1 if the channel is closed or if the
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002984 * buffer size is too little for the data.
2985 */
2986__LJMP static int hlua_channel_append(lua_State *L)
2987{
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002988 size_t len;
2989
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002990 MAY_LJMP(check_args(L, 2, "append"));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002991 MAY_LJMP(hlua_checkchannel(L, 1));
2992 MAY_LJMP(luaL_checklstring(L, 2, &len));
2993 MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002994 lua_pushinteger(L, 0);
2995
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002996 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002997}
2998
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002999/* Just a wrapper of "hlua_channel_append_yield". This wrapper starts
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003000 * his process by cleaning the buffer. The result is a replacement
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003001 * of the current data. It returns the length of the written string,
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003002 * or -1 if the channel is closed or if the buffer size is too
3003 * little for the data.
3004 */
3005__LJMP static int hlua_channel_set(lua_State *L)
3006{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003007 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003008
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003009 MAY_LJMP(check_args(L, 2, "set"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003010 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003011 lua_pushinteger(L, 0);
3012
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003013 b_set_data(&chn->buf, co_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003014
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003015 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003016}
3017
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003018/* Append data in the output side of the buffer. This data is immediately
3019 * sent. The function returns the amount of data written. If the buffer
3020 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003021 * if the channel is closed.
3022 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003023__LJMP static int hlua_channel_send_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003024{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003025 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003026 size_t len;
3027 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
3028 int l = MAY_LJMP(luaL_checkinteger(L, 3));
3029 int max;
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003030 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003031
Willy Tarreau47860ed2015-03-10 14:07:50 +01003032 if (unlikely(channel_output_closed(chn))) {
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003033 lua_pushinteger(L, -1);
3034 return 1;
3035 }
3036
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003037 /* Check if the buffer is available because HAProxy doesn't allocate
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01003038 * the request buffer if its not required.
3039 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003040 if (chn->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +01003041 si_rx_buff_blk(chn_prod(chn));
Willy Tarreau9635e032018-10-16 17:52:55 +02003042 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01003043 }
3044
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003045 /* The written data will be immediately sent, so we can check
3046 * the available space without taking in account the reserve.
3047 * The reserve is guaranteed for the processing of incoming
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003048 * data, because the buffer will be flushed.
3049 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003050 max = b_room(&chn->buf);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003051
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003052 /* If there is no space available, and the output buffer is empty.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003053 * in this case, we cannot add more data, so we cannot yield,
3054 * we return the amount of copyied data.
3055 */
Willy Tarreaua79021a2018-06-15 18:07:57 +02003056 if (max == 0 && co_data(chn) == 0)
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003057 return 1;
3058
3059 /* Adjust the real required length. */
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003060 if (max > len - l)
3061 max = len - l;
3062
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003063 /* The buffer available size may be not contiguous. This test
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003064 * detects a non contiguous buffer and realign it.
3065 */
Willy Tarreau3f679992018-06-15 15:06:42 +02003066 if (ci_space_for_replace(chn) < max)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003067 channel_slow_realign(chn, trash.area);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003068
3069 /* Copy input data in the buffer. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003070 max = b_rep_blk(&chn->buf, ci_head(chn), ci_head(chn), str + l, max);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003071
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003072 /* buffer replace considers that the input part is filled.
3073 * so, I must forward these new data in the output part.
3074 */
Willy Tarreaubcbd3932018-06-06 07:13:22 +02003075 c_adv(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003076
3077 l += max;
3078 lua_pop(L, 1);
3079 lua_pushinteger(L, l);
3080
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003081 /* If there is no space available, and the output buffer is empty.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003082 * in this case, we cannot add more data, so we cannot yield,
3083 * we return the amount of copyied data.
3084 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003085 max = b_room(&chn->buf);
Willy Tarreaua79021a2018-06-15 18:07:57 +02003086 if (max == 0 && co_data(chn) == 0)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003087 return 1;
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003088
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003089 if (l < len) {
3090 /* If we are waiting for space in the response buffer, we
3091 * must set the flag WAKERESWR. This flag required the task
3092 * wake up if any activity is detected on the response buffer.
3093 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003094 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003095 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01003096 else
3097 HLUA_SET_WAKEREQWR(hlua);
Willy Tarreau9635e032018-10-16 17:52:55 +02003098 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003099 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003100
3101 return 1;
3102}
3103
3104/* Just a wraper of "_hlua_channel_send". This wrapper permits
3105 * yield the LUA process, and resume it without checking the
3106 * input arguments.
3107 */
3108__LJMP static int hlua_channel_send(lua_State *L)
3109{
3110 MAY_LJMP(check_args(L, 2, "send"));
3111 lua_pushinteger(L, 0);
3112
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003113 return MAY_LJMP(hlua_channel_send_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003114}
3115
3116/* This function forward and amount of butes. The data pass from
3117 * the input side of the buffer to the output side, and can be
3118 * forwarded. This function never fails.
3119 *
3120 * The Lua function takes an amount of bytes to be forwarded in
3121 * imput. It returns the number of bytes forwarded.
3122 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003123__LJMP static int hlua_channel_forward_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003124{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003125 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003126 int len;
3127 int l;
3128 int max;
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003129 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003130
3131 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3132 len = MAY_LJMP(luaL_checkinteger(L, 2));
3133 l = MAY_LJMP(luaL_checkinteger(L, -1));
3134
3135 max = len - l;
Willy Tarreaua79021a2018-06-15 18:07:57 +02003136 if (max > ci_data(chn))
3137 max = ci_data(chn);
Willy Tarreau47860ed2015-03-10 14:07:50 +01003138 channel_forward(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003139 l += max;
3140
3141 lua_pop(L, 1);
3142 lua_pushinteger(L, l);
3143
3144 /* Check if it miss bytes to forward. */
3145 if (l < len) {
3146 /* The the input channel or the output channel are closed, we
3147 * must return the amount of data forwarded.
3148 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003149 if (channel_input_closed(chn) || channel_output_closed(chn))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003150 return 1;
3151
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003152 /* If we are waiting for space data in the response buffer, we
3153 * must set the flag WAKERESWR. This flag required the task
3154 * wake up if any activity is detected on the response buffer.
3155 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003156 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003157 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01003158 else
3159 HLUA_SET_WAKEREQWR(hlua);
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003160
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003161 /* Otherwise, we can yield waiting for new data in the inpout side. */
Willy Tarreau9635e032018-10-16 17:52:55 +02003162 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_forward_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003163 }
3164
3165 return 1;
3166}
3167
3168/* Just check the input and prepare the stack for the previous
3169 * function "hlua_channel_forward_yield"
3170 */
3171__LJMP static int hlua_channel_forward(lua_State *L)
3172{
3173 MAY_LJMP(check_args(L, 2, "forward"));
3174 MAY_LJMP(hlua_checkchannel(L, 1));
3175 MAY_LJMP(luaL_checkinteger(L, 2));
3176
3177 lua_pushinteger(L, 0);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003178 return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003179}
3180
3181/* Just returns the number of bytes available in the input
3182 * side of the buffer. This function never fails.
3183 */
3184__LJMP static int hlua_channel_get_in_len(lua_State *L)
3185{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003186 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003187
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003188 MAY_LJMP(check_args(L, 1, "get_in_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003189 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Willy Tarreaua79021a2018-06-15 18:07:57 +02003190 lua_pushinteger(L, ci_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003191 return 1;
3192}
3193
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003194/* Returns true if the channel is full. */
3195__LJMP static int hlua_channel_is_full(lua_State *L)
3196{
3197 struct channel *chn;
3198 int rem;
3199
3200 MAY_LJMP(check_args(L, 1, "is_full"));
3201 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3202
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003203 rem = b_room(&chn->buf);
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003204 rem -= global.tune.maxrewrite; /* Rewrite reserved size */
3205
3206 lua_pushboolean(L, rem <= 0);
3207 return 1;
3208}
3209
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003210/* Just returns the number of bytes available in the output
3211 * side of the buffer. This function never fails.
3212 */
3213__LJMP static int hlua_channel_get_out_len(lua_State *L)
3214{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003215 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003216
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003217 MAY_LJMP(check_args(L, 1, "get_out_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003218 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Willy Tarreaua79021a2018-06-15 18:07:57 +02003219 lua_pushinteger(L, co_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003220 return 1;
3221}
3222
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003223/*
3224 *
3225 *
3226 * Class Fetches
3227 *
3228 *
3229 */
3230
3231/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003232 * a class stream, otherwise it throws an error.
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003233 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003234__LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003235{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003236 return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003237}
3238
3239/* This function creates and push in the stack a fetch object according
3240 * with a current TXN.
3241 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003242static int hlua_fetches_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003243{
Willy Tarreau7073c472015-04-06 11:15:40 +02003244 struct hlua_smp *hsmp;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003245
3246 /* Check stack size. */
3247 if (!lua_checkstack(L, 3))
3248 return 0;
3249
3250 /* Create the object: obj[0] = userdata.
3251 * Note that the base of the Fetches object is the
3252 * transaction object.
3253 */
3254 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003255 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003256 lua_rawseti(L, -2, 0);
3257
Willy Tarreau7073c472015-04-06 11:15:40 +02003258 hsmp->s = txn->s;
3259 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003260 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003261 hsmp->flags = flags;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003262
3263 /* Pop a class sesison metatable and affect it to the userdata. */
3264 lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref);
3265 lua_setmetatable(L, -2);
3266
3267 return 1;
3268}
3269
3270/* This function is an LUA binding. It is called with each sample-fetch.
3271 * It uses closure argument to store the associated sample-fetch. It
3272 * returns only one argument or throws an error. An error is thrown
3273 * only if an error is encountered during the argument parsing. If
3274 * the "sample-fetch" function fails, nil is returned.
3275 */
3276__LJMP static int hlua_run_sample_fetch(lua_State *L)
3277{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003278 struct hlua_smp *hsmp;
Willy Tarreau2ec22742015-03-10 14:27:20 +01003279 struct sample_fetch *f;
Frédéric Lécaillef874a832018-06-15 13:56:04 +02003280 struct arg args[ARGM_NBARGS + 1] = {{0}};
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003281 int i;
3282 struct sample smp;
3283
3284 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003285 f = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003286
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003287 /* Get traditional arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003288 hsmp = MAY_LJMP(hlua_checkfetches(L, 1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003289
Thierry FOURNIERca988662015-12-20 18:43:03 +01003290 /* Check execution authorization. */
3291 if (f->use & SMP_USE_HTTP_ANY &&
3292 !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) {
3293 lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which "
3294 "is not available in Lua services", f->kw);
3295 WILL_LJMP(lua_error(L));
3296 }
3297
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003298 /* Get extra arguments. */
3299 for (i = 0; i < lua_gettop(L) - 1; i++) {
3300 if (i >= ARGM_NBARGS)
3301 break;
3302 hlua_lua2arg(L, i + 2, &args[i]);
3303 }
3304 args[i].type = ARGT_STOP;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003305 args[i].data.str.area = NULL;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003306
3307 /* Check arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003308 MAY_LJMP(hlua_lua2arg_check(L, 2, args, f->arg_mask, hsmp->p));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003309
3310 /* Run the special args checker. */
Willy Tarreau2ec22742015-03-10 14:27:20 +01003311 if (f->val_args && !f->val_args(args, NULL)) {
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003312 lua_pushfstring(L, "error in arguments");
3313 WILL_LJMP(lua_error(L));
3314 }
3315
3316 /* Initialise the sample. */
3317 memset(&smp, 0, sizeof(smp));
3318
3319 /* Run the sample fetch process. */
Willy Tarreau1777ea62016-03-10 16:15:46 +01003320 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003321 if (!f->process(args, &smp, f->kw, f->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003322 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003323 lua_pushstring(L, "");
3324 else
3325 lua_pushnil(L);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003326 return 1;
3327 }
3328
3329 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003330 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003331 hlua_smp2lua_str(L, &smp);
3332 else
3333 hlua_smp2lua(L, &smp);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003334 return 1;
3335}
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003336
3337/*
3338 *
3339 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003340 * Class Converters
3341 *
3342 *
3343 */
3344
3345/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003346 * a class stream, otherwise it throws an error.
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003347 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003348__LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003349{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003350 return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003351}
3352
3353/* This function creates and push in the stack a Converters object
3354 * according with a current TXN.
3355 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003356static int hlua_converters_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003357{
Willy Tarreau7073c472015-04-06 11:15:40 +02003358 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003359
3360 /* Check stack size. */
3361 if (!lua_checkstack(L, 3))
3362 return 0;
3363
3364 /* Create the object: obj[0] = userdata.
3365 * Note that the base of the Converters object is the
3366 * same than the TXN object.
3367 */
3368 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003369 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003370 lua_rawseti(L, -2, 0);
3371
Willy Tarreau7073c472015-04-06 11:15:40 +02003372 hsmp->s = txn->s;
3373 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003374 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003375 hsmp->flags = flags;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003376
Willy Tarreau87b09662015-04-03 00:22:06 +02003377 /* Pop a class stream metatable and affect it to the table. */
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003378 lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref);
3379 lua_setmetatable(L, -2);
3380
3381 return 1;
3382}
3383
3384/* This function is an LUA binding. It is called with each converter.
3385 * It uses closure argument to store the associated converter. It
3386 * returns only one argument or throws an error. An error is thrown
3387 * only if an error is encountered during the argument parsing. If
3388 * the converter function function fails, nil is returned.
3389 */
3390__LJMP static int hlua_run_sample_conv(lua_State *L)
3391{
Willy Tarreauda5f1082015-04-06 11:17:13 +02003392 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003393 struct sample_conv *conv;
Frédéric Lécaillef874a832018-06-15 13:56:04 +02003394 struct arg args[ARGM_NBARGS + 1] = {{0}};
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003395 int i;
3396 struct sample smp;
3397
3398 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003399 conv = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003400
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003401 /* Get traditional arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003402 hsmp = MAY_LJMP(hlua_checkconverters(L, 1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003403
3404 /* Get extra arguments. */
3405 for (i = 0; i < lua_gettop(L) - 2; i++) {
3406 if (i >= ARGM_NBARGS)
3407 break;
3408 hlua_lua2arg(L, i + 3, &args[i]);
3409 }
3410 args[i].type = ARGT_STOP;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003411 args[i].data.str.area = NULL;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003412
3413 /* Check arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003414 MAY_LJMP(hlua_lua2arg_check(L, 3, args, conv->arg_mask, hsmp->p));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003415
3416 /* Run the special args checker. */
3417 if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) {
3418 hlua_pusherror(L, "error in arguments");
3419 WILL_LJMP(lua_error(L));
3420 }
3421
3422 /* Initialise the sample. */
3423 if (!hlua_lua2smp(L, 2, &smp)) {
3424 hlua_pusherror(L, "error in the input argument");
3425 WILL_LJMP(lua_error(L));
3426 }
3427
Willy Tarreau1777ea62016-03-10 16:15:46 +01003428 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
3429
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003430 /* Apply expected cast. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003431 if (!sample_casts[smp.data.type][conv->in_type]) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003432 hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'",
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003433 smp_to_type[smp.data.type], smp_to_type[conv->in_type]);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003434 WILL_LJMP(lua_error(L));
3435 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003436 if (sample_casts[smp.data.type][conv->in_type] != c_none &&
3437 !sample_casts[smp.data.type][conv->in_type](&smp)) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003438 hlua_pusherror(L, "error during the input argument casting");
3439 WILL_LJMP(lua_error(L));
3440 }
3441
3442 /* Run the sample conversion process. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003443 if (!conv->process(args, &smp, conv->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003444 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003445 lua_pushstring(L, "");
3446 else
Willy Tarreaua678b432015-08-28 10:14:59 +02003447 lua_pushnil(L);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003448 return 1;
3449 }
3450
3451 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003452 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003453 hlua_smp2lua_str(L, &smp);
3454 else
3455 hlua_smp2lua(L, &smp);
Willy Tarreaua678b432015-08-28 10:14:59 +02003456 return 1;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003457}
3458
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003459/*
3460 *
3461 *
3462 * Class AppletTCP
3463 *
3464 *
3465 */
3466
3467/* Returns a struct hlua_txn if the stack entry "ud" is
3468 * a class stream, otherwise it throws an error.
3469 */
3470__LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud)
3471{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003472 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003473}
3474
3475/* This function creates and push in the stack an Applet object
3476 * according with a current TXN.
3477 */
3478static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx)
3479{
3480 struct hlua_appctx *appctx;
3481 struct stream_interface *si = ctx->owner;
3482 struct stream *s = si_strm(si);
3483 struct proxy *p = s->be;
3484
3485 /* Check stack size. */
3486 if (!lua_checkstack(L, 3))
3487 return 0;
3488
3489 /* Create the object: obj[0] = userdata.
3490 * Note that the base of the Converters object is the
3491 * same than the TXN object.
3492 */
3493 lua_newtable(L);
3494 appctx = lua_newuserdata(L, sizeof(*appctx));
3495 lua_rawseti(L, -2, 0);
3496 appctx->appctx = ctx;
3497 appctx->htxn.s = s;
3498 appctx->htxn.p = p;
3499
3500 /* Create the "f" field that contains a list of fetches. */
3501 lua_pushstring(L, "f");
3502 if (!hlua_fetches_new(L, &appctx->htxn, 0))
3503 return 0;
3504 lua_settable(L, -3);
3505
3506 /* Create the "sf" field that contains a list of stringsafe fetches. */
3507 lua_pushstring(L, "sf");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003508 if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003509 return 0;
3510 lua_settable(L, -3);
3511
3512 /* Create the "c" field that contains a list of converters. */
3513 lua_pushstring(L, "c");
3514 if (!hlua_converters_new(L, &appctx->htxn, 0))
3515 return 0;
3516 lua_settable(L, -3);
3517
3518 /* Create the "sc" field that contains a list of stringsafe converters. */
3519 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003520 if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003521 return 0;
3522 lua_settable(L, -3);
3523
3524 /* Pop a class stream metatable and affect it to the table. */
3525 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref);
3526 lua_setmetatable(L, -2);
3527
3528 return 1;
3529}
3530
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003531__LJMP static int hlua_applet_tcp_set_var(lua_State *L)
3532{
3533 struct hlua_appctx *appctx;
3534 struct stream *s;
3535 const char *name;
3536 size_t len;
3537 struct sample smp;
3538
3539 MAY_LJMP(check_args(L, 3, "set_var"));
3540
3541 /* It is useles to retrieve the stream, but this function
3542 * runs only in a stream context.
3543 */
3544 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3545 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3546 s = appctx->htxn.s;
3547
3548 /* Converts the third argument in a sample. */
3549 hlua_lua2smp(L, 3, &smp);
3550
3551 /* Store the sample in a variable. */
3552 smp_set_owner(&smp, s->be, s->sess, s, 0);
3553 vars_set_by_name(name, len, &smp);
3554 return 0;
3555}
3556
3557__LJMP static int hlua_applet_tcp_unset_var(lua_State *L)
3558{
3559 struct hlua_appctx *appctx;
3560 struct stream *s;
3561 const char *name;
3562 size_t len;
3563 struct sample smp;
3564
3565 MAY_LJMP(check_args(L, 2, "unset_var"));
3566
3567 /* It is useles to retrieve the stream, but this function
3568 * runs only in a stream context.
3569 */
3570 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3571 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3572 s = appctx->htxn.s;
3573
3574 /* Unset the variable. */
3575 smp_set_owner(&smp, s->be, s->sess, s, 0);
3576 vars_unset_by_name(name, len, &smp);
3577 return 0;
3578}
3579
3580__LJMP static int hlua_applet_tcp_get_var(lua_State *L)
3581{
3582 struct hlua_appctx *appctx;
3583 struct stream *s;
3584 const char *name;
3585 size_t len;
3586 struct sample smp;
3587
3588 MAY_LJMP(check_args(L, 2, "get_var"));
3589
3590 /* It is useles to retrieve the stream, but this function
3591 * runs only in a stream context.
3592 */
3593 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3594 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3595 s = appctx->htxn.s;
3596
3597 smp_set_owner(&smp, s->be, s->sess, s, 0);
3598 if (!vars_get_by_name(name, len, &smp)) {
3599 lua_pushnil(L);
3600 return 1;
3601 }
3602
3603 return hlua_smp2lua(L, &smp);
3604}
3605
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003606__LJMP static int hlua_applet_tcp_set_priv(lua_State *L)
3607{
3608 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3609 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003610 struct hlua *hlua;
3611
3612 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003613 if (!s->hlua)
3614 return 0;
3615 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003616
3617 MAY_LJMP(check_args(L, 2, "set_priv"));
3618
3619 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02003620 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003621
3622 /* Get and store new value. */
3623 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
3624 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
3625
3626 return 0;
3627}
3628
3629__LJMP static int hlua_applet_tcp_get_priv(lua_State *L)
3630{
3631 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3632 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003633 struct hlua *hlua;
3634
3635 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003636 if (!s->hlua) {
3637 lua_pushnil(L);
3638 return 1;
3639 }
3640 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003641
3642 /* Push configuration index in the stack. */
3643 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
3644
3645 return 1;
3646}
3647
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003648/* If expected data not yet available, it returns a yield. This function
3649 * consumes the data in the buffer. It returns a string containing the
3650 * data. This string can be empty.
3651 */
3652__LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx)
3653{
3654 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3655 struct stream_interface *si = appctx->appctx->owner;
3656 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02003657 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003658 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02003659 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003660 size_t len2;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003661
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003662 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003663 ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003664
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003665 /* Data not yet available. return yield. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003666 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003667 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003668 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_getline_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003669 }
3670
3671 /* End of data: commit the total strings and return. */
3672 if (ret < 0) {
3673 luaL_pushresult(&appctx->b);
3674 return 1;
3675 }
3676
3677 /* Ensure that the block 2 length is usable. */
3678 if (ret == 1)
3679 len2 = 0;
3680
3681 /* dont check the max length read and dont check. */
3682 luaL_addlstring(&appctx->b, blk1, len1);
3683 luaL_addlstring(&appctx->b, blk2, len2);
3684
3685 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003686 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003687 luaL_pushresult(&appctx->b);
3688 return 1;
3689}
3690
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003691/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003692__LJMP static int hlua_applet_tcp_getline(lua_State *L)
3693{
3694 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3695
3696 /* Initialise the string catenation. */
3697 luaL_buffinit(L, &appctx->b);
3698
3699 return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0));
3700}
3701
3702/* If expected data not yet available, it returns a yield. This function
3703 * consumes the data in the buffer. It returns a string containing the
3704 * data. This string can be empty.
3705 */
3706__LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx)
3707{
3708 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3709 struct stream_interface *si = appctx->appctx->owner;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003710 size_t len = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003711 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02003712 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003713 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02003714 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003715 size_t len2;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003716
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003717 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003718 ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003719
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003720 /* Data not yet available. return yield. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003721 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003722 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003723 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003724 }
3725
3726 /* End of data: commit the total strings and return. */
3727 if (ret < 0) {
3728 luaL_pushresult(&appctx->b);
3729 return 1;
3730 }
3731
3732 /* Ensure that the block 2 length is usable. */
3733 if (ret == 1)
3734 len2 = 0;
3735
3736 if (len == -1) {
3737
3738 /* If len == -1, catenate all the data avalaile and
3739 * yield because we want to get all the data until
3740 * the end of data stream.
3741 */
3742 luaL_addlstring(&appctx->b, blk1, len1);
3743 luaL_addlstring(&appctx->b, blk2, len2);
Willy Tarreau06d80a92017-10-19 14:32:15 +02003744 co_skip(si_oc(si), len1 + len2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003745 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003746 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003747
3748 } else {
3749
3750 /* Copy the fisrt block caping to the length required. */
3751 if (len1 > len)
3752 len1 = len;
3753 luaL_addlstring(&appctx->b, blk1, len1);
3754 len -= len1;
3755
3756 /* Copy the second block. */
3757 if (len2 > len)
3758 len2 = len;
3759 luaL_addlstring(&appctx->b, blk2, len2);
3760 len -= len2;
3761
3762 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003763 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003764
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003765 /* If there is no other data available, yield waiting for new data. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003766 if (len > 0) {
3767 lua_pushinteger(L, len);
3768 lua_replace(L, 2);
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
3773 /* return the result. */
3774 luaL_pushresult(&appctx->b);
3775 return 1;
3776 }
3777
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003778 /* we never execute this */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003779 hlua_pusherror(L, "Lua: internal error");
3780 WILL_LJMP(lua_error(L));
3781 return 0;
3782}
3783
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003784/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003785__LJMP static int hlua_applet_tcp_recv(lua_State *L)
3786{
3787 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3788 int len = -1;
3789
3790 if (lua_gettop(L) > 2)
3791 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
3792 if (lua_gettop(L) >= 2) {
3793 len = MAY_LJMP(luaL_checkinteger(L, 2));
3794 lua_pop(L, 1);
3795 }
3796
3797 /* Confirm or set the required length */
3798 lua_pushinteger(L, len);
3799
3800 /* Initialise the string catenation. */
3801 luaL_buffinit(L, &appctx->b);
3802
3803 return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0));
3804}
3805
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003806/* Append data in the output side of the buffer. This data is immediately
3807 * sent. The function returns the amount of data written. If the buffer
3808 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003809 * if the channel is closed.
3810 */
3811__LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx)
3812{
3813 size_t len;
3814 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3815 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
3816 int l = MAY_LJMP(luaL_checkinteger(L, 3));
3817 struct stream_interface *si = appctx->appctx->owner;
3818 struct channel *chn = si_ic(si);
3819 int max;
3820
3821 /* Get the max amount of data which can write as input in the channel. */
3822 max = channel_recv_max(chn);
3823 if (max > (len - l))
3824 max = len - l;
3825
3826 /* Copy data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003827 ci_putblk(chn, str + l, max);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003828
3829 /* update counters. */
3830 l += max;
3831 lua_pop(L, 1);
3832 lua_pushinteger(L, l);
3833
3834 /* If some data is not send, declares the situation to the
3835 * applet, and returns a yield.
3836 */
3837 if (l < len) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003838 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003839 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003840 }
3841
3842 return 1;
3843}
3844
3845/* Just a wraper of "hlua_applet_tcp_send_yield". This wrapper permits
3846 * yield the LUA process, and resume it without checking the
3847 * input arguments.
3848 */
3849__LJMP static int hlua_applet_tcp_send(lua_State *L)
3850{
3851 MAY_LJMP(check_args(L, 2, "send"));
3852 lua_pushinteger(L, 0);
3853
3854 return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0));
3855}
3856
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003857/*
3858 *
3859 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003860 * Class AppletHTTP
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003861 *
3862 *
3863 */
3864
3865/* Returns a struct hlua_txn if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003866 * a class stream, otherwise it throws an error.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003867 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003868__LJMP static struct hlua_appctx *hlua_checkapplet_http(lua_State *L, int ud)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003869{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003870 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003871}
3872
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003873/* This function creates and push in the stack an Applet object
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003874 * according with a current TXN.
3875 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003876static int hlua_applet_http_new(lua_State *L, struct appctx *ctx)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003877{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003878 struct hlua_appctx *appctx;
Thierry FOURNIER841475e2015-12-11 17:10:09 +01003879 struct hlua_txn htxn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003880 struct stream_interface *si = ctx->owner;
3881 struct stream *s = si_strm(si);
3882 struct proxy *px = s->be;
3883 struct http_txn *txn = s->txn;
3884 const char *path;
3885 const char *end;
3886 const char *p;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003887
3888 /* Check stack size. */
3889 if (!lua_checkstack(L, 3))
3890 return 0;
3891
3892 /* Create the object: obj[0] = userdata.
3893 * Note that the base of the Converters object is the
3894 * same than the TXN object.
3895 */
3896 lua_newtable(L);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003897 appctx = lua_newuserdata(L, sizeof(*appctx));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003898 lua_rawseti(L, -2, 0);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003899 appctx->appctx = ctx;
3900 appctx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */
Robin H. Johnson52f5db22017-01-01 13:10:52 -08003901 appctx->appctx->ctx.hlua_apphttp.reason = NULL; /* Use default reason based on status */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003902 appctx->htxn.s = s;
3903 appctx->htxn.p = px;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003904
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003905 /* Create the "f" field that contains a list of fetches. */
3906 lua_pushstring(L, "f");
3907 if (!hlua_fetches_new(L, &appctx->htxn, 0))
3908 return 0;
3909 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003910
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003911 /* Create the "sf" field that contains a list of stringsafe fetches. */
3912 lua_pushstring(L, "sf");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003913 if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003914 return 0;
3915 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003916
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003917 /* Create the "c" field that contains a list of converters. */
3918 lua_pushstring(L, "c");
3919 if (!hlua_converters_new(L, &appctx->htxn, 0))
3920 return 0;
3921 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003922
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003923 /* Create the "sc" field that contains a list of stringsafe converters. */
3924 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003925 if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003926 return 0;
3927 lua_settable(L, -3);
Willy Tarreaueee5b512015-04-03 23:46:31 +02003928
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003929 /* Stores the request method. */
3930 lua_pushstring(L, "method");
Willy Tarreaua79021a2018-06-15 18:07:57 +02003931 lua_pushlstring(L, ci_head(txn->req.chn), txn->req.sl.rq.m_l);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003932 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003933
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003934 /* Stores the http version. */
3935 lua_pushstring(L, "version");
Willy Tarreaua79021a2018-06-15 18:07:57 +02003936 lua_pushlstring(L, ci_head(txn->req.chn) + txn->req.sl.rq.v, txn->req.sl.rq.v_l);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003937 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003938
Thierry FOURNIER841475e2015-12-11 17:10:09 +01003939 /* creates an array of headers. hlua_http_get_headers() crates and push
3940 * the array on the top of the stack.
3941 */
3942 lua_pushstring(L, "headers");
3943 htxn.s = s;
3944 htxn.p = px;
3945 htxn.dir = SMP_OPT_DIR_REQ;
3946 if (!hlua_http_get_headers(L, &htxn, &htxn.s->txn->req))
3947 return 0;
3948 lua_settable(L, -3);
3949
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003950 /* Get path and qs */
Willy Tarreau6b952c82018-09-10 17:45:34 +02003951 path = http_txn_get_path(txn);
Thierry FOURNIER7d388632017-02-22 02:06:16 +01003952 if (path) {
Willy Tarreaua79021a2018-06-15 18:07:57 +02003953 end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Thierry FOURNIER7d388632017-02-22 02:06:16 +01003954 p = path;
3955 while (p < end && *p != '?')
3956 p++;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003957
Thierry FOURNIER7d388632017-02-22 02:06:16 +01003958 /* Stores the request path. */
3959 lua_pushstring(L, "path");
3960 lua_pushlstring(L, path, p - path);
3961 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003962
Thierry FOURNIER7d388632017-02-22 02:06:16 +01003963 /* Stores the query string. */
3964 lua_pushstring(L, "qs");
3965 if (*p == '?')
3966 p++;
3967 lua_pushlstring(L, p, end - p);
3968 lua_settable(L, -3);
3969 }
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003970
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003971 /* Stores the request path. */
3972 lua_pushstring(L, "length");
3973 lua_pushinteger(L, txn->req.body_len);
3974 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003975
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003976 /* Create an array of HTTP request headers. */
3977 lua_pushstring(L, "headers");
3978 MAY_LJMP(hlua_http_get_headers(L, &appctx->htxn, &appctx->htxn.s->txn->req));
3979 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003980
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003981 /* Create an empty array of HTTP request headers. */
3982 lua_pushstring(L, "response");
3983 lua_newtable(L);
3984 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003985
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003986 /* Pop a class stream metatable and affect it to the table. */
3987 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref);
3988 lua_setmetatable(L, -2);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003989
3990 return 1;
3991}
3992
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003993__LJMP static int hlua_applet_http_set_var(lua_State *L)
3994{
3995 struct hlua_appctx *appctx;
3996 struct stream *s;
3997 const char *name;
3998 size_t len;
3999 struct sample smp;
4000
4001 MAY_LJMP(check_args(L, 3, "set_var"));
4002
4003 /* It is useles to retrieve the stream, but this function
4004 * runs only in a stream context.
4005 */
4006 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4007 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4008 s = appctx->htxn.s;
4009
4010 /* Converts the third argument in a sample. */
4011 hlua_lua2smp(L, 3, &smp);
4012
4013 /* Store the sample in a variable. */
4014 smp_set_owner(&smp, s->be, s->sess, s, 0);
4015 vars_set_by_name(name, len, &smp);
4016 return 0;
4017}
4018
4019__LJMP static int hlua_applet_http_unset_var(lua_State *L)
4020{
4021 struct hlua_appctx *appctx;
4022 struct stream *s;
4023 const char *name;
4024 size_t len;
4025 struct sample smp;
4026
4027 MAY_LJMP(check_args(L, 2, "unset_var"));
4028
4029 /* It is useles to retrieve the stream, but this function
4030 * runs only in a stream context.
4031 */
4032 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4033 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4034 s = appctx->htxn.s;
4035
4036 /* Unset the variable. */
4037 smp_set_owner(&smp, s->be, s->sess, s, 0);
4038 vars_unset_by_name(name, len, &smp);
4039 return 0;
4040}
4041
4042__LJMP static int hlua_applet_http_get_var(lua_State *L)
4043{
4044 struct hlua_appctx *appctx;
4045 struct stream *s;
4046 const char *name;
4047 size_t len;
4048 struct sample smp;
4049
4050 MAY_LJMP(check_args(L, 2, "get_var"));
4051
4052 /* It is useles to retrieve the stream, but this function
4053 * runs only in a stream context.
4054 */
4055 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4056 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4057 s = appctx->htxn.s;
4058
4059 smp_set_owner(&smp, s->be, s->sess, s, 0);
4060 if (!vars_get_by_name(name, len, &smp)) {
4061 lua_pushnil(L);
4062 return 1;
4063 }
4064
4065 return hlua_smp2lua(L, &smp);
4066}
4067
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004068__LJMP static int hlua_applet_http_set_priv(lua_State *L)
4069{
4070 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4071 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004072 struct hlua *hlua;
4073
4074 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004075 if (!s->hlua)
4076 return 0;
4077 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004078
4079 MAY_LJMP(check_args(L, 2, "set_priv"));
4080
4081 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02004082 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004083
4084 /* Get and store new value. */
4085 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
4086 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
4087
4088 return 0;
4089}
4090
4091__LJMP static int hlua_applet_http_get_priv(lua_State *L)
4092{
4093 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4094 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004095 struct hlua *hlua;
4096
4097 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004098 if (!s->hlua) {
4099 lua_pushnil(L);
4100 return 1;
4101 }
4102 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004103
4104 /* Push configuration index in the stack. */
4105 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
4106
4107 return 1;
4108}
4109
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004110/* If expected data not yet available, it returns a yield. This function
4111 * consumes the data in the buffer. It returns a string containing the
4112 * data. This string can be empty.
4113 */
4114__LJMP static int hlua_applet_http_getline_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004115{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004116 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4117 struct stream_interface *si = appctx->appctx->owner;
4118 struct channel *chn = si_ic(si);
4119 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02004120 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02004121 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02004122 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02004123 size_t len2;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004124
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004125 /* Maybe we cant send a 100-continue ? */
4126 if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_100C) {
Willy Tarreau04f1e2d2018-09-10 18:04:24 +02004127 ret = ci_putblk(chn, HTTP_100.ptr, HTTP_100.len);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004128 /* if ret == -2 or -3 the channel closed or the message si too
4129 * big for the buffers. We cant send anything. So, we ignoring
4130 * the error, considers that the 100-continue is sent, and try
4131 * to receive.
4132 * If ret is -1, we dont have room in the buffer, so we yield.
4133 */
4134 if (ret == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01004135 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004136 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_getline_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004137 }
4138 appctx->appctx->ctx.hlua_apphttp.flags &= ~APPLET_100C;
4139 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004140
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004141 /* Check for the end of the data. */
4142 if (appctx->appctx->ctx.hlua_apphttp.left_bytes <= 0) {
4143 luaL_pushresult(&appctx->b);
4144 return 1;
4145 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004146
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004147 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004148 ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004149
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004150 /* Data not yet available. return yield. */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004151 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01004152 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004153 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_getline_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004154 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004155
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004156 /* End of data: commit the total strings and return. */
4157 if (ret < 0) {
4158 luaL_pushresult(&appctx->b);
4159 return 1;
4160 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004161
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004162 /* Ensure that the block 2 length is usable. */
4163 if (ret == 1)
4164 len2 = 0;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004165
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004166 /* Copy the fisrt block caping to the length required. */
4167 if (len1 > appctx->appctx->ctx.hlua_apphttp.left_bytes)
4168 len1 = appctx->appctx->ctx.hlua_apphttp.left_bytes;
4169 luaL_addlstring(&appctx->b, blk1, len1);
4170 appctx->appctx->ctx.hlua_apphttp.left_bytes -= len1;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004171
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004172 /* Copy the second block. */
4173 if (len2 > appctx->appctx->ctx.hlua_apphttp.left_bytes)
4174 len2 = appctx->appctx->ctx.hlua_apphttp.left_bytes;
4175 luaL_addlstring(&appctx->b, blk2, len2);
4176 appctx->appctx->ctx.hlua_apphttp.left_bytes -= len2;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004177
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004178 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004179 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004180 luaL_pushresult(&appctx->b);
4181 return 1;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004182}
4183
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004184/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004185__LJMP static int hlua_applet_http_getline(lua_State *L)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004186{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004187 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004188
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004189 /* Initialise the string catenation. */
4190 luaL_buffinit(L, &appctx->b);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004191
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004192 return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004193}
4194
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004195/* If expected data not yet available, it returns a yield. This function
4196 * consumes the data in the buffer. It returns a string containing the
4197 * data. This string can be empty.
4198 */
4199__LJMP static int hlua_applet_http_recv_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004200{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004201 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4202 struct stream_interface *si = appctx->appctx->owner;
4203 int len = MAY_LJMP(luaL_checkinteger(L, 2));
4204 struct channel *chn = si_ic(si);
4205 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02004206 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02004207 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02004208 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02004209 size_t len2;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004210
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004211 /* Maybe we cant send a 100-continue ? */
4212 if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_100C) {
Willy Tarreau04f1e2d2018-09-10 18:04:24 +02004213 ret = ci_putblk(chn, HTTP_100.ptr, HTTP_100.len);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004214 /* if ret == -2 or -3 the channel closed or the message si too
4215 * big for the buffers. We cant send anything. So, we ignoring
4216 * the error, considers that the 100-continue is sent, and try
4217 * to receive.
4218 * If ret is -1, we dont have room in the buffer, so we yield.
4219 */
4220 if (ret == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01004221 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004222 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004223 }
4224 appctx->appctx->ctx.hlua_apphttp.flags &= ~APPLET_100C;
4225 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004226
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004227 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004228 ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004229
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004230 /* Data not yet available. return yield. */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004231 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01004232 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004233 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004234 }
4235
4236 /* End of data: commit the total strings and return. */
4237 if (ret < 0) {
4238 luaL_pushresult(&appctx->b);
4239 return 1;
4240 }
4241
4242 /* Ensure that the block 2 length is usable. */
4243 if (ret == 1)
4244 len2 = 0;
4245
4246 /* Copy the fisrt block caping to the length required. */
4247 if (len1 > len)
4248 len1 = len;
4249 luaL_addlstring(&appctx->b, blk1, len1);
4250 len -= len1;
4251
4252 /* Copy the second block. */
4253 if (len2 > len)
4254 len2 = len;
4255 luaL_addlstring(&appctx->b, blk2, len2);
4256 len -= len2;
4257
4258 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004259 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004260 if (appctx->appctx->ctx.hlua_apphttp.left_bytes != -1)
4261 appctx->appctx->ctx.hlua_apphttp.left_bytes -= len;
4262
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004263 /* If we are no other data available, yield waiting for new data. */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004264 if (len > 0) {
4265 lua_pushinteger(L, len);
4266 lua_replace(L, 2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01004267 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004268 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004269 }
4270
4271 /* return the result. */
4272 luaL_pushresult(&appctx->b);
4273 return 1;
4274}
4275
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004276/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004277__LJMP static int hlua_applet_http_recv(lua_State *L)
4278{
4279 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4280 int len = -1;
4281
4282 /* Check arguments. */
4283 if (lua_gettop(L) > 2)
4284 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
4285 if (lua_gettop(L) >= 2) {
4286 len = MAY_LJMP(luaL_checkinteger(L, 2));
4287 lua_pop(L, 1);
4288 }
4289
4290 /* Check the required length */
4291 if (len == -1 || len > appctx->appctx->ctx.hlua_apphttp.left_bytes)
4292 len = appctx->appctx->ctx.hlua_apphttp.left_bytes;
4293 lua_pushinteger(L, len);
4294
4295 /* Initialise the string catenation. */
4296 luaL_buffinit(L, &appctx->b);
4297
4298 return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0));
4299}
4300
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004301/* Append data in the output side of the buffer. This data is immediately
4302 * sent. The function returns the amount of data written. If the buffer
4303 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004304 * if the channel is closed.
4305 */
4306__LJMP static int hlua_applet_http_send_yield(lua_State *L, int status, lua_KContext ctx)
4307{
4308 size_t len;
4309 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4310 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
4311 int l = MAY_LJMP(luaL_checkinteger(L, 3));
4312 struct stream_interface *si = appctx->appctx->owner;
4313 struct channel *chn = si_ic(si);
4314 int max;
4315
4316 /* Get the max amount of data which can write as input in the channel. */
4317 max = channel_recv_max(chn);
4318 if (max > (len - l))
4319 max = len - l;
4320
4321 /* Copy data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004322 ci_putblk(chn, str + l, max);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004323
4324 /* update counters. */
4325 l += max;
4326 lua_pop(L, 1);
4327 lua_pushinteger(L, l);
4328
4329 /* If some data is not send, declares the situation to the
4330 * applet, and returns a yield.
4331 */
4332 if (l < len) {
Willy Tarreaudb398432018-11-15 11:08:52 +01004333 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004334 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004335 }
4336
4337 return 1;
4338}
4339
4340/* Just a wraper of "hlua_applet_send_yield". This wrapper permits
4341 * yield the LUA process, and resume it without checking the
4342 * input arguments.
4343 */
4344__LJMP static int hlua_applet_http_send(lua_State *L)
4345{
4346 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4347 size_t len;
4348 char hex[10];
4349
4350 MAY_LJMP(luaL_checklstring(L, 2, &len));
4351
4352 /* If transfer encoding chunked is selected, we surround the data
4353 * by chunk data.
4354 */
4355 if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_CHUNKED) {
4356 snprintf(hex, 9, "%x", (unsigned int)len);
4357 lua_pushfstring(L, "%s\r\n", hex);
4358 lua_insert(L, 2); /* swap the last 2 entries. */
4359 lua_pushstring(L, "\r\n");
4360 lua_concat(L, 3);
4361 }
4362
4363 /* This interger is used for followinf the amount of data sent. */
4364 lua_pushinteger(L, 0);
4365
4366 /* We want to send some data. Headers must be sent. */
4367 if (!(appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
4368 hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data.");
4369 WILL_LJMP(lua_error(L));
4370 }
4371
4372 return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0));
4373}
4374
4375__LJMP static int hlua_applet_http_addheader(lua_State *L)
4376{
4377 const char *name;
4378 int ret;
4379
4380 MAY_LJMP(hlua_checkapplet_http(L, 1));
4381 name = MAY_LJMP(luaL_checkstring(L, 2));
4382 MAY_LJMP(luaL_checkstring(L, 3));
4383
4384 /* Push in the stack the "response" entry. */
4385 ret = lua_getfield(L, 1, "response");
4386 if (ret != LUA_TTABLE) {
4387 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] "
4388 "is expected as an array. %s found", lua_typename(L, ret));
4389 WILL_LJMP(lua_error(L));
4390 }
4391
4392 /* check if the header is already registered if it is not
4393 * the case, register it.
4394 */
4395 ret = lua_getfield(L, -1, name);
4396 if (ret == LUA_TNIL) {
4397
4398 /* Entry not found. */
4399 lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */
4400
4401 /* Insert the new header name in the array in the top of the stack.
4402 * It left the new array in the top of the stack.
4403 */
4404 lua_newtable(L);
4405 lua_pushvalue(L, 2);
4406 lua_pushvalue(L, -2);
4407 lua_settable(L, -4);
4408
4409 } else if (ret != LUA_TTABLE) {
4410
4411 /* corruption error. */
4412 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] "
4413 "is expected as an array. %s found", name, lua_typename(L, ret));
4414 WILL_LJMP(lua_error(L));
4415 }
4416
4417 /* Now the top od thestack is an array of values. We push
4418 * the header value as new entry.
4419 */
4420 lua_pushvalue(L, 3);
4421 ret = lua_rawlen(L, -2);
4422 lua_rawseti(L, -2, ret + 1);
4423 lua_pushboolean(L, 1);
4424 return 1;
4425}
4426
4427__LJMP static int hlua_applet_http_status(lua_State *L)
4428{
4429 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4430 int status = MAY_LJMP(luaL_checkinteger(L, 2));
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004431 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004432
4433 if (status < 100 || status > 599) {
4434 lua_pushboolean(L, 0);
4435 return 1;
4436 }
4437
4438 appctx->appctx->ctx.hlua_apphttp.status = status;
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004439 appctx->appctx->ctx.hlua_apphttp.reason = reason;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004440 lua_pushboolean(L, 1);
4441 return 1;
4442}
4443
4444/* We will build the status line and the headers of the HTTP response.
4445 * We will try send at once if its not possible, we give back the hand
4446 * waiting for more room.
4447 */
4448__LJMP static int hlua_applet_http_start_response_yield(lua_State *L, int status, lua_KContext ctx)
4449{
4450 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4451 struct stream_interface *si = appctx->appctx->owner;
4452 struct channel *chn = si_ic(si);
4453 int ret;
4454 size_t len;
4455 const char *msg;
4456
4457 /* Get the message as the first argument on the stack. */
4458 msg = MAY_LJMP(luaL_checklstring(L, 2, &len));
4459
4460 /* Send the message at once. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004461 ret = ci_putblk(chn, msg, len);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004462
4463 /* if ret == -2 or -3 the channel closed or the message si too
4464 * big for the buffers.
4465 */
4466 if (ret == -2 || ret == -3) {
4467 hlua_pusherror(L, "Lua: 'start_response': response header block too big");
4468 WILL_LJMP(lua_error(L));
4469 }
4470
4471 /* If ret is -1, we dont have room in the buffer, so we yield. */
4472 if (ret == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01004473 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004474 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_start_response_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004475 }
4476
4477 /* Headers sent, set the flag. */
4478 appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT;
4479 return 0;
4480}
4481
4482__LJMP static int hlua_applet_http_start_response(lua_State *L)
4483{
Willy Tarreau83061a82018-07-13 11:56:34 +02004484 struct buffer *tmp = get_trash_chunk();
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004485 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004486 const char *name;
Willy Tarreaua3294632017-08-23 11:24:47 +02004487 size_t name_len;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004488 const char *value;
Willy Tarreaua3294632017-08-23 11:24:47 +02004489 size_t value_len;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004490 int id;
Willy Tarreauc9f4ea02017-08-23 09:32:06 +02004491 long long hdr_contentlength = -1;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004492 int hdr_chunked = 0;
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004493 const char *reason = appctx->appctx->ctx.hlua_apphttp.reason;
4494
4495 if (reason == NULL)
Willy Tarreau04f1e2d2018-09-10 18:04:24 +02004496 reason = http_get_reason(appctx->appctx->ctx.hlua_apphttp.status);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004497
4498 /* Use the same http version than the request. */
4499 chunk_appendf(tmp, "HTTP/1.%c %d %s\r\n",
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +01004500 appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11 ? '1' : '0',
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004501 appctx->appctx->ctx.hlua_apphttp.status,
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004502 reason);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004503
4504 /* Get the array associated to the field "response" in the object AppletHTTP. */
4505 lua_pushvalue(L, 0);
4506 if (lua_getfield(L, 1, "response") != LUA_TTABLE) {
4507 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n",
4508 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4509 WILL_LJMP(lua_error(L));
4510 }
4511
4512 /* Browse the list of headers. */
4513 lua_pushnil(L);
4514 while(lua_next(L, -2) != 0) {
4515
4516 /* We expect a string as -2. */
4517 if (lua_type(L, -2) != LUA_TSTRING) {
4518 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n",
4519 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4520 lua_typename(L, lua_type(L, -2)));
4521 WILL_LJMP(lua_error(L));
4522 }
Willy Tarreaua3294632017-08-23 11:24:47 +02004523 name = lua_tolstring(L, -2, &name_len);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004524
4525 /* We expect an array as -1. */
4526 if (lua_type(L, -1) != LUA_TTABLE) {
4527 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n",
4528 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4529 name,
4530 lua_typename(L, lua_type(L, -1)));
4531 WILL_LJMP(lua_error(L));
4532 }
4533
4534 /* Browse the table who is on the top of the stack. */
4535 lua_pushnil(L);
4536 while(lua_next(L, -2) != 0) {
4537
4538 /* We expect a number as -2. */
4539 if (lua_type(L, -2) != LUA_TNUMBER) {
4540 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n",
4541 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4542 name,
4543 lua_typename(L, lua_type(L, -2)));
4544 WILL_LJMP(lua_error(L));
4545 }
4546 id = lua_tointeger(L, -2);
4547
4548 /* We expect a string as -2. */
4549 if (lua_type(L, -1) != LUA_TSTRING) {
4550 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n",
4551 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4552 name, id,
4553 lua_typename(L, lua_type(L, -1)));
4554 WILL_LJMP(lua_error(L));
4555 }
Willy Tarreaua3294632017-08-23 11:24:47 +02004556 value = lua_tolstring(L, -1, &value_len);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004557
4558 /* Catenate a new header. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004559 if (tmp->data + name_len + 2 + value_len + 2 < tmp->size) {
4560 memcpy(tmp->area + tmp->data, name, name_len);
4561 tmp->data += name_len;
4562 tmp->area[tmp->data++] = ':';
4563 tmp->area[tmp->data++] = ' ';
Willy Tarreaua3294632017-08-23 11:24:47 +02004564
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004565 memcpy(tmp->area + tmp->data, value,
4566 value_len);
4567 tmp->data += value_len;
4568 tmp->area[tmp->data++] = '\r';
4569 tmp->area[tmp->data++] = '\n';
Willy Tarreaua3294632017-08-23 11:24:47 +02004570 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004571
4572 /* Protocol checks. */
4573
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004574 /* Copy the header content length. The length conversion
Willy Tarreauc9f4ea02017-08-23 09:32:06 +02004575 * is done without control. If it contains a bad value,
4576 * the content-length remains negative so that we can
4577 * switch to either chunked encoding or close.
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004578 */
Willy Tarreaua3294632017-08-23 11:24:47 +02004579 if (name_len == 14 && strcasecmp("content-length", name) == 0)
Willy Tarreauc9f4ea02017-08-23 09:32:06 +02004580 strl2llrc(value, strlen(value), &hdr_contentlength);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004581
4582 /* Check if the client annouces a transfer-encoding chunked it self. */
Willy Tarreaua3294632017-08-23 11:24:47 +02004583 if (name_len == 17 && value_len == 7 &&
4584 strcasecmp("transfer-encoding", name) == 0 &&
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004585 strcasecmp("chunked", value) == 0)
4586 hdr_chunked = 1;
4587
4588 /* Remove the array from the stack, and get next element with a remaining string. */
4589 lua_pop(L, 1);
4590 }
4591
4592 /* Remove the array from the stack, and get next element with a remaining string. */
4593 lua_pop(L, 1);
4594 }
4595
Willy Tarreau06c75fe2017-08-23 09:10:38 +02004596 /* If we dont have a content-length set, and the HTTP version is 1.1
4597 * and the status code implies the presence of a message body, we must
4598 * announce a transfer encoding chunked. This is required by haproxy
4599 * for the keepalive compliance. If the applet annouces a transfer-encoding
4600 * chunked itslef, don't do anything.
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004601 */
Willy Tarreauc9f4ea02017-08-23 09:32:06 +02004602 if (hdr_contentlength < 0 && hdr_chunked == 0 &&
Willy Tarreau06c75fe2017-08-23 09:10:38 +02004603 (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) &&
4604 appctx->appctx->ctx.hlua_apphttp.status >= 200 &&
4605 appctx->appctx->ctx.hlua_apphttp.status != 204 &&
4606 appctx->appctx->ctx.hlua_apphttp.status != 304) {
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004607 chunk_appendf(tmp, "Transfer-encoding: chunked\r\n");
4608 appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_CHUNKED;
4609 }
4610
4611 /* Finalize headers. */
4612 chunk_appendf(tmp, "\r\n");
4613
4614 /* Remove the last entry and the array of headers */
4615 lua_pop(L, 2);
4616
4617 /* Push the headers block. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004618 lua_pushlstring(L, tmp->area, tmp->data);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004619
4620 return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0));
4621}
4622
4623/*
4624 *
4625 *
4626 * Class HTTP
4627 *
4628 *
4629 */
4630
4631/* Returns a struct hlua_txn if the stack entry "ud" is
4632 * a class stream, otherwise it throws an error.
4633 */
4634__LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud)
4635{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02004636 return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004637}
4638
4639/* This function creates and push in the stack a HTTP object
4640 * according with a current TXN.
4641 */
4642static int hlua_http_new(lua_State *L, struct hlua_txn *txn)
4643{
4644 struct hlua_txn *htxn;
4645
4646 /* Check stack size. */
4647 if (!lua_checkstack(L, 3))
4648 return 0;
4649
4650 /* Create the object: obj[0] = userdata.
4651 * Note that the base of the Converters object is the
4652 * same than the TXN object.
4653 */
4654 lua_newtable(L);
4655 htxn = lua_newuserdata(L, sizeof(*htxn));
4656 lua_rawseti(L, -2, 0);
4657
4658 htxn->s = txn->s;
4659 htxn->p = txn->p;
4660
4661 /* Pop a class stream metatable and affect it to the table. */
4662 lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref);
4663 lua_setmetatable(L, -2);
4664
4665 return 1;
4666}
4667
4668/* This function creates ans returns an array of HTTP headers.
4669 * This function does not fails. It is used as wrapper with the
4670 * 2 following functions.
4671 */
4672__LJMP static int hlua_http_get_headers(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg)
4673{
4674 const char *cur_ptr, *cur_next, *p;
4675 int old_idx, cur_idx;
4676 struct hdr_idx_elem *cur_hdr;
4677 const char *hn, *hv;
4678 int hnl, hvl;
4679 int type;
4680 const char *in;
4681 char *out;
4682 int len;
4683
4684 /* Create the table. */
4685 lua_newtable(L);
4686
4687 if (!htxn->s->txn)
4688 return 1;
4689
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004690 /* Check if a valid response is parsed */
4691 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4692 return 1;
4693
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004694 /* Build array of headers. */
4695 old_idx = 0;
Willy Tarreaua79021a2018-06-15 18:07:57 +02004696 cur_next = ci_head(msg->chn) + hdr_idx_first_pos(&htxn->s->txn->hdr_idx);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004697
4698 while (1) {
4699 cur_idx = htxn->s->txn->hdr_idx.v[old_idx].next;
4700 if (!cur_idx)
4701 break;
4702 old_idx = cur_idx;
4703
4704 cur_hdr = &htxn->s->txn->hdr_idx.v[cur_idx];
4705 cur_ptr = cur_next;
4706 cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1;
4707
4708 /* Now we have one full header at cur_ptr of len cur_hdr->len,
4709 * and the next header starts at cur_next. We'll check
4710 * this header in the list as well as against the default
4711 * rule.
4712 */
4713
4714 /* look for ': *'. */
4715 hn = cur_ptr;
4716 for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++);
4717 if (p >= cur_ptr+cur_hdr->len)
4718 continue;
4719 hnl = p - hn;
4720 p++;
4721 while (p < cur_ptr+cur_hdr->len && ( *p == ' ' || *p == '\t' ))
4722 p++;
4723 if (p >= cur_ptr+cur_hdr->len)
4724 continue;
4725 hv = p;
4726 hvl = cur_ptr+cur_hdr->len-p;
4727
4728 /* Lowercase the key. Don't check the size of trash, it have
4729 * the size of one buffer and the input data contains in one
4730 * buffer.
4731 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004732 out = trash.area;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004733 for (in=hn; in<hn+hnl; in++, out++)
4734 *out = tolower(*in);
4735 *out = '\0';
4736
4737 /* Check for existing entry:
4738 * assume that the table is on the top of the stack, and
4739 * push the key in the stack, the function lua_gettable()
4740 * perform the lookup.
4741 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004742 lua_pushlstring(L, trash.area, hnl);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004743 lua_gettable(L, -2);
4744 type = lua_type(L, -1);
4745
4746 switch (type) {
4747 case LUA_TNIL:
4748 /* Table not found, create it. */
4749 lua_pop(L, 1); /* remove the nil value. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004750 lua_pushlstring(L, trash.area, hnl); /* push the header name as key. */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004751 lua_newtable(L); /* create and push empty table. */
4752 lua_pushlstring(L, hv, hvl); /* push header value. */
4753 lua_rawseti(L, -2, 0); /* index header value (pop it). */
4754 lua_rawset(L, -3); /* index new table with header name (pop the values). */
4755 break;
4756
4757 case LUA_TTABLE:
4758 /* Entry found: push the value in the table. */
4759 len = lua_rawlen(L, -1);
4760 lua_pushlstring(L, hv, hvl); /* push header value. */
4761 lua_rawseti(L, -2, len+1); /* index header value (pop it). */
4762 lua_pop(L, 1); /* remove the table (it is stored in the main table). */
4763 break;
4764
4765 default:
4766 /* Other cases are errors. */
4767 hlua_pusherror(L, "internal error during the parsing of headers.");
4768 WILL_LJMP(lua_error(L));
4769 }
4770 }
4771
4772 return 1;
4773}
4774
4775__LJMP static int hlua_http_req_get_headers(lua_State *L)
4776{
4777 struct hlua_txn *htxn;
4778
4779 MAY_LJMP(check_args(L, 1, "req_get_headers"));
4780 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4781
4782 return hlua_http_get_headers(L, htxn, &htxn->s->txn->req);
4783}
4784
4785__LJMP static int hlua_http_res_get_headers(lua_State *L)
4786{
4787 struct hlua_txn *htxn;
4788
4789 MAY_LJMP(check_args(L, 1, "res_get_headers"));
4790 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4791
4792 return hlua_http_get_headers(L, htxn, &htxn->s->txn->rsp);
4793}
4794
4795/* This function replace full header, or just a value in
4796 * the request or in the response. It is a wrapper fir the
4797 * 4 following functions.
4798 */
4799__LJMP static inline int hlua_http_rep_hdr(lua_State *L, struct hlua_txn *htxn,
4800 struct http_msg *msg, int action)
4801{
4802 size_t name_len;
4803 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
4804 const char *reg = MAY_LJMP(luaL_checkstring(L, 3));
4805 const char *value = MAY_LJMP(luaL_checkstring(L, 4));
4806 struct my_regex re;
4807
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004808 /* Check if a valid response is parsed */
4809 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4810 return 0;
4811
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004812 if (!regex_comp(reg, &re, 1, 1, NULL))
4813 WILL_LJMP(luaL_argerror(L, 3, "invalid regex"));
4814
4815 http_transform_header_str(htxn->s, msg, name, name_len, value, &re, action);
4816 regex_free(&re);
4817 return 0;
4818}
4819
4820__LJMP static int hlua_http_req_rep_hdr(lua_State *L)
4821{
4822 struct hlua_txn *htxn;
4823
4824 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
4825 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4826
4827 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_HDR));
4828}
4829
4830__LJMP static int hlua_http_res_rep_hdr(lua_State *L)
4831{
4832 struct hlua_txn *htxn;
4833
4834 MAY_LJMP(check_args(L, 4, "res_rep_hdr"));
4835 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4836
4837 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->rsp, ACT_HTTP_REPLACE_HDR));
4838}
4839
4840__LJMP static int hlua_http_req_rep_val(lua_State *L)
4841{
4842 struct hlua_txn *htxn;
4843
4844 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
4845 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4846
4847 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_VAL));
4848}
4849
4850__LJMP static int hlua_http_res_rep_val(lua_State *L)
4851{
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004852 struct hlua_txn *htxn;
4853
4854 MAY_LJMP(check_args(L, 4, "res_rep_val"));
4855 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4856
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02004857 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->rsp, ACT_HTTP_REPLACE_VAL));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004858}
4859
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004860/* This function deletes all the occurrences of an header.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004861 * It is a wrapper for the 2 following functions.
4862 */
4863__LJMP static inline int hlua_http_del_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg)
4864{
4865 size_t len;
4866 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4867 struct hdr_ctx ctx;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004868 struct http_txn *txn = htxn->s->txn;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004869
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004870 /* Check if a valid response is parsed */
4871 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4872 return 0;
4873
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004874 ctx.idx = 0;
Willy Tarreaua79021a2018-06-15 18:07:57 +02004875 while (http_find_header2(name, len, ci_head(msg->chn), &txn->hdr_idx, &ctx))
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004876 http_remove_header2(msg, &txn->hdr_idx, &ctx);
4877 return 0;
4878}
4879
4880__LJMP static int hlua_http_req_del_hdr(lua_State *L)
4881{
4882 struct hlua_txn *htxn;
4883
4884 MAY_LJMP(check_args(L, 2, "req_del_hdr"));
4885 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4886
Willy Tarreaueee5b512015-04-03 23:46:31 +02004887 return hlua_http_del_hdr(L, htxn, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004888}
4889
4890__LJMP static int hlua_http_res_del_hdr(lua_State *L)
4891{
4892 struct hlua_txn *htxn;
4893
4894 MAY_LJMP(check_args(L, 2, "req_del_hdr"));
4895 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4896
Willy Tarreaueee5b512015-04-03 23:46:31 +02004897 return hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004898}
4899
4900/* This function adds an header. It is a wrapper used by
4901 * the 2 following functions.
4902 */
4903__LJMP static inline int hlua_http_add_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg)
4904{
4905 size_t name_len;
4906 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
4907 size_t value_len;
4908 const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len));
4909 char *p;
4910
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004911 /* Check if a valid message is parsed */
4912 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4913 return 0;
4914
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004915 /* Check length. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004916 trash.data = value_len + name_len + 2;
4917 if (trash.data > trash.size)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004918 return 0;
4919
4920 /* Creates the header string. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004921 p = trash.area;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004922 memcpy(p, name, name_len);
4923 p += name_len;
4924 *p = ':';
4925 p++;
4926 *p = ' ';
4927 p++;
4928 memcpy(p, value, value_len);
4929
Willy Tarreaueee5b512015-04-03 23:46:31 +02004930 lua_pushboolean(L, http_header_add_tail2(msg, &htxn->s->txn->hdr_idx,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004931 trash.area, trash.data) != 0);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004932
4933 return 0;
4934}
4935
4936__LJMP static int hlua_http_req_add_hdr(lua_State *L)
4937{
4938 struct hlua_txn *htxn;
4939
4940 MAY_LJMP(check_args(L, 3, "req_add_hdr"));
4941 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4942
Willy Tarreaueee5b512015-04-03 23:46:31 +02004943 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004944}
4945
4946__LJMP static int hlua_http_res_add_hdr(lua_State *L)
4947{
4948 struct hlua_txn *htxn;
4949
4950 MAY_LJMP(check_args(L, 3, "res_add_hdr"));
4951 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4952
Willy Tarreaueee5b512015-04-03 23:46:31 +02004953 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004954}
4955
4956static int hlua_http_req_set_hdr(lua_State *L)
4957{
4958 struct hlua_txn *htxn;
4959
4960 MAY_LJMP(check_args(L, 3, "req_set_hdr"));
4961 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4962
Willy Tarreaueee5b512015-04-03 23:46:31 +02004963 hlua_http_del_hdr(L, htxn, &htxn->s->txn->req);
4964 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004965}
4966
4967static int hlua_http_res_set_hdr(lua_State *L)
4968{
4969 struct hlua_txn *htxn;
4970
4971 MAY_LJMP(check_args(L, 3, "res_set_hdr"));
4972 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4973
Willy Tarreaueee5b512015-04-03 23:46:31 +02004974 hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp);
4975 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004976}
4977
4978/* This function set the method. */
4979static int hlua_http_req_set_meth(lua_State *L)
4980{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02004981 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004982 size_t name_len;
4983 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004984
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004985 /* Check if a valid request is parsed */
4986 if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) {
4987 lua_pushboolean(L, 0);
4988 return 1;
4989 }
4990
Willy Tarreau987e3fb2015-04-04 01:09:08 +02004991 lua_pushboolean(L, http_replace_req_line(0, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004992 return 1;
4993}
4994
4995/* This function set the method. */
4996static int hlua_http_req_set_path(lua_State *L)
4997{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02004998 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004999 size_t name_len;
5000 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02005001
5002 /* Check if a valid request is parsed */
5003 if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) {
5004 lua_pushboolean(L, 0);
5005 return 1;
5006 }
5007
Willy Tarreau987e3fb2015-04-04 01:09:08 +02005008 lua_pushboolean(L, http_replace_req_line(1, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005009 return 1;
5010}
5011
5012/* This function set the query-string. */
5013static int hlua_http_req_set_query(lua_State *L)
5014{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005015 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005016 size_t name_len;
5017 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005018
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02005019 /* Check if a valid request is parsed */
5020 if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) {
5021 lua_pushboolean(L, 0);
5022 return 1;
5023 }
5024
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005025 /* Check length. */
5026 if (name_len > trash.size - 1) {
5027 lua_pushboolean(L, 0);
5028 return 1;
5029 }
5030
5031 /* Add the mark question as prefix. */
5032 chunk_reset(&trash);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005033 trash.area[trash.data++] = '?';
5034 memcpy(trash.area + trash.data, name, name_len);
5035 trash.data += name_len;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005036
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005037 lua_pushboolean(L,
5038 http_replace_req_line(2, trash.area, trash.data, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005039 return 1;
5040}
5041
5042/* This function set the uri. */
5043static int hlua_http_req_set_uri(lua_State *L)
5044{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005045 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005046 size_t name_len;
5047 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005048
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02005049 /* Check if a valid request is parsed */
5050 if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) {
5051 lua_pushboolean(L, 0);
5052 return 1;
5053 }
5054
Willy Tarreau987e3fb2015-04-04 01:09:08 +02005055 lua_pushboolean(L, http_replace_req_line(3, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005056 return 1;
5057}
5058
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005059/* This function set the response code & optionally reason. */
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005060static int hlua_http_res_set_status(lua_State *L)
5061{
5062 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5063 unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2));
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005064 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005065
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02005066 /* Check if a valid response is parsed */
5067 if (unlikely(htxn->s->txn->rsp.msg_state < HTTP_MSG_BODY))
5068 return 0;
5069
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005070 http_set_status(code, reason, htxn->s);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005071 return 0;
5072}
5073
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005074/*
5075 *
5076 *
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005077 * Class TXN
5078 *
5079 *
5080 */
5081
5082/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02005083 * a class stream, otherwise it throws an error.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005084 */
5085__LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud)
5086{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02005087 return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref));
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005088}
5089
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005090__LJMP static int hlua_set_var(lua_State *L)
5091{
5092 struct hlua_txn *htxn;
5093 const char *name;
5094 size_t len;
5095 struct sample smp;
5096
5097 MAY_LJMP(check_args(L, 3, "set_var"));
5098
5099 /* It is useles to retrieve the stream, but this function
5100 * runs only in a stream context.
5101 */
5102 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5103 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5104
5105 /* Converts the third argument in a sample. */
5106 hlua_lua2smp(L, 3, &smp);
5107
5108 /* Store the sample in a variable. */
Willy Tarreau7560dd42016-03-10 16:28:58 +01005109 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Willy Tarreau6204cd92016-03-10 16:33:04 +01005110 vars_set_by_name(name, len, &smp);
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005111 return 0;
5112}
5113
Christopher Faulet85d79c92016-11-09 16:54:56 +01005114__LJMP static int hlua_unset_var(lua_State *L)
5115{
5116 struct hlua_txn *htxn;
5117 const char *name;
5118 size_t len;
5119 struct sample smp;
5120
5121 MAY_LJMP(check_args(L, 2, "unset_var"));
5122
5123 /* It is useles to retrieve the stream, but this function
5124 * runs only in a stream context.
5125 */
5126 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5127 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5128
5129 /* Unset the variable. */
5130 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
5131 vars_unset_by_name(name, len, &smp);
5132 return 0;
5133}
5134
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005135__LJMP static int hlua_get_var(lua_State *L)
5136{
5137 struct hlua_txn *htxn;
5138 const char *name;
5139 size_t len;
5140 struct sample smp;
5141
5142 MAY_LJMP(check_args(L, 2, "get_var"));
5143
5144 /* It is useles to retrieve the stream, but this function
5145 * runs only in a stream context.
5146 */
5147 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5148 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5149
Willy Tarreau7560dd42016-03-10 16:28:58 +01005150 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Willy Tarreau6204cd92016-03-10 16:33:04 +01005151 if (!vars_get_by_name(name, len, &smp)) {
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005152 lua_pushnil(L);
5153 return 1;
5154 }
5155
5156 return hlua_smp2lua(L, &smp);
5157}
5158
Willy Tarreau59551662015-03-10 14:23:13 +01005159__LJMP static int hlua_set_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005160{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005161 struct hlua *hlua;
5162
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005163 MAY_LJMP(check_args(L, 2, "set_priv"));
5164
Willy Tarreau87b09662015-04-03 00:22:06 +02005165 /* It is useles to retrieve the stream, but this function
5166 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005167 */
5168 MAY_LJMP(hlua_checktxn(L, 1));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005169 hlua = hlua_gethlua(L);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005170
5171 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02005172 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005173
5174 /* Get and store new value. */
5175 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
5176 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
5177
5178 return 0;
5179}
5180
Willy Tarreau59551662015-03-10 14:23:13 +01005181__LJMP static int hlua_get_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005182{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005183 struct hlua *hlua;
5184
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005185 MAY_LJMP(check_args(L, 1, "get_priv"));
5186
Willy Tarreau87b09662015-04-03 00:22:06 +02005187 /* It is useles to retrieve the stream, but this function
5188 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005189 */
5190 MAY_LJMP(hlua_checktxn(L, 1));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005191 hlua = hlua_gethlua(L);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005192
5193 /* Push configuration index in the stack. */
5194 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
5195
5196 return 1;
5197}
5198
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005199/* Create stack entry containing a class TXN. This function
5200 * return 0 if the stack does not contains free slots,
5201 * otherwise it returns 1.
5202 */
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005203static int hlua_txn_new(lua_State *L, struct stream *s, struct proxy *p, int dir, int flags)
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005204{
Willy Tarreaude491382015-04-06 11:04:28 +02005205 struct hlua_txn *htxn;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005206
5207 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005208 if (!lua_checkstack(L, 3))
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005209 return 0;
5210
5211 /* NOTE: The allocation never fails. The failure
5212 * throw an error, and the function never returns.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08005213 * if the throw is not available, the process is aborted.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005214 */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005215 /* Create the object: obj[0] = userdata. */
5216 lua_newtable(L);
Willy Tarreaude491382015-04-06 11:04:28 +02005217 htxn = lua_newuserdata(L, sizeof(*htxn));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005218 lua_rawseti(L, -2, 0);
5219
Willy Tarreaude491382015-04-06 11:04:28 +02005220 htxn->s = s;
5221 htxn->p = p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01005222 htxn->dir = dir;
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005223 htxn->flags = flags;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005224
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005225 /* Create the "f" field that contains a list of fetches. */
5226 lua_pushstring(L, "f");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005227 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005228 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005229 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005230
5231 /* Create the "sf" field that contains a list of stringsafe fetches. */
5232 lua_pushstring(L, "sf");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005233 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP | HLUA_F_AS_STRING))
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005234 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005235 lua_rawset(L, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005236
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005237 /* Create the "c" field that contains a list of converters. */
5238 lua_pushstring(L, "c");
Willy Tarreaude491382015-04-06 11:04:28 +02005239 if (!hlua_converters_new(L, htxn, 0))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005240 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005241 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005242
5243 /* Create the "sc" field that contains a list of stringsafe converters. */
5244 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01005245 if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING))
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005246 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005247 lua_rawset(L, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005248
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005249 /* Create the "req" field that contains the request channel object. */
5250 lua_pushstring(L, "req");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005251 if (!hlua_channel_new(L, &s->req))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005252 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005253 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005254
5255 /* Create the "res" field that contains the response channel object. */
5256 lua_pushstring(L, "res");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005257 if (!hlua_channel_new(L, &s->res))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005258 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005259 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005260
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005261 /* Creates the HTTP object is the current proxy allows http. */
5262 lua_pushstring(L, "http");
5263 if (p->mode == PR_MODE_HTTP) {
Willy Tarreaude491382015-04-06 11:04:28 +02005264 if (!hlua_http_new(L, htxn))
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005265 return 0;
5266 }
5267 else
5268 lua_pushnil(L);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005269 lua_rawset(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005270
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005271 /* Pop a class sesison metatable and affect it to the userdata. */
5272 lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref);
5273 lua_setmetatable(L, -2);
5274
5275 return 1;
5276}
5277
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005278__LJMP static int hlua_txn_deflog(lua_State *L)
5279{
5280 const char *msg;
5281 struct hlua_txn *htxn;
5282
5283 MAY_LJMP(check_args(L, 2, "deflog"));
5284 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5285 msg = MAY_LJMP(luaL_checkstring(L, 2));
5286
5287 hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg);
5288 return 0;
5289}
5290
5291__LJMP static int hlua_txn_log(lua_State *L)
5292{
5293 int level;
5294 const char *msg;
5295 struct hlua_txn *htxn;
5296
5297 MAY_LJMP(check_args(L, 3, "log"));
5298 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5299 level = MAY_LJMP(luaL_checkinteger(L, 2));
5300 msg = MAY_LJMP(luaL_checkstring(L, 3));
5301
5302 if (level < 0 || level >= NB_LOG_LEVELS)
5303 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
5304
5305 hlua_sendlog(htxn->s->be, level, msg);
5306 return 0;
5307}
5308
5309__LJMP static int hlua_txn_log_debug(lua_State *L)
5310{
5311 const char *msg;
5312 struct hlua_txn *htxn;
5313
5314 MAY_LJMP(check_args(L, 2, "Debug"));
5315 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5316 msg = MAY_LJMP(luaL_checkstring(L, 2));
5317 hlua_sendlog(htxn->s->be, LOG_DEBUG, msg);
5318 return 0;
5319}
5320
5321__LJMP static int hlua_txn_log_info(lua_State *L)
5322{
5323 const char *msg;
5324 struct hlua_txn *htxn;
5325
5326 MAY_LJMP(check_args(L, 2, "Info"));
5327 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5328 msg = MAY_LJMP(luaL_checkstring(L, 2));
5329 hlua_sendlog(htxn->s->be, LOG_INFO, msg);
5330 return 0;
5331}
5332
5333__LJMP static int hlua_txn_log_warning(lua_State *L)
5334{
5335 const char *msg;
5336 struct hlua_txn *htxn;
5337
5338 MAY_LJMP(check_args(L, 2, "Warning"));
5339 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5340 msg = MAY_LJMP(luaL_checkstring(L, 2));
5341 hlua_sendlog(htxn->s->be, LOG_WARNING, msg);
5342 return 0;
5343}
5344
5345__LJMP static int hlua_txn_log_alert(lua_State *L)
5346{
5347 const char *msg;
5348 struct hlua_txn *htxn;
5349
5350 MAY_LJMP(check_args(L, 2, "Alert"));
5351 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5352 msg = MAY_LJMP(luaL_checkstring(L, 2));
5353 hlua_sendlog(htxn->s->be, LOG_ALERT, msg);
5354 return 0;
5355}
5356
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005357__LJMP static int hlua_txn_set_loglevel(lua_State *L)
5358{
5359 struct hlua_txn *htxn;
5360 int ll;
5361
5362 MAY_LJMP(check_args(L, 2, "set_loglevel"));
5363 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5364 ll = MAY_LJMP(luaL_checkinteger(L, 2));
5365
5366 if (ll < 0 || ll > 7)
5367 WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7"));
5368
5369 htxn->s->logs.level = ll;
5370 return 0;
5371}
5372
5373__LJMP static int hlua_txn_set_tos(lua_State *L)
5374{
5375 struct hlua_txn *htxn;
5376 struct connection *cli_conn;
5377 int tos;
5378
5379 MAY_LJMP(check_args(L, 2, "set_tos"));
5380 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5381 tos = MAY_LJMP(luaL_checkinteger(L, 2));
5382
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02005383 if ((cli_conn = objt_conn(htxn->s->sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02005384 inet_set_tos(cli_conn->handle.fd, &cli_conn->addr.from, tos);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005385
5386 return 0;
5387}
5388
5389__LJMP static int hlua_txn_set_mark(lua_State *L)
5390{
5391#ifdef SO_MARK
5392 struct hlua_txn *htxn;
5393 struct connection *cli_conn;
5394 int mark;
5395
5396 MAY_LJMP(check_args(L, 2, "set_mark"));
5397 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5398 mark = MAY_LJMP(luaL_checkinteger(L, 2));
5399
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02005400 if ((cli_conn = objt_conn(htxn->s->sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02005401 setsockopt(cli_conn->handle.fd, SOL_SOCKET, SO_MARK, &mark, sizeof(mark));
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005402#endif
5403 return 0;
5404}
5405
Patrick Hemmer268a7072018-05-11 12:52:31 -04005406__LJMP static int hlua_txn_set_priority_class(lua_State *L)
5407{
5408 struct hlua_txn *htxn;
5409
5410 MAY_LJMP(check_args(L, 2, "set_priority_class"));
5411 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5412 htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2)));
5413 return 0;
5414}
5415
5416__LJMP static int hlua_txn_set_priority_offset(lua_State *L)
5417{
5418 struct hlua_txn *htxn;
5419
5420 MAY_LJMP(check_args(L, 2, "set_priority_offset"));
5421 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5422 htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2)));
5423 return 0;
5424}
5425
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005426/* This function is an Lua binding that send pending data
5427 * to the client, and close the stream interface.
5428 */
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005429__LJMP static int hlua_txn_done(lua_State *L)
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005430{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005431 struct hlua_txn *htxn;
Thierry FOURNIER9bd52d42016-07-14 11:45:33 +02005432 struct hlua *hlua;
Willy Tarreau81389672015-03-10 12:03:52 +01005433 struct channel *ic, *oc;
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005434
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005435 MAY_LJMP(check_args(L, 1, "close"));
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005436 htxn = MAY_LJMP(hlua_checktxn(L, 1));
Thierry FOURNIER9bd52d42016-07-14 11:45:33 +02005437 hlua = hlua_gethlua(L);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005438
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005439 /* If the flags NOTERM is set, we cannot terminate the http
5440 * session, so we just end the execution of the current
5441 * lua code.
5442 */
5443 if (htxn->flags & HLUA_TXN_NOTERM) {
5444 WILL_LJMP(hlua_done(L));
5445 return 0;
5446 }
5447
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005448 ic = &htxn->s->req;
5449 oc = &htxn->s->res;
Willy Tarreau81389672015-03-10 12:03:52 +01005450
Willy Tarreau630ef452015-08-28 10:06:15 +02005451 if (htxn->s->txn) {
5452 /* HTTP mode, let's stay in sync with the stream */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005453 b_del(&ic->buf, htxn->s->txn->req.sov);
Willy Tarreau630ef452015-08-28 10:06:15 +02005454 htxn->s->txn->req.next -= htxn->s->txn->req.sov;
5455 htxn->s->txn->req.sov = 0;
5456 ic->analysers &= AN_REQ_HTTP_XFER_BODY;
5457 oc->analysers = AN_RES_HTTP_XFER_BODY;
5458 htxn->s->txn->req.msg_state = HTTP_MSG_CLOSED;
5459 htxn->s->txn->rsp.msg_state = HTTP_MSG_DONE;
5460
Willy Tarreau630ef452015-08-28 10:06:15 +02005461 /* Note that if we want to support keep-alive, we need
5462 * to bypass the close/shutr_now calls below, but that
5463 * may only be done if the HTTP request was already
5464 * processed and the connection header is known (ie
5465 * not during TCP rules).
5466 */
5467 }
5468
Thierry FOURNIER10ec2142015-08-24 17:23:45 +02005469 channel_auto_read(ic);
Willy Tarreau81389672015-03-10 12:03:52 +01005470 channel_abort(ic);
5471 channel_auto_close(ic);
5472 channel_erase(ic);
Thierry FOURNIER10ec2142015-08-24 17:23:45 +02005473
5474 oc->wex = tick_add_ifset(now_ms, oc->wto);
Willy Tarreau81389672015-03-10 12:03:52 +01005475 channel_auto_read(oc);
5476 channel_auto_close(oc);
5477 channel_shutr_now(oc);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005478
Willy Tarreau0458b082015-08-28 09:40:04 +02005479 ic->analysers = 0;
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005480
Thierry FOURNIER9bd52d42016-07-14 11:45:33 +02005481 hlua->flags |= HLUA_STOP;
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005482 WILL_LJMP(hlua_done(L));
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005483 return 0;
5484}
5485
5486__LJMP static int hlua_log(lua_State *L)
5487{
5488 int level;
5489 const char *msg;
5490
5491 MAY_LJMP(check_args(L, 2, "log"));
5492 level = MAY_LJMP(luaL_checkinteger(L, 1));
5493 msg = MAY_LJMP(luaL_checkstring(L, 2));
5494
5495 if (level < 0 || level >= NB_LOG_LEVELS)
5496 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
5497
5498 hlua_sendlog(NULL, level, msg);
5499 return 0;
5500}
5501
5502__LJMP static int hlua_log_debug(lua_State *L)
5503{
5504 const char *msg;
5505
5506 MAY_LJMP(check_args(L, 1, "debug"));
5507 msg = MAY_LJMP(luaL_checkstring(L, 1));
5508 hlua_sendlog(NULL, LOG_DEBUG, msg);
5509 return 0;
5510}
5511
5512__LJMP static int hlua_log_info(lua_State *L)
5513{
5514 const char *msg;
5515
5516 MAY_LJMP(check_args(L, 1, "info"));
5517 msg = MAY_LJMP(luaL_checkstring(L, 1));
5518 hlua_sendlog(NULL, LOG_INFO, msg);
5519 return 0;
5520}
5521
5522__LJMP static int hlua_log_warning(lua_State *L)
5523{
5524 const char *msg;
5525
5526 MAY_LJMP(check_args(L, 1, "warning"));
5527 msg = MAY_LJMP(luaL_checkstring(L, 1));
5528 hlua_sendlog(NULL, LOG_WARNING, msg);
5529 return 0;
5530}
5531
5532__LJMP static int hlua_log_alert(lua_State *L)
5533{
5534 const char *msg;
5535
5536 MAY_LJMP(check_args(L, 1, "alert"));
5537 msg = MAY_LJMP(luaL_checkstring(L, 1));
5538 hlua_sendlog(NULL, LOG_ALERT, msg);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005539 return 0;
5540}
5541
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005542__LJMP static int hlua_sleep_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005543{
5544 int wakeup_ms = lua_tointeger(L, -1);
5545 if (now_ms < wakeup_ms)
Willy Tarreau9635e032018-10-16 17:52:55 +02005546 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005547 return 0;
5548}
5549
5550__LJMP static int hlua_sleep(lua_State *L)
5551{
5552 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005553 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005554
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005555 MAY_LJMP(check_args(L, 1, "sleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005556
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005557 delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005558 wakeup_ms = tick_add(now_ms, delay);
5559 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005560
Willy Tarreau9635e032018-10-16 17:52:55 +02005561 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005562 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005563}
5564
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005565__LJMP static int hlua_msleep(lua_State *L)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005566{
5567 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005568 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005569
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005570 MAY_LJMP(check_args(L, 1, "msleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005571
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005572 delay = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005573 wakeup_ms = tick_add(now_ms, delay);
5574 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005575
Willy Tarreau9635e032018-10-16 17:52:55 +02005576 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005577 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005578}
5579
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01005580/* This functionis an LUA binding. it permits to give back
5581 * the hand at the HAProxy scheduler. It is used when the
5582 * LUA processing consumes a lot of time.
5583 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005584__LJMP static int hlua_yield_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005585{
5586 return 0;
5587}
5588
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01005589__LJMP static int hlua_yield(lua_State *L)
5590{
Willy Tarreau9635e032018-10-16 17:52:55 +02005591 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_yield_yield, TICK_ETERNITY, HLUA_CTRLYIELD));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005592 return 0;
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01005593}
5594
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005595/* This function change the nice of the currently executed
5596 * task. It is used set low or high priority at the current
5597 * task.
5598 */
Willy Tarreau59551662015-03-10 14:23:13 +01005599__LJMP static int hlua_set_nice(lua_State *L)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005600{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005601 struct hlua *hlua;
5602 int nice;
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005603
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005604 MAY_LJMP(check_args(L, 1, "set_nice"));
5605 hlua = hlua_gethlua(L);
5606 nice = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005607
5608 /* If he task is not set, I'm in a start mode. */
5609 if (!hlua || !hlua->task)
5610 return 0;
5611
5612 if (nice < -1024)
5613 nice = -1024;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005614 else if (nice > 1024)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005615 nice = 1024;
5616
5617 hlua->task->nice = nice;
5618 return 0;
5619}
5620
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08005621/* This function is used as a callback of a task. It is called by the
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005622 * HAProxy task subsystem when the task is awaked. The LUA runtime can
5623 * return an E_AGAIN signal, the emmiter of this signal must set a
5624 * signal to wake the task.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005625 *
5626 * Task wrapper are longjmp safe because the only one Lua code
5627 * executed is the safe hlua_ctx_resume();
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005628 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02005629static struct task *hlua_process_task(struct task *task, void *context, unsigned short state)
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005630{
Olivier Houchard9f6af332018-05-25 14:04:04 +02005631 struct hlua *hlua = context;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005632 enum hlua_exec status;
5633
Christopher Faulet5bc99722018-04-25 10:34:45 +02005634 if (task->thread_mask == MAX_THREADS_MASK)
5635 task_set_affinity(task, tid_bit);
5636
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005637 /* If it is the first call to the task, we must initialize the
5638 * execution timeouts.
5639 */
5640 if (!HLUA_IS_RUNNING(hlua))
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02005641 hlua->max_time = hlua_timeout_task;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005642
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005643 /* Execute the Lua code. */
5644 status = hlua_ctx_resume(hlua, 1);
5645
5646 switch (status) {
5647 /* finished or yield */
5648 case HLUA_E_OK:
5649 hlua_ctx_destroy(hlua);
5650 task_delete(task);
5651 task_free(task);
Tim Duesterhuscd235c62018-04-24 13:56:01 +02005652 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005653 break;
5654
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01005655 case HLUA_E_AGAIN: /* co process or timeout wake me later. */
Thierry FOURNIERcb146882017-12-10 17:10:57 +01005656 notification_gc(&hlua->com);
PiBa-NLfe971b32018-05-02 22:27:14 +02005657 task->expire = hlua->wake_time;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005658 break;
5659
5660 /* finished with error. */
5661 case HLUA_E_ERRMSG:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005662 SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005663 hlua_ctx_destroy(hlua);
5664 task_delete(task);
5665 task_free(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02005666 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005667 break;
5668
5669 case HLUA_E_ERR:
5670 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005671 SEND_ERR(NULL, "Lua task: unknown error.\n");
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005672 hlua_ctx_destroy(hlua);
5673 task_delete(task);
5674 task_free(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02005675 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005676 break;
5677 }
Emeric Brun253e53e2017-10-17 18:58:40 +02005678 return task;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005679}
5680
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01005681/* This function is an LUA binding that register LUA function to be
5682 * executed after the HAProxy configuration parsing and before the
5683 * HAProxy scheduler starts. This function expect only one LUA
5684 * argument that is a function. This function returns nothing, but
5685 * throws if an error is encountered.
5686 */
5687__LJMP static int hlua_register_init(lua_State *L)
5688{
5689 struct hlua_init_function *init;
5690 int ref;
5691
5692 MAY_LJMP(check_args(L, 1, "register_init"));
5693
5694 ref = MAY_LJMP(hlua_checkfunction(L, 1));
5695
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02005696 init = calloc(1, sizeof(*init));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01005697 if (!init)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01005698 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01005699
5700 init->function_ref = ref;
5701 LIST_ADDQ(&hlua_init_functions, &init->l);
5702 return 0;
5703}
5704
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005705/* This functio is an LUA binding. It permits to register a task
5706 * executed in parallel of the main HAroxy activity. The task is
5707 * created and it is set in the HAProxy scheduler. It can be called
5708 * from the "init" section, "post init" or during the runtime.
5709 *
5710 * Lua prototype:
5711 *
5712 * <none> core.register_task(<function>)
5713 */
5714static int hlua_register_task(lua_State *L)
5715{
5716 struct hlua *hlua;
5717 struct task *task;
5718 int ref;
5719
5720 MAY_LJMP(check_args(L, 1, "register_task"));
5721
5722 ref = MAY_LJMP(hlua_checkfunction(L, 1));
5723
Willy Tarreaubafbe012017-11-24 17:34:44 +01005724 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005725 if (!hlua)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01005726 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005727
Emeric Brunc60def82017-09-27 14:59:38 +02005728 task = task_new(MAX_THREADS_MASK);
Willy Tarreaue09101e2018-10-16 17:37:12 +02005729 if (!task)
5730 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
5731
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005732 task->context = hlua;
5733 task->process = hlua_process_task;
5734
5735 if (!hlua_ctx_init(hlua, task))
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01005736 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005737
5738 /* Restore the function in the stack. */
5739 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref);
5740 hlua->nargs = 0;
5741
5742 /* Schedule task. */
5743 task_schedule(task, now_ms);
5744
5745 return 0;
5746}
5747
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005748/* Wrapper called by HAProxy to execute an LUA converter. This wrapper
5749 * doesn't allow "yield" functions because the HAProxy engine cannot
5750 * resume converters.
5751 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02005752static int hlua_sample_conv_wrapper(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005753{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02005754 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02005755 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01005756 const char *error;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005757
Willy Tarreaube508f12016-03-10 11:47:01 +01005758 if (!stream)
5759 return 0;
5760
Christopher Fauletafd8f102018-11-08 11:34:21 +01005761 if (IS_HTX_STRM(stream)) {
5762 SEND_ERR(stream->be, "Lua converter '%s': Lua fetches cannot be used when the"
5763 " HTX internal representation is enabled.\n", fcn->name);
5764 return 0;
5765 }
Willy Tarreau87b09662015-04-03 00:22:06 +02005766 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01005767 * Lua context can be not initialized. This behavior
5768 * permits to save performances because a systematic
5769 * Lua initialization cause 5% performances loss.
5770 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005771 if (!stream->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01005772 stream->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005773 if (!stream->hlua) {
5774 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
5775 return 0;
5776 }
5777 if (!hlua_ctx_init(stream->hlua, stream->task)) {
5778 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
5779 return 0;
5780 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01005781 }
5782
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005783 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005784 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005785
5786 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005787 if (!SET_SAFE_LJMP(stream->hlua->T)) {
5788 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
5789 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01005790 else
5791 error = "critical error";
5792 SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005793 return 0;
5794 }
5795
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005796 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005797 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005798 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005799 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005800 return 0;
5801 }
5802
5803 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005804 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005805
5806 /* convert input sample and pust-it in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005807 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005808 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005809 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005810 return 0;
5811 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005812 hlua_smp2lua(stream->hlua->T, smp);
5813 stream->hlua->nargs = 1;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005814
5815 /* push keywords in the stack. */
5816 if (arg_p) {
5817 for (; arg_p->type != ARGT_STOP; arg_p++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005818 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005819 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005820 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005821 return 0;
5822 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005823 hlua_arg2lua(stream->hlua->T, arg_p);
5824 stream->hlua->nargs++;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005825 }
5826 }
5827
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005828 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005829 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005830
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005831 /* At this point the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005832 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005833 }
5834
5835 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005836 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005837 /* finished. */
5838 case HLUA_E_OK:
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02005839 /* If the stack is empty, the function fails. */
5840 if (lua_gettop(stream->hlua->T) <= 0)
5841 return 0;
5842
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005843 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005844 hlua_lua2smp(stream->hlua->T, -1, smp);
5845 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005846 return 1;
5847
5848 /* yield. */
5849 case HLUA_E_AGAIN:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005850 SEND_ERR(stream->be, "Lua converter '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005851 return 0;
5852
5853 /* finished with error. */
5854 case HLUA_E_ERRMSG:
5855 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005856 SEND_ERR(stream->be, "Lua converter '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005857 fcn->name, lua_tostring(stream->hlua->T, -1));
5858 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005859 return 0;
5860
Thierry Fournierd5b073c2018-05-21 19:42:47 +02005861 case HLUA_E_ETMOUT:
5862 SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name);
5863 return 0;
5864
5865 case HLUA_E_NOMEM:
5866 SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name);
5867 return 0;
5868
5869 case HLUA_E_YIELD:
5870 SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name);
5871 return 0;
5872
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005873 case HLUA_E_ERR:
5874 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005875 SEND_ERR(stream->be, "Lua converter '%s' returns an unknown error.\n", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005876
5877 default:
5878 return 0;
5879 }
5880}
5881
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005882/* Wrapper called by HAProxy to execute a sample-fetch. this wrapper
5883 * doesn't allow "yield" functions because the HAProxy engine cannot
Willy Tarreaube508f12016-03-10 11:47:01 +01005884 * resume sample-fetches. This function will be called by the sample
5885 * fetch engine to call lua-based fetch operations.
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005886 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +02005887static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp,
5888 const char *kw, void *private)
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005889{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02005890 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02005891 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01005892 const char *error;
Willy Tarreau83061a82018-07-13 11:56:34 +02005893 const struct buffer msg = { };
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005894
Willy Tarreaube508f12016-03-10 11:47:01 +01005895 if (!stream)
5896 return 0;
Christopher Fauletafd8f102018-11-08 11:34:21 +01005897
5898 if (IS_HTX_STRM(stream)) {
5899 SEND_ERR(stream->be, "Lua sample-fetch '%s': Lua fetches cannot be used when the"
5900 " HTX internal representation is enabled.\n", fcn->name);
5901 return 0;
5902 }
Willy Tarreaube508f12016-03-10 11:47:01 +01005903
Willy Tarreau87b09662015-04-03 00:22:06 +02005904 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01005905 * Lua context can be not initialized. This behavior
5906 * permits to save performances because a systematic
5907 * Lua initialization cause 5% performances loss.
5908 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005909 if (!stream->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01005910 stream->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005911 if (!stream->hlua) {
5912 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
5913 return 0;
5914 }
5915 if (!hlua_ctx_init(stream->hlua, stream->task)) {
5916 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
5917 return 0;
5918 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01005919 }
5920
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005921 consistency_set(stream, smp->opt, &stream->hlua->cons);
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005922
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005923 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005924 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005925
5926 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005927 if (!SET_SAFE_LJMP(stream->hlua->T)) {
5928 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
5929 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01005930 else
5931 error = "critical error";
5932 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005933 return 0;
5934 }
5935
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005936 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005937 if (!lua_checkstack(stream->hlua->T, 2)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005938 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005939 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005940 return 0;
5941 }
5942
5943 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005944 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005945
5946 /* push arguments in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005947 if (!hlua_txn_new(stream->hlua->T, stream, smp->px, smp->opt & SMP_OPT_DIR,
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005948 HLUA_TXN_NOTERM)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005949 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005950 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005951 return 0;
5952 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005953 stream->hlua->nargs = 1;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005954
5955 /* push keywords in the stack. */
5956 for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) {
5957 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005958 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005959 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005960 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005961 return 0;
5962 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005963 hlua_arg2lua(stream->hlua->T, arg_p);
5964 stream->hlua->nargs++;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005965 }
5966
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005967 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005968 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005969
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005970 /* At this point the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005971 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005972 }
5973
5974 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005975 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005976 /* finished. */
5977 case HLUA_E_OK:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005978 if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) {
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005979 stream_int_retnclose(&stream->si[0], &msg);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02005980 return 0;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005981 }
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02005982 /* If the stack is empty, the function fails. */
5983 if (lua_gettop(stream->hlua->T) <= 0)
5984 return 0;
5985
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005986 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005987 hlua_lua2smp(stream->hlua->T, -1, smp);
5988 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005989
5990 /* Set the end of execution flag. */
5991 smp->flags &= ~SMP_F_MAY_CHANGE;
5992 return 1;
5993
5994 /* yield. */
5995 case HLUA_E_AGAIN:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005996 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005997 stream_int_retnclose(&stream->si[0], &msg);
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005998 SEND_ERR(smp->px, "Lua sample-fetch '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005999 return 0;
6000
6001 /* finished with error. */
6002 case HLUA_E_ERRMSG:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006003 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006004 stream_int_retnclose(&stream->si[0], &msg);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006005 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006006 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006007 fcn->name, lua_tostring(stream->hlua->T, -1));
6008 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006009 return 0;
6010
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006011 case HLUA_E_ETMOUT:
6012 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
6013 stream_int_retnclose(&stream->si[0], &msg);
6014 SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name);
6015 return 0;
6016
6017 case HLUA_E_NOMEM:
6018 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
6019 stream_int_retnclose(&stream->si[0], &msg);
6020 SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name);
6021 return 0;
6022
6023 case HLUA_E_YIELD:
6024 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
6025 stream_int_retnclose(&stream->si[0], &msg);
6026 SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name);
6027 return 0;
6028
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006029 case HLUA_E_ERR:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006030 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006031 stream_int_retnclose(&stream->si[0], &msg);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006032 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006033 SEND_ERR(smp->px, "Lua sample-fetch '%s' returns an unknown error.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006034
6035 default:
6036 return 0;
6037 }
6038}
6039
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006040/* This function is an LUA binding used for registering
6041 * "sample-conv" functions. It expects a converter name used
6042 * in the haproxy configuration file, and an LUA function.
6043 */
6044__LJMP static int hlua_register_converters(lua_State *L)
6045{
6046 struct sample_conv_kw_list *sck;
6047 const char *name;
6048 int ref;
6049 int len;
6050 struct hlua_function *fcn;
6051
6052 MAY_LJMP(check_args(L, 2, "register_converters"));
6053
6054 /* First argument : converter name. */
6055 name = MAY_LJMP(luaL_checkstring(L, 1));
6056
6057 /* Second argument : lua function. */
6058 ref = MAY_LJMP(hlua_checkfunction(L, 2));
6059
6060 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006061 sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006062 if (!sck)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006063 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006064 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006065 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006066 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006067
6068 /* Fill fcn. */
6069 fcn->name = strdup(name);
6070 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006071 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006072 fcn->function_ref = ref;
6073
6074 /* List head */
6075 sck->list.n = sck->list.p = NULL;
6076
6077 /* converter keyword. */
6078 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006079 sck->kw[0].kw = calloc(1, len);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006080 if (!sck->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006081 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006082
6083 snprintf((char *)sck->kw[0].kw, len, "lua.%s", name);
6084 sck->kw[0].process = hlua_sample_conv_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01006085 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 +01006086 sck->kw[0].val_args = NULL;
6087 sck->kw[0].in_type = SMP_T_STR;
6088 sck->kw[0].out_type = SMP_T_STR;
6089 sck->kw[0].private = fcn;
6090
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006091 /* Register this new converter */
6092 sample_register_convs(sck);
6093
6094 return 0;
6095}
6096
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006097/* This function is an LUA binding used for registering
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006098 * "sample-fetch" functions. It expects a converter name used
6099 * in the haproxy configuration file, and an LUA function.
6100 */
6101__LJMP static int hlua_register_fetches(lua_State *L)
6102{
6103 const char *name;
6104 int ref;
6105 int len;
6106 struct sample_fetch_kw_list *sfk;
6107 struct hlua_function *fcn;
6108
6109 MAY_LJMP(check_args(L, 2, "register_fetches"));
6110
6111 /* First argument : sample-fetch name. */
6112 name = MAY_LJMP(luaL_checkstring(L, 1));
6113
6114 /* Second argument : lua function. */
6115 ref = MAY_LJMP(hlua_checkfunction(L, 2));
6116
6117 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006118 sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006119 if (!sfk)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006120 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006121 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006122 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006123 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006124
6125 /* Fill fcn. */
6126 fcn->name = strdup(name);
6127 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006128 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006129 fcn->function_ref = ref;
6130
6131 /* List head */
6132 sfk->list.n = sfk->list.p = NULL;
6133
6134 /* sample-fetch keyword. */
6135 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006136 sfk->kw[0].kw = calloc(1, len);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006137 if (!sfk->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006138 return luaL_error(L, "Lua out of memory error.");
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006139
6140 snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name);
6141 sfk->kw[0].process = hlua_sample_fetch_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01006142 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 +01006143 sfk->kw[0].val_args = NULL;
6144 sfk->kw[0].out_type = SMP_T_STR;
6145 sfk->kw[0].use = SMP_USE_HTTP_ANY;
6146 sfk->kw[0].val = 0;
6147 sfk->kw[0].private = fcn;
6148
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006149 /* Register this new fetch. */
6150 sample_register_fetches(sfk);
6151
6152 return 0;
6153}
6154
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006155/* This function is a wrapper to execute each LUA function declared
6156 * as an action wrapper during the initialisation period. This function
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006157 * return ACT_RET_CONT if the processing is finished (with or without
6158 * error) and return ACT_RET_YIELD if the function must be called again
6159 * because the LUA returns a yield.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006160 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006161static enum act_return hlua_action(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +02006162 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006163{
6164 char **arg;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006165 unsigned int analyzer;
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02006166 int dir;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006167 const char *error;
Willy Tarreau83061a82018-07-13 11:56:34 +02006168 const struct buffer msg = { };
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006169
6170 switch (rule->from) {
Thierry FOURNIER6e01f382015-11-02 09:52:54 +01006171 case ACT_F_TCP_REQ_CNT: analyzer = AN_REQ_INSPECT_FE ; dir = SMP_OPT_DIR_REQ; break;
6172 case ACT_F_TCP_RES_CNT: analyzer = AN_RES_INSPECT ; dir = SMP_OPT_DIR_RES; break;
6173 case ACT_F_HTTP_REQ: analyzer = AN_REQ_HTTP_PROCESS_FE; dir = SMP_OPT_DIR_REQ; break;
6174 case ACT_F_HTTP_RES: analyzer = AN_RES_HTTP_PROCESS_BE; dir = SMP_OPT_DIR_RES; break;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006175 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006176 SEND_ERR(px, "Lua: internal error while execute action.\n");
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006177 return ACT_RET_CONT;
6178 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006179
Willy Tarreau87b09662015-04-03 00:22:06 +02006180 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006181 * Lua context can be not initialized. This behavior
6182 * permits to save performances because a systematic
6183 * Lua initialization cause 5% performances loss.
6184 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006185 if (!s->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006186 s->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006187 if (!s->hlua) {
6188 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
6189 rule->arg.hlua_rule->fcn.name);
6190 return ACT_RET_CONT;
6191 }
6192 if (!hlua_ctx_init(s->hlua, s->task)) {
6193 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
6194 rule->arg.hlua_rule->fcn.name);
6195 return ACT_RET_CONT;
6196 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006197 }
6198
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006199 consistency_set(s, dir, &s->hlua->cons);
6200
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006201 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006202 if (!HLUA_IS_RUNNING(s->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006203
6204 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006205 if (!SET_SAFE_LJMP(s->hlua->T)) {
6206 if (lua_type(s->hlua->T, -1) == LUA_TSTRING)
6207 error = lua_tostring(s->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006208 else
6209 error = "critical error";
6210 SEND_ERR(px, "Lua function '%s': %s.\n",
6211 rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006212 return ACT_RET_CONT;
6213 }
6214
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006215 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006216 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006217 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006218 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006219 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006220 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006221 }
6222
6223 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006224 lua_rawgeti(s->hlua->T, LUA_REGISTRYINDEX, rule->arg.hlua_rule->fcn.function_ref);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006225
Willy Tarreau87b09662015-04-03 00:22:06 +02006226 /* Create and and push object stream in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006227 if (!hlua_txn_new(s->hlua->T, s, px, dir, 0)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006228 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006229 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006230 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006231 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006232 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006233 s->hlua->nargs = 1;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006234
6235 /* push keywords in the stack. */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006236 for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006237 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006238 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006239 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006240 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006241 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006242 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006243 lua_pushstring(s->hlua->T, *arg);
6244 s->hlua->nargs++;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006245 }
6246
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006247 /* Now the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006248 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006249
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006250 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006251 s->hlua->max_time = hlua_timeout_session;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006252 }
6253
6254 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006255 switch (hlua_ctx_resume(s->hlua, !(flags & ACT_FLAG_FINAL))) {
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006256 /* finished. */
6257 case HLUA_E_OK:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006258 if (!consistency_check(s, dir, &s->hlua->cons)) {
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006259 stream_int_retnclose(&s->si[0], &msg);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02006260 return ACT_RET_ERR;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006261 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006262 if (s->hlua->flags & HLUA_STOP)
Thierry FOURNIER9bd52d42016-07-14 11:45:33 +02006263 return ACT_RET_STOP;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006264 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006265
6266 /* yield. */
6267 case HLUA_E_AGAIN:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006268 /* Set timeout in the required channel. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006269 if (s->hlua->wake_time != TICK_ETERNITY) {
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006270 if (analyzer & (AN_REQ_INSPECT_FE|AN_REQ_HTTP_PROCESS_FE))
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006271 s->req.analyse_exp = s->hlua->wake_time;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006272 else if (analyzer & (AN_RES_INSPECT|AN_RES_HTTP_PROCESS_BE))
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006273 s->res.analyse_exp = s->hlua->wake_time;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006274 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006275 /* Some actions can be wake up when a "write" event
6276 * is detected on a response channel. This is useful
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006277 * only for actions targeted on the requests.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006278 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006279 if (HLUA_IS_WAKERESWR(s->hlua)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006280 s->res.flags |= CF_WAKE_WRITE;
Willy Tarreau76bd97f2015-03-10 17:16:10 +01006281 if ((analyzer & (AN_REQ_INSPECT_FE|AN_REQ_HTTP_PROCESS_FE)))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006282 s->res.analysers |= analyzer;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006283 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006284 if (HLUA_IS_WAKEREQWR(s->hlua))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006285 s->req.flags |= CF_WAKE_WRITE;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006286 /* We can quit the function without consistency check
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006287 * because HAProxy is not able to manipulate data, it
6288 * is only allowed to call me again. */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006289 return ACT_RET_YIELD;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006290
6291 /* finished with error. */
6292 case HLUA_E_ERRMSG:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006293 if (!consistency_check(s, dir, &s->hlua->cons)) {
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006294 stream_int_retnclose(&s->si[0], &msg);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02006295 return ACT_RET_ERR;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006296 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006297 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006298 SEND_ERR(px, "Lua function '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006299 rule->arg.hlua_rule->fcn.name, lua_tostring(s->hlua->T, -1));
6300 lua_pop(s->hlua->T, 1);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006301 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006302
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006303 case HLUA_E_ETMOUT:
6304 if (!consistency_check(s, dir, &s->hlua->cons)) {
6305 stream_int_retnclose(&s->si[0], &msg);
6306 return ACT_RET_ERR;
6307 }
6308 SEND_ERR(px, "Lua function '%s': execution timeout.\n", rule->arg.hlua_rule->fcn.name);
6309 return 0;
6310
6311 case HLUA_E_NOMEM:
6312 if (!consistency_check(s, dir, &s->hlua->cons)) {
6313 stream_int_retnclose(&s->si[0], &msg);
6314 return ACT_RET_ERR;
6315 }
6316 SEND_ERR(px, "Lua function '%s': out of memory error.\n", rule->arg.hlua_rule->fcn.name);
6317 return 0;
6318
6319 case HLUA_E_YIELD:
6320 if (!consistency_check(s, dir, &s->hlua->cons)) {
6321 stream_int_retnclose(&s->si[0], &msg);
6322 return ACT_RET_ERR;
6323 }
6324 SEND_ERR(px, "Lua function '%s': aborting Lua processing on expired timeout.\n",
6325 rule->arg.hlua_rule->fcn.name);
6326 return 0;
6327
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006328 case HLUA_E_ERR:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006329 if (!consistency_check(s, dir, &s->hlua->cons)) {
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006330 stream_int_retnclose(&s->si[0], &msg);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02006331 return ACT_RET_ERR;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006332 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006333 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006334 SEND_ERR(px, "Lua function '%s' return an unknown error.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006335 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006336
6337 default:
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006338 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006339 }
6340}
6341
Olivier Houchard9f6af332018-05-25 14:04:04 +02006342struct task *hlua_applet_wakeup(struct task *t, void *context, unsigned short state)
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006343{
Olivier Houchard9f6af332018-05-25 14:04:04 +02006344 struct appctx *ctx = context;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006345
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006346 appctx_wakeup(ctx);
Willy Tarreaud9587412017-08-23 16:07:33 +02006347 t->expire = TICK_ETERNITY;
Willy Tarreaud1aa41f2017-07-21 16:41:56 +02006348 return t;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006349}
6350
6351static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
6352{
6353 struct stream_interface *si = ctx->owner;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006354 struct hlua *hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006355 struct task *task;
6356 char **arg;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006357 const char *error;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006358
Willy Tarreaubafbe012017-11-24 17:34:44 +01006359 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006360 if (!hlua) {
6361 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
6362 ctx->rule->arg.hlua_rule->fcn.name);
6363 return 0;
6364 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006365 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006366 ctx->ctx.hlua_apptcp.hlua = hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006367 ctx->ctx.hlua_apptcp.flags = 0;
6368
6369 /* Create task used by signal to wakeup applets. */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01006370 task = task_new(tid_bit);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006371 if (!task) {
6372 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
6373 ctx->rule->arg.hlua_rule->fcn.name);
6374 return 0;
6375 }
6376 task->nice = 0;
6377 task->context = ctx;
6378 task->process = hlua_applet_wakeup;
6379 ctx->ctx.hlua_apptcp.task = task;
6380
6381 /* In the execution wrappers linked with a stream, the
6382 * Lua context can be not initialized. This behavior
6383 * permits to save performances because a systematic
6384 * Lua initialization cause 5% performances loss.
6385 */
6386 if (!hlua_ctx_init(hlua, task)) {
6387 SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n",
6388 ctx->rule->arg.hlua_rule->fcn.name);
6389 return 0;
6390 }
6391
6392 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02006393 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006394
6395 /* The following Lua calls can fail. */
6396 if (!SET_SAFE_LJMP(hlua->T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01006397 if (lua_type(hlua->T, -1) == LUA_TSTRING)
6398 error = lua_tostring(hlua->T, -1);
6399 else
6400 error = "critical error";
6401 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
6402 ctx->rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006403 return 0;
6404 }
6405
6406 /* Check stack available size. */
6407 if (!lua_checkstack(hlua->T, 1)) {
6408 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
6409 ctx->rule->arg.hlua_rule->fcn.name);
6410 RESET_SAFE_LJMP(hlua->T);
6411 return 0;
6412 }
6413
6414 /* Restore the function in the stack. */
6415 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref);
6416
6417 /* Create and and push object stream in the stack. */
6418 if (!hlua_applet_tcp_new(hlua->T, ctx)) {
6419 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
6420 ctx->rule->arg.hlua_rule->fcn.name);
6421 RESET_SAFE_LJMP(hlua->T);
6422 return 0;
6423 }
6424 hlua->nargs = 1;
6425
6426 /* push keywords in the stack. */
6427 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
6428 if (!lua_checkstack(hlua->T, 1)) {
6429 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
6430 ctx->rule->arg.hlua_rule->fcn.name);
6431 RESET_SAFE_LJMP(hlua->T);
6432 return 0;
6433 }
6434 lua_pushstring(hlua->T, *arg);
6435 hlua->nargs++;
6436 }
6437
6438 RESET_SAFE_LJMP(hlua->T);
6439
6440 /* Wakeup the applet ASAP. */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01006441 si_cant_get(si);
Willy Tarreau3367d412018-11-15 10:57:41 +01006442 si_rx_endp_more(si);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006443
6444 return 1;
6445}
6446
6447static void hlua_applet_tcp_fct(struct appctx *ctx)
6448{
6449 struct stream_interface *si = ctx->owner;
6450 struct stream *strm = si_strm(si);
6451 struct channel *res = si_ic(si);
6452 struct act_rule *rule = ctx->rule;
6453 struct proxy *px = strm->be;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006454 struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006455
6456 /* The applet execution is already done. */
Olivier Houchard594c8c52018-08-28 14:41:31 +02006457 if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) {
6458 /* eat the whole request */
6459 co_skip(si_oc(si), co_data(si_oc(si)));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006460 return;
Olivier Houchard594c8c52018-08-28 14:41:31 +02006461 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006462
6463 /* If the stream is disconnect or closed, ldo nothing. */
6464 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
6465 return;
6466
6467 /* Execute the function. */
6468 switch (hlua_ctx_resume(hlua, 1)) {
6469 /* finished. */
6470 case HLUA_E_OK:
6471 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
6472
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006473 /* eat the whole request */
Willy Tarreaua79021a2018-06-15 18:07:57 +02006474 co_skip(si_oc(si), co_data(si_oc(si)));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006475 res->flags |= CF_READ_NULL;
6476 si_shutr(si);
6477 return;
6478
6479 /* yield. */
6480 case HLUA_E_AGAIN:
Thierry Fournier0164f202016-02-20 17:47:43 +01006481 if (hlua->wake_time != TICK_ETERNITY)
6482 task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006483 return;
6484
6485 /* finished with error. */
6486 case HLUA_E_ERRMSG:
6487 /* Display log. */
6488 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
6489 rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1));
6490 lua_pop(hlua->T, 1);
6491 goto error;
6492
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006493 case HLUA_E_ETMOUT:
6494 SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n",
6495 rule->arg.hlua_rule->fcn.name);
6496 goto error;
6497
6498 case HLUA_E_NOMEM:
6499 SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n",
6500 rule->arg.hlua_rule->fcn.name);
6501 goto error;
6502
6503 case HLUA_E_YIELD: /* unexpected */
6504 SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n",
6505 rule->arg.hlua_rule->fcn.name);
6506 goto error;
6507
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006508 case HLUA_E_ERR:
6509 /* Display log. */
6510 SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n",
6511 rule->arg.hlua_rule->fcn.name);
6512 goto error;
6513
6514 default:
6515 goto error;
6516 }
6517
6518error:
6519
6520 /* For all other cases, just close the stream. */
6521 si_shutw(si);
6522 si_shutr(si);
6523 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
6524}
6525
6526static void hlua_applet_tcp_release(struct appctx *ctx)
6527{
Willy Tarreaubd7fc952017-07-24 17:35:27 +02006528 task_delete(ctx->ctx.hlua_apptcp.task);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006529 task_free(ctx->ctx.hlua_apptcp.task);
6530 ctx->ctx.hlua_apptcp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006531 hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006532 ctx->ctx.hlua_apptcp.hlua = NULL;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006533}
6534
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006535/* The function returns 1 if the initialisation is complete, 0 if
6536 * an errors occurs and -1 if more data are required for initializing
6537 * the applet.
6538 */
6539static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
6540{
6541 struct stream_interface *si = ctx->owner;
6542 struct channel *req = si_oc(si);
6543 struct http_msg *msg;
6544 struct http_txn *txn;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006545 struct hlua *hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006546 char **arg;
6547 struct hdr_ctx hdr;
6548 struct task *task;
6549 struct sample smp; /* just used for a valid call to smp_prefetch_http. */
Thierry Fournierfd107a22016-02-19 19:57:23 +01006550 const char *error;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006551
6552 /* Wait for a full HTTP request. */
6553 if (!smp_prefetch_http(px, strm, 0, NULL, &smp, 0)) {
6554 if (smp.flags & SMP_F_MAY_CHANGE)
6555 return -1;
6556 return 0;
6557 }
6558 txn = strm->txn;
6559 msg = &txn->req;
6560
Willy Tarreau0078bfc2015-10-07 20:20:28 +02006561 /* We want two things in HTTP mode :
6562 * - enforce server-close mode if we were in keep-alive, so that the
6563 * applet is released after each response ;
6564 * - enable request body transfer to the applet in order to resync
6565 * with the response body.
6566 */
6567 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)
6568 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreau0078bfc2015-10-07 20:20:28 +02006569
Willy Tarreaubafbe012017-11-24 17:34:44 +01006570 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006571 if (!hlua) {
6572 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
6573 ctx->rule->arg.hlua_rule->fcn.name);
6574 return 0;
6575 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006576 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006577 ctx->ctx.hlua_apphttp.hlua = hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006578 ctx->ctx.hlua_apphttp.left_bytes = -1;
6579 ctx->ctx.hlua_apphttp.flags = 0;
6580
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +01006581 if (txn->req.flags & HTTP_MSGF_VER_11)
6582 ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11;
6583
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006584 /* Create task used by signal to wakeup applets. */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01006585 task = task_new(tid_bit);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006586 if (!task) {
6587 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
6588 ctx->rule->arg.hlua_rule->fcn.name);
6589 return 0;
6590 }
6591 task->nice = 0;
6592 task->context = ctx;
6593 task->process = hlua_applet_wakeup;
6594 ctx->ctx.hlua_apphttp.task = task;
6595
6596 /* In the execution wrappers linked with a stream, the
6597 * Lua context can be not initialized. This behavior
6598 * permits to save performances because a systematic
6599 * Lua initialization cause 5% performances loss.
6600 */
6601 if (!hlua_ctx_init(hlua, task)) {
6602 SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n",
6603 ctx->rule->arg.hlua_rule->fcn.name);
6604 return 0;
6605 }
6606
6607 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02006608 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006609
6610 /* The following Lua calls can fail. */
6611 if (!SET_SAFE_LJMP(hlua->T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01006612 if (lua_type(hlua->T, -1) == LUA_TSTRING)
6613 error = lua_tostring(hlua->T, -1);
6614 else
6615 error = "critical error";
6616 SEND_ERR(px, "Lua applet http '%s': %s.\n",
6617 ctx->rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006618 return 0;
6619 }
6620
6621 /* Check stack available size. */
6622 if (!lua_checkstack(hlua->T, 1)) {
6623 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
6624 ctx->rule->arg.hlua_rule->fcn.name);
6625 RESET_SAFE_LJMP(hlua->T);
6626 return 0;
6627 }
6628
6629 /* Restore the function in the stack. */
6630 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref);
6631
6632 /* Create and and push object stream in the stack. */
6633 if (!hlua_applet_http_new(hlua->T, ctx)) {
6634 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
6635 ctx->rule->arg.hlua_rule->fcn.name);
6636 RESET_SAFE_LJMP(hlua->T);
6637 return 0;
6638 }
6639 hlua->nargs = 1;
6640
6641 /* Look for a 100-continue expected. */
6642 if (msg->flags & HTTP_MSGF_VER_11) {
6643 hdr.idx = 0;
Willy Tarreaua79021a2018-06-15 18:07:57 +02006644 if (http_find_header2("Expect", 6, ci_head(req), &txn->hdr_idx, &hdr) &&
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006645 unlikely(hdr.vlen == 12 && strncasecmp(hdr.line+hdr.val, "100-continue", 12) == 0))
6646 ctx->ctx.hlua_apphttp.flags |= APPLET_100C;
6647 }
6648
6649 /* push keywords in the stack. */
6650 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
6651 if (!lua_checkstack(hlua->T, 1)) {
6652 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
6653 ctx->rule->arg.hlua_rule->fcn.name);
6654 RESET_SAFE_LJMP(hlua->T);
6655 return 0;
6656 }
6657 lua_pushstring(hlua->T, *arg);
6658 hlua->nargs++;
6659 }
6660
6661 RESET_SAFE_LJMP(hlua->T);
6662
6663 /* Wakeup the applet when data is ready for read. */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01006664 si_cant_get(si);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006665
6666 return 1;
6667}
6668
6669static void hlua_applet_http_fct(struct appctx *ctx)
6670{
6671 struct stream_interface *si = ctx->owner;
6672 struct stream *strm = si_strm(si);
6673 struct channel *res = si_ic(si);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006674 struct act_rule *rule = ctx->rule;
6675 struct proxy *px = strm->be;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006676 struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua;
Willy Tarreau206ba832018-06-14 15:27:31 +02006677 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02006678 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02006679 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02006680 size_t len2;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006681 int ret;
6682
6683 /* If the stream is disconnect or closed, ldo nothing. */
6684 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
6685 return;
6686
6687 /* Set the currently running flag. */
6688 if (!HLUA_IS_RUNNING(hlua) &&
6689 !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
6690
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006691 /* Wait for full HTTP analysys. */
6692 if (unlikely(strm->txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01006693 si_cant_get(si);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006694 return;
6695 }
6696
6697 /* Store the max amount of bytes that we can read. */
6698 ctx->ctx.hlua_apphttp.left_bytes = strm->txn->req.body_len;
6699
6700 /* We need to flush the request header. This left the body
6701 * for the Lua.
6702 */
6703
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006704 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02006705 ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006706 if (ret == -1)
6707 return;
6708
6709 /* No data available, ask for more data. */
6710 if (ret == 1)
6711 len2 = 0;
6712 if (ret == 0)
6713 len1 = 0;
Thierry FOURNIER70d318c2018-06-30 10:37:33 +02006714 if (len1 + len2 < strm->txn->req.eoh + strm->txn->req.eol) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01006715 si_cant_get(si);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006716 return;
6717 }
6718
6719 /* skip the requests bytes. */
Thierry FOURNIER70d318c2018-06-30 10:37:33 +02006720 co_skip(si_oc(si), strm->txn->req.eoh + strm->txn->req.eol);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006721 }
6722
6723 /* Executes The applet if it is not done. */
6724 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
6725
6726 /* Execute the function. */
6727 switch (hlua_ctx_resume(hlua, 1)) {
6728 /* finished. */
6729 case HLUA_E_OK:
6730 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
6731 break;
6732
6733 /* yield. */
6734 case HLUA_E_AGAIN:
Thierry Fournier0164f202016-02-20 17:47:43 +01006735 if (hlua->wake_time != TICK_ETERNITY)
6736 task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006737 return;
6738
6739 /* finished with error. */
6740 case HLUA_E_ERRMSG:
6741 /* Display log. */
6742 SEND_ERR(px, "Lua applet http '%s': %s.\n",
6743 rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1));
6744 lua_pop(hlua->T, 1);
6745 goto error;
6746
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006747 case HLUA_E_ETMOUT:
6748 SEND_ERR(px, "Lua applet http '%s': execution timeout.\n",
6749 rule->arg.hlua_rule->fcn.name);
6750 goto error;
6751
6752 case HLUA_E_NOMEM:
6753 SEND_ERR(px, "Lua applet http '%s': out of memory error.\n",
6754 rule->arg.hlua_rule->fcn.name);
6755 goto error;
6756
6757 case HLUA_E_YIELD: /* unexpected */
6758 SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n",
6759 rule->arg.hlua_rule->fcn.name);
6760 goto error;
6761
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006762 case HLUA_E_ERR:
6763 /* Display log. */
6764 SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n",
6765 rule->arg.hlua_rule->fcn.name);
6766 goto error;
6767
6768 default:
6769 goto error;
6770 }
6771 }
6772
6773 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
6774
6775 /* We must send the final chunk. */
6776 if (ctx->ctx.hlua_apphttp.flags & APPLET_CHUNKED &&
6777 !(ctx->ctx.hlua_apphttp.flags & APPLET_LAST_CHK)) {
6778
6779 /* sent last chunk at once. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02006780 ret = ci_putblk(res, "0\r\n\r\n", 5);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006781
6782 /* critical error. */
6783 if (ret == -2 || ret == -3) {
6784 SEND_ERR(px, "Lua applet http '%s'cannont send last chunk.\n",
6785 rule->arg.hlua_rule->fcn.name);
6786 goto error;
6787 }
6788
6789 /* no enough space error. */
6790 if (ret == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01006791 si_rx_room_blk(si);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006792 return;
6793 }
6794
6795 /* set the last chunk sent. */
6796 ctx->ctx.hlua_apphttp.flags |= APPLET_LAST_CHK;
6797 }
6798
6799 /* close the connection. */
6800
Frédéric Lécaille83ed5d52018-07-18 14:25:26 +02006801 /* status */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006802 strm->txn->status = ctx->ctx.hlua_apphttp.status;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006803
6804 /* eat the whole request */
Willy Tarreaua79021a2018-06-15 18:07:57 +02006805 co_skip(si_oc(si), co_data(si_oc(si)));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006806 res->flags |= CF_READ_NULL;
6807 si_shutr(si);
6808
6809 return;
6810 }
6811
6812error:
6813
6814 /* If we are in HTTP mode, and we are not send any
6815 * data, return a 500 server error in best effort:
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006816 * if there is no room available in the buffer,
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006817 * just close the connection.
6818 */
Willy Tarreau06d80a92017-10-19 14:32:15 +02006819 ci_putblk(res, error_500, strlen(error_500));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006820 if (!(strm->flags & SF_ERR_MASK))
6821 strm->flags |= SF_ERR_RESOURCE;
6822 si_shutw(si);
6823 si_shutr(si);
6824 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
6825}
6826
6827static void hlua_applet_http_release(struct appctx *ctx)
6828{
Willy Tarreaubd7fc952017-07-24 17:35:27 +02006829 task_delete(ctx->ctx.hlua_apphttp.task);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006830 task_free(ctx->ctx.hlua_apphttp.task);
6831 ctx->ctx.hlua_apphttp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006832 hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006833 ctx->ctx.hlua_apphttp.hlua = NULL;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006834}
6835
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006836/* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in
6837 * succes case, else return ACT_RET_PRS_ERR.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006838 *
6839 * This function can fail with an abort() due to an Lua critical error.
6840 * We are in the configuration parsing process of HAProxy, this abort() is
6841 * tolerated.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006842 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006843static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px,
6844 struct act_rule *rule, char **err)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006845{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006846 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006847 int i;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006848
Christopher Fauletafd8f102018-11-08 11:34:21 +01006849 if (px->options2 & PR_O2_USE_HTX) {
6850 memprintf(err, "Lua actions cannot be used when the HTX internal representation is enabled");
6851 return ACT_RET_PRS_ERR;
6852 }
6853
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006854 /* Memory for the rule. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006855 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006856 if (!rule->arg.hlua_rule) {
6857 memprintf(err, "out of memory error");
Thierry FOURNIERafa80492015-08-19 09:04:15 +02006858 return ACT_RET_PRS_ERR;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006859 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006860
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006861 /* Memory for arguments. */
6862 rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, sizeof(char *));
6863 if (!rule->arg.hlua_rule->args) {
6864 memprintf(err, "out of memory error");
6865 return ACT_RET_PRS_ERR;
6866 }
6867
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006868 /* Reference the Lua function and store the reference. */
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006869 rule->arg.hlua_rule->fcn = *fcn;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006870
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006871 /* Expect some arguments */
6872 for (i = 0; i < fcn->nargs; i++) {
6873 if (*args[i+1] == '\0') {
6874 memprintf(err, "expect %d arguments", fcn->nargs);
6875 return ACT_RET_PRS_ERR;
6876 }
6877 rule->arg.hlua_rule->args[i] = strdup(args[i + 1]);
6878 if (!rule->arg.hlua_rule->args[i]) {
6879 memprintf(err, "out of memory error");
6880 return ACT_RET_PRS_ERR;
6881 }
6882 (*cur_arg)++;
6883 }
6884 rule->arg.hlua_rule->args[i] = NULL;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006885
Thierry FOURNIER42148732015-09-02 17:17:33 +02006886 rule->action = ACT_CUSTOM;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006887 rule->action_ptr = hlua_action;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02006888 return ACT_RET_PRS_OK;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006889}
6890
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006891static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px,
6892 struct act_rule *rule, char **err)
6893{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006894 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006895
Christopher Fauletafd8f102018-11-08 11:34:21 +01006896 if (px->options2 & PR_O2_USE_HTX) {
6897 memprintf(err, "Lua services cannot be used when the HTX internal representation is enabled");
6898 return ACT_RET_PRS_ERR;
6899 }
6900
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01006901 /* HTTP applets are forbidden in tcp-request rules.
6902 * HTTP applet request requires everything initilized by
6903 * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER).
6904 * The applet will be immediately initilized, but its before
6905 * the call of this analyzer.
6906 */
6907 if (rule->from != ACT_F_HTTP_REQ) {
6908 memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets");
6909 return ACT_RET_PRS_ERR;
6910 }
6911
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006912 /* Memory for the rule. */
6913 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
6914 if (!rule->arg.hlua_rule) {
6915 memprintf(err, "out of memory error");
6916 return ACT_RET_PRS_ERR;
6917 }
6918
6919 /* Reference the Lua function and store the reference. */
6920 rule->arg.hlua_rule->fcn = *fcn;
6921
6922 /* TODO: later accept arguments. */
6923 rule->arg.hlua_rule->args = NULL;
6924
6925 /* Add applet pointer in the rule. */
6926 rule->applet.obj_type = OBJ_TYPE_APPLET;
6927 rule->applet.name = fcn->name;
6928 rule->applet.init = hlua_applet_http_init;
6929 rule->applet.fct = hlua_applet_http_fct;
6930 rule->applet.release = hlua_applet_http_release;
6931 rule->applet.timeout = hlua_timeout_applet;
6932
6933 return ACT_RET_PRS_OK;
6934}
6935
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006936/* This function is an LUA binding used for registering
6937 * "sample-conv" functions. It expects a converter name used
6938 * in the haproxy configuration file, and an LUA function.
6939 */
6940__LJMP static int hlua_register_action(lua_State *L)
6941{
6942 struct action_kw_list *akl;
6943 const char *name;
6944 int ref;
6945 int len;
6946 struct hlua_function *fcn;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006947 int nargs;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006948
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006949 /* Initialise the number of expected arguments at 0. */
6950 nargs = 0;
6951
6952 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
6953 WILL_LJMP(luaL_error(L, "'register_action' needs between 3 and 4 arguments"));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006954
6955 /* First argument : converter name. */
6956 name = MAY_LJMP(luaL_checkstring(L, 1));
6957
6958 /* Second argument : environment. */
6959 if (lua_type(L, 2) != LUA_TTABLE)
6960 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
6961
6962 /* Third argument : lua function. */
6963 ref = MAY_LJMP(hlua_checkfunction(L, 3));
6964
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006965 /* Fourth argument : number of mandatory arguments expected on the configuration line. */
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006966 if (lua_gettop(L) >= 4)
6967 nargs = MAY_LJMP(luaL_checkinteger(L, 4));
6968
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006969 /* browse the second argument as an array. */
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006970 lua_pushnil(L);
6971 while (lua_next(L, 2) != 0) {
6972 if (lua_type(L, -1) != LUA_TSTRING)
6973 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
6974
6975 /* Check required environment. Only accepted "http" or "tcp". */
6976 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006977 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006978 if (!akl)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006979 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006980 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006981 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006982 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006983
6984 /* Fill fcn. */
6985 fcn->name = strdup(name);
6986 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006987 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006988 fcn->function_ref = ref;
6989
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006990 /* Set the expected number od arguments. */
6991 fcn->nargs = nargs;
6992
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006993 /* List head */
6994 akl->list.n = akl->list.p = NULL;
6995
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006996 /* action keyword. */
6997 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006998 akl->kw[0].kw = calloc(1, len);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006999 if (!akl->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007000 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007001
7002 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
7003
7004 akl->kw[0].match_pfx = 0;
7005 akl->kw[0].private = fcn;
7006 akl->kw[0].parse = action_register_lua;
7007
7008 /* select the action registering point. */
7009 if (strcmp(lua_tostring(L, -1), "tcp-req") == 0)
7010 tcp_req_cont_keywords_register(akl);
7011 else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0)
7012 tcp_res_cont_keywords_register(akl);
7013 else if (strcmp(lua_tostring(L, -1), "http-req") == 0)
7014 http_req_keywords_register(akl);
7015 else if (strcmp(lua_tostring(L, -1), "http-res") == 0)
7016 http_res_keywords_register(akl);
7017 else
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007018 WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. "
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007019 "'tcp-req', 'tcp-res', 'http-req' or 'http-res' "
7020 "are expected.", lua_tostring(L, -1)));
7021
7022 /* pop the environment string. */
7023 lua_pop(L, 1);
7024 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007025 return ACT_RET_PRS_OK;
7026}
7027
7028static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px,
7029 struct act_rule *rule, char **err)
7030{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007031 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007032
Christopher Fauletafd8f102018-11-08 11:34:21 +01007033 if (px->options2 & PR_O2_USE_HTX) {
7034 memprintf(err, "Lua services cannot be used when the HTX internal representation is enabled");
7035 return ACT_RET_PRS_ERR;
7036 }
7037
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007038 /* Memory for the rule. */
7039 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
7040 if (!rule->arg.hlua_rule) {
7041 memprintf(err, "out of memory error");
7042 return ACT_RET_PRS_ERR;
7043 }
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007044
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007045 /* Reference the Lua function and store the reference. */
7046 rule->arg.hlua_rule->fcn = *fcn;
7047
7048 /* TODO: later accept arguments. */
7049 rule->arg.hlua_rule->args = NULL;
7050
7051 /* Add applet pointer in the rule. */
7052 rule->applet.obj_type = OBJ_TYPE_APPLET;
7053 rule->applet.name = fcn->name;
7054 rule->applet.init = hlua_applet_tcp_init;
7055 rule->applet.fct = hlua_applet_tcp_fct;
7056 rule->applet.release = hlua_applet_tcp_release;
7057 rule->applet.timeout = hlua_timeout_applet;
7058
7059 return 0;
7060}
7061
7062/* This function is an LUA binding used for registering
7063 * "sample-conv" functions. It expects a converter name used
7064 * in the haproxy configuration file, and an LUA function.
7065 */
7066__LJMP static int hlua_register_service(lua_State *L)
7067{
7068 struct action_kw_list *akl;
7069 const char *name;
7070 const char *env;
7071 int ref;
7072 int len;
7073 struct hlua_function *fcn;
7074
7075 MAY_LJMP(check_args(L, 3, "register_service"));
7076
7077 /* First argument : converter name. */
7078 name = MAY_LJMP(luaL_checkstring(L, 1));
7079
7080 /* Second argument : environment. */
7081 env = MAY_LJMP(luaL_checkstring(L, 2));
7082
7083 /* Third argument : lua function. */
7084 ref = MAY_LJMP(hlua_checkfunction(L, 3));
7085
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007086 /* Allocate and fill the sample fetch keyword struct. */
7087 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
7088 if (!akl)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007089 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007090 fcn = calloc(1, sizeof(*fcn));
7091 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007092 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007093
7094 /* Fill fcn. */
7095 len = strlen("<lua.>") + strlen(name) + 1;
7096 fcn->name = calloc(1, len);
7097 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007098 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007099 snprintf((char *)fcn->name, len, "<lua.%s>", name);
7100 fcn->function_ref = ref;
7101
7102 /* List head */
7103 akl->list.n = akl->list.p = NULL;
7104
7105 /* converter keyword. */
7106 len = strlen("lua.") + strlen(name) + 1;
7107 akl->kw[0].kw = calloc(1, len);
7108 if (!akl->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007109 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007110
7111 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
7112
Thierry FOURNIER / OZON.IO02564fd2016-11-12 11:07:05 +01007113 /* Check required environment. Only accepted "http" or "tcp". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007114 if (strcmp(env, "tcp") == 0)
7115 akl->kw[0].parse = action_register_service_tcp;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007116 else if (strcmp(env, "http") == 0)
7117 akl->kw[0].parse = action_register_service_http;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007118 else
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007119 WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. "
Eric Salamafe7456f2017-12-21 14:30:07 +01007120 "'tcp' or 'http' are expected.", env));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007121
7122 akl->kw[0].match_pfx = 0;
7123 akl->kw[0].private = fcn;
7124
7125 /* End of array. */
7126 memset(&akl->kw[1], 0, sizeof(*akl->kw));
7127
7128 /* Register this new converter */
7129 service_keywords_register(akl);
7130
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007131 return 0;
7132}
7133
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007134/* This function initialises Lua cli handler. It copies the
7135 * arguments in the Lua stack and create channel IO objects.
7136 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02007137static int hlua_cli_parse_fct(char **args, char *payload, struct appctx *appctx, void *private)
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007138{
7139 struct hlua *hlua;
7140 struct hlua_function *fcn;
7141 int i;
7142 const char *error;
7143
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007144 fcn = private;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007145 appctx->ctx.hlua_cli.fcn = private;
7146
Willy Tarreaubafbe012017-11-24 17:34:44 +01007147 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007148 if (!hlua) {
7149 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007150 return 1;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007151 }
7152 HLUA_INIT(hlua);
7153 appctx->ctx.hlua_cli.hlua = hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007154
7155 /* Create task used by signal to wakeup applets.
7156 * We use the same wakeup fonction than the Lua applet_tcp and
7157 * applet_http. It is absolutely compatible.
7158 */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01007159 appctx->ctx.hlua_cli.task = task_new(tid_bit);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007160 if (!appctx->ctx.hlua_cli.task) {
Thierry FOURNIERffbf5692016-12-16 11:14:06 +01007161 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007162 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007163 }
7164 appctx->ctx.hlua_cli.task->nice = 0;
7165 appctx->ctx.hlua_cli.task->context = appctx;
7166 appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup;
7167
7168 /* Initialises the Lua context */
7169 if (!hlua_ctx_init(hlua, appctx->ctx.hlua_cli.task)) {
7170 SEND_ERR(NULL, "Lua cli '%s': can't initialize Lua context.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007171 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007172 }
7173
7174 /* The following Lua calls can fail. */
7175 if (!SET_SAFE_LJMP(hlua->T)) {
7176 if (lua_type(hlua->T, -1) == LUA_TSTRING)
7177 error = lua_tostring(hlua->T, -1);
7178 else
7179 error = "critical error";
7180 SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error);
7181 goto error;
7182 }
7183
7184 /* Check stack available size. */
7185 if (!lua_checkstack(hlua->T, 2)) {
7186 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7187 goto error;
7188 }
7189
7190 /* Restore the function in the stack. */
7191 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
7192
7193 /* Once the arguments parsed, the CLI is like an AppletTCP,
7194 * so push AppletTCP in the stack.
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007195 */
7196 if (!hlua_applet_tcp_new(hlua->T, appctx)) {
7197 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7198 goto error;
7199 }
7200 hlua->nargs = 1;
7201
7202 /* push keywords in the stack. */
7203 for (i = 0; *args[i]; i++) {
7204 /* Check stack available size. */
7205 if (!lua_checkstack(hlua->T, 1)) {
7206 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7207 goto error;
7208 }
7209 lua_pushstring(hlua->T, args[i]);
7210 hlua->nargs++;
7211 }
7212
7213 /* We must initialize the execution timeouts. */
7214 hlua->max_time = hlua_timeout_session;
7215
7216 /* At this point the execution is safe. */
7217 RESET_SAFE_LJMP(hlua->T);
7218
7219 /* It's ok */
7220 return 0;
7221
7222 /* It's not ok. */
7223error:
7224 RESET_SAFE_LJMP(hlua->T);
7225 hlua_ctx_destroy(hlua);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007226 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007227 return 1;
7228}
7229
7230static int hlua_cli_io_handler_fct(struct appctx *appctx)
7231{
7232 struct hlua *hlua;
7233 struct stream_interface *si;
7234 struct hlua_function *fcn;
7235
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007236 hlua = appctx->ctx.hlua_cli.hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007237 si = appctx->owner;
Willy Tarreau8ae4f752016-12-14 15:41:45 +01007238 fcn = appctx->ctx.hlua_cli.fcn;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007239
7240 /* If the stream is disconnect or closed, ldo nothing. */
7241 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
7242 return 1;
7243
7244 /* Execute the function. */
7245 switch (hlua_ctx_resume(hlua, 1)) {
7246
7247 /* finished. */
7248 case HLUA_E_OK:
7249 return 1;
7250
7251 /* yield. */
7252 case HLUA_E_AGAIN:
7253 /* We want write. */
7254 if (HLUA_IS_WAKERESWR(hlua))
Willy Tarreaudb398432018-11-15 11:08:52 +01007255 si_rx_room_blk(si);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007256 /* Set the timeout. */
7257 if (hlua->wake_time != TICK_ETERNITY)
7258 task_schedule(hlua->task, hlua->wake_time);
7259 return 0;
7260
7261 /* finished with error. */
7262 case HLUA_E_ERRMSG:
7263 /* Display log. */
7264 SEND_ERR(NULL, "Lua cli '%s': %s.\n",
7265 fcn->name, lua_tostring(hlua->T, -1));
7266 lua_pop(hlua->T, 1);
7267 return 1;
7268
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007269 case HLUA_E_ETMOUT:
7270 SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n",
7271 fcn->name);
7272 return 1;
7273
7274 case HLUA_E_NOMEM:
7275 SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n",
7276 fcn->name);
7277 return 1;
7278
7279 case HLUA_E_YIELD: /* unexpected */
7280 SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n",
7281 fcn->name);
7282 return 1;
7283
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007284 case HLUA_E_ERR:
7285 /* Display log. */
7286 SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n",
7287 fcn->name);
7288 return 1;
7289
7290 default:
7291 return 1;
7292 }
7293
7294 return 1;
7295}
7296
7297static void hlua_cli_io_release_fct(struct appctx *appctx)
7298{
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007299 hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007300 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007301}
7302
7303/* This function is an LUA binding used for registering
7304 * new keywords in the cli. It expects a list of keywords
7305 * which are the "path". It is limited to 5 keywords. A
7306 * description of the command, a function to be executed
7307 * for the parsing and a function for io handlers.
7308 */
7309__LJMP static int hlua_register_cli(lua_State *L)
7310{
7311 struct cli_kw_list *cli_kws;
7312 const char *message;
7313 int ref_io;
7314 int len;
7315 struct hlua_function *fcn;
7316 int index;
7317 int i;
7318
7319 MAY_LJMP(check_args(L, 3, "register_cli"));
7320
7321 /* First argument : an array of maximum 5 keywords. */
7322 if (!lua_istable(L, 1))
7323 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table"));
7324
7325 /* Second argument : string with contextual message. */
7326 message = MAY_LJMP(luaL_checkstring(L, 2));
7327
7328 /* Third and fourth argument : lua function. */
7329 ref_io = MAY_LJMP(hlua_checkfunction(L, 3));
7330
7331 /* Allocate and fill the sample fetch keyword struct. */
7332 cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2);
7333 if (!cli_kws)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007334 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007335 fcn = calloc(1, sizeof(*fcn));
7336 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007337 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007338
7339 /* Fill path. */
7340 index = 0;
7341 lua_pushnil(L);
7342 while(lua_next(L, 1) != 0) {
7343 if (index >= 5)
7344 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries"));
7345 if (lua_type(L, -1) != LUA_TSTRING)
7346 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings"));
7347 cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1));
7348 if (!cli_kws->kw[0].str_kw[index])
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007349 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007350 index++;
7351 lua_pop(L, 1);
7352 }
7353
7354 /* Copy help message. */
7355 cli_kws->kw[0].usage = strdup(message);
7356 if (!cli_kws->kw[0].usage)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007357 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007358
7359 /* Fill fcn io handler. */
7360 len = strlen("<lua.cli>") + 1;
7361 for (i = 0; i < index; i++)
7362 len += strlen(cli_kws->kw[0].str_kw[i]) + 1;
7363 fcn->name = calloc(1, len);
7364 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007365 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007366 strncat((char *)fcn->name, "<lua.cli", len);
7367 for (i = 0; i < index; i++) {
7368 strncat((char *)fcn->name, ".", len);
7369 strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len);
7370 }
7371 strncat((char *)fcn->name, ">", len);
7372 fcn->function_ref = ref_io;
7373
7374 /* Fill last entries. */
7375 cli_kws->kw[0].private = fcn;
7376 cli_kws->kw[0].parse = hlua_cli_parse_fct;
7377 cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct;
7378 cli_kws->kw[0].io_release = hlua_cli_io_release_fct;
7379
7380 /* Register this new converter */
7381 cli_register_kw(cli_kws);
7382
7383 return 0;
7384}
7385
Thierry FOURNIERbd413492015-03-03 16:52:26 +01007386static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx,
7387 struct proxy *defpx, const char *file, int line,
7388 char **err, unsigned int *timeout)
7389{
7390 const char *error;
7391
7392 error = parse_time_err(args[1], timeout, TIME_UNIT_MS);
7393 if (error && *error != '\0') {
7394 memprintf(err, "%s: invalid timeout", args[0]);
7395 return -1;
7396 }
7397 return 0;
7398}
7399
7400static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx,
7401 struct proxy *defpx, const char *file, int line,
7402 char **err)
7403{
7404 return hlua_read_timeout(args, section_type, curpx, defpx,
7405 file, line, err, &hlua_timeout_session);
7406}
7407
7408static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx,
7409 struct proxy *defpx, const char *file, int line,
7410 char **err)
7411{
7412 return hlua_read_timeout(args, section_type, curpx, defpx,
7413 file, line, err, &hlua_timeout_task);
7414}
7415
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007416static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx,
7417 struct proxy *defpx, const char *file, int line,
7418 char **err)
7419{
7420 return hlua_read_timeout(args, section_type, curpx, defpx,
7421 file, line, err, &hlua_timeout_applet);
7422}
7423
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01007424static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx,
7425 struct proxy *defpx, const char *file, int line,
7426 char **err)
7427{
7428 char *error;
7429
7430 hlua_nb_instruction = strtoll(args[1], &error, 10);
7431 if (*error != '\0') {
7432 memprintf(err, "%s: invalid number", args[0]);
7433 return -1;
7434 }
7435 return 0;
7436}
7437
Willy Tarreau32f61e22015-03-18 17:54:59 +01007438static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx,
7439 struct proxy *defpx, const char *file, int line,
7440 char **err)
7441{
7442 char *error;
7443
7444 if (*(args[1]) == 0) {
7445 memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]);
7446 return -1;
7447 }
7448 hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L;
7449 if (*error != '\0') {
7450 memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error);
7451 return -1;
7452 }
7453 return 0;
7454}
7455
7456
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007457/* This function is called by the main configuration key "lua-load". It loads and
7458 * execute an lua file during the parsing of the HAProxy configuration file. It is
7459 * the main lua entry point.
7460 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007461 * This function runs with the HAProxy keywords API. It returns -1 if an error
7462 * occurs, otherwise it returns 0.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007463 *
7464 * In some error case, LUA set an error message in top of the stack. This function
7465 * returns this error message in the HAProxy logs and pop it from the stack.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007466 *
7467 * This function can fail with an abort() due to an Lua critical error.
7468 * We are in the configuration parsing process of HAProxy, this abort() is
7469 * tolerated.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007470 */
7471static int hlua_load(char **args, int section_type, struct proxy *curpx,
7472 struct proxy *defpx, const char *file, int line,
7473 char **err)
7474{
7475 int error;
7476
7477 /* Just load and compile the file. */
7478 error = luaL_loadfile(gL.T, args[1]);
7479 if (error) {
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007480 memprintf(err, "error in Lua file '%s': %s", args[1], lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007481 lua_pop(gL.T, 1);
7482 return -1;
7483 }
7484
7485 /* If no syntax error where detected, execute the code. */
7486 error = lua_pcall(gL.T, 0, LUA_MULTRET, 0);
7487 switch (error) {
7488 case LUA_OK:
7489 break;
7490 case LUA_ERRRUN:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007491 memprintf(err, "Lua runtime error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007492 lua_pop(gL.T, 1);
7493 return -1;
7494 case LUA_ERRMEM:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007495 memprintf(err, "Lua out of memory error.n");
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007496 return -1;
7497 case LUA_ERRERR:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007498 memprintf(err, "Lua message handler error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007499 lua_pop(gL.T, 1);
7500 return -1;
7501 case LUA_ERRGCMM:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007502 memprintf(err, "Lua garbage collector error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007503 lua_pop(gL.T, 1);
7504 return -1;
7505 default:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007506 memprintf(err, "Lua unknonwn error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007507 lua_pop(gL.T, 1);
7508 return -1;
7509 }
7510
7511 return 0;
7512}
7513
7514/* configuration keywords declaration */
7515static struct cfg_kw_list cfg_kws = {{ },{
Thierry FOURNIERbd413492015-03-03 16:52:26 +01007516 { CFG_GLOBAL, "lua-load", hlua_load },
7517 { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout },
7518 { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout },
Thierry FOURNIER56da1012015-10-01 08:42:31 +02007519 { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout },
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01007520 { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield },
Willy Tarreau32f61e22015-03-18 17:54:59 +01007521 { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem },
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007522 { 0, NULL, NULL },
7523}};
7524
Willy Tarreau0108d902018-11-25 19:14:37 +01007525INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
7526
Christopher Fauletafd8f102018-11-08 11:34:21 +01007527static int hlua_check_config()
7528{
7529 struct proxy *px;
7530 struct acl *acl;
7531 struct acl_expr *expr;
7532 struct sample_fetch *fetch;
7533 int err = 0;
7534
7535 for (px = proxies_list; px; px = px->next) {
7536 if (!(px->options2 & PR_O2_USE_HTX))
7537 continue;
7538
7539 list_for_each_entry(acl, &px->acl, list) {
7540 list_for_each_entry(expr, &acl->expr, list) {
7541 fetch = expr->smp->fetch;
7542 if (fetch->process != hlua_sample_fetch_wrapper)
7543 continue;
7544
7545 ha_alert("config: %s '%s': sample-fetch '%s' cannot be used used "
7546 "when the HTX internal representation is enabled.\n",
7547 proxy_type_str(px), px->id, fetch->kw);
7548 err++;
7549 }
7550 }
7551 }
7552 return err;
7553}
7554
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007555/* This function can fail with an abort() due to an Lua critical error.
7556 * We are in the initialisation process of HAProxy, this abort() is
7557 * tolerated.
7558 */
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007559int hlua_post_init()
7560{
7561 struct hlua_init_function *init;
7562 const char *msg;
7563 enum hlua_exec ret;
Thierry Fournierfd107a22016-02-19 19:57:23 +01007564 const char *error;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007565
Thierry Fournier3d4a6752016-02-19 20:53:30 +01007566 /* Call post initialisation function in safe environement. */
7567 if (!SET_SAFE_LJMP(gL.T)) {
7568 if (lua_type(gL.T, -1) == LUA_TSTRING)
7569 error = lua_tostring(gL.T, -1);
7570 else
7571 error = "critical error";
7572 fprintf(stderr, "Lua post-init: %s.\n", error);
7573 exit(1);
7574 }
Frédéric Lécaille54f2bcf2018-08-29 13:46:24 +02007575
7576#if USE_OPENSSL
7577 /* Initialize SSL server. */
7578 if (socket_ssl.xprt->prepare_srv) {
7579 int saved_used_backed = global.ssl_used_backend;
7580 // don't affect maxconn automatic computation
7581 socket_ssl.xprt->prepare_srv(&socket_ssl);
7582 global.ssl_used_backend = saved_used_backed;
7583 }
7584#endif
7585
Thierry Fournier3d4a6752016-02-19 20:53:30 +01007586 hlua_fcn_post_init(gL.T);
7587 RESET_SAFE_LJMP(gL.T);
7588
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007589 list_for_each_entry(init, &hlua_init_functions, l) {
7590 lua_rawgeti(gL.T, LUA_REGISTRYINDEX, init->function_ref);
7591 ret = hlua_ctx_resume(&gL, 0);
7592 switch (ret) {
7593 case HLUA_E_OK:
7594 lua_pop(gL.T, -1);
7595 return 1;
7596 case HLUA_E_AGAIN:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007597 ha_alert("Lua init: yield not allowed.\n");
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007598 return 0;
7599 case HLUA_E_ERRMSG:
7600 msg = lua_tostring(gL.T, -1);
Christopher Faulet767a84b2017-11-24 16:50:31 +01007601 ha_alert("lua init: %s.\n", msg);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007602 return 0;
7603 case HLUA_E_ERR:
7604 default:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007605 ha_alert("Lua init: unknown runtime error.\n");
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007606 return 0;
7607 }
7608 }
7609 return 1;
7610}
7611
Willy Tarreau32f61e22015-03-18 17:54:59 +01007612/* The memory allocator used by the Lua stack. <ud> is a pointer to the
7613 * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize>
7614 * is the previously allocated size or the kind of object in case of a new
7615 * allocation. <nsize> is the requested new size.
7616 */
7617static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
7618{
7619 struct hlua_mem_allocator *zone = ud;
7620
7621 if (nsize == 0) {
7622 /* it's a free */
7623 if (ptr)
7624 zone->allocated -= osize;
7625 free(ptr);
7626 return NULL;
7627 }
7628
7629 if (!ptr) {
7630 /* it's a new allocation */
7631 if (zone->limit && zone->allocated + nsize > zone->limit)
7632 return NULL;
7633
7634 ptr = malloc(nsize);
7635 if (ptr)
7636 zone->allocated += nsize;
7637 return ptr;
7638 }
7639
7640 /* it's a realloc */
7641 if (zone->limit && zone->allocated + nsize - osize > zone->limit)
7642 return NULL;
7643
7644 ptr = realloc(ptr, nsize);
7645 if (ptr)
7646 zone->allocated += nsize - osize;
7647 return ptr;
7648}
7649
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007650/* Ithis function can fail with an abort() due to an Lua critical error.
7651 * We are in the initialisation process of HAProxy, this abort() is
7652 * tolerated.
7653 */
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01007654void hlua_init(void)
7655{
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007656 int i;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01007657 int idx;
7658 struct sample_fetch *sf;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007659 struct sample_conv *sc;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01007660 char *p;
Thierry Fournierfd107a22016-02-19 19:57:23 +01007661 const char *error_msg;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01007662#ifdef USE_OPENSSL
Willy Tarreau80f5fae2015-02-27 16:38:20 +01007663 struct srv_kw *kw;
7664 int tmp_error;
7665 char *error;
Thierry FOURNIER36d13742015-03-17 16:48:53 +01007666 char *args[] = { /* SSL client configuration. */
7667 "ssl",
7668 "verify",
7669 "none",
Thierry FOURNIER36d13742015-03-17 16:48:53 +01007670 NULL
7671 };
Willy Tarreau80f5fae2015-02-27 16:38:20 +01007672#endif
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007673
Thierry FOURNIER380d0932015-01-23 14:27:52 +01007674 /* Init main lua stack. */
7675 gL.Mref = LUA_REFNIL;
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01007676 gL.flags = 0;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01007677 LIST_INIT(&gL.com);
Willy Tarreau42ef75f2017-04-12 21:40:29 +02007678 gL.T = lua_newstate(hlua_alloc, &hlua_global_allocator);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01007679 hlua_sethlua(&gL);
7680 gL.Tref = LUA_REFNIL;
7681 gL.task = NULL;
7682
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007683 /* From this point, until the end of the initialisation function,
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007684 * the Lua function can fail with an abort. We are in the initialisation
7685 * process of HAProxy, this abort() is tolerated.
7686 */
7687
Thierry FOURNIER380d0932015-01-23 14:27:52 +01007688 /* Initialise lua. */
7689 luaL_openlibs(gL.T);
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007690
Thierry Fournier75933d42016-01-21 09:30:18 +01007691 /* Set safe environment for the initialisation. */
7692 if (!SET_SAFE_LJMP(gL.T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01007693 if (lua_type(gL.T, -1) == LUA_TSTRING)
7694 error_msg = lua_tostring(gL.T, -1);
7695 else
7696 error_msg = "critical error";
7697 fprintf(stderr, "Lua init: %s.\n", error_msg);
Thierry Fournier75933d42016-01-21 09:30:18 +01007698 exit(1);
7699 }
7700
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007701 /*
7702 *
7703 * Create "core" object.
7704 *
7705 */
7706
Thierry FOURNIERa2d8c652015-03-11 17:29:39 +01007707 /* This table entry is the object "core" base. */
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007708 lua_newtable(gL.T);
7709
7710 /* Push the loglevel constants. */
Willy Tarreau80f5fae2015-02-27 16:38:20 +01007711 for (i = 0; i < NB_LOG_LEVELS; i++)
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007712 hlua_class_const_int(gL.T, log_levels[i], i);
7713
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007714 /* Register special functions. */
7715 hlua_class_function(gL.T, "register_init", hlua_register_init);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01007716 hlua_class_function(gL.T, "register_task", hlua_register_task);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01007717 hlua_class_function(gL.T, "register_fetches", hlua_register_fetches);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01007718 hlua_class_function(gL.T, "register_converters", hlua_register_converters);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007719 hlua_class_function(gL.T, "register_action", hlua_register_action);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007720 hlua_class_function(gL.T, "register_service", hlua_register_service);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007721 hlua_class_function(gL.T, "register_cli", hlua_register_cli);
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01007722 hlua_class_function(gL.T, "yield", hlua_yield);
Willy Tarreau59551662015-03-10 14:23:13 +01007723 hlua_class_function(gL.T, "set_nice", hlua_set_nice);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01007724 hlua_class_function(gL.T, "sleep", hlua_sleep);
7725 hlua_class_function(gL.T, "msleep", hlua_msleep);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01007726 hlua_class_function(gL.T, "add_acl", hlua_add_acl);
7727 hlua_class_function(gL.T, "del_acl", hlua_del_acl);
7728 hlua_class_function(gL.T, "set_map", hlua_set_map);
7729 hlua_class_function(gL.T, "del_map", hlua_del_map);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007730 hlua_class_function(gL.T, "tcp", hlua_socket_new);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01007731 hlua_class_function(gL.T, "log", hlua_log);
7732 hlua_class_function(gL.T, "Debug", hlua_log_debug);
7733 hlua_class_function(gL.T, "Info", hlua_log_info);
7734 hlua_class_function(gL.T, "Warning", hlua_log_warning);
7735 hlua_class_function(gL.T, "Alert", hlua_log_alert);
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02007736 hlua_class_function(gL.T, "done", hlua_done);
Thierry Fournierfb0b5462016-01-21 09:28:58 +01007737 hlua_fcn_reg_core_fcn(gL.T);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007738
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007739 lua_setglobal(gL.T, "core");
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01007740
7741 /*
7742 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02007743 * Register class Map
7744 *
7745 */
7746
7747 /* This table entry is the object "Map" base. */
7748 lua_newtable(gL.T);
7749
7750 /* register pattern types. */
7751 for (i=0; i<PAT_MATCH_NUM; i++)
7752 hlua_class_const_int(gL.T, pat_match_names[i], i);
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01007753 for (i=0; i<PAT_MATCH_NUM; i++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007754 snprintf(trash.area, trash.size, "_%s", pat_match_names[i]);
7755 hlua_class_const_int(gL.T, trash.area, i);
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01007756 }
Thierry FOURNIER3def3932015-04-07 11:27:54 +02007757
7758 /* register constructor. */
7759 hlua_class_function(gL.T, "new", hlua_map_new);
7760
7761 /* Create and fill the metatable. */
7762 lua_newtable(gL.T);
7763
7764 /* Create and fille the __index entry. */
7765 lua_pushstring(gL.T, "__index");
7766 lua_newtable(gL.T);
7767
7768 /* Register . */
7769 hlua_class_function(gL.T, "lookup", hlua_map_lookup);
7770 hlua_class_function(gL.T, "slookup", hlua_map_slookup);
7771
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007772 lua_rawset(gL.T, -3);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02007773
Thierry Fournier45e78d72016-02-19 18:34:46 +01007774 /* Register previous table in the registry with reference and named entry.
7775 * The function hlua_register_metatable() pops the stack, so we
7776 * previously create a copy of the table.
7777 */
Thierry FOURNIER3def3932015-04-07 11:27:54 +02007778 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007779 class_map_ref = hlua_register_metatable(gL.T, CLASS_MAP);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02007780
7781 /* Assign the metatable to the mai Map object. */
7782 lua_setmetatable(gL.T, -2);
7783
7784 /* Set a name to the table. */
7785 lua_setglobal(gL.T, "Map");
7786
7787 /*
7788 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01007789 * Register class Channel
7790 *
7791 */
7792
7793 /* Create and fill the metatable. */
7794 lua_newtable(gL.T);
7795
7796 /* Create and fille the __index entry. */
7797 lua_pushstring(gL.T, "__index");
7798 lua_newtable(gL.T);
7799
7800 /* Register . */
7801 hlua_class_function(gL.T, "get", hlua_channel_get);
7802 hlua_class_function(gL.T, "dup", hlua_channel_dup);
7803 hlua_class_function(gL.T, "getline", hlua_channel_getline);
7804 hlua_class_function(gL.T, "set", hlua_channel_set);
7805 hlua_class_function(gL.T, "append", hlua_channel_append);
7806 hlua_class_function(gL.T, "send", hlua_channel_send);
7807 hlua_class_function(gL.T, "forward", hlua_channel_forward);
7808 hlua_class_function(gL.T, "get_in_len", hlua_channel_get_in_len);
7809 hlua_class_function(gL.T, "get_out_len", hlua_channel_get_out_len);
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01007810 hlua_class_function(gL.T, "is_full", hlua_channel_is_full);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01007811
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007812 lua_rawset(gL.T, -3);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01007813
7814 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007815 class_channel_ref = hlua_register_metatable(gL.T, CLASS_CHANNEL);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01007816
7817 /*
7818 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01007819 * Register class Fetches
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01007820 *
7821 */
7822
7823 /* Create and fill the metatable. */
7824 lua_newtable(gL.T);
7825
7826 /* Create and fille the __index entry. */
7827 lua_pushstring(gL.T, "__index");
7828 lua_newtable(gL.T);
7829
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01007830 /* Browse existing fetches and create the associated
7831 * object method.
7832 */
7833 sf = NULL;
7834 while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) {
7835
7836 /* Dont register the keywork if the arguments check function are
7837 * not safe during the runtime.
7838 */
7839 if ((sf->val_args != NULL) &&
7840 (sf->val_args != val_payload_lv) &&
7841 (sf->val_args != val_hdr))
7842 continue;
7843
7844 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
7845 * by an underscore.
7846 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007847 strncpy(trash.area, sf->kw, trash.size);
7848 trash.area[trash.size - 1] = '\0';
7849 for (p = trash.area; *p; p++)
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01007850 if (*p == '.' || *p == '-' || *p == '+')
7851 *p = '_';
7852
7853 /* Register the function. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007854 lua_pushstring(gL.T, trash.area);
Willy Tarreau2ec22742015-03-10 14:27:20 +01007855 lua_pushlightuserdata(gL.T, sf);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01007856 lua_pushcclosure(gL.T, hlua_run_sample_fetch, 1);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007857 lua_rawset(gL.T, -3);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01007858 }
7859
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007860 lua_rawset(gL.T, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01007861
7862 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007863 class_fetches_ref = hlua_register_metatable(gL.T, CLASS_FETCHES);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01007864
7865 /*
7866 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007867 * Register class Converters
7868 *
7869 */
7870
7871 /* Create and fill the metatable. */
7872 lua_newtable(gL.T);
7873
7874 /* Create and fill the __index entry. */
7875 lua_pushstring(gL.T, "__index");
7876 lua_newtable(gL.T);
7877
7878 /* Browse existing converters and create the associated
7879 * object method.
7880 */
7881 sc = NULL;
7882 while ((sc = sample_conv_getnext(sc, &idx)) != NULL) {
7883 /* Dont register the keywork if the arguments check function are
7884 * not safe during the runtime.
7885 */
7886 if (sc->val_args != NULL)
7887 continue;
7888
7889 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
7890 * by an underscore.
7891 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007892 strncpy(trash.area, sc->kw, trash.size);
7893 trash.area[trash.size - 1] = '\0';
7894 for (p = trash.area; *p; p++)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007895 if (*p == '.' || *p == '-' || *p == '+')
7896 *p = '_';
7897
7898 /* Register the function. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007899 lua_pushstring(gL.T, trash.area);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007900 lua_pushlightuserdata(gL.T, sc);
7901 lua_pushcclosure(gL.T, hlua_run_sample_conv, 1);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007902 lua_rawset(gL.T, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007903 }
7904
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007905 lua_rawset(gL.T, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007906
7907 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007908 class_converters_ref = hlua_register_metatable(gL.T, CLASS_CONVERTERS);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007909
7910 /*
7911 *
Thierry FOURNIER08504f42015-03-16 14:17:08 +01007912 * Register class HTTP
7913 *
7914 */
7915
7916 /* Create and fill the metatable. */
7917 lua_newtable(gL.T);
7918
7919 /* Create and fille the __index entry. */
7920 lua_pushstring(gL.T, "__index");
7921 lua_newtable(gL.T);
7922
7923 /* Register Lua functions. */
7924 hlua_class_function(gL.T, "req_get_headers",hlua_http_req_get_headers);
7925 hlua_class_function(gL.T, "req_del_header", hlua_http_req_del_hdr);
7926 hlua_class_function(gL.T, "req_rep_header", hlua_http_req_rep_hdr);
7927 hlua_class_function(gL.T, "req_rep_value", hlua_http_req_rep_val);
7928 hlua_class_function(gL.T, "req_add_header", hlua_http_req_add_hdr);
7929 hlua_class_function(gL.T, "req_set_header", hlua_http_req_set_hdr);
7930 hlua_class_function(gL.T, "req_set_method", hlua_http_req_set_meth);
7931 hlua_class_function(gL.T, "req_set_path", hlua_http_req_set_path);
7932 hlua_class_function(gL.T, "req_set_query", hlua_http_req_set_query);
7933 hlua_class_function(gL.T, "req_set_uri", hlua_http_req_set_uri);
7934
7935 hlua_class_function(gL.T, "res_get_headers",hlua_http_res_get_headers);
7936 hlua_class_function(gL.T, "res_del_header", hlua_http_res_del_hdr);
7937 hlua_class_function(gL.T, "res_rep_header", hlua_http_res_rep_hdr);
7938 hlua_class_function(gL.T, "res_rep_value", hlua_http_res_rep_val);
7939 hlua_class_function(gL.T, "res_add_header", hlua_http_res_add_hdr);
7940 hlua_class_function(gL.T, "res_set_header", hlua_http_res_set_hdr);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02007941 hlua_class_function(gL.T, "res_set_status", hlua_http_res_set_status);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01007942
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007943 lua_rawset(gL.T, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01007944
7945 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007946 class_http_ref = hlua_register_metatable(gL.T, CLASS_HTTP);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01007947
7948 /*
7949 *
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007950 * Register class AppletTCP
7951 *
7952 */
7953
7954 /* Create and fill the metatable. */
7955 lua_newtable(gL.T);
7956
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007957 /* Create and fille the __index entry. */
7958 lua_pushstring(gL.T, "__index");
7959 lua_newtable(gL.T);
7960
7961 /* Register Lua functions. */
Thierry FOURNIER / OZON.IO3e1d7912016-12-12 12:29:34 +01007962 hlua_class_function(gL.T, "getline", hlua_applet_tcp_getline);
7963 hlua_class_function(gL.T, "receive", hlua_applet_tcp_recv);
7964 hlua_class_function(gL.T, "send", hlua_applet_tcp_send);
7965 hlua_class_function(gL.T, "set_priv", hlua_applet_tcp_set_priv);
7966 hlua_class_function(gL.T, "get_priv", hlua_applet_tcp_get_priv);
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01007967 hlua_class_function(gL.T, "set_var", hlua_applet_tcp_set_var);
7968 hlua_class_function(gL.T, "unset_var", hlua_applet_tcp_unset_var);
7969 hlua_class_function(gL.T, "get_var", hlua_applet_tcp_get_var);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007970
7971 lua_settable(gL.T, -3);
7972
7973 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007974 class_applet_tcp_ref = hlua_register_metatable(gL.T, CLASS_APPLET_TCP);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007975
7976 /*
7977 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007978 * Register class AppletHTTP
7979 *
7980 */
7981
7982 /* Create and fill the metatable. */
7983 lua_newtable(gL.T);
7984
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007985 /* Create and fille the __index entry. */
7986 lua_pushstring(gL.T, "__index");
7987 lua_newtable(gL.T);
7988
7989 /* Register Lua functions. */
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01007990 hlua_class_function(gL.T, "set_priv", hlua_applet_http_set_priv);
7991 hlua_class_function(gL.T, "get_priv", hlua_applet_http_get_priv);
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01007992 hlua_class_function(gL.T, "set_var", hlua_applet_http_set_var);
7993 hlua_class_function(gL.T, "unset_var", hlua_applet_http_unset_var);
7994 hlua_class_function(gL.T, "get_var", hlua_applet_http_get_var);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007995 hlua_class_function(gL.T, "getline", hlua_applet_http_getline);
7996 hlua_class_function(gL.T, "receive", hlua_applet_http_recv);
7997 hlua_class_function(gL.T, "send", hlua_applet_http_send);
7998 hlua_class_function(gL.T, "add_header", hlua_applet_http_addheader);
7999 hlua_class_function(gL.T, "set_status", hlua_applet_http_status);
8000 hlua_class_function(gL.T, "start_response", hlua_applet_http_start_response);
8001
8002 lua_settable(gL.T, -3);
8003
8004 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008005 class_applet_http_ref = hlua_register_metatable(gL.T, CLASS_APPLET_HTTP);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02008006
8007 /*
8008 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008009 * Register class TXN
8010 *
8011 */
8012
8013 /* Create and fill the metatable. */
8014 lua_newtable(gL.T);
8015
8016 /* Create and fille the __index entry. */
8017 lua_pushstring(gL.T, "__index");
8018 lua_newtable(gL.T);
8019
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01008020 /* Register Lua functions. */
Patrick Hemmer268a7072018-05-11 12:52:31 -04008021 hlua_class_function(gL.T, "set_priv", hlua_set_priv);
8022 hlua_class_function(gL.T, "get_priv", hlua_get_priv);
8023 hlua_class_function(gL.T, "set_var", hlua_set_var);
8024 hlua_class_function(gL.T, "unset_var", hlua_unset_var);
8025 hlua_class_function(gL.T, "get_var", hlua_get_var);
8026 hlua_class_function(gL.T, "done", hlua_txn_done);
8027 hlua_class_function(gL.T, "set_loglevel", hlua_txn_set_loglevel);
8028 hlua_class_function(gL.T, "set_tos", hlua_txn_set_tos);
8029 hlua_class_function(gL.T, "set_mark", hlua_txn_set_mark);
8030 hlua_class_function(gL.T, "set_priority_class", hlua_txn_set_priority_class);
8031 hlua_class_function(gL.T, "set_priority_offset", hlua_txn_set_priority_offset);
8032 hlua_class_function(gL.T, "deflog", hlua_txn_deflog);
8033 hlua_class_function(gL.T, "log", hlua_txn_log);
8034 hlua_class_function(gL.T, "Debug", hlua_txn_log_debug);
8035 hlua_class_function(gL.T, "Info", hlua_txn_log_info);
8036 hlua_class_function(gL.T, "Warning", hlua_txn_log_warning);
8037 hlua_class_function(gL.T, "Alert", hlua_txn_log_alert);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01008038
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008039 lua_rawset(gL.T, -3);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008040
8041 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008042 class_txn_ref = hlua_register_metatable(gL.T, CLASS_TXN);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008043
8044 /*
8045 *
8046 * Register class Socket
8047 *
8048 */
8049
8050 /* Create and fill the metatable. */
8051 lua_newtable(gL.T);
8052
8053 /* Create and fille the __index entry. */
8054 lua_pushstring(gL.T, "__index");
8055 lua_newtable(gL.T);
8056
Baptiste Assmann84bb4932015-03-02 21:40:06 +01008057#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008058 hlua_class_function(gL.T, "connect_ssl", hlua_socket_connect_ssl);
Baptiste Assmann84bb4932015-03-02 21:40:06 +01008059#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008060 hlua_class_function(gL.T, "connect", hlua_socket_connect);
8061 hlua_class_function(gL.T, "send", hlua_socket_send);
8062 hlua_class_function(gL.T, "receive", hlua_socket_receive);
8063 hlua_class_function(gL.T, "close", hlua_socket_close);
8064 hlua_class_function(gL.T, "getpeername", hlua_socket_getpeername);
8065 hlua_class_function(gL.T, "getsockname", hlua_socket_getsockname);
8066 hlua_class_function(gL.T, "setoption", hlua_socket_setoption);
8067 hlua_class_function(gL.T, "settimeout", hlua_socket_settimeout);
8068
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008069 lua_rawset(gL.T, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008070
8071 /* Register the garbage collector entry. */
8072 lua_pushstring(gL.T, "__gc");
8073 lua_pushcclosure(gL.T, hlua_socket_gc, 0);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008074 lua_rawset(gL.T, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008075
8076 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008077 class_socket_ref = hlua_register_metatable(gL.T, CLASS_SOCKET);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008078
8079 /* Proxy and server configuration initialisation. */
8080 memset(&socket_proxy, 0, sizeof(socket_proxy));
8081 init_new_proxy(&socket_proxy);
8082 socket_proxy.parent = NULL;
8083 socket_proxy.last_change = now.tv_sec;
8084 socket_proxy.id = "LUA-SOCKET";
8085 socket_proxy.cap = PR_CAP_FE | PR_CAP_BE;
8086 socket_proxy.maxconn = 0;
8087 socket_proxy.accept = NULL;
8088 socket_proxy.options2 |= PR_O2_INDEPSTR;
8089 socket_proxy.srv = NULL;
8090 socket_proxy.conn_retries = 0;
8091 socket_proxy.timeout.connect = 5000; /* By default the timeout connection is 5s. */
8092
8093 /* Init TCP server: unchanged parameters */
8094 memset(&socket_tcp, 0, sizeof(socket_tcp));
8095 socket_tcp.next = NULL;
8096 socket_tcp.proxy = &socket_proxy;
8097 socket_tcp.obj_type = OBJ_TYPE_SERVER;
8098 LIST_INIT(&socket_tcp.actconns);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04008099 socket_tcp.pendconns = EB_ROOT;
Christopher Faulet40a007c2017-07-03 15:41:01 +02008100 socket_tcp.priv_conns = NULL;
8101 socket_tcp.idle_conns = NULL;
8102 socket_tcp.safe_conns = NULL;
Emeric Brun52a91d32017-08-31 14:41:55 +02008103 socket_tcp.next_state = SRV_ST_RUNNING; /* early server setup */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008104 socket_tcp.last_change = 0;
8105 socket_tcp.id = "LUA-TCP-CONN";
8106 socket_tcp.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8107 socket_tcp.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8108 socket_tcp.pp_opts = 0; /* Remove proxy protocol. */
8109
8110 /* XXX: Copy default parameter from default server,
8111 * but the default server is not initialized.
8112 */
8113 socket_tcp.maxqueue = socket_proxy.defsrv.maxqueue;
8114 socket_tcp.minconn = socket_proxy.defsrv.minconn;
8115 socket_tcp.maxconn = socket_proxy.defsrv.maxconn;
8116 socket_tcp.slowstart = socket_proxy.defsrv.slowstart;
8117 socket_tcp.onerror = socket_proxy.defsrv.onerror;
8118 socket_tcp.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
8119 socket_tcp.onmarkedup = socket_proxy.defsrv.onmarkedup;
8120 socket_tcp.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
8121 socket_tcp.uweight = socket_proxy.defsrv.iweight;
8122 socket_tcp.iweight = socket_proxy.defsrv.iweight;
8123
8124 socket_tcp.check.status = HCHK_STATUS_INI;
8125 socket_tcp.check.rise = socket_proxy.defsrv.check.rise;
8126 socket_tcp.check.fall = socket_proxy.defsrv.check.fall;
8127 socket_tcp.check.health = socket_tcp.check.rise; /* socket, but will fall down at first failure */
8128 socket_tcp.check.server = &socket_tcp;
8129
8130 socket_tcp.agent.status = HCHK_STATUS_INI;
8131 socket_tcp.agent.rise = socket_proxy.defsrv.agent.rise;
8132 socket_tcp.agent.fall = socket_proxy.defsrv.agent.fall;
8133 socket_tcp.agent.health = socket_tcp.agent.rise; /* socket, but will fall down at first failure */
8134 socket_tcp.agent.server = &socket_tcp;
8135
Willy Tarreaua261e9b2016-12-22 20:44:00 +01008136 socket_tcp.xprt = xprt_get(XPRT_RAW);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008137
8138#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008139 /* Init TCP server: unchanged parameters */
8140 memset(&socket_ssl, 0, sizeof(socket_ssl));
8141 socket_ssl.next = NULL;
8142 socket_ssl.proxy = &socket_proxy;
8143 socket_ssl.obj_type = OBJ_TYPE_SERVER;
8144 LIST_INIT(&socket_ssl.actconns);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04008145 socket_ssl.pendconns = EB_ROOT;
Christopher Faulet40a007c2017-07-03 15:41:01 +02008146 socket_tcp.priv_conns = NULL;
8147 socket_tcp.idle_conns = NULL;
8148 socket_tcp.safe_conns = NULL;
Emeric Brun52a91d32017-08-31 14:41:55 +02008149 socket_ssl.next_state = SRV_ST_RUNNING; /* early server setup */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008150 socket_ssl.last_change = 0;
8151 socket_ssl.id = "LUA-SSL-CONN";
8152 socket_ssl.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8153 socket_ssl.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8154 socket_ssl.pp_opts = 0; /* Remove proxy protocol. */
8155
8156 /* XXX: Copy default parameter from default server,
8157 * but the default server is not initialized.
8158 */
8159 socket_ssl.maxqueue = socket_proxy.defsrv.maxqueue;
8160 socket_ssl.minconn = socket_proxy.defsrv.minconn;
8161 socket_ssl.maxconn = socket_proxy.defsrv.maxconn;
8162 socket_ssl.slowstart = socket_proxy.defsrv.slowstart;
8163 socket_ssl.onerror = socket_proxy.defsrv.onerror;
8164 socket_ssl.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
8165 socket_ssl.onmarkedup = socket_proxy.defsrv.onmarkedup;
8166 socket_ssl.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
8167 socket_ssl.uweight = socket_proxy.defsrv.iweight;
8168 socket_ssl.iweight = socket_proxy.defsrv.iweight;
8169
8170 socket_ssl.check.status = HCHK_STATUS_INI;
8171 socket_ssl.check.rise = socket_proxy.defsrv.check.rise;
8172 socket_ssl.check.fall = socket_proxy.defsrv.check.fall;
8173 socket_ssl.check.health = socket_ssl.check.rise; /* socket, but will fall down at first failure */
8174 socket_ssl.check.server = &socket_ssl;
8175
8176 socket_ssl.agent.status = HCHK_STATUS_INI;
8177 socket_ssl.agent.rise = socket_proxy.defsrv.agent.rise;
8178 socket_ssl.agent.fall = socket_proxy.defsrv.agent.fall;
8179 socket_ssl.agent.health = socket_ssl.agent.rise; /* socket, but will fall down at first failure */
8180 socket_ssl.agent.server = &socket_ssl;
8181
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008182 socket_ssl.use_ssl = 1;
Willy Tarreaua261e9b2016-12-22 20:44:00 +01008183 socket_ssl.xprt = xprt_get(XPRT_SSL);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008184
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008185 for (idx = 0; args[idx] != NULL; idx++) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008186 if ((kw = srv_find_kw(args[idx])) != NULL) { /* Maybe it's registered server keyword */
8187 /*
8188 *
8189 * If the keyword is not known, we can search in the registered
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08008190 * server keywords. This is useful to configure special SSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008191 * features like client certificates and ssl_verify.
8192 *
8193 */
8194 tmp_error = kw->parse(args, &idx, &socket_proxy, &socket_ssl, &error);
8195 if (tmp_error != 0) {
8196 fprintf(stderr, "INTERNAL ERROR: %s\n", error);
8197 abort(); /* This must be never arrives because the command line
8198 not editable by the user. */
8199 }
8200 idx += kw->skip;
8201 }
8202 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008203#endif
Thierry Fournier75933d42016-01-21 09:30:18 +01008204
8205 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01008206}
Willy Tarreaubb57d942016-12-21 19:04:56 +01008207
Willy Tarreau80713382018-11-26 10:19:54 +01008208static void hlua_register_build_options(void)
8209{
Willy Tarreaubb57d942016-12-21 19:04:56 +01008210 char *ptr = NULL;
Willy Tarreau80713382018-11-26 10:19:54 +01008211
Willy Tarreaubb57d942016-12-21 19:04:56 +01008212 memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE);
8213 hap_register_build_opts(ptr, 1);
8214}
Willy Tarreau80713382018-11-26 10:19:54 +01008215
8216INITCALL0(STG_REGISTER, hlua_register_build_options);
Willy Tarreaue6552512018-11-26 11:33:13 +01008217REGISTER_CONFIG_POSTPARSER("hlua", hlua_check_config);