blob: 0e105059deeedaa30ff47f343d71d72776c84f24 [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 Tarreau3367d412018-11-15 10:57:41 +01001619 si_rx_endp_more(si);
Willy Tarreaud4da1962015-04-20 01:31:23 +02001620 return;
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001621 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001622
1623 /* This function is called after the connect. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01001624 appctx->ctx.hlua_cosocket.connected = 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001625
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001626 /* Wake the tasks which wants to write if the buffer have available space. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001627 if (channel_may_recv(si_ic(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001628 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001629
1630 /* Wake the tasks which wants to read if the buffer contains data. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001631 if (!channel_is_empty(si_oc(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001632 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001633
1634 /* Some data were injected in the buffer, notify the stream
1635 * interface.
1636 */
1637 if (!channel_is_empty(si_ic(si)))
1638 stream_int_update(si);
1639
1640 /* If write notifications are registered, we considers we want
Willy Tarreau3367d412018-11-15 10:57:41 +01001641 * to write, so we clear the blocking flag.
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001642 */
1643 if (notification_registered(&appctx->ctx.hlua_cosocket.wake_on_write))
Willy Tarreau3367d412018-11-15 10:57:41 +01001644 si_rx_endp_more(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001645}
1646
Willy Tarreau87b09662015-04-03 00:22:06 +02001647/* This function is called when the "struct stream" is destroyed.
1648 * Remove the link from the object to this stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001649 * Wake all the pending signals.
1650 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001651static void hlua_socket_release(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001652{
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001653 struct xref *peer;
1654
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001655 /* Remove my link in the original object. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001656 peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref);
1657 if (peer)
1658 xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001659
1660 /* Wake all the task waiting for me. */
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001661 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1662 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001663}
1664
1665/* If the garbage collectio of the object is launch, nobody
Willy Tarreau87b09662015-04-03 00:22:06 +02001666 * uses this object. If the stream does not exists, just quit.
1667 * Send the shutdown signal to the stream. In some cases,
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001668 * pending signal can rest in the read and write lists. destroy
1669 * it.
1670 */
1671__LJMP static int hlua_socket_gc(lua_State *L)
1672{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001673 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001674 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001675 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001676
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001677 MAY_LJMP(check_args(L, 1, "__gc"));
1678
1679 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001680 peer = xref_get_peer_and_lock(&socket->xref);
1681 if (!peer)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001682 return 0;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001683 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001684
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001685 /* Set the flag which destroy the session. */
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001686 appctx->ctx.hlua_cosocket.die = 1;
1687 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001688
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001689 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001690 xref_disconnect(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001691 return 0;
1692}
1693
1694/* The close function send shutdown signal and break the
Willy Tarreau87b09662015-04-03 00:22:06 +02001695 * links between the stream and the object.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001696 */
sada05ed3302018-05-11 11:48:18 -07001697__LJMP static int hlua_socket_close_helper(lua_State *L)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001698{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001699 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001700 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001701 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001702
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001703 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001704
1705 /* Check if we run on the same thread than the xreator thread.
1706 * We cannot access to the socket if the thread is different.
1707 */
1708 if (socket->tid != tid)
1709 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1710
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001711 peer = xref_get_peer_and_lock(&socket->xref);
1712 if (!peer)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001713 return 0;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001714 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001715
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001716 /* Set the flag which destroy the session. */
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001717 appctx->ctx.hlua_cosocket.die = 1;
1718 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001719
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001720 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001721 xref_disconnect(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001722 return 0;
1723}
1724
sada05ed3302018-05-11 11:48:18 -07001725/* The close function calls close_helper.
1726 */
1727__LJMP static int hlua_socket_close(lua_State *L)
1728{
1729 MAY_LJMP(check_args(L, 1, "close"));
1730 return hlua_socket_close_helper(L);
1731}
1732
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001733/* This Lua function assumes that the stack contain three parameters.
1734 * 1 - USERDATA containing a struct socket
1735 * 2 - INTEGER with values of the macro defined below
1736 * If the integer is -1, we must read at most one line.
1737 * If the integer is -2, we ust read all the data until the
1738 * end of the stream.
1739 * If the integer is positive value, we must read a number of
1740 * bytes corresponding to this value.
1741 */
1742#define HLSR_READ_LINE (-1)
1743#define HLSR_READ_ALL (-2)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001744__LJMP static int hlua_socket_receive_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001745{
1746 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
1747 int wanted = lua_tointeger(L, 2);
1748 struct hlua *hlua = hlua_gethlua(L);
1749 struct appctx *appctx;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001750 size_t len;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001751 int nblk;
Willy Tarreau206ba832018-06-14 15:27:31 +02001752 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001753 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02001754 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001755 size_t len2;
Thierry FOURNIER00543922015-03-09 18:35:06 +01001756 int skip_at_end = 0;
Willy Tarreau81389672015-03-10 12:03:52 +01001757 struct channel *oc;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001758 struct stream_interface *si;
1759 struct stream *s;
1760 struct xref *peer;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001761 int missing_bytes;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001762
1763 /* Check if this lua stack is schedulable. */
1764 if (!hlua || !hlua->task)
1765 WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in "
1766 "'frontend', 'backend' or 'task'"));
1767
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001768 /* Check if we run on the same thread than the xreator thread.
1769 * We cannot access to the socket if the thread is different.
1770 */
1771 if (socket->tid != tid)
1772 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1773
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001774 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001775 peer = xref_get_peer_and_lock(&socket->xref);
1776 if (!peer)
1777 goto no_peer;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001778 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
1779 si = appctx->owner;
1780 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001781
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001782 oc = &s->res;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001783 if (wanted == HLSR_READ_LINE) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001784 /* Read line. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001785 nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001786 if (nblk < 0) /* Connection close. */
1787 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001788 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001789 goto connection_empty;
Thierry FOURNIER00543922015-03-09 18:35:06 +01001790
1791 /* remove final \r\n. */
1792 if (nblk == 1) {
1793 if (blk1[len1-1] == '\n') {
1794 len1--;
1795 skip_at_end++;
1796 if (blk1[len1-1] == '\r') {
1797 len1--;
1798 skip_at_end++;
1799 }
1800 }
1801 }
1802 else {
1803 if (blk2[len2-1] == '\n') {
1804 len2--;
1805 skip_at_end++;
1806 if (blk2[len2-1] == '\r') {
1807 len2--;
1808 skip_at_end++;
1809 }
1810 }
1811 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001812 }
1813
1814 else if (wanted == HLSR_READ_ALL) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001815 /* Read all the available data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001816 nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001817 if (nblk < 0) /* Connection close. */
1818 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001819 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001820 goto connection_empty;
1821 }
1822
1823 else {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001824 /* Read a block of data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001825 nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001826 if (nblk < 0) /* Connection close. */
1827 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001828 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001829 goto connection_empty;
1830
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001831 missing_bytes = wanted - socket->b.n;
1832 if (len1 > missing_bytes) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001833 nblk = 1;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001834 len1 = missing_bytes;
1835 } if (nblk == 2 && len1 + len2 > missing_bytes)
1836 len2 = missing_bytes - len1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001837 }
1838
1839 len = len1;
1840
1841 luaL_addlstring(&socket->b, blk1, len1);
1842 if (nblk == 2) {
1843 len += len2;
1844 luaL_addlstring(&socket->b, blk2, len2);
1845 }
1846
1847 /* Consume data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001848 co_skip(oc, len + skip_at_end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001849
1850 /* Don't wait anything. */
Thierry FOURNIER7e4ee472018-05-25 15:03:50 +02001851 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001852
1853 /* If the pattern reclaim to read all the data
1854 * in the connection, got out.
1855 */
1856 if (wanted == HLSR_READ_ALL)
1857 goto connection_empty;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001858 else if (wanted >= 0 && socket->b.n < wanted)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001859 goto connection_empty;
1860
1861 /* Return result. */
1862 luaL_pushresult(&socket->b);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001863 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001864 return 1;
1865
1866connection_closed:
1867
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001868 xref_unlock(&socket->xref, peer);
1869
1870no_peer:
1871
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001872 /* If the buffer containds data. */
1873 if (socket->b.n > 0) {
1874 luaL_pushresult(&socket->b);
1875 return 1;
1876 }
1877 lua_pushnil(L);
1878 lua_pushstring(L, "connection closed.");
1879 return 2;
1880
1881connection_empty:
1882
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001883 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) {
1884 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001885 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001886 }
1887 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02001888 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_receive_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001889 return 0;
1890}
1891
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001892/* This Lua function gets two parameters. The first one can be string
1893 * or a number. If the string is "*l", the user requires one line. If
1894 * the string is "*a", the user requires all the contents of the stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001895 * If the value is a number, the user require a number of bytes equal
1896 * to the value. The default value is "*l" (a line).
1897 *
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001898 * This parameter with a variable type is converted in integer. This
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001899 * integer takes this values:
1900 * -1 : read a line
1901 * -2 : read all the stream
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001902 * >0 : amount of bytes.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001903 *
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001904 * The second parameter is optional. It contains a string that must be
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001905 * concatenated with the read data.
1906 */
1907__LJMP static int hlua_socket_receive(struct lua_State *L)
1908{
1909 int wanted = HLSR_READ_LINE;
1910 const char *pattern;
1911 int type;
1912 char *error;
1913 size_t len;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001914 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001915
1916 if (lua_gettop(L) < 1 || lua_gettop(L) > 3)
1917 WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments."));
1918
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001919 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001920
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001921 /* Check if we run on the same thread than the xreator thread.
1922 * We cannot access to the socket if the thread is different.
1923 */
1924 if (socket->tid != tid)
1925 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1926
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001927 /* check for pattern. */
1928 if (lua_gettop(L) >= 2) {
1929 type = lua_type(L, 2);
1930 if (type == LUA_TSTRING) {
1931 pattern = lua_tostring(L, 2);
1932 if (strcmp(pattern, "*a") == 0)
1933 wanted = HLSR_READ_ALL;
1934 else if (strcmp(pattern, "*l") == 0)
1935 wanted = HLSR_READ_LINE;
1936 else {
1937 wanted = strtoll(pattern, &error, 10);
1938 if (*error != '\0')
1939 WILL_LJMP(luaL_error(L, "Unsupported pattern."));
1940 }
1941 }
1942 else if (type == LUA_TNUMBER) {
1943 wanted = lua_tointeger(L, 2);
1944 if (wanted < 0)
1945 WILL_LJMP(luaL_error(L, "Unsupported size."));
1946 }
1947 }
1948
1949 /* Set pattern. */
1950 lua_pushinteger(L, wanted);
Tim Duesterhusc6e377e2018-01-04 19:32:13 +01001951
1952 /* Check if we would replace the top by itself. */
1953 if (lua_gettop(L) != 2)
1954 lua_replace(L, 2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001955
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001956 /* init buffer, and fill it with prefix. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001957 luaL_buffinit(L, &socket->b);
1958
1959 /* Check prefix. */
1960 if (lua_gettop(L) >= 3) {
1961 if (lua_type(L, 3) != LUA_TSTRING)
1962 WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix"));
1963 pattern = lua_tolstring(L, 3, &len);
1964 luaL_addlstring(&socket->b, pattern, len);
1965 }
1966
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001967 return __LJMP(hlua_socket_receive_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001968}
1969
1970/* Write the Lua input string in the output buffer.
Mark Lakes22154b42018-01-29 14:38:40 -08001971 * This function returns a yield if no space is available.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001972 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001973static int hlua_socket_write_yield(struct lua_State *L,int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001974{
1975 struct hlua_socket *socket;
1976 struct hlua *hlua = hlua_gethlua(L);
1977 struct appctx *appctx;
1978 size_t buf_len;
1979 const char *buf;
1980 int len;
1981 int send_len;
1982 int sent;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001983 struct xref *peer;
1984 struct stream_interface *si;
1985 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001986
1987 /* Check if this lua stack is schedulable. */
1988 if (!hlua || !hlua->task)
1989 WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in "
1990 "'frontend', 'backend' or 'task'"));
1991
1992 /* Get object */
1993 socket = MAY_LJMP(hlua_checksocket(L, 1));
1994 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001995 sent = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001996
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001997 /* Check if we run on the same thread than the xreator thread.
1998 * We cannot access to the socket if the thread is different.
1999 */
2000 if (socket->tid != tid)
2001 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2002
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002003 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002004 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002005 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002006 lua_pushinteger(L, -1);
2007 return 1;
2008 }
2009 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2010 si = appctx->owner;
2011 s = si_strm(si);
2012
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002013 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002014 if (channel_output_closed(&s->req)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002015 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002016 lua_pushinteger(L, -1);
2017 return 1;
2018 }
2019
2020 /* Update the input buffer data. */
2021 buf += sent;
2022 send_len = buf_len - sent;
2023
2024 /* All the data are sent. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002025 if (sent >= buf_len) {
2026 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002027 return 1; /* Implicitly return the length sent. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002028 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002029
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002030 /* Check if the buffer is available because HAProxy doesn't allocate
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01002031 * the request buffer if its not required.
2032 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002033 if (s->req.buf.size == 0) {
Willy Tarreau581abd32018-10-25 10:21:41 +02002034 if (!si_alloc_ibuf(si, &appctx->buffer_wait))
Christopher Faulet33834b12016-12-19 09:29:06 +01002035 goto hlua_socket_write_yield_return;
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01002036 }
2037
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002038 /* Check for available space. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002039 len = b_room(&s->req.buf);
Christopher Faulet33834b12016-12-19 09:29:06 +01002040 if (len <= 0) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002041 goto hlua_socket_write_yield_return;
Christopher Faulet33834b12016-12-19 09:29:06 +01002042 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002043
2044 /* send data */
2045 if (len < send_len)
2046 send_len = len;
Thierry FOURNIER66b89192018-05-27 01:14:47 +02002047 len = ci_putblk(&s->req, buf, send_len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002048
2049 /* "Not enough space" (-1), "Buffer too little to contain
2050 * the data" (-2) are not expected because the available length
2051 * is tested.
2052 * Other unknown error are also not expected.
2053 */
2054 if (len <= 0) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01002055 if (len == -1)
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002056 s->req.flags |= CF_WAKE_WRITE;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002057
sada05ed3302018-05-11 11:48:18 -07002058 MAY_LJMP(hlua_socket_close_helper(L));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002059 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002060 lua_pushinteger(L, -1);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002061 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002062 return 1;
2063 }
2064
2065 /* update buffers. */
Thierry FOURNIER101b9762018-05-27 01:27:40 +02002066 appctx_wakeup(appctx);
Willy Tarreaude70fa12015-09-26 11:25:05 +02002067
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002068 s->req.rex = TICK_ETERNITY;
2069 s->res.wex = TICK_ETERNITY;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002070
2071 /* Update length sent. */
2072 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002073 lua_pushinteger(L, sent + len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002074
2075 /* All the data buffer is sent ? */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002076 if (sent + len >= buf_len) {
2077 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002078 return 1;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002079 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002080
2081hlua_socket_write_yield_return:
Thierry FOURNIERba42fcd2018-05-27 00:59:48 +02002082 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2083 xref_unlock(&socket->xref, peer);
2084 WILL_LJMP(luaL_error(L, "out of memory"));
2085 }
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002086 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002087 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_write_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002088 return 0;
2089}
2090
2091/* This function initiate the send of data. It just check the input
2092 * parameters and push an integer in the Lua stack that contain the
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002093 * amount of data written to the buffer. This is used by the function
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002094 * "hlua_socket_write_yield" that can yield.
2095 *
2096 * The Lua function gets between 3 and 4 parameters. The first one is
2097 * the associated object. The second is a string buffer. The third is
2098 * a facultative integer that represents where is the buffer position
2099 * of the start of the data that can send. The first byte is the
2100 * position "1". The default value is "1". The fourth argument is a
2101 * facultative integer that represents where is the buffer position
2102 * of the end of the data that can send. The default is the last byte.
2103 */
2104static int hlua_socket_send(struct lua_State *L)
2105{
2106 int i;
2107 int j;
2108 const char *buf;
2109 size_t buf_len;
2110
2111 /* Check number of arguments. */
2112 if (lua_gettop(L) < 2 || lua_gettop(L) > 4)
2113 WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments"));
2114
2115 /* Get the string. */
2116 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
2117
2118 /* Get and check j. */
2119 if (lua_gettop(L) == 4) {
2120 j = MAY_LJMP(luaL_checkinteger(L, 4));
2121 if (j < 0)
2122 j = buf_len + j + 1;
2123 if (j > buf_len)
2124 j = buf_len + 1;
2125 lua_pop(L, 1);
2126 }
2127 else
2128 j = buf_len;
2129
2130 /* Get and check i. */
2131 if (lua_gettop(L) == 3) {
2132 i = MAY_LJMP(luaL_checkinteger(L, 3));
2133 if (i < 0)
2134 i = buf_len + i + 1;
2135 if (i > buf_len)
2136 i = buf_len + 1;
2137 lua_pop(L, 1);
2138 } else
2139 i = 1;
2140
2141 /* Check bth i and j. */
2142 if (i > j) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002143 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002144 return 1;
2145 }
2146 if (i == 0 && j == 0) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002147 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002148 return 1;
2149 }
2150 if (i == 0)
2151 i = 1;
2152 if (j == 0)
2153 j = 1;
2154
2155 /* Pop the string. */
2156 lua_pop(L, 1);
2157
2158 /* Update the buffer length. */
2159 buf += i - 1;
2160 buf_len = j - i + 1;
2161 lua_pushlstring(L, buf, buf_len);
2162
2163 /* This unsigned is used to remember the amount of sent data. */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002164 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002165
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002166 return MAY_LJMP(hlua_socket_write_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002167}
2168
Willy Tarreau22b0a682015-06-17 19:43:49 +02002169#define SOCKET_INFO_MAX_LEN sizeof("[0000:0000:0000:0000:0000:0000:0000:0000]:12345")
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002170__LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr)
2171{
2172 static char buffer[SOCKET_INFO_MAX_LEN];
2173 int ret;
2174 int len;
2175 char *p;
2176
2177 ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1);
2178 if (ret <= 0) {
2179 lua_pushnil(L);
2180 return 1;
2181 }
2182
2183 if (ret == AF_UNIX) {
2184 lua_pushstring(L, buffer+1);
2185 return 1;
2186 }
2187 else if (ret == AF_INET6) {
2188 buffer[0] = '[';
2189 len = strlen(buffer);
2190 buffer[len] = ']';
2191 len++;
2192 buffer[len] = ':';
2193 len++;
2194 p = buffer;
2195 }
2196 else if (ret == AF_INET) {
2197 p = buffer + 1;
2198 len = strlen(p);
2199 p[len] = ':';
2200 len++;
2201 }
2202 else {
2203 lua_pushnil(L);
2204 return 1;
2205 }
2206
2207 if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) {
2208 lua_pushnil(L);
2209 return 1;
2210 }
2211
2212 lua_pushstring(L, p);
2213 return 1;
2214}
2215
2216/* Returns information about the peer of the connection. */
2217__LJMP static int hlua_socket_getpeername(struct lua_State *L)
2218{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002219 struct hlua_socket *socket;
2220 struct connection *conn;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002221 struct xref *peer;
2222 struct appctx *appctx;
2223 struct stream_interface *si;
2224 struct stream *s;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002225 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002226
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002227 MAY_LJMP(check_args(L, 1, "getpeername"));
2228
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002229 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002230
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002231 /* Check if we run on the same thread than the xreator thread.
2232 * We cannot access to the socket if the thread is different.
2233 */
2234 if (socket->tid != tid)
2235 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2236
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002237 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002238 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002239 if (!peer) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002240 lua_pushnil(L);
2241 return 1;
2242 }
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002243 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2244 si = appctx->owner;
2245 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002246
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002247 conn = cs_conn(objt_cs(s->si[1].end));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002248 if (!conn) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002249 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002250 lua_pushnil(L);
2251 return 1;
2252 }
2253
Willy Tarreaua71f6422016-11-16 17:00:14 +01002254 conn_get_to_addr(conn);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002255 if (!(conn->flags & CO_FL_ADDR_TO_SET)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002256 xref_unlock(&socket->xref, peer);
Willy Tarreaua71f6422016-11-16 17:00:14 +01002257 lua_pushnil(L);
2258 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002259 }
2260
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002261 ret = MAY_LJMP(hlua_socket_info(L, &conn->addr.to));
2262 xref_unlock(&socket->xref, peer);
2263 return ret;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002264}
2265
2266/* Returns information about my connection side. */
2267static int hlua_socket_getsockname(struct lua_State *L)
2268{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002269 struct hlua_socket *socket;
2270 struct connection *conn;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002271 struct appctx *appctx;
2272 struct xref *peer;
2273 struct stream_interface *si;
2274 struct stream *s;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002275 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002276
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002277 MAY_LJMP(check_args(L, 1, "getsockname"));
2278
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002279 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002280
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002281 /* Check if we run on the same thread than the xreator thread.
2282 * We cannot access to the socket if the thread is different.
2283 */
2284 if (socket->tid != tid)
2285 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2286
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002287 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002288 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002289 if (!peer) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002290 lua_pushnil(L);
2291 return 1;
2292 }
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002293 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2294 si = appctx->owner;
2295 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002296
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002297 conn = cs_conn(objt_cs(s->si[1].end));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002298 if (!conn) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002299 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002300 lua_pushnil(L);
2301 return 1;
2302 }
2303
Willy Tarreaua71f6422016-11-16 17:00:14 +01002304 conn_get_from_addr(conn);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002305 if (!(conn->flags & CO_FL_ADDR_FROM_SET)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002306 xref_unlock(&socket->xref, peer);
Willy Tarreaua71f6422016-11-16 17:00:14 +01002307 lua_pushnil(L);
2308 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002309 }
2310
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002311 ret = hlua_socket_info(L, &conn->addr.from);
2312 xref_unlock(&socket->xref, peer);
2313 return ret;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002314}
2315
2316/* This struct define the applet. */
Willy Tarreau30576452015-04-13 13:50:30 +02002317static struct applet update_applet = {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002318 .obj_type = OBJ_TYPE_APPLET,
2319 .name = "<LUA_TCP>",
2320 .fct = hlua_socket_handler,
2321 .release = hlua_socket_release,
2322};
2323
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002324__LJMP static int hlua_socket_connect_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002325{
2326 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
2327 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002328 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002329 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002330 struct stream_interface *si;
2331 struct stream *s;
2332
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002333 /* Check if we run on the same thread than the xreator thread.
2334 * We cannot access to the socket if the thread is different.
2335 */
2336 if (socket->tid != tid)
2337 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2338
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002339 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002340 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002341 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002342 lua_pushnil(L);
2343 lua_pushstring(L, "Can't connect");
2344 return 2;
2345 }
2346 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2347 si = appctx->owner;
2348 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002349
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002350 /* Check if we run on the same thread than the xreator thread.
2351 * We cannot access to the socket if the thread is different.
2352 */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002353 if (socket->tid != tid) {
2354 xref_unlock(&socket->xref, peer);
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002355 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002356 }
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002357
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002358 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002359 if (!hlua || channel_output_closed(&s->req)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002360 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002361 lua_pushnil(L);
2362 lua_pushstring(L, "Can't connect");
2363 return 2;
2364 }
2365
Willy Tarreaue09101e2018-10-16 17:37:12 +02002366 appctx = __objt_appctx(s->si[0].end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002367
2368 /* Check for connection established. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002369 if (appctx->ctx.hlua_cosocket.connected) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002370 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002371 lua_pushinteger(L, 1);
2372 return 1;
2373 }
2374
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002375 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2376 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002377 WILL_LJMP(luaL_error(L, "out of memory error"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002378 }
2379 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002380 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002381 return 0;
2382}
2383
2384/* This function fail or initite the connection. */
2385__LJMP static int hlua_socket_connect(struct lua_State *L)
2386{
2387 struct hlua_socket *socket;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002388 int port = -1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002389 const char *ip;
2390 struct connection *conn;
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002391 struct hlua *hlua;
2392 struct appctx *appctx;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002393 int low, high;
2394 struct sockaddr_storage *addr;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002395 struct xref *peer;
2396 struct stream_interface *si;
2397 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002398
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002399 if (lua_gettop(L) < 2)
2400 WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments"));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002401
2402 /* Get args. */
2403 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002404
2405 /* Check if we run on the same thread than the xreator thread.
2406 * We cannot access to the socket if the thread is different.
2407 */
2408 if (socket->tid != tid)
2409 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2410
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002411 ip = MAY_LJMP(luaL_checkstring(L, 2));
Tim Duesterhus6edab862018-01-06 19:04:45 +01002412 if (lua_gettop(L) >= 3) {
2413 luaL_Buffer b;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002414 port = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002415
Tim Duesterhus6edab862018-01-06 19:04:45 +01002416 /* Force the ip to end with a colon, to support IPv6 addresses
2417 * that are not enclosed within square brackets.
2418 */
2419 if (port > 0) {
2420 luaL_buffinit(L, &b);
2421 luaL_addstring(&b, ip);
2422 luaL_addchar(&b, ':');
2423 luaL_pushresult(&b);
2424 ip = lua_tolstring(L, lua_gettop(L), NULL);
2425 }
2426 }
2427
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002428 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002429 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002430 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002431 lua_pushnil(L);
2432 return 1;
2433 }
2434 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2435 si = appctx->owner;
2436 s = si_strm(si);
2437
2438 /* Initialise connection. */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002439 conn = cs_conn(si_alloc_cs(&s->si[1], NULL));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002440 if (!conn) {
2441 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002442 WILL_LJMP(luaL_error(L, "connect: internal error"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002443 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002444
Willy Tarreau3adac082015-09-26 17:51:09 +02002445 /* needed for the connection not to be closed */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002446 conn->target = s->target;
Willy Tarreau3adac082015-09-26 17:51:09 +02002447
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002448 /* Parse ip address. */
Willy Tarreau48ef4c92017-01-06 18:32:38 +01002449 addr = str2sa_range(ip, NULL, &low, &high, NULL, NULL, NULL, 0);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002450 if (!addr) {
2451 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002452 WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002453 }
2454 if (low != high) {
2455 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002456 WILL_LJMP(luaL_error(L, "connect: port ranges not supported : address '%s'", ip));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002457 }
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002458 memcpy(&conn->addr.to, addr, sizeof(struct sockaddr_storage));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002459
2460 /* Set port. */
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002461 if (low == 0) {
2462 if (conn->addr.to.ss_family == AF_INET) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002463 if (port == -1) {
2464 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002465 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002466 }
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002467 ((struct sockaddr_in *)&conn->addr.to)->sin_port = htons(port);
2468 } else if (conn->addr.to.ss_family == AF_INET6) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002469 if (port == -1) {
2470 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002471 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002472 }
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002473 ((struct sockaddr_in6 *)&conn->addr.to)->sin6_port = htons(port);
2474 }
2475 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002476
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002477 hlua = hlua_gethlua(L);
Willy Tarreaue09101e2018-10-16 17:37:12 +02002478 appctx = __objt_appctx(s->si[0].end);
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002479
2480 /* inform the stream that we want to be notified whenever the
2481 * connection completes.
2482 */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01002483 si_cant_get(&s->si[0]);
Willy Tarreau3367d412018-11-15 10:57:41 +01002484 si_rx_endp_more(&s->si[0]);
Thierry FOURNIER8c8fbbe2015-09-26 17:02:35 +02002485 appctx_wakeup(appctx);
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002486
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +02002487 hlua->flags |= HLUA_MUST_GC;
2488
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002489 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2490 xref_unlock(&socket->xref, peer);
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002491 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002492 }
2493 xref_unlock(&socket->xref, peer);
PiBa-NL706d5ee2018-05-05 23:51:42 +02002494
2495 task_wakeup(s->task, TASK_WOKEN_INIT);
2496 /* Return yield waiting for connection. */
2497
Willy Tarreau9635e032018-10-16 17:52:55 +02002498 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002499
2500 return 0;
2501}
2502
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002503#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002504__LJMP static int hlua_socket_connect_ssl(struct lua_State *L)
2505{
2506 struct hlua_socket *socket;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002507 struct xref *peer;
2508 struct appctx *appctx;
2509 struct stream_interface *si;
2510 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002511
2512 MAY_LJMP(check_args(L, 3, "connect_ssl"));
2513 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002514
2515 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002516 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002517 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002518 lua_pushnil(L);
2519 return 1;
2520 }
2521 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2522 si = appctx->owner;
2523 s = si_strm(si);
2524
2525 s->target = &socket_ssl.obj_type;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002526 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002527 return MAY_LJMP(hlua_socket_connect(L));
2528}
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002529#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002530
2531__LJMP static int hlua_socket_setoption(struct lua_State *L)
2532{
2533 return 0;
2534}
2535
2536__LJMP static int hlua_socket_settimeout(struct lua_State *L)
2537{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002538 struct hlua_socket *socket;
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002539 int tmout;
Mark Lakes56cc1252018-03-27 09:48:06 +02002540 double dtmout;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002541 struct xref *peer;
2542 struct appctx *appctx;
2543 struct stream_interface *si;
2544 struct stream *s;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002545
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002546 MAY_LJMP(check_args(L, 2, "settimeout"));
2547
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002548 socket = MAY_LJMP(hlua_checksocket(L, 1));
Mark Lakes56cc1252018-03-27 09:48:06 +02002549
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002550 /* convert the timeout to millis */
2551 dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002552
Thierry Fournier17a921b2018-03-08 09:59:02 +01002553 /* Check for negative values */
Mark Lakes56cc1252018-03-27 09:48:06 +02002554 if (dtmout < 0)
Thierry Fournier17a921b2018-03-08 09:59:02 +01002555 WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values"));
2556
Mark Lakes56cc1252018-03-27 09:48:06 +02002557 if (dtmout > INT_MAX) /* overflow check */
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002558 WILL_LJMP(luaL_error(L, "settimeout: cannot set values larger than %d ms", INT_MAX));
Mark Lakes56cc1252018-03-27 09:48:06 +02002559
2560 tmout = MS_TO_TICKS((int)dtmout);
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002561 if (tmout == 0)
2562 tmout++; /* very small timeouts are adjusted to a minium of 1ms */
Mark Lakes56cc1252018-03-27 09:48:06 +02002563
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002564 /* Check if we run on the same thread than the xreator thread.
2565 * We cannot access to the socket if the thread is different.
2566 */
2567 if (socket->tid != tid)
2568 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2569
Mark Lakes56cc1252018-03-27 09:48:06 +02002570 /* check for connection break. If some data were read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002571 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002572 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002573 hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts.");
2574 WILL_LJMP(lua_error(L));
2575 return 0;
2576 }
2577 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2578 si = appctx->owner;
2579 s = si_strm(si);
2580
Cyril Bonté7bb63452018-08-17 23:51:02 +02002581 s->sess->fe->timeout.connect = tmout;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002582 s->req.rto = tmout;
2583 s->req.wto = tmout;
2584 s->res.rto = tmout;
2585 s->res.wto = tmout;
Cyril Bonté7bb63452018-08-17 23:51:02 +02002586 s->req.rex = tick_add_ifset(now_ms, tmout);
2587 s->req.wex = tick_add_ifset(now_ms, tmout);
2588 s->res.rex = tick_add_ifset(now_ms, tmout);
2589 s->res.wex = tick_add_ifset(now_ms, tmout);
2590
2591 s->task->expire = tick_add_ifset(now_ms, tmout);
2592 task_queue(s->task);
2593
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002594 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002595
Thierry Fourniere9636f12018-03-08 09:54:32 +01002596 lua_pushinteger(L, 1);
Tim Duesterhus119a5f12018-01-06 19:16:25 +01002597 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002598}
2599
2600__LJMP static int hlua_socket_new(lua_State *L)
2601{
2602 struct hlua_socket *socket;
2603 struct appctx *appctx;
Willy Tarreau15b5e142015-04-04 14:38:25 +02002604 struct session *sess;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002605 struct stream *strm;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002606
2607 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002608 if (!lua_checkstack(L, 3)) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002609 hlua_pusherror(L, "socket: full stack");
2610 goto out_fail_conf;
2611 }
2612
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002613 /* Create the object: obj[0] = userdata. */
2614 lua_newtable(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002615 socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket)));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002616 lua_rawseti(L, -2, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002617 memset(socket, 0, sizeof(*socket));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002618 socket->tid = tid;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002619
Thierry FOURNIER4a6170c2015-03-09 17:07:10 +01002620 /* Check if the various memory pools are intialized. */
Willy Tarreaubafbe012017-11-24 17:34:44 +01002621 if (!pool_head_stream || !pool_head_buffer) {
Thierry FOURNIER4a6170c2015-03-09 17:07:10 +01002622 hlua_pusherror(L, "socket: uninitialized pools.");
2623 goto out_fail_conf;
2624 }
2625
Willy Tarreau87b09662015-04-03 00:22:06 +02002626 /* Pop a class stream metatable and affect it to the userdata. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002627 lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref);
2628 lua_setmetatable(L, -2);
2629
Willy Tarreaud420a972015-04-06 00:39:18 +02002630 /* Create the applet context */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01002631 appctx = appctx_new(&update_applet, tid_bit);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002632 if (!appctx) {
2633 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaufeb76402015-04-03 14:10:06 +02002634 goto out_fail_conf;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002635 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002636
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002637 appctx->ctx.hlua_cosocket.connected = 0;
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02002638 appctx->ctx.hlua_cosocket.die = 0;
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002639 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write);
2640 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read);
Willy Tarreaub2bf8332015-04-04 15:58:58 +02002641
Willy Tarreaud420a972015-04-06 00:39:18 +02002642 /* Now create a session, task and stream for this applet */
2643 sess = session_new(&socket_proxy, NULL, &appctx->obj_type);
2644 if (!sess) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002645 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002646 goto out_fail_sess;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002647 }
2648
Willy Tarreau87787ac2017-08-28 16:22:54 +02002649 strm = stream_new(sess, &appctx->obj_type);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002650 if (!strm) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002651 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002652 goto out_fail_stream;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002653 }
2654
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002655 /* Initialise cross reference between stream and Lua socket object. */
2656 xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002657
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002658 /* Configure "right" stream interface. this "si" is used to connect
2659 * and retrieve data from the server. The connection is initialized
2660 * with the "struct server".
2661 */
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002662 si_set_state(&strm->si[1], SI_ST_ASS);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002663
2664 /* Force destination server. */
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002665 strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET | SF_BE_ASSIGNED;
2666 strm->target = &socket_tcp.obj_type;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002667
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002668 return 1;
2669
Willy Tarreaud420a972015-04-06 00:39:18 +02002670 out_fail_stream:
Willy Tarreau11c36242015-04-04 15:54:03 +02002671 session_free(sess);
Willy Tarreaud420a972015-04-06 00:39:18 +02002672 out_fail_sess:
2673 appctx_free(appctx);
2674 out_fail_conf:
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002675 WILL_LJMP(lua_error(L));
2676 return 0;
2677}
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01002678
2679/*
2680 *
2681 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002682 * Class Channel
2683 *
2684 *
2685 */
2686
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002687/* The state between the channel data and the HTTP parser state can be
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002688 * inconsistent, so reset the parser and call it again. Warning, this
2689 * action does not revalidate the request and does not send a 400 if the modified
2690 * request is not valid.
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002691 *
Thierry FOURNIER6e01f382015-11-02 09:52:54 +01002692 * This function never fails. The direction is set using dir, which equals
2693 * either SMP_OPT_DIR_REQ or SMP_OPT_DIR_RES.
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002694 */
2695static void hlua_resynchonize_proto(struct stream *stream, int dir)
2696{
2697 /* Protocol HTTP. */
2698 if (stream->be->mode == PR_MODE_HTTP) {
2699
Thierry FOURNIER6e01f382015-11-02 09:52:54 +01002700 if (dir == SMP_OPT_DIR_REQ)
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002701 http_txn_reset_req(stream->txn);
Thierry FOURNIER6e01f382015-11-02 09:52:54 +01002702 else if (dir == SMP_OPT_DIR_RES)
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002703 http_txn_reset_res(stream->txn);
2704
2705 if (stream->txn->hdr_idx.v)
2706 hdr_idx_init(&stream->txn->hdr_idx);
2707
Thierry FOURNIER6e01f382015-11-02 09:52:54 +01002708 if (dir == SMP_OPT_DIR_REQ)
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002709 http_msg_analyzer(&stream->txn->req, &stream->txn->hdr_idx);
Thierry FOURNIER6e01f382015-11-02 09:52:54 +01002710 else if (dir == SMP_OPT_DIR_RES)
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002711 http_msg_analyzer(&stream->txn->rsp, &stream->txn->hdr_idx);
2712 }
2713}
2714
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002715/* This function is called before the Lua execution. It stores
2716 * the differents parsers state before executing some Lua code.
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002717 */
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002718static inline void consistency_set(struct stream *stream, int opt, struct hlua_consistency *c)
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002719{
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002720 c->mode = stream->be->mode;
2721 switch (c->mode) {
2722 case PR_MODE_HTTP:
2723 c->data.http.dir = opt & SMP_OPT_DIR;
2724 if (c->data.http.dir == SMP_OPT_DIR_REQ)
2725 c->data.http.state = stream->txn->req.msg_state;
2726 else
2727 c->data.http.state = stream->txn->rsp.msg_state;
2728 break;
2729 default:
2730 break;
2731 }
2732}
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002733
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002734/* This function is called after the Lua execution. it
2735 * returns true if the parser state is consistent, otherwise,
2736 * it return false.
2737 *
2738 * In HTTP mode, the parser state must be in the same state
2739 * or greater when we exit the function. Even if we do a
2740 * control yield. This prevent to break the HTTP message
2741 * from the Lua code.
2742 */
2743static inline int consistency_check(struct stream *stream, int opt, struct hlua_consistency *c)
2744{
2745 if (c->mode != stream->be->mode)
2746 return 0;
2747
2748 switch (c->mode) {
2749 case PR_MODE_HTTP:
2750 if (c->data.http.dir != (opt & SMP_OPT_DIR))
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002751 return 0;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002752 if (c->data.http.dir == SMP_OPT_DIR_REQ)
2753 return stream->txn->req.msg_state >= c->data.http.state;
2754 else
2755 return stream->txn->rsp.msg_state >= c->data.http.state;
2756 default:
2757 return 1;
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002758 }
2759 return 1;
2760}
2761
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002762/* Returns the struct hlua_channel join to the class channel in the
2763 * stack entry "ud" or throws an argument error.
2764 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002765__LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002766{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02002767 return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002768}
2769
Willy Tarreau47860ed2015-03-10 14:07:50 +01002770/* Pushes the channel onto the top of the stack. If the stask does not have a
2771 * free slots, the function fails and returns 0;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002772 */
Willy Tarreau2a71af42015-03-10 13:51:50 +01002773static int hlua_channel_new(lua_State *L, struct channel *channel)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002774{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002775 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002776 if (!lua_checkstack(L, 3))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002777 return 0;
2778
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002779 lua_newtable(L);
Willy Tarreau47860ed2015-03-10 14:07:50 +01002780 lua_pushlightuserdata(L, channel);
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002781 lua_rawseti(L, -2, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002782
2783 /* Pop a class sesison metatable and affect it to the userdata. */
2784 lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref);
2785 lua_setmetatable(L, -2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002786 return 1;
2787}
2788
2789/* Duplicate all the data present in the input channel and put it
2790 * in a string LUA variables. Returns -1 and push a nil value in
2791 * the stack if the channel is closed and all the data are consumed,
2792 * returns 0 if no data are available, otherwise it returns the length
2793 * of the builded string.
2794 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002795static inline int _hlua_channel_dup(struct channel *chn, lua_State *L)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002796{
2797 char *blk1;
2798 char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02002799 size_t len1;
2800 size_t len2;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002801 int ret;
2802 luaL_Buffer b;
2803
Willy Tarreau06d80a92017-10-19 14:32:15 +02002804 ret = ci_getblk_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002805 if (unlikely(ret == 0))
2806 return 0;
2807
2808 if (unlikely(ret < 0)) {
2809 lua_pushnil(L);
2810 return -1;
2811 }
2812
2813 luaL_buffinit(L, &b);
2814 luaL_addlstring(&b, blk1, len1);
2815 if (unlikely(ret == 2))
2816 luaL_addlstring(&b, blk2, len2);
2817 luaL_pushresult(&b);
2818
2819 if (unlikely(ret == 2))
2820 return len1 + len2;
2821 return len1;
2822}
2823
2824/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2825 * a yield. This function keep the data in the buffer.
2826 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002827__LJMP static int hlua_channel_dup_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002828{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002829 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002830
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002831 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2832
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002833 if (_hlua_channel_dup(chn, L) == 0)
Willy Tarreau9635e032018-10-16 17:52:55 +02002834 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_dup_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002835 return 1;
2836}
2837
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002838/* Check arguments for the function "hlua_channel_dup_yield". */
2839__LJMP static int hlua_channel_dup(lua_State *L)
2840{
2841 MAY_LJMP(check_args(L, 1, "dup"));
2842 MAY_LJMP(hlua_checkchannel(L, 1));
2843 return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0));
2844}
2845
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002846/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2847 * a yield. This function consumes the data in the buffer. It returns
2848 * a string containing the data or a nil pointer if no data are available
2849 * and the channel is closed.
2850 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002851__LJMP static int hlua_channel_get_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002852{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002853 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002854 int ret;
2855
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002856 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002857
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002858 ret = _hlua_channel_dup(chn, L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002859 if (unlikely(ret == 0))
Willy Tarreau9635e032018-10-16 17:52:55 +02002860 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002861
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002862 if (unlikely(ret == -1))
2863 return 1;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002864
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002865 b_sub(&chn->buf, ret);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002866 hlua_resynchonize_proto(chn_strm(chn), !!(chn->flags & CF_ISRESP));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002867 return 1;
2868}
2869
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002870/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002871__LJMP static int hlua_channel_get(lua_State *L)
2872{
2873 MAY_LJMP(check_args(L, 1, "get"));
2874 MAY_LJMP(hlua_checkchannel(L, 1));
2875 return MAY_LJMP(hlua_channel_get_yield(L, 0, 0));
2876}
2877
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002878/* This functions consumes and returns one line. If the channel is closed,
2879 * and the last data does not contains a final '\n', the data are returned
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002880 * without the final '\n'. When no more data are available, it returns nil
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002881 * value.
2882 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002883__LJMP static int hlua_channel_getline_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002884{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002885 char *blk1;
2886 char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02002887 size_t len1;
2888 size_t len2;
2889 size_t len;
Willy Tarreau47860ed2015-03-10 14:07:50 +01002890 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002891 int ret;
2892 luaL_Buffer b;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002893
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002894 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2895
Willy Tarreau06d80a92017-10-19 14:32:15 +02002896 ret = ci_getline_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002897 if (ret == 0)
Willy Tarreau9635e032018-10-16 17:52:55 +02002898 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_getline_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002899
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002900 if (ret == -1) {
2901 lua_pushnil(L);
2902 return 1;
2903 }
2904
2905 luaL_buffinit(L, &b);
2906 luaL_addlstring(&b, blk1, len1);
2907 len = len1;
2908 if (unlikely(ret == 2)) {
2909 luaL_addlstring(&b, blk2, len2);
2910 len += len2;
2911 }
2912 luaL_pushresult(&b);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002913 b_rep_blk(&chn->buf, ci_head(chn), ci_head(chn) + len, NULL, 0);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002914 hlua_resynchonize_proto(chn_strm(chn), !!(chn->flags & CF_ISRESP));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002915 return 1;
2916}
2917
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002918/* Check arguments for the function "hlua_channel_getline_yield". */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002919__LJMP static int hlua_channel_getline(lua_State *L)
2920{
2921 MAY_LJMP(check_args(L, 1, "getline"));
2922 MAY_LJMP(hlua_checkchannel(L, 1));
2923 return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0));
2924}
2925
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002926/* This function takes a string as input, and append it at the
2927 * input side of channel. If the data is too big, but a space
2928 * is probably available after sending some data, the function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002929 * yields. If the data is bigger than the buffer, or if the
2930 * channel is closed, it returns -1. Otherwise, it returns the
2931 * amount of data written.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002932 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002933__LJMP static int hlua_channel_append_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002934{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002935 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002936 size_t len;
2937 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
2938 int l = MAY_LJMP(luaL_checkinteger(L, 3));
2939 int ret;
2940 int max;
2941
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002942 /* Check if the buffer is available because HAProxy doesn't allocate
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002943 * the request buffer if its not required.
2944 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002945 if (chn->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +01002946 si_rx_buff_blk(chn_prod(chn));
Willy Tarreau9635e032018-10-16 17:52:55 +02002947 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002948 }
2949
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002950 max = channel_recv_limit(chn) - b_data(&chn->buf);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002951 if (max > len - l)
2952 max = len - l;
2953
Willy Tarreau06d80a92017-10-19 14:32:15 +02002954 ret = ci_putblk(chn, str + l, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002955 if (ret == -2 || ret == -3) {
2956 lua_pushinteger(L, -1);
2957 return 1;
2958 }
Willy Tarreaubc18da12015-03-13 14:00:47 +01002959 if (ret == -1) {
2960 chn->flags |= CF_WAKE_WRITE;
Willy Tarreau9635e032018-10-16 17:52:55 +02002961 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Willy Tarreaubc18da12015-03-13 14:00:47 +01002962 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002963 l += ret;
2964 lua_pop(L, 1);
2965 lua_pushinteger(L, l);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002966 hlua_resynchonize_proto(chn_strm(chn), !!(chn->flags & CF_ISRESP));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002967
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002968 max = channel_recv_limit(chn) - b_data(&chn->buf);
Willy Tarreaua79021a2018-06-15 18:07:57 +02002969 if (max == 0 && co_data(chn) == 0) {
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002970 /* There are no space available, and the output buffer is empty.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002971 * in this case, we cannot add more data, so we cannot yield,
2972 * we return the amount of copyied data.
2973 */
2974 return 1;
2975 }
2976 if (l < len)
Willy Tarreau9635e032018-10-16 17:52:55 +02002977 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002978 return 1;
2979}
2980
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002981/* Just a wrapper of "hlua_channel_append_yield". It returns the length
2982 * of the written string, or -1 if the channel is closed or if the
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002983 * buffer size is too little for the data.
2984 */
2985__LJMP static int hlua_channel_append(lua_State *L)
2986{
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002987 size_t len;
2988
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002989 MAY_LJMP(check_args(L, 2, "append"));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002990 MAY_LJMP(hlua_checkchannel(L, 1));
2991 MAY_LJMP(luaL_checklstring(L, 2, &len));
2992 MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002993 lua_pushinteger(L, 0);
2994
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002995 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002996}
2997
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002998/* Just a wrapper of "hlua_channel_append_yield". This wrapper starts
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002999 * his process by cleaning the buffer. The result is a replacement
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003000 * of the current data. It returns the length of the written string,
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003001 * or -1 if the channel is closed or if the buffer size is too
3002 * little for the data.
3003 */
3004__LJMP static int hlua_channel_set(lua_State *L)
3005{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003006 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003007
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003008 MAY_LJMP(check_args(L, 2, "set"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003009 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003010 lua_pushinteger(L, 0);
3011
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003012 b_set_data(&chn->buf, co_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003013
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003014 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003015}
3016
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003017/* Append data in the output side of the buffer. This data is immediately
3018 * sent. The function returns the amount of data written. If the buffer
3019 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003020 * if the channel is closed.
3021 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003022__LJMP static int hlua_channel_send_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003023{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003024 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003025 size_t len;
3026 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
3027 int l = MAY_LJMP(luaL_checkinteger(L, 3));
3028 int max;
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003029 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003030
Willy Tarreau47860ed2015-03-10 14:07:50 +01003031 if (unlikely(channel_output_closed(chn))) {
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003032 lua_pushinteger(L, -1);
3033 return 1;
3034 }
3035
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003036 /* Check if the buffer is available because HAProxy doesn't allocate
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01003037 * the request buffer if its not required.
3038 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003039 if (chn->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +01003040 si_rx_buff_blk(chn_prod(chn));
Willy Tarreau9635e032018-10-16 17:52:55 +02003041 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01003042 }
3043
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003044 /* The written data will be immediately sent, so we can check
3045 * the available space without taking in account the reserve.
3046 * The reserve is guaranteed for the processing of incoming
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003047 * data, because the buffer will be flushed.
3048 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003049 max = b_room(&chn->buf);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003050
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003051 /* If there is no space available, and the output buffer is empty.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003052 * in this case, we cannot add more data, so we cannot yield,
3053 * we return the amount of copyied data.
3054 */
Willy Tarreaua79021a2018-06-15 18:07:57 +02003055 if (max == 0 && co_data(chn) == 0)
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003056 return 1;
3057
3058 /* Adjust the real required length. */
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003059 if (max > len - l)
3060 max = len - l;
3061
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003062 /* The buffer available size may be not contiguous. This test
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003063 * detects a non contiguous buffer and realign it.
3064 */
Willy Tarreau3f679992018-06-15 15:06:42 +02003065 if (ci_space_for_replace(chn) < max)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003066 channel_slow_realign(chn, trash.area);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003067
3068 /* Copy input data in the buffer. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003069 max = b_rep_blk(&chn->buf, ci_head(chn), ci_head(chn), str + l, max);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003070
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003071 /* buffer replace considers that the input part is filled.
3072 * so, I must forward these new data in the output part.
3073 */
Willy Tarreaubcbd3932018-06-06 07:13:22 +02003074 c_adv(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003075
3076 l += max;
3077 lua_pop(L, 1);
3078 lua_pushinteger(L, l);
3079
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003080 /* If there is no space available, and the output buffer is empty.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003081 * in this case, we cannot add more data, so we cannot yield,
3082 * we return the amount of copyied data.
3083 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003084 max = b_room(&chn->buf);
Willy Tarreaua79021a2018-06-15 18:07:57 +02003085 if (max == 0 && co_data(chn) == 0)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003086 return 1;
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003087
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003088 if (l < len) {
3089 /* If we are waiting for space in the response buffer, we
3090 * must set the flag WAKERESWR. This flag required the task
3091 * wake up if any activity is detected on the response buffer.
3092 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003093 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003094 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01003095 else
3096 HLUA_SET_WAKEREQWR(hlua);
Willy Tarreau9635e032018-10-16 17:52:55 +02003097 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003098 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003099
3100 return 1;
3101}
3102
3103/* Just a wraper of "_hlua_channel_send". This wrapper permits
3104 * yield the LUA process, and resume it without checking the
3105 * input arguments.
3106 */
3107__LJMP static int hlua_channel_send(lua_State *L)
3108{
3109 MAY_LJMP(check_args(L, 2, "send"));
3110 lua_pushinteger(L, 0);
3111
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003112 return MAY_LJMP(hlua_channel_send_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003113}
3114
3115/* This function forward and amount of butes. The data pass from
3116 * the input side of the buffer to the output side, and can be
3117 * forwarded. This function never fails.
3118 *
3119 * The Lua function takes an amount of bytes to be forwarded in
3120 * imput. It returns the number of bytes forwarded.
3121 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003122__LJMP static int hlua_channel_forward_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003123{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003124 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003125 int len;
3126 int l;
3127 int max;
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003128 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003129
3130 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3131 len = MAY_LJMP(luaL_checkinteger(L, 2));
3132 l = MAY_LJMP(luaL_checkinteger(L, -1));
3133
3134 max = len - l;
Willy Tarreaua79021a2018-06-15 18:07:57 +02003135 if (max > ci_data(chn))
3136 max = ci_data(chn);
Willy Tarreau47860ed2015-03-10 14:07:50 +01003137 channel_forward(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003138 l += max;
3139
3140 lua_pop(L, 1);
3141 lua_pushinteger(L, l);
3142
3143 /* Check if it miss bytes to forward. */
3144 if (l < len) {
3145 /* The the input channel or the output channel are closed, we
3146 * must return the amount of data forwarded.
3147 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003148 if (channel_input_closed(chn) || channel_output_closed(chn))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003149 return 1;
3150
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003151 /* If we are waiting for space data in the response buffer, we
3152 * must set the flag WAKERESWR. This flag required the task
3153 * wake up if any activity is detected on the response buffer.
3154 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003155 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003156 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01003157 else
3158 HLUA_SET_WAKEREQWR(hlua);
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003159
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003160 /* Otherwise, we can yield waiting for new data in the inpout side. */
Willy Tarreau9635e032018-10-16 17:52:55 +02003161 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_forward_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003162 }
3163
3164 return 1;
3165}
3166
3167/* Just check the input and prepare the stack for the previous
3168 * function "hlua_channel_forward_yield"
3169 */
3170__LJMP static int hlua_channel_forward(lua_State *L)
3171{
3172 MAY_LJMP(check_args(L, 2, "forward"));
3173 MAY_LJMP(hlua_checkchannel(L, 1));
3174 MAY_LJMP(luaL_checkinteger(L, 2));
3175
3176 lua_pushinteger(L, 0);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003177 return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003178}
3179
3180/* Just returns the number of bytes available in the input
3181 * side of the buffer. This function never fails.
3182 */
3183__LJMP static int hlua_channel_get_in_len(lua_State *L)
3184{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003185 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003186
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003187 MAY_LJMP(check_args(L, 1, "get_in_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003188 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Willy Tarreaua79021a2018-06-15 18:07:57 +02003189 lua_pushinteger(L, ci_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003190 return 1;
3191}
3192
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003193/* Returns true if the channel is full. */
3194__LJMP static int hlua_channel_is_full(lua_State *L)
3195{
3196 struct channel *chn;
3197 int rem;
3198
3199 MAY_LJMP(check_args(L, 1, "is_full"));
3200 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3201
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003202 rem = b_room(&chn->buf);
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003203 rem -= global.tune.maxrewrite; /* Rewrite reserved size */
3204
3205 lua_pushboolean(L, rem <= 0);
3206 return 1;
3207}
3208
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003209/* Just returns the number of bytes available in the output
3210 * side of the buffer. This function never fails.
3211 */
3212__LJMP static int hlua_channel_get_out_len(lua_State *L)
3213{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003214 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003215
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003216 MAY_LJMP(check_args(L, 1, "get_out_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003217 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Willy Tarreaua79021a2018-06-15 18:07:57 +02003218 lua_pushinteger(L, co_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003219 return 1;
3220}
3221
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003222/*
3223 *
3224 *
3225 * Class Fetches
3226 *
3227 *
3228 */
3229
3230/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003231 * a class stream, otherwise it throws an error.
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003232 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003233__LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003234{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003235 return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003236}
3237
3238/* This function creates and push in the stack a fetch object according
3239 * with a current TXN.
3240 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003241static int hlua_fetches_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003242{
Willy Tarreau7073c472015-04-06 11:15:40 +02003243 struct hlua_smp *hsmp;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003244
3245 /* Check stack size. */
3246 if (!lua_checkstack(L, 3))
3247 return 0;
3248
3249 /* Create the object: obj[0] = userdata.
3250 * Note that the base of the Fetches object is the
3251 * transaction object.
3252 */
3253 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003254 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003255 lua_rawseti(L, -2, 0);
3256
Willy Tarreau7073c472015-04-06 11:15:40 +02003257 hsmp->s = txn->s;
3258 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003259 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003260 hsmp->flags = flags;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003261
3262 /* Pop a class sesison metatable and affect it to the userdata. */
3263 lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref);
3264 lua_setmetatable(L, -2);
3265
3266 return 1;
3267}
3268
3269/* This function is an LUA binding. It is called with each sample-fetch.
3270 * It uses closure argument to store the associated sample-fetch. It
3271 * returns only one argument or throws an error. An error is thrown
3272 * only if an error is encountered during the argument parsing. If
3273 * the "sample-fetch" function fails, nil is returned.
3274 */
3275__LJMP static int hlua_run_sample_fetch(lua_State *L)
3276{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003277 struct hlua_smp *hsmp;
Willy Tarreau2ec22742015-03-10 14:27:20 +01003278 struct sample_fetch *f;
Frédéric Lécaillef874a832018-06-15 13:56:04 +02003279 struct arg args[ARGM_NBARGS + 1] = {{0}};
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003280 int i;
3281 struct sample smp;
3282
3283 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003284 f = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003285
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003286 /* Get traditional arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003287 hsmp = MAY_LJMP(hlua_checkfetches(L, 1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003288
Thierry FOURNIERca988662015-12-20 18:43:03 +01003289 /* Check execution authorization. */
3290 if (f->use & SMP_USE_HTTP_ANY &&
3291 !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) {
3292 lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which "
3293 "is not available in Lua services", f->kw);
3294 WILL_LJMP(lua_error(L));
3295 }
3296
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003297 /* Get extra arguments. */
3298 for (i = 0; i < lua_gettop(L) - 1; i++) {
3299 if (i >= ARGM_NBARGS)
3300 break;
3301 hlua_lua2arg(L, i + 2, &args[i]);
3302 }
3303 args[i].type = ARGT_STOP;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003304 args[i].data.str.area = NULL;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003305
3306 /* Check arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003307 MAY_LJMP(hlua_lua2arg_check(L, 2, args, f->arg_mask, hsmp->p));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003308
3309 /* Run the special args checker. */
Willy Tarreau2ec22742015-03-10 14:27:20 +01003310 if (f->val_args && !f->val_args(args, NULL)) {
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003311 lua_pushfstring(L, "error in arguments");
3312 WILL_LJMP(lua_error(L));
3313 }
3314
3315 /* Initialise the sample. */
3316 memset(&smp, 0, sizeof(smp));
3317
3318 /* Run the sample fetch process. */
Willy Tarreau1777ea62016-03-10 16:15:46 +01003319 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003320 if (!f->process(args, &smp, f->kw, f->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003321 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003322 lua_pushstring(L, "");
3323 else
3324 lua_pushnil(L);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003325 return 1;
3326 }
3327
3328 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003329 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003330 hlua_smp2lua_str(L, &smp);
3331 else
3332 hlua_smp2lua(L, &smp);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003333 return 1;
3334}
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003335
3336/*
3337 *
3338 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003339 * Class Converters
3340 *
3341 *
3342 */
3343
3344/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003345 * a class stream, otherwise it throws an error.
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003346 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003347__LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003348{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003349 return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003350}
3351
3352/* This function creates and push in the stack a Converters object
3353 * according with a current TXN.
3354 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003355static int hlua_converters_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003356{
Willy Tarreau7073c472015-04-06 11:15:40 +02003357 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003358
3359 /* Check stack size. */
3360 if (!lua_checkstack(L, 3))
3361 return 0;
3362
3363 /* Create the object: obj[0] = userdata.
3364 * Note that the base of the Converters object is the
3365 * same than the TXN object.
3366 */
3367 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003368 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003369 lua_rawseti(L, -2, 0);
3370
Willy Tarreau7073c472015-04-06 11:15:40 +02003371 hsmp->s = txn->s;
3372 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003373 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003374 hsmp->flags = flags;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003375
Willy Tarreau87b09662015-04-03 00:22:06 +02003376 /* Pop a class stream metatable and affect it to the table. */
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003377 lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref);
3378 lua_setmetatable(L, -2);
3379
3380 return 1;
3381}
3382
3383/* This function is an LUA binding. It is called with each converter.
3384 * It uses closure argument to store the associated converter. It
3385 * returns only one argument or throws an error. An error is thrown
3386 * only if an error is encountered during the argument parsing. If
3387 * the converter function function fails, nil is returned.
3388 */
3389__LJMP static int hlua_run_sample_conv(lua_State *L)
3390{
Willy Tarreauda5f1082015-04-06 11:17:13 +02003391 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003392 struct sample_conv *conv;
Frédéric Lécaillef874a832018-06-15 13:56:04 +02003393 struct arg args[ARGM_NBARGS + 1] = {{0}};
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003394 int i;
3395 struct sample smp;
3396
3397 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003398 conv = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003399
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003400 /* Get traditional arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003401 hsmp = MAY_LJMP(hlua_checkconverters(L, 1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003402
3403 /* Get extra arguments. */
3404 for (i = 0; i < lua_gettop(L) - 2; i++) {
3405 if (i >= ARGM_NBARGS)
3406 break;
3407 hlua_lua2arg(L, i + 3, &args[i]);
3408 }
3409 args[i].type = ARGT_STOP;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003410 args[i].data.str.area = NULL;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003411
3412 /* Check arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003413 MAY_LJMP(hlua_lua2arg_check(L, 3, args, conv->arg_mask, hsmp->p));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003414
3415 /* Run the special args checker. */
3416 if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) {
3417 hlua_pusherror(L, "error in arguments");
3418 WILL_LJMP(lua_error(L));
3419 }
3420
3421 /* Initialise the sample. */
3422 if (!hlua_lua2smp(L, 2, &smp)) {
3423 hlua_pusherror(L, "error in the input argument");
3424 WILL_LJMP(lua_error(L));
3425 }
3426
Willy Tarreau1777ea62016-03-10 16:15:46 +01003427 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
3428
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003429 /* Apply expected cast. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003430 if (!sample_casts[smp.data.type][conv->in_type]) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003431 hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'",
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003432 smp_to_type[smp.data.type], smp_to_type[conv->in_type]);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003433 WILL_LJMP(lua_error(L));
3434 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003435 if (sample_casts[smp.data.type][conv->in_type] != c_none &&
3436 !sample_casts[smp.data.type][conv->in_type](&smp)) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003437 hlua_pusherror(L, "error during the input argument casting");
3438 WILL_LJMP(lua_error(L));
3439 }
3440
3441 /* Run the sample conversion process. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003442 if (!conv->process(args, &smp, conv->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003443 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003444 lua_pushstring(L, "");
3445 else
Willy Tarreaua678b432015-08-28 10:14:59 +02003446 lua_pushnil(L);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003447 return 1;
3448 }
3449
3450 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003451 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003452 hlua_smp2lua_str(L, &smp);
3453 else
3454 hlua_smp2lua(L, &smp);
Willy Tarreaua678b432015-08-28 10:14:59 +02003455 return 1;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003456}
3457
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003458/*
3459 *
3460 *
3461 * Class AppletTCP
3462 *
3463 *
3464 */
3465
3466/* Returns a struct hlua_txn if the stack entry "ud" is
3467 * a class stream, otherwise it throws an error.
3468 */
3469__LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud)
3470{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003471 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003472}
3473
3474/* This function creates and push in the stack an Applet object
3475 * according with a current TXN.
3476 */
3477static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx)
3478{
3479 struct hlua_appctx *appctx;
3480 struct stream_interface *si = ctx->owner;
3481 struct stream *s = si_strm(si);
3482 struct proxy *p = s->be;
3483
3484 /* Check stack size. */
3485 if (!lua_checkstack(L, 3))
3486 return 0;
3487
3488 /* Create the object: obj[0] = userdata.
3489 * Note that the base of the Converters object is the
3490 * same than the TXN object.
3491 */
3492 lua_newtable(L);
3493 appctx = lua_newuserdata(L, sizeof(*appctx));
3494 lua_rawseti(L, -2, 0);
3495 appctx->appctx = ctx;
3496 appctx->htxn.s = s;
3497 appctx->htxn.p = p;
3498
3499 /* Create the "f" field that contains a list of fetches. */
3500 lua_pushstring(L, "f");
3501 if (!hlua_fetches_new(L, &appctx->htxn, 0))
3502 return 0;
3503 lua_settable(L, -3);
3504
3505 /* Create the "sf" field that contains a list of stringsafe fetches. */
3506 lua_pushstring(L, "sf");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003507 if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003508 return 0;
3509 lua_settable(L, -3);
3510
3511 /* Create the "c" field that contains a list of converters. */
3512 lua_pushstring(L, "c");
3513 if (!hlua_converters_new(L, &appctx->htxn, 0))
3514 return 0;
3515 lua_settable(L, -3);
3516
3517 /* Create the "sc" field that contains a list of stringsafe converters. */
3518 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003519 if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003520 return 0;
3521 lua_settable(L, -3);
3522
3523 /* Pop a class stream metatable and affect it to the table. */
3524 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref);
3525 lua_setmetatable(L, -2);
3526
3527 return 1;
3528}
3529
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003530__LJMP static int hlua_applet_tcp_set_var(lua_State *L)
3531{
3532 struct hlua_appctx *appctx;
3533 struct stream *s;
3534 const char *name;
3535 size_t len;
3536 struct sample smp;
3537
3538 MAY_LJMP(check_args(L, 3, "set_var"));
3539
3540 /* It is useles to retrieve the stream, but this function
3541 * runs only in a stream context.
3542 */
3543 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3544 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3545 s = appctx->htxn.s;
3546
3547 /* Converts the third argument in a sample. */
3548 hlua_lua2smp(L, 3, &smp);
3549
3550 /* Store the sample in a variable. */
3551 smp_set_owner(&smp, s->be, s->sess, s, 0);
3552 vars_set_by_name(name, len, &smp);
3553 return 0;
3554}
3555
3556__LJMP static int hlua_applet_tcp_unset_var(lua_State *L)
3557{
3558 struct hlua_appctx *appctx;
3559 struct stream *s;
3560 const char *name;
3561 size_t len;
3562 struct sample smp;
3563
3564 MAY_LJMP(check_args(L, 2, "unset_var"));
3565
3566 /* It is useles to retrieve the stream, but this function
3567 * runs only in a stream context.
3568 */
3569 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3570 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3571 s = appctx->htxn.s;
3572
3573 /* Unset the variable. */
3574 smp_set_owner(&smp, s->be, s->sess, s, 0);
3575 vars_unset_by_name(name, len, &smp);
3576 return 0;
3577}
3578
3579__LJMP static int hlua_applet_tcp_get_var(lua_State *L)
3580{
3581 struct hlua_appctx *appctx;
3582 struct stream *s;
3583 const char *name;
3584 size_t len;
3585 struct sample smp;
3586
3587 MAY_LJMP(check_args(L, 2, "get_var"));
3588
3589 /* It is useles to retrieve the stream, but this function
3590 * runs only in a stream context.
3591 */
3592 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3593 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3594 s = appctx->htxn.s;
3595
3596 smp_set_owner(&smp, s->be, s->sess, s, 0);
3597 if (!vars_get_by_name(name, len, &smp)) {
3598 lua_pushnil(L);
3599 return 1;
3600 }
3601
3602 return hlua_smp2lua(L, &smp);
3603}
3604
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003605__LJMP static int hlua_applet_tcp_set_priv(lua_State *L)
3606{
3607 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3608 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003609 struct hlua *hlua;
3610
3611 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003612 if (!s->hlua)
3613 return 0;
3614 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003615
3616 MAY_LJMP(check_args(L, 2, "set_priv"));
3617
3618 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02003619 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003620
3621 /* Get and store new value. */
3622 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
3623 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
3624
3625 return 0;
3626}
3627
3628__LJMP static int hlua_applet_tcp_get_priv(lua_State *L)
3629{
3630 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3631 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003632 struct hlua *hlua;
3633
3634 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003635 if (!s->hlua) {
3636 lua_pushnil(L);
3637 return 1;
3638 }
3639 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003640
3641 /* Push configuration index in the stack. */
3642 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
3643
3644 return 1;
3645}
3646
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003647/* If expected data not yet available, it returns a yield. This function
3648 * consumes the data in the buffer. It returns a string containing the
3649 * data. This string can be empty.
3650 */
3651__LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx)
3652{
3653 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3654 struct stream_interface *si = appctx->appctx->owner;
3655 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02003656 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003657 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02003658 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003659 size_t len2;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003660
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003661 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003662 ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003663
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003664 /* Data not yet available. return yield. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003665 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003666 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003667 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_getline_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003668 }
3669
3670 /* End of data: commit the total strings and return. */
3671 if (ret < 0) {
3672 luaL_pushresult(&appctx->b);
3673 return 1;
3674 }
3675
3676 /* Ensure that the block 2 length is usable. */
3677 if (ret == 1)
3678 len2 = 0;
3679
3680 /* dont check the max length read and dont check. */
3681 luaL_addlstring(&appctx->b, blk1, len1);
3682 luaL_addlstring(&appctx->b, blk2, len2);
3683
3684 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003685 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003686 luaL_pushresult(&appctx->b);
3687 return 1;
3688}
3689
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003690/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003691__LJMP static int hlua_applet_tcp_getline(lua_State *L)
3692{
3693 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3694
3695 /* Initialise the string catenation. */
3696 luaL_buffinit(L, &appctx->b);
3697
3698 return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0));
3699}
3700
3701/* If expected data not yet available, it returns a yield. This function
3702 * consumes the data in the buffer. It returns a string containing the
3703 * data. This string can be empty.
3704 */
3705__LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx)
3706{
3707 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3708 struct stream_interface *si = appctx->appctx->owner;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003709 size_t len = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003710 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02003711 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003712 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02003713 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003714 size_t len2;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003715
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003716 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003717 ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003718
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003719 /* Data not yet available. return yield. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003720 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003721 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003722 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003723 }
3724
3725 /* End of data: commit the total strings and return. */
3726 if (ret < 0) {
3727 luaL_pushresult(&appctx->b);
3728 return 1;
3729 }
3730
3731 /* Ensure that the block 2 length is usable. */
3732 if (ret == 1)
3733 len2 = 0;
3734
3735 if (len == -1) {
3736
3737 /* If len == -1, catenate all the data avalaile and
3738 * yield because we want to get all the data until
3739 * the end of data stream.
3740 */
3741 luaL_addlstring(&appctx->b, blk1, len1);
3742 luaL_addlstring(&appctx->b, blk2, len2);
Willy Tarreau06d80a92017-10-19 14:32:15 +02003743 co_skip(si_oc(si), len1 + len2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003744 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003745 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003746
3747 } else {
3748
3749 /* Copy the fisrt block caping to the length required. */
3750 if (len1 > len)
3751 len1 = len;
3752 luaL_addlstring(&appctx->b, blk1, len1);
3753 len -= len1;
3754
3755 /* Copy the second block. */
3756 if (len2 > len)
3757 len2 = len;
3758 luaL_addlstring(&appctx->b, blk2, len2);
3759 len -= len2;
3760
3761 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003762 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003763
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003764 /* If there is no other data available, yield waiting for new data. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003765 if (len > 0) {
3766 lua_pushinteger(L, len);
3767 lua_replace(L, 2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003768 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003769 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003770 }
3771
3772 /* return the result. */
3773 luaL_pushresult(&appctx->b);
3774 return 1;
3775 }
3776
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003777 /* we never execute this */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003778 hlua_pusherror(L, "Lua: internal error");
3779 WILL_LJMP(lua_error(L));
3780 return 0;
3781}
3782
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003783/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003784__LJMP static int hlua_applet_tcp_recv(lua_State *L)
3785{
3786 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3787 int len = -1;
3788
3789 if (lua_gettop(L) > 2)
3790 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
3791 if (lua_gettop(L) >= 2) {
3792 len = MAY_LJMP(luaL_checkinteger(L, 2));
3793 lua_pop(L, 1);
3794 }
3795
3796 /* Confirm or set the required length */
3797 lua_pushinteger(L, len);
3798
3799 /* Initialise the string catenation. */
3800 luaL_buffinit(L, &appctx->b);
3801
3802 return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0));
3803}
3804
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003805/* Append data in the output side of the buffer. This data is immediately
3806 * sent. The function returns the amount of data written. If the buffer
3807 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003808 * if the channel is closed.
3809 */
3810__LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx)
3811{
3812 size_t len;
3813 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3814 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
3815 int l = MAY_LJMP(luaL_checkinteger(L, 3));
3816 struct stream_interface *si = appctx->appctx->owner;
3817 struct channel *chn = si_ic(si);
3818 int max;
3819
3820 /* Get the max amount of data which can write as input in the channel. */
3821 max = channel_recv_max(chn);
3822 if (max > (len - l))
3823 max = len - l;
3824
3825 /* Copy data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003826 ci_putblk(chn, str + l, max);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003827
3828 /* update counters. */
3829 l += max;
3830 lua_pop(L, 1);
3831 lua_pushinteger(L, l);
3832
3833 /* If some data is not send, declares the situation to the
3834 * applet, and returns a yield.
3835 */
3836 if (l < len) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003837 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003838 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003839 }
3840
3841 return 1;
3842}
3843
3844/* Just a wraper of "hlua_applet_tcp_send_yield". This wrapper permits
3845 * yield the LUA process, and resume it without checking the
3846 * input arguments.
3847 */
3848__LJMP static int hlua_applet_tcp_send(lua_State *L)
3849{
3850 MAY_LJMP(check_args(L, 2, "send"));
3851 lua_pushinteger(L, 0);
3852
3853 return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0));
3854}
3855
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003856/*
3857 *
3858 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003859 * Class AppletHTTP
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003860 *
3861 *
3862 */
3863
3864/* Returns a struct hlua_txn if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003865 * a class stream, otherwise it throws an error.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003866 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003867__LJMP static struct hlua_appctx *hlua_checkapplet_http(lua_State *L, int ud)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003868{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003869 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003870}
3871
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003872/* This function creates and push in the stack an Applet object
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003873 * according with a current TXN.
3874 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003875static int hlua_applet_http_new(lua_State *L, struct appctx *ctx)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003876{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003877 struct hlua_appctx *appctx;
Thierry FOURNIER841475e2015-12-11 17:10:09 +01003878 struct hlua_txn htxn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003879 struct stream_interface *si = ctx->owner;
3880 struct stream *s = si_strm(si);
3881 struct proxy *px = s->be;
3882 struct http_txn *txn = s->txn;
3883 const char *path;
3884 const char *end;
3885 const char *p;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003886
3887 /* Check stack size. */
3888 if (!lua_checkstack(L, 3))
3889 return 0;
3890
3891 /* Create the object: obj[0] = userdata.
3892 * Note that the base of the Converters object is the
3893 * same than the TXN object.
3894 */
3895 lua_newtable(L);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003896 appctx = lua_newuserdata(L, sizeof(*appctx));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003897 lua_rawseti(L, -2, 0);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003898 appctx->appctx = ctx;
3899 appctx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */
Robin H. Johnson52f5db22017-01-01 13:10:52 -08003900 appctx->appctx->ctx.hlua_apphttp.reason = NULL; /* Use default reason based on status */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003901 appctx->htxn.s = s;
3902 appctx->htxn.p = px;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003903
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003904 /* Create the "f" field that contains a list of fetches. */
3905 lua_pushstring(L, "f");
3906 if (!hlua_fetches_new(L, &appctx->htxn, 0))
3907 return 0;
3908 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003909
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003910 /* Create the "sf" field that contains a list of stringsafe fetches. */
3911 lua_pushstring(L, "sf");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003912 if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003913 return 0;
3914 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003915
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003916 /* Create the "c" field that contains a list of converters. */
3917 lua_pushstring(L, "c");
3918 if (!hlua_converters_new(L, &appctx->htxn, 0))
3919 return 0;
3920 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003921
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003922 /* Create the "sc" field that contains a list of stringsafe converters. */
3923 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003924 if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003925 return 0;
3926 lua_settable(L, -3);
Willy Tarreaueee5b512015-04-03 23:46:31 +02003927
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003928 /* Stores the request method. */
3929 lua_pushstring(L, "method");
Willy Tarreaua79021a2018-06-15 18:07:57 +02003930 lua_pushlstring(L, ci_head(txn->req.chn), txn->req.sl.rq.m_l);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003931 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003932
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003933 /* Stores the http version. */
3934 lua_pushstring(L, "version");
Willy Tarreaua79021a2018-06-15 18:07:57 +02003935 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 +02003936 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003937
Thierry FOURNIER841475e2015-12-11 17:10:09 +01003938 /* creates an array of headers. hlua_http_get_headers() crates and push
3939 * the array on the top of the stack.
3940 */
3941 lua_pushstring(L, "headers");
3942 htxn.s = s;
3943 htxn.p = px;
3944 htxn.dir = SMP_OPT_DIR_REQ;
3945 if (!hlua_http_get_headers(L, &htxn, &htxn.s->txn->req))
3946 return 0;
3947 lua_settable(L, -3);
3948
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003949 /* Get path and qs */
Willy Tarreau6b952c82018-09-10 17:45:34 +02003950 path = http_txn_get_path(txn);
Thierry FOURNIER7d388632017-02-22 02:06:16 +01003951 if (path) {
Willy Tarreaua79021a2018-06-15 18:07:57 +02003952 end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Thierry FOURNIER7d388632017-02-22 02:06:16 +01003953 p = path;
3954 while (p < end && *p != '?')
3955 p++;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003956
Thierry FOURNIER7d388632017-02-22 02:06:16 +01003957 /* Stores the request path. */
3958 lua_pushstring(L, "path");
3959 lua_pushlstring(L, path, p - path);
3960 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003961
Thierry FOURNIER7d388632017-02-22 02:06:16 +01003962 /* Stores the query string. */
3963 lua_pushstring(L, "qs");
3964 if (*p == '?')
3965 p++;
3966 lua_pushlstring(L, p, end - p);
3967 lua_settable(L, -3);
3968 }
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003969
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003970 /* Stores the request path. */
3971 lua_pushstring(L, "length");
3972 lua_pushinteger(L, txn->req.body_len);
3973 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003974
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003975 /* Create an array of HTTP request headers. */
3976 lua_pushstring(L, "headers");
3977 MAY_LJMP(hlua_http_get_headers(L, &appctx->htxn, &appctx->htxn.s->txn->req));
3978 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003979
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003980 /* Create an empty array of HTTP request headers. */
3981 lua_pushstring(L, "response");
3982 lua_newtable(L);
3983 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003984
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003985 /* Pop a class stream metatable and affect it to the table. */
3986 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref);
3987 lua_setmetatable(L, -2);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003988
3989 return 1;
3990}
3991
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003992__LJMP static int hlua_applet_http_set_var(lua_State *L)
3993{
3994 struct hlua_appctx *appctx;
3995 struct stream *s;
3996 const char *name;
3997 size_t len;
3998 struct sample smp;
3999
4000 MAY_LJMP(check_args(L, 3, "set_var"));
4001
4002 /* It is useles to retrieve the stream, but this function
4003 * runs only in a stream context.
4004 */
4005 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4006 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4007 s = appctx->htxn.s;
4008
4009 /* Converts the third argument in a sample. */
4010 hlua_lua2smp(L, 3, &smp);
4011
4012 /* Store the sample in a variable. */
4013 smp_set_owner(&smp, s->be, s->sess, s, 0);
4014 vars_set_by_name(name, len, &smp);
4015 return 0;
4016}
4017
4018__LJMP static int hlua_applet_http_unset_var(lua_State *L)
4019{
4020 struct hlua_appctx *appctx;
4021 struct stream *s;
4022 const char *name;
4023 size_t len;
4024 struct sample smp;
4025
4026 MAY_LJMP(check_args(L, 2, "unset_var"));
4027
4028 /* It is useles to retrieve the stream, but this function
4029 * runs only in a stream context.
4030 */
4031 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4032 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4033 s = appctx->htxn.s;
4034
4035 /* Unset the variable. */
4036 smp_set_owner(&smp, s->be, s->sess, s, 0);
4037 vars_unset_by_name(name, len, &smp);
4038 return 0;
4039}
4040
4041__LJMP static int hlua_applet_http_get_var(lua_State *L)
4042{
4043 struct hlua_appctx *appctx;
4044 struct stream *s;
4045 const char *name;
4046 size_t len;
4047 struct sample smp;
4048
4049 MAY_LJMP(check_args(L, 2, "get_var"));
4050
4051 /* It is useles to retrieve the stream, but this function
4052 * runs only in a stream context.
4053 */
4054 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4055 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4056 s = appctx->htxn.s;
4057
4058 smp_set_owner(&smp, s->be, s->sess, s, 0);
4059 if (!vars_get_by_name(name, len, &smp)) {
4060 lua_pushnil(L);
4061 return 1;
4062 }
4063
4064 return hlua_smp2lua(L, &smp);
4065}
4066
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004067__LJMP static int hlua_applet_http_set_priv(lua_State *L)
4068{
4069 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4070 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004071 struct hlua *hlua;
4072
4073 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004074 if (!s->hlua)
4075 return 0;
4076 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004077
4078 MAY_LJMP(check_args(L, 2, "set_priv"));
4079
4080 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02004081 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004082
4083 /* Get and store new value. */
4084 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
4085 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
4086
4087 return 0;
4088}
4089
4090__LJMP static int hlua_applet_http_get_priv(lua_State *L)
4091{
4092 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4093 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004094 struct hlua *hlua;
4095
4096 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004097 if (!s->hlua) {
4098 lua_pushnil(L);
4099 return 1;
4100 }
4101 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004102
4103 /* Push configuration index in the stack. */
4104 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
4105
4106 return 1;
4107}
4108
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004109/* If expected data not yet available, it returns a yield. This function
4110 * consumes the data in the buffer. It returns a string containing the
4111 * data. This string can be empty.
4112 */
4113__LJMP static int hlua_applet_http_getline_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004114{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004115 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4116 struct stream_interface *si = appctx->appctx->owner;
4117 struct channel *chn = si_ic(si);
4118 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02004119 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02004120 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02004121 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02004122 size_t len2;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004123
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004124 /* Maybe we cant send a 100-continue ? */
4125 if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_100C) {
Willy Tarreau04f1e2d2018-09-10 18:04:24 +02004126 ret = ci_putblk(chn, HTTP_100.ptr, HTTP_100.len);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004127 /* if ret == -2 or -3 the channel closed or the message si too
4128 * big for the buffers. We cant send anything. So, we ignoring
4129 * the error, considers that the 100-continue is sent, and try
4130 * to receive.
4131 * If ret is -1, we dont have room in the buffer, so we yield.
4132 */
4133 if (ret == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01004134 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004135 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_getline_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004136 }
4137 appctx->appctx->ctx.hlua_apphttp.flags &= ~APPLET_100C;
4138 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004139
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004140 /* Check for the end of the data. */
4141 if (appctx->appctx->ctx.hlua_apphttp.left_bytes <= 0) {
4142 luaL_pushresult(&appctx->b);
4143 return 1;
4144 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004145
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004146 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004147 ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004148
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004149 /* Data not yet available. return yield. */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004150 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01004151 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004152 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_getline_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004153 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004154
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004155 /* End of data: commit the total strings and return. */
4156 if (ret < 0) {
4157 luaL_pushresult(&appctx->b);
4158 return 1;
4159 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004160
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004161 /* Ensure that the block 2 length is usable. */
4162 if (ret == 1)
4163 len2 = 0;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004164
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004165 /* Copy the fisrt block caping to the length required. */
4166 if (len1 > appctx->appctx->ctx.hlua_apphttp.left_bytes)
4167 len1 = appctx->appctx->ctx.hlua_apphttp.left_bytes;
4168 luaL_addlstring(&appctx->b, blk1, len1);
4169 appctx->appctx->ctx.hlua_apphttp.left_bytes -= len1;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004170
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004171 /* Copy the second block. */
4172 if (len2 > appctx->appctx->ctx.hlua_apphttp.left_bytes)
4173 len2 = appctx->appctx->ctx.hlua_apphttp.left_bytes;
4174 luaL_addlstring(&appctx->b, blk2, len2);
4175 appctx->appctx->ctx.hlua_apphttp.left_bytes -= len2;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004176
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004177 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004178 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004179 luaL_pushresult(&appctx->b);
4180 return 1;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004181}
4182
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004183/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004184__LJMP static int hlua_applet_http_getline(lua_State *L)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004185{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004186 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004187
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004188 /* Initialise the string catenation. */
4189 luaL_buffinit(L, &appctx->b);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004190
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004191 return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004192}
4193
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004194/* If expected data not yet available, it returns a yield. This function
4195 * consumes the data in the buffer. It returns a string containing the
4196 * data. This string can be empty.
4197 */
4198__LJMP static int hlua_applet_http_recv_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004199{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004200 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4201 struct stream_interface *si = appctx->appctx->owner;
4202 int len = MAY_LJMP(luaL_checkinteger(L, 2));
4203 struct channel *chn = si_ic(si);
4204 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02004205 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02004206 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02004207 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02004208 size_t len2;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004209
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004210 /* Maybe we cant send a 100-continue ? */
4211 if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_100C) {
Willy Tarreau04f1e2d2018-09-10 18:04:24 +02004212 ret = ci_putblk(chn, HTTP_100.ptr, HTTP_100.len);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004213 /* if ret == -2 or -3 the channel closed or the message si too
4214 * big for the buffers. We cant send anything. So, we ignoring
4215 * the error, considers that the 100-continue is sent, and try
4216 * to receive.
4217 * If ret is -1, we dont have room in the buffer, so we yield.
4218 */
4219 if (ret == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01004220 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004221 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004222 }
4223 appctx->appctx->ctx.hlua_apphttp.flags &= ~APPLET_100C;
4224 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004225
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004226 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004227 ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004228
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004229 /* Data not yet available. return yield. */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004230 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01004231 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004232 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004233 }
4234
4235 /* End of data: commit the total strings and return. */
4236 if (ret < 0) {
4237 luaL_pushresult(&appctx->b);
4238 return 1;
4239 }
4240
4241 /* Ensure that the block 2 length is usable. */
4242 if (ret == 1)
4243 len2 = 0;
4244
4245 /* Copy the fisrt block caping to the length required. */
4246 if (len1 > len)
4247 len1 = len;
4248 luaL_addlstring(&appctx->b, blk1, len1);
4249 len -= len1;
4250
4251 /* Copy the second block. */
4252 if (len2 > len)
4253 len2 = len;
4254 luaL_addlstring(&appctx->b, blk2, len2);
4255 len -= len2;
4256
4257 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004258 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004259 if (appctx->appctx->ctx.hlua_apphttp.left_bytes != -1)
4260 appctx->appctx->ctx.hlua_apphttp.left_bytes -= len;
4261
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004262 /* If we are no other data available, yield waiting for new data. */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004263 if (len > 0) {
4264 lua_pushinteger(L, len);
4265 lua_replace(L, 2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01004266 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004267 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004268 }
4269
4270 /* return the result. */
4271 luaL_pushresult(&appctx->b);
4272 return 1;
4273}
4274
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004275/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004276__LJMP static int hlua_applet_http_recv(lua_State *L)
4277{
4278 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4279 int len = -1;
4280
4281 /* Check arguments. */
4282 if (lua_gettop(L) > 2)
4283 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
4284 if (lua_gettop(L) >= 2) {
4285 len = MAY_LJMP(luaL_checkinteger(L, 2));
4286 lua_pop(L, 1);
4287 }
4288
4289 /* Check the required length */
4290 if (len == -1 || len > appctx->appctx->ctx.hlua_apphttp.left_bytes)
4291 len = appctx->appctx->ctx.hlua_apphttp.left_bytes;
4292 lua_pushinteger(L, len);
4293
4294 /* Initialise the string catenation. */
4295 luaL_buffinit(L, &appctx->b);
4296
4297 return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0));
4298}
4299
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004300/* Append data in the output side of the buffer. This data is immediately
4301 * sent. The function returns the amount of data written. If the buffer
4302 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004303 * if the channel is closed.
4304 */
4305__LJMP static int hlua_applet_http_send_yield(lua_State *L, int status, lua_KContext ctx)
4306{
4307 size_t len;
4308 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4309 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
4310 int l = MAY_LJMP(luaL_checkinteger(L, 3));
4311 struct stream_interface *si = appctx->appctx->owner;
4312 struct channel *chn = si_ic(si);
4313 int max;
4314
4315 /* Get the max amount of data which can write as input in the channel. */
4316 max = channel_recv_max(chn);
4317 if (max > (len - l))
4318 max = len - l;
4319
4320 /* Copy data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004321 ci_putblk(chn, str + l, max);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004322
4323 /* update counters. */
4324 l += max;
4325 lua_pop(L, 1);
4326 lua_pushinteger(L, l);
4327
4328 /* If some data is not send, declares the situation to the
4329 * applet, and returns a yield.
4330 */
4331 if (l < len) {
Willy Tarreaudb398432018-11-15 11:08:52 +01004332 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004333 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004334 }
4335
4336 return 1;
4337}
4338
4339/* Just a wraper of "hlua_applet_send_yield". This wrapper permits
4340 * yield the LUA process, and resume it without checking the
4341 * input arguments.
4342 */
4343__LJMP static int hlua_applet_http_send(lua_State *L)
4344{
4345 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4346 size_t len;
4347 char hex[10];
4348
4349 MAY_LJMP(luaL_checklstring(L, 2, &len));
4350
4351 /* If transfer encoding chunked is selected, we surround the data
4352 * by chunk data.
4353 */
4354 if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_CHUNKED) {
4355 snprintf(hex, 9, "%x", (unsigned int)len);
4356 lua_pushfstring(L, "%s\r\n", hex);
4357 lua_insert(L, 2); /* swap the last 2 entries. */
4358 lua_pushstring(L, "\r\n");
4359 lua_concat(L, 3);
4360 }
4361
4362 /* This interger is used for followinf the amount of data sent. */
4363 lua_pushinteger(L, 0);
4364
4365 /* We want to send some data. Headers must be sent. */
4366 if (!(appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
4367 hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data.");
4368 WILL_LJMP(lua_error(L));
4369 }
4370
4371 return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0));
4372}
4373
4374__LJMP static int hlua_applet_http_addheader(lua_State *L)
4375{
4376 const char *name;
4377 int ret;
4378
4379 MAY_LJMP(hlua_checkapplet_http(L, 1));
4380 name = MAY_LJMP(luaL_checkstring(L, 2));
4381 MAY_LJMP(luaL_checkstring(L, 3));
4382
4383 /* Push in the stack the "response" entry. */
4384 ret = lua_getfield(L, 1, "response");
4385 if (ret != LUA_TTABLE) {
4386 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] "
4387 "is expected as an array. %s found", lua_typename(L, ret));
4388 WILL_LJMP(lua_error(L));
4389 }
4390
4391 /* check if the header is already registered if it is not
4392 * the case, register it.
4393 */
4394 ret = lua_getfield(L, -1, name);
4395 if (ret == LUA_TNIL) {
4396
4397 /* Entry not found. */
4398 lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */
4399
4400 /* Insert the new header name in the array in the top of the stack.
4401 * It left the new array in the top of the stack.
4402 */
4403 lua_newtable(L);
4404 lua_pushvalue(L, 2);
4405 lua_pushvalue(L, -2);
4406 lua_settable(L, -4);
4407
4408 } else if (ret != LUA_TTABLE) {
4409
4410 /* corruption error. */
4411 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] "
4412 "is expected as an array. %s found", name, lua_typename(L, ret));
4413 WILL_LJMP(lua_error(L));
4414 }
4415
4416 /* Now the top od thestack is an array of values. We push
4417 * the header value as new entry.
4418 */
4419 lua_pushvalue(L, 3);
4420 ret = lua_rawlen(L, -2);
4421 lua_rawseti(L, -2, ret + 1);
4422 lua_pushboolean(L, 1);
4423 return 1;
4424}
4425
4426__LJMP static int hlua_applet_http_status(lua_State *L)
4427{
4428 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4429 int status = MAY_LJMP(luaL_checkinteger(L, 2));
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004430 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004431
4432 if (status < 100 || status > 599) {
4433 lua_pushboolean(L, 0);
4434 return 1;
4435 }
4436
4437 appctx->appctx->ctx.hlua_apphttp.status = status;
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004438 appctx->appctx->ctx.hlua_apphttp.reason = reason;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004439 lua_pushboolean(L, 1);
4440 return 1;
4441}
4442
4443/* We will build the status line and the headers of the HTTP response.
4444 * We will try send at once if its not possible, we give back the hand
4445 * waiting for more room.
4446 */
4447__LJMP static int hlua_applet_http_start_response_yield(lua_State *L, int status, lua_KContext ctx)
4448{
4449 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4450 struct stream_interface *si = appctx->appctx->owner;
4451 struct channel *chn = si_ic(si);
4452 int ret;
4453 size_t len;
4454 const char *msg;
4455
4456 /* Get the message as the first argument on the stack. */
4457 msg = MAY_LJMP(luaL_checklstring(L, 2, &len));
4458
4459 /* Send the message at once. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004460 ret = ci_putblk(chn, msg, len);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004461
4462 /* if ret == -2 or -3 the channel closed or the message si too
4463 * big for the buffers.
4464 */
4465 if (ret == -2 || ret == -3) {
4466 hlua_pusherror(L, "Lua: 'start_response': response header block too big");
4467 WILL_LJMP(lua_error(L));
4468 }
4469
4470 /* If ret is -1, we dont have room in the buffer, so we yield. */
4471 if (ret == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01004472 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004473 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_start_response_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004474 }
4475
4476 /* Headers sent, set the flag. */
4477 appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT;
4478 return 0;
4479}
4480
4481__LJMP static int hlua_applet_http_start_response(lua_State *L)
4482{
Willy Tarreau83061a82018-07-13 11:56:34 +02004483 struct buffer *tmp = get_trash_chunk();
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004484 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004485 const char *name;
Willy Tarreaua3294632017-08-23 11:24:47 +02004486 size_t name_len;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004487 const char *value;
Willy Tarreaua3294632017-08-23 11:24:47 +02004488 size_t value_len;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004489 int id;
Willy Tarreauc9f4ea02017-08-23 09:32:06 +02004490 long long hdr_contentlength = -1;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004491 int hdr_chunked = 0;
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004492 const char *reason = appctx->appctx->ctx.hlua_apphttp.reason;
4493
4494 if (reason == NULL)
Willy Tarreau04f1e2d2018-09-10 18:04:24 +02004495 reason = http_get_reason(appctx->appctx->ctx.hlua_apphttp.status);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004496
4497 /* Use the same http version than the request. */
4498 chunk_appendf(tmp, "HTTP/1.%c %d %s\r\n",
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +01004499 appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11 ? '1' : '0',
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004500 appctx->appctx->ctx.hlua_apphttp.status,
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004501 reason);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004502
4503 /* Get the array associated to the field "response" in the object AppletHTTP. */
4504 lua_pushvalue(L, 0);
4505 if (lua_getfield(L, 1, "response") != LUA_TTABLE) {
4506 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n",
4507 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4508 WILL_LJMP(lua_error(L));
4509 }
4510
4511 /* Browse the list of headers. */
4512 lua_pushnil(L);
4513 while(lua_next(L, -2) != 0) {
4514
4515 /* We expect a string as -2. */
4516 if (lua_type(L, -2) != LUA_TSTRING) {
4517 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n",
4518 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4519 lua_typename(L, lua_type(L, -2)));
4520 WILL_LJMP(lua_error(L));
4521 }
Willy Tarreaua3294632017-08-23 11:24:47 +02004522 name = lua_tolstring(L, -2, &name_len);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004523
4524 /* We expect an array as -1. */
4525 if (lua_type(L, -1) != LUA_TTABLE) {
4526 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n",
4527 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4528 name,
4529 lua_typename(L, lua_type(L, -1)));
4530 WILL_LJMP(lua_error(L));
4531 }
4532
4533 /* Browse the table who is on the top of the stack. */
4534 lua_pushnil(L);
4535 while(lua_next(L, -2) != 0) {
4536
4537 /* We expect a number as -2. */
4538 if (lua_type(L, -2) != LUA_TNUMBER) {
4539 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n",
4540 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4541 name,
4542 lua_typename(L, lua_type(L, -2)));
4543 WILL_LJMP(lua_error(L));
4544 }
4545 id = lua_tointeger(L, -2);
4546
4547 /* We expect a string as -2. */
4548 if (lua_type(L, -1) != LUA_TSTRING) {
4549 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n",
4550 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4551 name, id,
4552 lua_typename(L, lua_type(L, -1)));
4553 WILL_LJMP(lua_error(L));
4554 }
Willy Tarreaua3294632017-08-23 11:24:47 +02004555 value = lua_tolstring(L, -1, &value_len);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004556
4557 /* Catenate a new header. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004558 if (tmp->data + name_len + 2 + value_len + 2 < tmp->size) {
4559 memcpy(tmp->area + tmp->data, name, name_len);
4560 tmp->data += name_len;
4561 tmp->area[tmp->data++] = ':';
4562 tmp->area[tmp->data++] = ' ';
Willy Tarreaua3294632017-08-23 11:24:47 +02004563
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004564 memcpy(tmp->area + tmp->data, value,
4565 value_len);
4566 tmp->data += value_len;
4567 tmp->area[tmp->data++] = '\r';
4568 tmp->area[tmp->data++] = '\n';
Willy Tarreaua3294632017-08-23 11:24:47 +02004569 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004570
4571 /* Protocol checks. */
4572
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004573 /* Copy the header content length. The length conversion
Willy Tarreauc9f4ea02017-08-23 09:32:06 +02004574 * is done without control. If it contains a bad value,
4575 * the content-length remains negative so that we can
4576 * switch to either chunked encoding or close.
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004577 */
Willy Tarreaua3294632017-08-23 11:24:47 +02004578 if (name_len == 14 && strcasecmp("content-length", name) == 0)
Willy Tarreauc9f4ea02017-08-23 09:32:06 +02004579 strl2llrc(value, strlen(value), &hdr_contentlength);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004580
4581 /* Check if the client annouces a transfer-encoding chunked it self. */
Willy Tarreaua3294632017-08-23 11:24:47 +02004582 if (name_len == 17 && value_len == 7 &&
4583 strcasecmp("transfer-encoding", name) == 0 &&
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004584 strcasecmp("chunked", value) == 0)
4585 hdr_chunked = 1;
4586
4587 /* Remove the array from the stack, and get next element with a remaining string. */
4588 lua_pop(L, 1);
4589 }
4590
4591 /* Remove the array from the stack, and get next element with a remaining string. */
4592 lua_pop(L, 1);
4593 }
4594
Willy Tarreau06c75fe2017-08-23 09:10:38 +02004595 /* If we dont have a content-length set, and the HTTP version is 1.1
4596 * and the status code implies the presence of a message body, we must
4597 * announce a transfer encoding chunked. This is required by haproxy
4598 * for the keepalive compliance. If the applet annouces a transfer-encoding
4599 * chunked itslef, don't do anything.
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004600 */
Willy Tarreauc9f4ea02017-08-23 09:32:06 +02004601 if (hdr_contentlength < 0 && hdr_chunked == 0 &&
Willy Tarreau06c75fe2017-08-23 09:10:38 +02004602 (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) &&
4603 appctx->appctx->ctx.hlua_apphttp.status >= 200 &&
4604 appctx->appctx->ctx.hlua_apphttp.status != 204 &&
4605 appctx->appctx->ctx.hlua_apphttp.status != 304) {
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004606 chunk_appendf(tmp, "Transfer-encoding: chunked\r\n");
4607 appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_CHUNKED;
4608 }
4609
4610 /* Finalize headers. */
4611 chunk_appendf(tmp, "\r\n");
4612
4613 /* Remove the last entry and the array of headers */
4614 lua_pop(L, 2);
4615
4616 /* Push the headers block. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004617 lua_pushlstring(L, tmp->area, tmp->data);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004618
4619 return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0));
4620}
4621
4622/*
4623 *
4624 *
4625 * Class HTTP
4626 *
4627 *
4628 */
4629
4630/* Returns a struct hlua_txn if the stack entry "ud" is
4631 * a class stream, otherwise it throws an error.
4632 */
4633__LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud)
4634{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02004635 return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004636}
4637
4638/* This function creates and push in the stack a HTTP object
4639 * according with a current TXN.
4640 */
4641static int hlua_http_new(lua_State *L, struct hlua_txn *txn)
4642{
4643 struct hlua_txn *htxn;
4644
4645 /* Check stack size. */
4646 if (!lua_checkstack(L, 3))
4647 return 0;
4648
4649 /* Create the object: obj[0] = userdata.
4650 * Note that the base of the Converters object is the
4651 * same than the TXN object.
4652 */
4653 lua_newtable(L);
4654 htxn = lua_newuserdata(L, sizeof(*htxn));
4655 lua_rawseti(L, -2, 0);
4656
4657 htxn->s = txn->s;
4658 htxn->p = txn->p;
4659
4660 /* Pop a class stream metatable and affect it to the table. */
4661 lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref);
4662 lua_setmetatable(L, -2);
4663
4664 return 1;
4665}
4666
4667/* This function creates ans returns an array of HTTP headers.
4668 * This function does not fails. It is used as wrapper with the
4669 * 2 following functions.
4670 */
4671__LJMP static int hlua_http_get_headers(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg)
4672{
4673 const char *cur_ptr, *cur_next, *p;
4674 int old_idx, cur_idx;
4675 struct hdr_idx_elem *cur_hdr;
4676 const char *hn, *hv;
4677 int hnl, hvl;
4678 int type;
4679 const char *in;
4680 char *out;
4681 int len;
4682
4683 /* Create the table. */
4684 lua_newtable(L);
4685
4686 if (!htxn->s->txn)
4687 return 1;
4688
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004689 /* Check if a valid response is parsed */
4690 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4691 return 1;
4692
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004693 /* Build array of headers. */
4694 old_idx = 0;
Willy Tarreaua79021a2018-06-15 18:07:57 +02004695 cur_next = ci_head(msg->chn) + hdr_idx_first_pos(&htxn->s->txn->hdr_idx);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004696
4697 while (1) {
4698 cur_idx = htxn->s->txn->hdr_idx.v[old_idx].next;
4699 if (!cur_idx)
4700 break;
4701 old_idx = cur_idx;
4702
4703 cur_hdr = &htxn->s->txn->hdr_idx.v[cur_idx];
4704 cur_ptr = cur_next;
4705 cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1;
4706
4707 /* Now we have one full header at cur_ptr of len cur_hdr->len,
4708 * and the next header starts at cur_next. We'll check
4709 * this header in the list as well as against the default
4710 * rule.
4711 */
4712
4713 /* look for ': *'. */
4714 hn = cur_ptr;
4715 for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++);
4716 if (p >= cur_ptr+cur_hdr->len)
4717 continue;
4718 hnl = p - hn;
4719 p++;
4720 while (p < cur_ptr+cur_hdr->len && ( *p == ' ' || *p == '\t' ))
4721 p++;
4722 if (p >= cur_ptr+cur_hdr->len)
4723 continue;
4724 hv = p;
4725 hvl = cur_ptr+cur_hdr->len-p;
4726
4727 /* Lowercase the key. Don't check the size of trash, it have
4728 * the size of one buffer and the input data contains in one
4729 * buffer.
4730 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004731 out = trash.area;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004732 for (in=hn; in<hn+hnl; in++, out++)
4733 *out = tolower(*in);
4734 *out = '\0';
4735
4736 /* Check for existing entry:
4737 * assume that the table is on the top of the stack, and
4738 * push the key in the stack, the function lua_gettable()
4739 * perform the lookup.
4740 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004741 lua_pushlstring(L, trash.area, hnl);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004742 lua_gettable(L, -2);
4743 type = lua_type(L, -1);
4744
4745 switch (type) {
4746 case LUA_TNIL:
4747 /* Table not found, create it. */
4748 lua_pop(L, 1); /* remove the nil value. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004749 lua_pushlstring(L, trash.area, hnl); /* push the header name as key. */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004750 lua_newtable(L); /* create and push empty table. */
4751 lua_pushlstring(L, hv, hvl); /* push header value. */
4752 lua_rawseti(L, -2, 0); /* index header value (pop it). */
4753 lua_rawset(L, -3); /* index new table with header name (pop the values). */
4754 break;
4755
4756 case LUA_TTABLE:
4757 /* Entry found: push the value in the table. */
4758 len = lua_rawlen(L, -1);
4759 lua_pushlstring(L, hv, hvl); /* push header value. */
4760 lua_rawseti(L, -2, len+1); /* index header value (pop it). */
4761 lua_pop(L, 1); /* remove the table (it is stored in the main table). */
4762 break;
4763
4764 default:
4765 /* Other cases are errors. */
4766 hlua_pusherror(L, "internal error during the parsing of headers.");
4767 WILL_LJMP(lua_error(L));
4768 }
4769 }
4770
4771 return 1;
4772}
4773
4774__LJMP static int hlua_http_req_get_headers(lua_State *L)
4775{
4776 struct hlua_txn *htxn;
4777
4778 MAY_LJMP(check_args(L, 1, "req_get_headers"));
4779 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4780
4781 return hlua_http_get_headers(L, htxn, &htxn->s->txn->req);
4782}
4783
4784__LJMP static int hlua_http_res_get_headers(lua_State *L)
4785{
4786 struct hlua_txn *htxn;
4787
4788 MAY_LJMP(check_args(L, 1, "res_get_headers"));
4789 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4790
4791 return hlua_http_get_headers(L, htxn, &htxn->s->txn->rsp);
4792}
4793
4794/* This function replace full header, or just a value in
4795 * the request or in the response. It is a wrapper fir the
4796 * 4 following functions.
4797 */
4798__LJMP static inline int hlua_http_rep_hdr(lua_State *L, struct hlua_txn *htxn,
4799 struct http_msg *msg, int action)
4800{
4801 size_t name_len;
4802 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
4803 const char *reg = MAY_LJMP(luaL_checkstring(L, 3));
4804 const char *value = MAY_LJMP(luaL_checkstring(L, 4));
4805 struct my_regex re;
4806
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004807 /* Check if a valid response is parsed */
4808 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4809 return 0;
4810
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004811 if (!regex_comp(reg, &re, 1, 1, NULL))
4812 WILL_LJMP(luaL_argerror(L, 3, "invalid regex"));
4813
4814 http_transform_header_str(htxn->s, msg, name, name_len, value, &re, action);
4815 regex_free(&re);
4816 return 0;
4817}
4818
4819__LJMP static int hlua_http_req_rep_hdr(lua_State *L)
4820{
4821 struct hlua_txn *htxn;
4822
4823 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
4824 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4825
4826 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_HDR));
4827}
4828
4829__LJMP static int hlua_http_res_rep_hdr(lua_State *L)
4830{
4831 struct hlua_txn *htxn;
4832
4833 MAY_LJMP(check_args(L, 4, "res_rep_hdr"));
4834 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4835
4836 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->rsp, ACT_HTTP_REPLACE_HDR));
4837}
4838
4839__LJMP static int hlua_http_req_rep_val(lua_State *L)
4840{
4841 struct hlua_txn *htxn;
4842
4843 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
4844 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4845
4846 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_VAL));
4847}
4848
4849__LJMP static int hlua_http_res_rep_val(lua_State *L)
4850{
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004851 struct hlua_txn *htxn;
4852
4853 MAY_LJMP(check_args(L, 4, "res_rep_val"));
4854 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4855
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02004856 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->rsp, ACT_HTTP_REPLACE_VAL));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004857}
4858
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004859/* This function deletes all the occurrences of an header.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004860 * It is a wrapper for the 2 following functions.
4861 */
4862__LJMP static inline int hlua_http_del_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg)
4863{
4864 size_t len;
4865 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4866 struct hdr_ctx ctx;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004867 struct http_txn *txn = htxn->s->txn;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004868
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004869 /* Check if a valid response is parsed */
4870 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4871 return 0;
4872
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004873 ctx.idx = 0;
Willy Tarreaua79021a2018-06-15 18:07:57 +02004874 while (http_find_header2(name, len, ci_head(msg->chn), &txn->hdr_idx, &ctx))
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004875 http_remove_header2(msg, &txn->hdr_idx, &ctx);
4876 return 0;
4877}
4878
4879__LJMP static int hlua_http_req_del_hdr(lua_State *L)
4880{
4881 struct hlua_txn *htxn;
4882
4883 MAY_LJMP(check_args(L, 2, "req_del_hdr"));
4884 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4885
Willy Tarreaueee5b512015-04-03 23:46:31 +02004886 return hlua_http_del_hdr(L, htxn, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004887}
4888
4889__LJMP static int hlua_http_res_del_hdr(lua_State *L)
4890{
4891 struct hlua_txn *htxn;
4892
4893 MAY_LJMP(check_args(L, 2, "req_del_hdr"));
4894 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4895
Willy Tarreaueee5b512015-04-03 23:46:31 +02004896 return hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004897}
4898
4899/* This function adds an header. It is a wrapper used by
4900 * the 2 following functions.
4901 */
4902__LJMP static inline int hlua_http_add_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg)
4903{
4904 size_t name_len;
4905 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
4906 size_t value_len;
4907 const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len));
4908 char *p;
4909
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004910 /* Check if a valid message is parsed */
4911 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4912 return 0;
4913
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004914 /* Check length. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004915 trash.data = value_len + name_len + 2;
4916 if (trash.data > trash.size)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004917 return 0;
4918
4919 /* Creates the header string. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004920 p = trash.area;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004921 memcpy(p, name, name_len);
4922 p += name_len;
4923 *p = ':';
4924 p++;
4925 *p = ' ';
4926 p++;
4927 memcpy(p, value, value_len);
4928
Willy Tarreaueee5b512015-04-03 23:46:31 +02004929 lua_pushboolean(L, http_header_add_tail2(msg, &htxn->s->txn->hdr_idx,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004930 trash.area, trash.data) != 0);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004931
4932 return 0;
4933}
4934
4935__LJMP static int hlua_http_req_add_hdr(lua_State *L)
4936{
4937 struct hlua_txn *htxn;
4938
4939 MAY_LJMP(check_args(L, 3, "req_add_hdr"));
4940 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4941
Willy Tarreaueee5b512015-04-03 23:46:31 +02004942 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004943}
4944
4945__LJMP static int hlua_http_res_add_hdr(lua_State *L)
4946{
4947 struct hlua_txn *htxn;
4948
4949 MAY_LJMP(check_args(L, 3, "res_add_hdr"));
4950 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4951
Willy Tarreaueee5b512015-04-03 23:46:31 +02004952 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004953}
4954
4955static int hlua_http_req_set_hdr(lua_State *L)
4956{
4957 struct hlua_txn *htxn;
4958
4959 MAY_LJMP(check_args(L, 3, "req_set_hdr"));
4960 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4961
Willy Tarreaueee5b512015-04-03 23:46:31 +02004962 hlua_http_del_hdr(L, htxn, &htxn->s->txn->req);
4963 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004964}
4965
4966static int hlua_http_res_set_hdr(lua_State *L)
4967{
4968 struct hlua_txn *htxn;
4969
4970 MAY_LJMP(check_args(L, 3, "res_set_hdr"));
4971 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4972
Willy Tarreaueee5b512015-04-03 23:46:31 +02004973 hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp);
4974 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004975}
4976
4977/* This function set the method. */
4978static int hlua_http_req_set_meth(lua_State *L)
4979{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02004980 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004981 size_t name_len;
4982 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004983
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004984 /* Check if a valid request is parsed */
4985 if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) {
4986 lua_pushboolean(L, 0);
4987 return 1;
4988 }
4989
Willy Tarreau987e3fb2015-04-04 01:09:08 +02004990 lua_pushboolean(L, http_replace_req_line(0, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004991 return 1;
4992}
4993
4994/* This function set the method. */
4995static int hlua_http_req_set_path(lua_State *L)
4996{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02004997 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004998 size_t name_len;
4999 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02005000
5001 /* Check if a valid request is parsed */
5002 if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) {
5003 lua_pushboolean(L, 0);
5004 return 1;
5005 }
5006
Willy Tarreau987e3fb2015-04-04 01:09:08 +02005007 lua_pushboolean(L, http_replace_req_line(1, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005008 return 1;
5009}
5010
5011/* This function set the query-string. */
5012static int hlua_http_req_set_query(lua_State *L)
5013{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005014 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005015 size_t name_len;
5016 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005017
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02005018 /* Check if a valid request is parsed */
5019 if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) {
5020 lua_pushboolean(L, 0);
5021 return 1;
5022 }
5023
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005024 /* Check length. */
5025 if (name_len > trash.size - 1) {
5026 lua_pushboolean(L, 0);
5027 return 1;
5028 }
5029
5030 /* Add the mark question as prefix. */
5031 chunk_reset(&trash);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005032 trash.area[trash.data++] = '?';
5033 memcpy(trash.area + trash.data, name, name_len);
5034 trash.data += name_len;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005035
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005036 lua_pushboolean(L,
5037 http_replace_req_line(2, trash.area, trash.data, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005038 return 1;
5039}
5040
5041/* This function set the uri. */
5042static int hlua_http_req_set_uri(lua_State *L)
5043{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005044 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005045 size_t name_len;
5046 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005047
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02005048 /* Check if a valid request is parsed */
5049 if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) {
5050 lua_pushboolean(L, 0);
5051 return 1;
5052 }
5053
Willy Tarreau987e3fb2015-04-04 01:09:08 +02005054 lua_pushboolean(L, http_replace_req_line(3, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005055 return 1;
5056}
5057
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005058/* This function set the response code & optionally reason. */
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005059static int hlua_http_res_set_status(lua_State *L)
5060{
5061 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5062 unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2));
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005063 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005064
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02005065 /* Check if a valid response is parsed */
5066 if (unlikely(htxn->s->txn->rsp.msg_state < HTTP_MSG_BODY))
5067 return 0;
5068
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005069 http_set_status(code, reason, htxn->s);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005070 return 0;
5071}
5072
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005073/*
5074 *
5075 *
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005076 * Class TXN
5077 *
5078 *
5079 */
5080
5081/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02005082 * a class stream, otherwise it throws an error.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005083 */
5084__LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud)
5085{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02005086 return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref));
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005087}
5088
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005089__LJMP static int hlua_set_var(lua_State *L)
5090{
5091 struct hlua_txn *htxn;
5092 const char *name;
5093 size_t len;
5094 struct sample smp;
5095
5096 MAY_LJMP(check_args(L, 3, "set_var"));
5097
5098 /* It is useles to retrieve the stream, but this function
5099 * runs only in a stream context.
5100 */
5101 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5102 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5103
5104 /* Converts the third argument in a sample. */
5105 hlua_lua2smp(L, 3, &smp);
5106
5107 /* Store the sample in a variable. */
Willy Tarreau7560dd42016-03-10 16:28:58 +01005108 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Willy Tarreau6204cd92016-03-10 16:33:04 +01005109 vars_set_by_name(name, len, &smp);
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005110 return 0;
5111}
5112
Christopher Faulet85d79c92016-11-09 16:54:56 +01005113__LJMP static int hlua_unset_var(lua_State *L)
5114{
5115 struct hlua_txn *htxn;
5116 const char *name;
5117 size_t len;
5118 struct sample smp;
5119
5120 MAY_LJMP(check_args(L, 2, "unset_var"));
5121
5122 /* It is useles to retrieve the stream, but this function
5123 * runs only in a stream context.
5124 */
5125 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5126 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5127
5128 /* Unset the variable. */
5129 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
5130 vars_unset_by_name(name, len, &smp);
5131 return 0;
5132}
5133
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005134__LJMP static int hlua_get_var(lua_State *L)
5135{
5136 struct hlua_txn *htxn;
5137 const char *name;
5138 size_t len;
5139 struct sample smp;
5140
5141 MAY_LJMP(check_args(L, 2, "get_var"));
5142
5143 /* It is useles to retrieve the stream, but this function
5144 * runs only in a stream context.
5145 */
5146 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5147 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5148
Willy Tarreau7560dd42016-03-10 16:28:58 +01005149 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Willy Tarreau6204cd92016-03-10 16:33:04 +01005150 if (!vars_get_by_name(name, len, &smp)) {
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005151 lua_pushnil(L);
5152 return 1;
5153 }
5154
5155 return hlua_smp2lua(L, &smp);
5156}
5157
Willy Tarreau59551662015-03-10 14:23:13 +01005158__LJMP static int hlua_set_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005159{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005160 struct hlua *hlua;
5161
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005162 MAY_LJMP(check_args(L, 2, "set_priv"));
5163
Willy Tarreau87b09662015-04-03 00:22:06 +02005164 /* It is useles to retrieve the stream, but this function
5165 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005166 */
5167 MAY_LJMP(hlua_checktxn(L, 1));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005168 hlua = hlua_gethlua(L);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005169
5170 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02005171 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005172
5173 /* Get and store new value. */
5174 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
5175 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
5176
5177 return 0;
5178}
5179
Willy Tarreau59551662015-03-10 14:23:13 +01005180__LJMP static int hlua_get_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005181{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005182 struct hlua *hlua;
5183
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005184 MAY_LJMP(check_args(L, 1, "get_priv"));
5185
Willy Tarreau87b09662015-04-03 00:22:06 +02005186 /* It is useles to retrieve the stream, but this function
5187 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005188 */
5189 MAY_LJMP(hlua_checktxn(L, 1));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005190 hlua = hlua_gethlua(L);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005191
5192 /* Push configuration index in the stack. */
5193 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
5194
5195 return 1;
5196}
5197
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005198/* Create stack entry containing a class TXN. This function
5199 * return 0 if the stack does not contains free slots,
5200 * otherwise it returns 1.
5201 */
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005202static int hlua_txn_new(lua_State *L, struct stream *s, struct proxy *p, int dir, int flags)
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005203{
Willy Tarreaude491382015-04-06 11:04:28 +02005204 struct hlua_txn *htxn;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005205
5206 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005207 if (!lua_checkstack(L, 3))
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005208 return 0;
5209
5210 /* NOTE: The allocation never fails. The failure
5211 * throw an error, and the function never returns.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08005212 * if the throw is not available, the process is aborted.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005213 */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005214 /* Create the object: obj[0] = userdata. */
5215 lua_newtable(L);
Willy Tarreaude491382015-04-06 11:04:28 +02005216 htxn = lua_newuserdata(L, sizeof(*htxn));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005217 lua_rawseti(L, -2, 0);
5218
Willy Tarreaude491382015-04-06 11:04:28 +02005219 htxn->s = s;
5220 htxn->p = p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01005221 htxn->dir = dir;
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005222 htxn->flags = flags;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005223
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005224 /* Create the "f" field that contains a list of fetches. */
5225 lua_pushstring(L, "f");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005226 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005227 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005228 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005229
5230 /* Create the "sf" field that contains a list of stringsafe fetches. */
5231 lua_pushstring(L, "sf");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005232 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP | HLUA_F_AS_STRING))
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005233 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005234 lua_rawset(L, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005235
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005236 /* Create the "c" field that contains a list of converters. */
5237 lua_pushstring(L, "c");
Willy Tarreaude491382015-04-06 11:04:28 +02005238 if (!hlua_converters_new(L, htxn, 0))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005239 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005240 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005241
5242 /* Create the "sc" field that contains a list of stringsafe converters. */
5243 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01005244 if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING))
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005245 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005246 lua_rawset(L, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005247
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005248 /* Create the "req" field that contains the request channel object. */
5249 lua_pushstring(L, "req");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005250 if (!hlua_channel_new(L, &s->req))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005251 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005252 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005253
5254 /* Create the "res" field that contains the response channel object. */
5255 lua_pushstring(L, "res");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005256 if (!hlua_channel_new(L, &s->res))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005257 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005258 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005259
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005260 /* Creates the HTTP object is the current proxy allows http. */
5261 lua_pushstring(L, "http");
5262 if (p->mode == PR_MODE_HTTP) {
Willy Tarreaude491382015-04-06 11:04:28 +02005263 if (!hlua_http_new(L, htxn))
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005264 return 0;
5265 }
5266 else
5267 lua_pushnil(L);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005268 lua_rawset(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005269
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005270 /* Pop a class sesison metatable and affect it to the userdata. */
5271 lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref);
5272 lua_setmetatable(L, -2);
5273
5274 return 1;
5275}
5276
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005277__LJMP static int hlua_txn_deflog(lua_State *L)
5278{
5279 const char *msg;
5280 struct hlua_txn *htxn;
5281
5282 MAY_LJMP(check_args(L, 2, "deflog"));
5283 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5284 msg = MAY_LJMP(luaL_checkstring(L, 2));
5285
5286 hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg);
5287 return 0;
5288}
5289
5290__LJMP static int hlua_txn_log(lua_State *L)
5291{
5292 int level;
5293 const char *msg;
5294 struct hlua_txn *htxn;
5295
5296 MAY_LJMP(check_args(L, 3, "log"));
5297 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5298 level = MAY_LJMP(luaL_checkinteger(L, 2));
5299 msg = MAY_LJMP(luaL_checkstring(L, 3));
5300
5301 if (level < 0 || level >= NB_LOG_LEVELS)
5302 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
5303
5304 hlua_sendlog(htxn->s->be, level, msg);
5305 return 0;
5306}
5307
5308__LJMP static int hlua_txn_log_debug(lua_State *L)
5309{
5310 const char *msg;
5311 struct hlua_txn *htxn;
5312
5313 MAY_LJMP(check_args(L, 2, "Debug"));
5314 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5315 msg = MAY_LJMP(luaL_checkstring(L, 2));
5316 hlua_sendlog(htxn->s->be, LOG_DEBUG, msg);
5317 return 0;
5318}
5319
5320__LJMP static int hlua_txn_log_info(lua_State *L)
5321{
5322 const char *msg;
5323 struct hlua_txn *htxn;
5324
5325 MAY_LJMP(check_args(L, 2, "Info"));
5326 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5327 msg = MAY_LJMP(luaL_checkstring(L, 2));
5328 hlua_sendlog(htxn->s->be, LOG_INFO, msg);
5329 return 0;
5330}
5331
5332__LJMP static int hlua_txn_log_warning(lua_State *L)
5333{
5334 const char *msg;
5335 struct hlua_txn *htxn;
5336
5337 MAY_LJMP(check_args(L, 2, "Warning"));
5338 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5339 msg = MAY_LJMP(luaL_checkstring(L, 2));
5340 hlua_sendlog(htxn->s->be, LOG_WARNING, msg);
5341 return 0;
5342}
5343
5344__LJMP static int hlua_txn_log_alert(lua_State *L)
5345{
5346 const char *msg;
5347 struct hlua_txn *htxn;
5348
5349 MAY_LJMP(check_args(L, 2, "Alert"));
5350 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5351 msg = MAY_LJMP(luaL_checkstring(L, 2));
5352 hlua_sendlog(htxn->s->be, LOG_ALERT, msg);
5353 return 0;
5354}
5355
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005356__LJMP static int hlua_txn_set_loglevel(lua_State *L)
5357{
5358 struct hlua_txn *htxn;
5359 int ll;
5360
5361 MAY_LJMP(check_args(L, 2, "set_loglevel"));
5362 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5363 ll = MAY_LJMP(luaL_checkinteger(L, 2));
5364
5365 if (ll < 0 || ll > 7)
5366 WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7"));
5367
5368 htxn->s->logs.level = ll;
5369 return 0;
5370}
5371
5372__LJMP static int hlua_txn_set_tos(lua_State *L)
5373{
5374 struct hlua_txn *htxn;
5375 struct connection *cli_conn;
5376 int tos;
5377
5378 MAY_LJMP(check_args(L, 2, "set_tos"));
5379 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5380 tos = MAY_LJMP(luaL_checkinteger(L, 2));
5381
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02005382 if ((cli_conn = objt_conn(htxn->s->sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02005383 inet_set_tos(cli_conn->handle.fd, &cli_conn->addr.from, tos);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005384
5385 return 0;
5386}
5387
5388__LJMP static int hlua_txn_set_mark(lua_State *L)
5389{
5390#ifdef SO_MARK
5391 struct hlua_txn *htxn;
5392 struct connection *cli_conn;
5393 int mark;
5394
5395 MAY_LJMP(check_args(L, 2, "set_mark"));
5396 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5397 mark = MAY_LJMP(luaL_checkinteger(L, 2));
5398
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02005399 if ((cli_conn = objt_conn(htxn->s->sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02005400 setsockopt(cli_conn->handle.fd, SOL_SOCKET, SO_MARK, &mark, sizeof(mark));
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005401#endif
5402 return 0;
5403}
5404
Patrick Hemmer268a7072018-05-11 12:52:31 -04005405__LJMP static int hlua_txn_set_priority_class(lua_State *L)
5406{
5407 struct hlua_txn *htxn;
5408
5409 MAY_LJMP(check_args(L, 2, "set_priority_class"));
5410 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5411 htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2)));
5412 return 0;
5413}
5414
5415__LJMP static int hlua_txn_set_priority_offset(lua_State *L)
5416{
5417 struct hlua_txn *htxn;
5418
5419 MAY_LJMP(check_args(L, 2, "set_priority_offset"));
5420 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5421 htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2)));
5422 return 0;
5423}
5424
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005425/* This function is an Lua binding that send pending data
5426 * to the client, and close the stream interface.
5427 */
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005428__LJMP static int hlua_txn_done(lua_State *L)
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005429{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005430 struct hlua_txn *htxn;
Thierry FOURNIER9bd52d42016-07-14 11:45:33 +02005431 struct hlua *hlua;
Willy Tarreau81389672015-03-10 12:03:52 +01005432 struct channel *ic, *oc;
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005433
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005434 MAY_LJMP(check_args(L, 1, "close"));
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005435 htxn = MAY_LJMP(hlua_checktxn(L, 1));
Thierry FOURNIER9bd52d42016-07-14 11:45:33 +02005436 hlua = hlua_gethlua(L);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005437
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005438 /* If the flags NOTERM is set, we cannot terminate the http
5439 * session, so we just end the execution of the current
5440 * lua code.
5441 */
5442 if (htxn->flags & HLUA_TXN_NOTERM) {
5443 WILL_LJMP(hlua_done(L));
5444 return 0;
5445 }
5446
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005447 ic = &htxn->s->req;
5448 oc = &htxn->s->res;
Willy Tarreau81389672015-03-10 12:03:52 +01005449
Willy Tarreau630ef452015-08-28 10:06:15 +02005450 if (htxn->s->txn) {
5451 /* HTTP mode, let's stay in sync with the stream */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005452 b_del(&ic->buf, htxn->s->txn->req.sov);
Willy Tarreau630ef452015-08-28 10:06:15 +02005453 htxn->s->txn->req.next -= htxn->s->txn->req.sov;
5454 htxn->s->txn->req.sov = 0;
5455 ic->analysers &= AN_REQ_HTTP_XFER_BODY;
5456 oc->analysers = AN_RES_HTTP_XFER_BODY;
5457 htxn->s->txn->req.msg_state = HTTP_MSG_CLOSED;
5458 htxn->s->txn->rsp.msg_state = HTTP_MSG_DONE;
5459
Willy Tarreau630ef452015-08-28 10:06:15 +02005460 /* Note that if we want to support keep-alive, we need
5461 * to bypass the close/shutr_now calls below, but that
5462 * may only be done if the HTTP request was already
5463 * processed and the connection header is known (ie
5464 * not during TCP rules).
5465 */
5466 }
5467
Thierry FOURNIER10ec2142015-08-24 17:23:45 +02005468 channel_auto_read(ic);
Willy Tarreau81389672015-03-10 12:03:52 +01005469 channel_abort(ic);
5470 channel_auto_close(ic);
5471 channel_erase(ic);
Thierry FOURNIER10ec2142015-08-24 17:23:45 +02005472
5473 oc->wex = tick_add_ifset(now_ms, oc->wto);
Willy Tarreau81389672015-03-10 12:03:52 +01005474 channel_auto_read(oc);
5475 channel_auto_close(oc);
5476 channel_shutr_now(oc);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005477
Willy Tarreau0458b082015-08-28 09:40:04 +02005478 ic->analysers = 0;
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005479
Thierry FOURNIER9bd52d42016-07-14 11:45:33 +02005480 hlua->flags |= HLUA_STOP;
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005481 WILL_LJMP(hlua_done(L));
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005482 return 0;
5483}
5484
5485__LJMP static int hlua_log(lua_State *L)
5486{
5487 int level;
5488 const char *msg;
5489
5490 MAY_LJMP(check_args(L, 2, "log"));
5491 level = MAY_LJMP(luaL_checkinteger(L, 1));
5492 msg = MAY_LJMP(luaL_checkstring(L, 2));
5493
5494 if (level < 0 || level >= NB_LOG_LEVELS)
5495 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
5496
5497 hlua_sendlog(NULL, level, msg);
5498 return 0;
5499}
5500
5501__LJMP static int hlua_log_debug(lua_State *L)
5502{
5503 const char *msg;
5504
5505 MAY_LJMP(check_args(L, 1, "debug"));
5506 msg = MAY_LJMP(luaL_checkstring(L, 1));
5507 hlua_sendlog(NULL, LOG_DEBUG, msg);
5508 return 0;
5509}
5510
5511__LJMP static int hlua_log_info(lua_State *L)
5512{
5513 const char *msg;
5514
5515 MAY_LJMP(check_args(L, 1, "info"));
5516 msg = MAY_LJMP(luaL_checkstring(L, 1));
5517 hlua_sendlog(NULL, LOG_INFO, msg);
5518 return 0;
5519}
5520
5521__LJMP static int hlua_log_warning(lua_State *L)
5522{
5523 const char *msg;
5524
5525 MAY_LJMP(check_args(L, 1, "warning"));
5526 msg = MAY_LJMP(luaL_checkstring(L, 1));
5527 hlua_sendlog(NULL, LOG_WARNING, msg);
5528 return 0;
5529}
5530
5531__LJMP static int hlua_log_alert(lua_State *L)
5532{
5533 const char *msg;
5534
5535 MAY_LJMP(check_args(L, 1, "alert"));
5536 msg = MAY_LJMP(luaL_checkstring(L, 1));
5537 hlua_sendlog(NULL, LOG_ALERT, msg);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005538 return 0;
5539}
5540
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005541__LJMP static int hlua_sleep_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005542{
5543 int wakeup_ms = lua_tointeger(L, -1);
5544 if (now_ms < wakeup_ms)
Willy Tarreau9635e032018-10-16 17:52:55 +02005545 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005546 return 0;
5547}
5548
5549__LJMP static int hlua_sleep(lua_State *L)
5550{
5551 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005552 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005553
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005554 MAY_LJMP(check_args(L, 1, "sleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005555
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005556 delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005557 wakeup_ms = tick_add(now_ms, delay);
5558 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005559
Willy Tarreau9635e032018-10-16 17:52:55 +02005560 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005561 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005562}
5563
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005564__LJMP static int hlua_msleep(lua_State *L)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005565{
5566 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005567 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005568
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005569 MAY_LJMP(check_args(L, 1, "msleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005570
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005571 delay = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005572 wakeup_ms = tick_add(now_ms, delay);
5573 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005574
Willy Tarreau9635e032018-10-16 17:52:55 +02005575 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005576 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005577}
5578
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01005579/* This functionis an LUA binding. it permits to give back
5580 * the hand at the HAProxy scheduler. It is used when the
5581 * LUA processing consumes a lot of time.
5582 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005583__LJMP static int hlua_yield_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005584{
5585 return 0;
5586}
5587
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01005588__LJMP static int hlua_yield(lua_State *L)
5589{
Willy Tarreau9635e032018-10-16 17:52:55 +02005590 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_yield_yield, TICK_ETERNITY, HLUA_CTRLYIELD));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005591 return 0;
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01005592}
5593
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005594/* This function change the nice of the currently executed
5595 * task. It is used set low or high priority at the current
5596 * task.
5597 */
Willy Tarreau59551662015-03-10 14:23:13 +01005598__LJMP static int hlua_set_nice(lua_State *L)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005599{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005600 struct hlua *hlua;
5601 int nice;
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005602
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005603 MAY_LJMP(check_args(L, 1, "set_nice"));
5604 hlua = hlua_gethlua(L);
5605 nice = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005606
5607 /* If he task is not set, I'm in a start mode. */
5608 if (!hlua || !hlua->task)
5609 return 0;
5610
5611 if (nice < -1024)
5612 nice = -1024;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005613 else if (nice > 1024)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005614 nice = 1024;
5615
5616 hlua->task->nice = nice;
5617 return 0;
5618}
5619
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08005620/* This function is used as a callback of a task. It is called by the
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005621 * HAProxy task subsystem when the task is awaked. The LUA runtime can
5622 * return an E_AGAIN signal, the emmiter of this signal must set a
5623 * signal to wake the task.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005624 *
5625 * Task wrapper are longjmp safe because the only one Lua code
5626 * executed is the safe hlua_ctx_resume();
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005627 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02005628static struct task *hlua_process_task(struct task *task, void *context, unsigned short state)
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005629{
Olivier Houchard9f6af332018-05-25 14:04:04 +02005630 struct hlua *hlua = context;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005631 enum hlua_exec status;
5632
Christopher Faulet5bc99722018-04-25 10:34:45 +02005633 if (task->thread_mask == MAX_THREADS_MASK)
5634 task_set_affinity(task, tid_bit);
5635
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005636 /* If it is the first call to the task, we must initialize the
5637 * execution timeouts.
5638 */
5639 if (!HLUA_IS_RUNNING(hlua))
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02005640 hlua->max_time = hlua_timeout_task;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005641
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005642 /* Execute the Lua code. */
5643 status = hlua_ctx_resume(hlua, 1);
5644
5645 switch (status) {
5646 /* finished or yield */
5647 case HLUA_E_OK:
5648 hlua_ctx_destroy(hlua);
5649 task_delete(task);
5650 task_free(task);
Tim Duesterhuscd235c62018-04-24 13:56:01 +02005651 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005652 break;
5653
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01005654 case HLUA_E_AGAIN: /* co process or timeout wake me later. */
Thierry FOURNIERcb146882017-12-10 17:10:57 +01005655 notification_gc(&hlua->com);
PiBa-NLfe971b32018-05-02 22:27:14 +02005656 task->expire = hlua->wake_time;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005657 break;
5658
5659 /* finished with error. */
5660 case HLUA_E_ERRMSG:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005661 SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005662 hlua_ctx_destroy(hlua);
5663 task_delete(task);
5664 task_free(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02005665 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005666 break;
5667
5668 case HLUA_E_ERR:
5669 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005670 SEND_ERR(NULL, "Lua task: unknown error.\n");
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005671 hlua_ctx_destroy(hlua);
5672 task_delete(task);
5673 task_free(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02005674 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005675 break;
5676 }
Emeric Brun253e53e2017-10-17 18:58:40 +02005677 return task;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005678}
5679
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01005680/* This function is an LUA binding that register LUA function to be
5681 * executed after the HAProxy configuration parsing and before the
5682 * HAProxy scheduler starts. This function expect only one LUA
5683 * argument that is a function. This function returns nothing, but
5684 * throws if an error is encountered.
5685 */
5686__LJMP static int hlua_register_init(lua_State *L)
5687{
5688 struct hlua_init_function *init;
5689 int ref;
5690
5691 MAY_LJMP(check_args(L, 1, "register_init"));
5692
5693 ref = MAY_LJMP(hlua_checkfunction(L, 1));
5694
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02005695 init = calloc(1, sizeof(*init));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01005696 if (!init)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01005697 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01005698
5699 init->function_ref = ref;
5700 LIST_ADDQ(&hlua_init_functions, &init->l);
5701 return 0;
5702}
5703
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005704/* This functio is an LUA binding. It permits to register a task
5705 * executed in parallel of the main HAroxy activity. The task is
5706 * created and it is set in the HAProxy scheduler. It can be called
5707 * from the "init" section, "post init" or during the runtime.
5708 *
5709 * Lua prototype:
5710 *
5711 * <none> core.register_task(<function>)
5712 */
5713static int hlua_register_task(lua_State *L)
5714{
5715 struct hlua *hlua;
5716 struct task *task;
5717 int ref;
5718
5719 MAY_LJMP(check_args(L, 1, "register_task"));
5720
5721 ref = MAY_LJMP(hlua_checkfunction(L, 1));
5722
Willy Tarreaubafbe012017-11-24 17:34:44 +01005723 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005724 if (!hlua)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01005725 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005726
Emeric Brunc60def82017-09-27 14:59:38 +02005727 task = task_new(MAX_THREADS_MASK);
Willy Tarreaue09101e2018-10-16 17:37:12 +02005728 if (!task)
5729 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
5730
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005731 task->context = hlua;
5732 task->process = hlua_process_task;
5733
5734 if (!hlua_ctx_init(hlua, task))
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01005735 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005736
5737 /* Restore the function in the stack. */
5738 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref);
5739 hlua->nargs = 0;
5740
5741 /* Schedule task. */
5742 task_schedule(task, now_ms);
5743
5744 return 0;
5745}
5746
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005747/* Wrapper called by HAProxy to execute an LUA converter. This wrapper
5748 * doesn't allow "yield" functions because the HAProxy engine cannot
5749 * resume converters.
5750 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02005751static int hlua_sample_conv_wrapper(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005752{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02005753 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02005754 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01005755 const char *error;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005756
Willy Tarreaube508f12016-03-10 11:47:01 +01005757 if (!stream)
5758 return 0;
5759
Christopher Fauletafd8f102018-11-08 11:34:21 +01005760 if (IS_HTX_STRM(stream)) {
5761 SEND_ERR(stream->be, "Lua converter '%s': Lua fetches cannot be used when the"
5762 " HTX internal representation is enabled.\n", fcn->name);
5763 return 0;
5764 }
Willy Tarreau87b09662015-04-03 00:22:06 +02005765 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01005766 * Lua context can be not initialized. This behavior
5767 * permits to save performances because a systematic
5768 * Lua initialization cause 5% performances loss.
5769 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005770 if (!stream->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01005771 stream->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005772 if (!stream->hlua) {
5773 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
5774 return 0;
5775 }
5776 if (!hlua_ctx_init(stream->hlua, stream->task)) {
5777 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
5778 return 0;
5779 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01005780 }
5781
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005782 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005783 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005784
5785 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005786 if (!SET_SAFE_LJMP(stream->hlua->T)) {
5787 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
5788 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01005789 else
5790 error = "critical error";
5791 SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005792 return 0;
5793 }
5794
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005795 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005796 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005797 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005798 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005799 return 0;
5800 }
5801
5802 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005803 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005804
5805 /* convert input sample and pust-it in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005806 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005807 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005808 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005809 return 0;
5810 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005811 hlua_smp2lua(stream->hlua->T, smp);
5812 stream->hlua->nargs = 1;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005813
5814 /* push keywords in the stack. */
5815 if (arg_p) {
5816 for (; arg_p->type != ARGT_STOP; arg_p++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005817 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005818 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005819 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005820 return 0;
5821 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005822 hlua_arg2lua(stream->hlua->T, arg_p);
5823 stream->hlua->nargs++;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005824 }
5825 }
5826
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005827 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005828 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005829
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005830 /* At this point the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005831 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005832 }
5833
5834 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005835 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005836 /* finished. */
5837 case HLUA_E_OK:
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02005838 /* If the stack is empty, the function fails. */
5839 if (lua_gettop(stream->hlua->T) <= 0)
5840 return 0;
5841
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005842 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005843 hlua_lua2smp(stream->hlua->T, -1, smp);
5844 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005845 return 1;
5846
5847 /* yield. */
5848 case HLUA_E_AGAIN:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005849 SEND_ERR(stream->be, "Lua converter '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005850 return 0;
5851
5852 /* finished with error. */
5853 case HLUA_E_ERRMSG:
5854 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005855 SEND_ERR(stream->be, "Lua converter '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005856 fcn->name, lua_tostring(stream->hlua->T, -1));
5857 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005858 return 0;
5859
Thierry Fournierd5b073c2018-05-21 19:42:47 +02005860 case HLUA_E_ETMOUT:
5861 SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name);
5862 return 0;
5863
5864 case HLUA_E_NOMEM:
5865 SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name);
5866 return 0;
5867
5868 case HLUA_E_YIELD:
5869 SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name);
5870 return 0;
5871
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005872 case HLUA_E_ERR:
5873 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005874 SEND_ERR(stream->be, "Lua converter '%s' returns an unknown error.\n", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005875
5876 default:
5877 return 0;
5878 }
5879}
5880
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005881/* Wrapper called by HAProxy to execute a sample-fetch. this wrapper
5882 * doesn't allow "yield" functions because the HAProxy engine cannot
Willy Tarreaube508f12016-03-10 11:47:01 +01005883 * resume sample-fetches. This function will be called by the sample
5884 * fetch engine to call lua-based fetch operations.
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005885 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +02005886static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp,
5887 const char *kw, void *private)
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005888{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02005889 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02005890 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01005891 const char *error;
Willy Tarreau83061a82018-07-13 11:56:34 +02005892 const struct buffer msg = { };
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005893
Willy Tarreaube508f12016-03-10 11:47:01 +01005894 if (!stream)
5895 return 0;
Christopher Fauletafd8f102018-11-08 11:34:21 +01005896
5897 if (IS_HTX_STRM(stream)) {
5898 SEND_ERR(stream->be, "Lua sample-fetch '%s': Lua fetches cannot be used when the"
5899 " HTX internal representation is enabled.\n", fcn->name);
5900 return 0;
5901 }
Willy Tarreaube508f12016-03-10 11:47:01 +01005902
Willy Tarreau87b09662015-04-03 00:22:06 +02005903 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01005904 * Lua context can be not initialized. This behavior
5905 * permits to save performances because a systematic
5906 * Lua initialization cause 5% performances loss.
5907 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005908 if (!stream->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01005909 stream->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005910 if (!stream->hlua) {
5911 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
5912 return 0;
5913 }
5914 if (!hlua_ctx_init(stream->hlua, stream->task)) {
5915 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
5916 return 0;
5917 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01005918 }
5919
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005920 consistency_set(stream, smp->opt, &stream->hlua->cons);
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005921
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005922 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005923 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005924
5925 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005926 if (!SET_SAFE_LJMP(stream->hlua->T)) {
5927 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
5928 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01005929 else
5930 error = "critical error";
5931 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005932 return 0;
5933 }
5934
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005935 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005936 if (!lua_checkstack(stream->hlua->T, 2)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005937 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005938 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005939 return 0;
5940 }
5941
5942 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005943 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005944
5945 /* push arguments in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005946 if (!hlua_txn_new(stream->hlua->T, stream, smp->px, smp->opt & SMP_OPT_DIR,
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005947 HLUA_TXN_NOTERM)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005948 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005949 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005950 return 0;
5951 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005952 stream->hlua->nargs = 1;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005953
5954 /* push keywords in the stack. */
5955 for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) {
5956 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005957 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005958 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005959 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005960 return 0;
5961 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005962 hlua_arg2lua(stream->hlua->T, arg_p);
5963 stream->hlua->nargs++;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005964 }
5965
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005966 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005967 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005968
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005969 /* At this point the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005970 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005971 }
5972
5973 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005974 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005975 /* finished. */
5976 case HLUA_E_OK:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005977 if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) {
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005978 stream_int_retnclose(&stream->si[0], &msg);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02005979 return 0;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005980 }
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02005981 /* If the stack is empty, the function fails. */
5982 if (lua_gettop(stream->hlua->T) <= 0)
5983 return 0;
5984
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005985 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005986 hlua_lua2smp(stream->hlua->T, -1, smp);
5987 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005988
5989 /* Set the end of execution flag. */
5990 smp->flags &= ~SMP_F_MAY_CHANGE;
5991 return 1;
5992
5993 /* yield. */
5994 case HLUA_E_AGAIN:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005995 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005996 stream_int_retnclose(&stream->si[0], &msg);
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005997 SEND_ERR(smp->px, "Lua sample-fetch '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005998 return 0;
5999
6000 /* finished with error. */
6001 case HLUA_E_ERRMSG:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006002 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006003 stream_int_retnclose(&stream->si[0], &msg);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006004 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006005 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006006 fcn->name, lua_tostring(stream->hlua->T, -1));
6007 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006008 return 0;
6009
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006010 case HLUA_E_ETMOUT:
6011 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
6012 stream_int_retnclose(&stream->si[0], &msg);
6013 SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name);
6014 return 0;
6015
6016 case HLUA_E_NOMEM:
6017 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
6018 stream_int_retnclose(&stream->si[0], &msg);
6019 SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name);
6020 return 0;
6021
6022 case HLUA_E_YIELD:
6023 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
6024 stream_int_retnclose(&stream->si[0], &msg);
6025 SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name);
6026 return 0;
6027
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006028 case HLUA_E_ERR:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006029 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006030 stream_int_retnclose(&stream->si[0], &msg);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006031 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006032 SEND_ERR(smp->px, "Lua sample-fetch '%s' returns an unknown error.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006033
6034 default:
6035 return 0;
6036 }
6037}
6038
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006039/* This function is an LUA binding used for registering
6040 * "sample-conv" functions. It expects a converter name used
6041 * in the haproxy configuration file, and an LUA function.
6042 */
6043__LJMP static int hlua_register_converters(lua_State *L)
6044{
6045 struct sample_conv_kw_list *sck;
6046 const char *name;
6047 int ref;
6048 int len;
6049 struct hlua_function *fcn;
6050
6051 MAY_LJMP(check_args(L, 2, "register_converters"));
6052
6053 /* First argument : converter name. */
6054 name = MAY_LJMP(luaL_checkstring(L, 1));
6055
6056 /* Second argument : lua function. */
6057 ref = MAY_LJMP(hlua_checkfunction(L, 2));
6058
6059 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006060 sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006061 if (!sck)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006062 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006063 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006064 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006065 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006066
6067 /* Fill fcn. */
6068 fcn->name = strdup(name);
6069 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006070 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006071 fcn->function_ref = ref;
6072
6073 /* List head */
6074 sck->list.n = sck->list.p = NULL;
6075
6076 /* converter keyword. */
6077 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006078 sck->kw[0].kw = calloc(1, len);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006079 if (!sck->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006080 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006081
6082 snprintf((char *)sck->kw[0].kw, len, "lua.%s", name);
6083 sck->kw[0].process = hlua_sample_conv_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01006084 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 +01006085 sck->kw[0].val_args = NULL;
6086 sck->kw[0].in_type = SMP_T_STR;
6087 sck->kw[0].out_type = SMP_T_STR;
6088 sck->kw[0].private = fcn;
6089
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006090 /* Register this new converter */
6091 sample_register_convs(sck);
6092
6093 return 0;
6094}
6095
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006096/* This function is an LUA binding used for registering
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006097 * "sample-fetch" functions. It expects a converter name used
6098 * in the haproxy configuration file, and an LUA function.
6099 */
6100__LJMP static int hlua_register_fetches(lua_State *L)
6101{
6102 const char *name;
6103 int ref;
6104 int len;
6105 struct sample_fetch_kw_list *sfk;
6106 struct hlua_function *fcn;
6107
6108 MAY_LJMP(check_args(L, 2, "register_fetches"));
6109
6110 /* First argument : sample-fetch name. */
6111 name = MAY_LJMP(luaL_checkstring(L, 1));
6112
6113 /* Second argument : lua function. */
6114 ref = MAY_LJMP(hlua_checkfunction(L, 2));
6115
6116 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006117 sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006118 if (!sfk)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006119 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006120 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006121 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006122 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006123
6124 /* Fill fcn. */
6125 fcn->name = strdup(name);
6126 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006127 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006128 fcn->function_ref = ref;
6129
6130 /* List head */
6131 sfk->list.n = sfk->list.p = NULL;
6132
6133 /* sample-fetch keyword. */
6134 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006135 sfk->kw[0].kw = calloc(1, len);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006136 if (!sfk->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006137 return luaL_error(L, "Lua out of memory error.");
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006138
6139 snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name);
6140 sfk->kw[0].process = hlua_sample_fetch_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01006141 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 +01006142 sfk->kw[0].val_args = NULL;
6143 sfk->kw[0].out_type = SMP_T_STR;
6144 sfk->kw[0].use = SMP_USE_HTTP_ANY;
6145 sfk->kw[0].val = 0;
6146 sfk->kw[0].private = fcn;
6147
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006148 /* Register this new fetch. */
6149 sample_register_fetches(sfk);
6150
6151 return 0;
6152}
6153
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006154/* This function is a wrapper to execute each LUA function declared
6155 * as an action wrapper during the initialisation period. This function
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006156 * return ACT_RET_CONT if the processing is finished (with or without
6157 * error) and return ACT_RET_YIELD if the function must be called again
6158 * because the LUA returns a yield.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006159 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006160static enum act_return hlua_action(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +02006161 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006162{
6163 char **arg;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006164 unsigned int analyzer;
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02006165 int dir;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006166 const char *error;
Willy Tarreau83061a82018-07-13 11:56:34 +02006167 const struct buffer msg = { };
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006168
6169 switch (rule->from) {
Thierry FOURNIER6e01f382015-11-02 09:52:54 +01006170 case ACT_F_TCP_REQ_CNT: analyzer = AN_REQ_INSPECT_FE ; dir = SMP_OPT_DIR_REQ; break;
6171 case ACT_F_TCP_RES_CNT: analyzer = AN_RES_INSPECT ; dir = SMP_OPT_DIR_RES; break;
6172 case ACT_F_HTTP_REQ: analyzer = AN_REQ_HTTP_PROCESS_FE; dir = SMP_OPT_DIR_REQ; break;
6173 case ACT_F_HTTP_RES: analyzer = AN_RES_HTTP_PROCESS_BE; dir = SMP_OPT_DIR_RES; break;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006174 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006175 SEND_ERR(px, "Lua: internal error while execute action.\n");
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006176 return ACT_RET_CONT;
6177 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006178
Willy Tarreau87b09662015-04-03 00:22:06 +02006179 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006180 * Lua context can be not initialized. This behavior
6181 * permits to save performances because a systematic
6182 * Lua initialization cause 5% performances loss.
6183 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006184 if (!s->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006185 s->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006186 if (!s->hlua) {
6187 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
6188 rule->arg.hlua_rule->fcn.name);
6189 return ACT_RET_CONT;
6190 }
6191 if (!hlua_ctx_init(s->hlua, s->task)) {
6192 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
6193 rule->arg.hlua_rule->fcn.name);
6194 return ACT_RET_CONT;
6195 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006196 }
6197
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006198 consistency_set(s, dir, &s->hlua->cons);
6199
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006200 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006201 if (!HLUA_IS_RUNNING(s->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006202
6203 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006204 if (!SET_SAFE_LJMP(s->hlua->T)) {
6205 if (lua_type(s->hlua->T, -1) == LUA_TSTRING)
6206 error = lua_tostring(s->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006207 else
6208 error = "critical error";
6209 SEND_ERR(px, "Lua function '%s': %s.\n",
6210 rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006211 return ACT_RET_CONT;
6212 }
6213
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006214 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006215 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006216 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006217 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006218 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006219 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006220 }
6221
6222 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006223 lua_rawgeti(s->hlua->T, LUA_REGISTRYINDEX, rule->arg.hlua_rule->fcn.function_ref);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006224
Willy Tarreau87b09662015-04-03 00:22:06 +02006225 /* Create and and push object stream in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006226 if (!hlua_txn_new(s->hlua->T, s, px, dir, 0)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006227 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006228 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006229 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006230 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006231 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006232 s->hlua->nargs = 1;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006233
6234 /* push keywords in the stack. */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006235 for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006236 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006237 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006238 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006239 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006240 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006241 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006242 lua_pushstring(s->hlua->T, *arg);
6243 s->hlua->nargs++;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006244 }
6245
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006246 /* Now the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006247 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006248
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006249 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006250 s->hlua->max_time = hlua_timeout_session;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006251 }
6252
6253 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006254 switch (hlua_ctx_resume(s->hlua, !(flags & ACT_FLAG_FINAL))) {
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006255 /* finished. */
6256 case HLUA_E_OK:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006257 if (!consistency_check(s, dir, &s->hlua->cons)) {
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006258 stream_int_retnclose(&s->si[0], &msg);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02006259 return ACT_RET_ERR;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006260 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006261 if (s->hlua->flags & HLUA_STOP)
Thierry FOURNIER9bd52d42016-07-14 11:45:33 +02006262 return ACT_RET_STOP;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006263 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006264
6265 /* yield. */
6266 case HLUA_E_AGAIN:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006267 /* Set timeout in the required channel. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006268 if (s->hlua->wake_time != TICK_ETERNITY) {
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006269 if (analyzer & (AN_REQ_INSPECT_FE|AN_REQ_HTTP_PROCESS_FE))
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006270 s->req.analyse_exp = s->hlua->wake_time;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006271 else if (analyzer & (AN_RES_INSPECT|AN_RES_HTTP_PROCESS_BE))
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006272 s->res.analyse_exp = s->hlua->wake_time;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006273 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006274 /* Some actions can be wake up when a "write" event
6275 * is detected on a response channel. This is useful
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006276 * only for actions targeted on the requests.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006277 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006278 if (HLUA_IS_WAKERESWR(s->hlua)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006279 s->res.flags |= CF_WAKE_WRITE;
Willy Tarreau76bd97f2015-03-10 17:16:10 +01006280 if ((analyzer & (AN_REQ_INSPECT_FE|AN_REQ_HTTP_PROCESS_FE)))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006281 s->res.analysers |= analyzer;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006282 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006283 if (HLUA_IS_WAKEREQWR(s->hlua))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006284 s->req.flags |= CF_WAKE_WRITE;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006285 /* We can quit the function without consistency check
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006286 * because HAProxy is not able to manipulate data, it
6287 * is only allowed to call me again. */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006288 return ACT_RET_YIELD;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006289
6290 /* finished with error. */
6291 case HLUA_E_ERRMSG:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006292 if (!consistency_check(s, dir, &s->hlua->cons)) {
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006293 stream_int_retnclose(&s->si[0], &msg);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02006294 return ACT_RET_ERR;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006295 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006296 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006297 SEND_ERR(px, "Lua function '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006298 rule->arg.hlua_rule->fcn.name, lua_tostring(s->hlua->T, -1));
6299 lua_pop(s->hlua->T, 1);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006300 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006301
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006302 case HLUA_E_ETMOUT:
6303 if (!consistency_check(s, dir, &s->hlua->cons)) {
6304 stream_int_retnclose(&s->si[0], &msg);
6305 return ACT_RET_ERR;
6306 }
6307 SEND_ERR(px, "Lua function '%s': execution timeout.\n", rule->arg.hlua_rule->fcn.name);
6308 return 0;
6309
6310 case HLUA_E_NOMEM:
6311 if (!consistency_check(s, dir, &s->hlua->cons)) {
6312 stream_int_retnclose(&s->si[0], &msg);
6313 return ACT_RET_ERR;
6314 }
6315 SEND_ERR(px, "Lua function '%s': out of memory error.\n", rule->arg.hlua_rule->fcn.name);
6316 return 0;
6317
6318 case HLUA_E_YIELD:
6319 if (!consistency_check(s, dir, &s->hlua->cons)) {
6320 stream_int_retnclose(&s->si[0], &msg);
6321 return ACT_RET_ERR;
6322 }
6323 SEND_ERR(px, "Lua function '%s': aborting Lua processing on expired timeout.\n",
6324 rule->arg.hlua_rule->fcn.name);
6325 return 0;
6326
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006327 case HLUA_E_ERR:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006328 if (!consistency_check(s, dir, &s->hlua->cons)) {
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006329 stream_int_retnclose(&s->si[0], &msg);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02006330 return ACT_RET_ERR;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006331 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006332 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006333 SEND_ERR(px, "Lua function '%s' return an unknown error.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006334 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006335
6336 default:
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006337 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006338 }
6339}
6340
Olivier Houchard9f6af332018-05-25 14:04:04 +02006341struct task *hlua_applet_wakeup(struct task *t, void *context, unsigned short state)
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006342{
Olivier Houchard9f6af332018-05-25 14:04:04 +02006343 struct appctx *ctx = context;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006344
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006345 appctx_wakeup(ctx);
Willy Tarreaud9587412017-08-23 16:07:33 +02006346 t->expire = TICK_ETERNITY;
Willy Tarreaud1aa41f2017-07-21 16:41:56 +02006347 return t;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006348}
6349
6350static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
6351{
6352 struct stream_interface *si = ctx->owner;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006353 struct hlua *hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006354 struct task *task;
6355 char **arg;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006356 const char *error;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006357
Willy Tarreaubafbe012017-11-24 17:34:44 +01006358 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006359 if (!hlua) {
6360 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
6361 ctx->rule->arg.hlua_rule->fcn.name);
6362 return 0;
6363 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006364 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006365 ctx->ctx.hlua_apptcp.hlua = hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006366 ctx->ctx.hlua_apptcp.flags = 0;
6367
6368 /* Create task used by signal to wakeup applets. */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01006369 task = task_new(tid_bit);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006370 if (!task) {
6371 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
6372 ctx->rule->arg.hlua_rule->fcn.name);
6373 return 0;
6374 }
6375 task->nice = 0;
6376 task->context = ctx;
6377 task->process = hlua_applet_wakeup;
6378 ctx->ctx.hlua_apptcp.task = task;
6379
6380 /* In the execution wrappers linked with a stream, the
6381 * Lua context can be not initialized. This behavior
6382 * permits to save performances because a systematic
6383 * Lua initialization cause 5% performances loss.
6384 */
6385 if (!hlua_ctx_init(hlua, task)) {
6386 SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n",
6387 ctx->rule->arg.hlua_rule->fcn.name);
6388 return 0;
6389 }
6390
6391 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02006392 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006393
6394 /* The following Lua calls can fail. */
6395 if (!SET_SAFE_LJMP(hlua->T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01006396 if (lua_type(hlua->T, -1) == LUA_TSTRING)
6397 error = lua_tostring(hlua->T, -1);
6398 else
6399 error = "critical error";
6400 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
6401 ctx->rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006402 return 0;
6403 }
6404
6405 /* Check stack available size. */
6406 if (!lua_checkstack(hlua->T, 1)) {
6407 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
6408 ctx->rule->arg.hlua_rule->fcn.name);
6409 RESET_SAFE_LJMP(hlua->T);
6410 return 0;
6411 }
6412
6413 /* Restore the function in the stack. */
6414 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref);
6415
6416 /* Create and and push object stream in the stack. */
6417 if (!hlua_applet_tcp_new(hlua->T, ctx)) {
6418 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
6419 ctx->rule->arg.hlua_rule->fcn.name);
6420 RESET_SAFE_LJMP(hlua->T);
6421 return 0;
6422 }
6423 hlua->nargs = 1;
6424
6425 /* push keywords in the stack. */
6426 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
6427 if (!lua_checkstack(hlua->T, 1)) {
6428 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
6429 ctx->rule->arg.hlua_rule->fcn.name);
6430 RESET_SAFE_LJMP(hlua->T);
6431 return 0;
6432 }
6433 lua_pushstring(hlua->T, *arg);
6434 hlua->nargs++;
6435 }
6436
6437 RESET_SAFE_LJMP(hlua->T);
6438
6439 /* Wakeup the applet ASAP. */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01006440 si_cant_get(si);
Willy Tarreau3367d412018-11-15 10:57:41 +01006441 si_rx_endp_more(si);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006442
6443 return 1;
6444}
6445
6446static void hlua_applet_tcp_fct(struct appctx *ctx)
6447{
6448 struct stream_interface *si = ctx->owner;
6449 struct stream *strm = si_strm(si);
6450 struct channel *res = si_ic(si);
6451 struct act_rule *rule = ctx->rule;
6452 struct proxy *px = strm->be;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006453 struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006454
6455 /* The applet execution is already done. */
Olivier Houchard594c8c52018-08-28 14:41:31 +02006456 if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) {
6457 /* eat the whole request */
6458 co_skip(si_oc(si), co_data(si_oc(si)));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006459 return;
Olivier Houchard594c8c52018-08-28 14:41:31 +02006460 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006461
6462 /* If the stream is disconnect or closed, ldo nothing. */
6463 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
6464 return;
6465
6466 /* Execute the function. */
6467 switch (hlua_ctx_resume(hlua, 1)) {
6468 /* finished. */
6469 case HLUA_E_OK:
6470 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
6471
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006472 /* eat the whole request */
Willy Tarreaua79021a2018-06-15 18:07:57 +02006473 co_skip(si_oc(si), co_data(si_oc(si)));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006474 res->flags |= CF_READ_NULL;
6475 si_shutr(si);
6476 return;
6477
6478 /* yield. */
6479 case HLUA_E_AGAIN:
Thierry Fournier0164f202016-02-20 17:47:43 +01006480 if (hlua->wake_time != TICK_ETERNITY)
6481 task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006482 return;
6483
6484 /* finished with error. */
6485 case HLUA_E_ERRMSG:
6486 /* Display log. */
6487 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
6488 rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1));
6489 lua_pop(hlua->T, 1);
6490 goto error;
6491
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006492 case HLUA_E_ETMOUT:
6493 SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n",
6494 rule->arg.hlua_rule->fcn.name);
6495 goto error;
6496
6497 case HLUA_E_NOMEM:
6498 SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n",
6499 rule->arg.hlua_rule->fcn.name);
6500 goto error;
6501
6502 case HLUA_E_YIELD: /* unexpected */
6503 SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n",
6504 rule->arg.hlua_rule->fcn.name);
6505 goto error;
6506
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006507 case HLUA_E_ERR:
6508 /* Display log. */
6509 SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n",
6510 rule->arg.hlua_rule->fcn.name);
6511 goto error;
6512
6513 default:
6514 goto error;
6515 }
6516
6517error:
6518
6519 /* For all other cases, just close the stream. */
6520 si_shutw(si);
6521 si_shutr(si);
6522 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
6523}
6524
6525static void hlua_applet_tcp_release(struct appctx *ctx)
6526{
Willy Tarreaubd7fc952017-07-24 17:35:27 +02006527 task_delete(ctx->ctx.hlua_apptcp.task);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006528 task_free(ctx->ctx.hlua_apptcp.task);
6529 ctx->ctx.hlua_apptcp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006530 hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006531 ctx->ctx.hlua_apptcp.hlua = NULL;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006532}
6533
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006534/* The function returns 1 if the initialisation is complete, 0 if
6535 * an errors occurs and -1 if more data are required for initializing
6536 * the applet.
6537 */
6538static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
6539{
6540 struct stream_interface *si = ctx->owner;
6541 struct channel *req = si_oc(si);
6542 struct http_msg *msg;
6543 struct http_txn *txn;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006544 struct hlua *hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006545 char **arg;
6546 struct hdr_ctx hdr;
6547 struct task *task;
6548 struct sample smp; /* just used for a valid call to smp_prefetch_http. */
Thierry Fournierfd107a22016-02-19 19:57:23 +01006549 const char *error;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006550
6551 /* Wait for a full HTTP request. */
6552 if (!smp_prefetch_http(px, strm, 0, NULL, &smp, 0)) {
6553 if (smp.flags & SMP_F_MAY_CHANGE)
6554 return -1;
6555 return 0;
6556 }
6557 txn = strm->txn;
6558 msg = &txn->req;
6559
Willy Tarreau0078bfc2015-10-07 20:20:28 +02006560 /* We want two things in HTTP mode :
6561 * - enforce server-close mode if we were in keep-alive, so that the
6562 * applet is released after each response ;
6563 * - enable request body transfer to the applet in order to resync
6564 * with the response body.
6565 */
6566 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)
6567 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreau0078bfc2015-10-07 20:20:28 +02006568
Willy Tarreaubafbe012017-11-24 17:34:44 +01006569 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006570 if (!hlua) {
6571 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
6572 ctx->rule->arg.hlua_rule->fcn.name);
6573 return 0;
6574 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006575 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006576 ctx->ctx.hlua_apphttp.hlua = hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006577 ctx->ctx.hlua_apphttp.left_bytes = -1;
6578 ctx->ctx.hlua_apphttp.flags = 0;
6579
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +01006580 if (txn->req.flags & HTTP_MSGF_VER_11)
6581 ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11;
6582
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006583 /* Create task used by signal to wakeup applets. */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01006584 task = task_new(tid_bit);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006585 if (!task) {
6586 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
6587 ctx->rule->arg.hlua_rule->fcn.name);
6588 return 0;
6589 }
6590 task->nice = 0;
6591 task->context = ctx;
6592 task->process = hlua_applet_wakeup;
6593 ctx->ctx.hlua_apphttp.task = task;
6594
6595 /* In the execution wrappers linked with a stream, the
6596 * Lua context can be not initialized. This behavior
6597 * permits to save performances because a systematic
6598 * Lua initialization cause 5% performances loss.
6599 */
6600 if (!hlua_ctx_init(hlua, task)) {
6601 SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n",
6602 ctx->rule->arg.hlua_rule->fcn.name);
6603 return 0;
6604 }
6605
6606 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02006607 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006608
6609 /* The following Lua calls can fail. */
6610 if (!SET_SAFE_LJMP(hlua->T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01006611 if (lua_type(hlua->T, -1) == LUA_TSTRING)
6612 error = lua_tostring(hlua->T, -1);
6613 else
6614 error = "critical error";
6615 SEND_ERR(px, "Lua applet http '%s': %s.\n",
6616 ctx->rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006617 return 0;
6618 }
6619
6620 /* Check stack available size. */
6621 if (!lua_checkstack(hlua->T, 1)) {
6622 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
6623 ctx->rule->arg.hlua_rule->fcn.name);
6624 RESET_SAFE_LJMP(hlua->T);
6625 return 0;
6626 }
6627
6628 /* Restore the function in the stack. */
6629 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref);
6630
6631 /* Create and and push object stream in the stack. */
6632 if (!hlua_applet_http_new(hlua->T, ctx)) {
6633 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
6634 ctx->rule->arg.hlua_rule->fcn.name);
6635 RESET_SAFE_LJMP(hlua->T);
6636 return 0;
6637 }
6638 hlua->nargs = 1;
6639
6640 /* Look for a 100-continue expected. */
6641 if (msg->flags & HTTP_MSGF_VER_11) {
6642 hdr.idx = 0;
Willy Tarreaua79021a2018-06-15 18:07:57 +02006643 if (http_find_header2("Expect", 6, ci_head(req), &txn->hdr_idx, &hdr) &&
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006644 unlikely(hdr.vlen == 12 && strncasecmp(hdr.line+hdr.val, "100-continue", 12) == 0))
6645 ctx->ctx.hlua_apphttp.flags |= APPLET_100C;
6646 }
6647
6648 /* push keywords in the stack. */
6649 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
6650 if (!lua_checkstack(hlua->T, 1)) {
6651 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
6652 ctx->rule->arg.hlua_rule->fcn.name);
6653 RESET_SAFE_LJMP(hlua->T);
6654 return 0;
6655 }
6656 lua_pushstring(hlua->T, *arg);
6657 hlua->nargs++;
6658 }
6659
6660 RESET_SAFE_LJMP(hlua->T);
6661
6662 /* Wakeup the applet when data is ready for read. */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01006663 si_cant_get(si);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006664
6665 return 1;
6666}
6667
6668static void hlua_applet_http_fct(struct appctx *ctx)
6669{
6670 struct stream_interface *si = ctx->owner;
6671 struct stream *strm = si_strm(si);
6672 struct channel *res = si_ic(si);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006673 struct act_rule *rule = ctx->rule;
6674 struct proxy *px = strm->be;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006675 struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua;
Willy Tarreau206ba832018-06-14 15:27:31 +02006676 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02006677 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02006678 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02006679 size_t len2;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006680 int ret;
6681
6682 /* If the stream is disconnect or closed, ldo nothing. */
6683 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
6684 return;
6685
6686 /* Set the currently running flag. */
6687 if (!HLUA_IS_RUNNING(hlua) &&
6688 !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
6689
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006690 /* Wait for full HTTP analysys. */
6691 if (unlikely(strm->txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01006692 si_cant_get(si);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006693 return;
6694 }
6695
6696 /* Store the max amount of bytes that we can read. */
6697 ctx->ctx.hlua_apphttp.left_bytes = strm->txn->req.body_len;
6698
6699 /* We need to flush the request header. This left the body
6700 * for the Lua.
6701 */
6702
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006703 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02006704 ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006705 if (ret == -1)
6706 return;
6707
6708 /* No data available, ask for more data. */
6709 if (ret == 1)
6710 len2 = 0;
6711 if (ret == 0)
6712 len1 = 0;
Thierry FOURNIER70d318c2018-06-30 10:37:33 +02006713 if (len1 + len2 < strm->txn->req.eoh + strm->txn->req.eol) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01006714 si_cant_get(si);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006715 return;
6716 }
6717
6718 /* skip the requests bytes. */
Thierry FOURNIER70d318c2018-06-30 10:37:33 +02006719 co_skip(si_oc(si), strm->txn->req.eoh + strm->txn->req.eol);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006720 }
6721
6722 /* Executes The applet if it is not done. */
6723 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
6724
6725 /* Execute the function. */
6726 switch (hlua_ctx_resume(hlua, 1)) {
6727 /* finished. */
6728 case HLUA_E_OK:
6729 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
6730 break;
6731
6732 /* yield. */
6733 case HLUA_E_AGAIN:
Thierry Fournier0164f202016-02-20 17:47:43 +01006734 if (hlua->wake_time != TICK_ETERNITY)
6735 task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006736 return;
6737
6738 /* finished with error. */
6739 case HLUA_E_ERRMSG:
6740 /* Display log. */
6741 SEND_ERR(px, "Lua applet http '%s': %s.\n",
6742 rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1));
6743 lua_pop(hlua->T, 1);
6744 goto error;
6745
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006746 case HLUA_E_ETMOUT:
6747 SEND_ERR(px, "Lua applet http '%s': execution timeout.\n",
6748 rule->arg.hlua_rule->fcn.name);
6749 goto error;
6750
6751 case HLUA_E_NOMEM:
6752 SEND_ERR(px, "Lua applet http '%s': out of memory error.\n",
6753 rule->arg.hlua_rule->fcn.name);
6754 goto error;
6755
6756 case HLUA_E_YIELD: /* unexpected */
6757 SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n",
6758 rule->arg.hlua_rule->fcn.name);
6759 goto error;
6760
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006761 case HLUA_E_ERR:
6762 /* Display log. */
6763 SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n",
6764 rule->arg.hlua_rule->fcn.name);
6765 goto error;
6766
6767 default:
6768 goto error;
6769 }
6770 }
6771
6772 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
6773
6774 /* We must send the final chunk. */
6775 if (ctx->ctx.hlua_apphttp.flags & APPLET_CHUNKED &&
6776 !(ctx->ctx.hlua_apphttp.flags & APPLET_LAST_CHK)) {
6777
6778 /* sent last chunk at once. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02006779 ret = ci_putblk(res, "0\r\n\r\n", 5);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006780
6781 /* critical error. */
6782 if (ret == -2 || ret == -3) {
6783 SEND_ERR(px, "Lua applet http '%s'cannont send last chunk.\n",
6784 rule->arg.hlua_rule->fcn.name);
6785 goto error;
6786 }
6787
6788 /* no enough space error. */
6789 if (ret == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01006790 si_rx_room_blk(si);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006791 return;
6792 }
6793
6794 /* set the last chunk sent. */
6795 ctx->ctx.hlua_apphttp.flags |= APPLET_LAST_CHK;
6796 }
6797
6798 /* close the connection. */
6799
Frédéric Lécaille83ed5d52018-07-18 14:25:26 +02006800 /* status */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006801 strm->txn->status = ctx->ctx.hlua_apphttp.status;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006802
6803 /* eat the whole request */
Willy Tarreaua79021a2018-06-15 18:07:57 +02006804 co_skip(si_oc(si), co_data(si_oc(si)));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006805 res->flags |= CF_READ_NULL;
6806 si_shutr(si);
6807
6808 return;
6809 }
6810
6811error:
6812
6813 /* If we are in HTTP mode, and we are not send any
6814 * data, return a 500 server error in best effort:
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006815 * if there is no room available in the buffer,
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006816 * just close the connection.
6817 */
Willy Tarreau06d80a92017-10-19 14:32:15 +02006818 ci_putblk(res, error_500, strlen(error_500));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006819 if (!(strm->flags & SF_ERR_MASK))
6820 strm->flags |= SF_ERR_RESOURCE;
6821 si_shutw(si);
6822 si_shutr(si);
6823 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
6824}
6825
6826static void hlua_applet_http_release(struct appctx *ctx)
6827{
Willy Tarreaubd7fc952017-07-24 17:35:27 +02006828 task_delete(ctx->ctx.hlua_apphttp.task);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006829 task_free(ctx->ctx.hlua_apphttp.task);
6830 ctx->ctx.hlua_apphttp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006831 hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006832 ctx->ctx.hlua_apphttp.hlua = NULL;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006833}
6834
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006835/* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in
6836 * succes case, else return ACT_RET_PRS_ERR.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006837 *
6838 * This function can fail with an abort() due to an Lua critical error.
6839 * We are in the configuration parsing process of HAProxy, this abort() is
6840 * tolerated.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006841 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006842static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px,
6843 struct act_rule *rule, char **err)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006844{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006845 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006846 int i;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006847
Christopher Fauletafd8f102018-11-08 11:34:21 +01006848 if (px->options2 & PR_O2_USE_HTX) {
6849 memprintf(err, "Lua actions cannot be used when the HTX internal representation is enabled");
6850 return ACT_RET_PRS_ERR;
6851 }
6852
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006853 /* Memory for the rule. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006854 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006855 if (!rule->arg.hlua_rule) {
6856 memprintf(err, "out of memory error");
Thierry FOURNIERafa80492015-08-19 09:04:15 +02006857 return ACT_RET_PRS_ERR;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006858 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006859
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006860 /* Memory for arguments. */
6861 rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, sizeof(char *));
6862 if (!rule->arg.hlua_rule->args) {
6863 memprintf(err, "out of memory error");
6864 return ACT_RET_PRS_ERR;
6865 }
6866
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006867 /* Reference the Lua function and store the reference. */
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006868 rule->arg.hlua_rule->fcn = *fcn;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006869
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006870 /* Expect some arguments */
6871 for (i = 0; i < fcn->nargs; i++) {
6872 if (*args[i+1] == '\0') {
6873 memprintf(err, "expect %d arguments", fcn->nargs);
6874 return ACT_RET_PRS_ERR;
6875 }
6876 rule->arg.hlua_rule->args[i] = strdup(args[i + 1]);
6877 if (!rule->arg.hlua_rule->args[i]) {
6878 memprintf(err, "out of memory error");
6879 return ACT_RET_PRS_ERR;
6880 }
6881 (*cur_arg)++;
6882 }
6883 rule->arg.hlua_rule->args[i] = NULL;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006884
Thierry FOURNIER42148732015-09-02 17:17:33 +02006885 rule->action = ACT_CUSTOM;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006886 rule->action_ptr = hlua_action;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02006887 return ACT_RET_PRS_OK;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006888}
6889
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006890static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px,
6891 struct act_rule *rule, char **err)
6892{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006893 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006894
Christopher Fauletafd8f102018-11-08 11:34:21 +01006895 if (px->options2 & PR_O2_USE_HTX) {
6896 memprintf(err, "Lua services cannot be used when the HTX internal representation is enabled");
6897 return ACT_RET_PRS_ERR;
6898 }
6899
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01006900 /* HTTP applets are forbidden in tcp-request rules.
6901 * HTTP applet request requires everything initilized by
6902 * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER).
6903 * The applet will be immediately initilized, but its before
6904 * the call of this analyzer.
6905 */
6906 if (rule->from != ACT_F_HTTP_REQ) {
6907 memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets");
6908 return ACT_RET_PRS_ERR;
6909 }
6910
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006911 /* Memory for the rule. */
6912 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
6913 if (!rule->arg.hlua_rule) {
6914 memprintf(err, "out of memory error");
6915 return ACT_RET_PRS_ERR;
6916 }
6917
6918 /* Reference the Lua function and store the reference. */
6919 rule->arg.hlua_rule->fcn = *fcn;
6920
6921 /* TODO: later accept arguments. */
6922 rule->arg.hlua_rule->args = NULL;
6923
6924 /* Add applet pointer in the rule. */
6925 rule->applet.obj_type = OBJ_TYPE_APPLET;
6926 rule->applet.name = fcn->name;
6927 rule->applet.init = hlua_applet_http_init;
6928 rule->applet.fct = hlua_applet_http_fct;
6929 rule->applet.release = hlua_applet_http_release;
6930 rule->applet.timeout = hlua_timeout_applet;
6931
6932 return ACT_RET_PRS_OK;
6933}
6934
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006935/* This function is an LUA binding used for registering
6936 * "sample-conv" functions. It expects a converter name used
6937 * in the haproxy configuration file, and an LUA function.
6938 */
6939__LJMP static int hlua_register_action(lua_State *L)
6940{
6941 struct action_kw_list *akl;
6942 const char *name;
6943 int ref;
6944 int len;
6945 struct hlua_function *fcn;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006946 int nargs;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006947
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006948 /* Initialise the number of expected arguments at 0. */
6949 nargs = 0;
6950
6951 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
6952 WILL_LJMP(luaL_error(L, "'register_action' needs between 3 and 4 arguments"));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006953
6954 /* First argument : converter name. */
6955 name = MAY_LJMP(luaL_checkstring(L, 1));
6956
6957 /* Second argument : environment. */
6958 if (lua_type(L, 2) != LUA_TTABLE)
6959 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
6960
6961 /* Third argument : lua function. */
6962 ref = MAY_LJMP(hlua_checkfunction(L, 3));
6963
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006964 /* Fourth argument : number of mandatory arguments expected on the configuration line. */
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006965 if (lua_gettop(L) >= 4)
6966 nargs = MAY_LJMP(luaL_checkinteger(L, 4));
6967
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006968 /* browse the second argument as an array. */
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006969 lua_pushnil(L);
6970 while (lua_next(L, 2) != 0) {
6971 if (lua_type(L, -1) != LUA_TSTRING)
6972 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
6973
6974 /* Check required environment. Only accepted "http" or "tcp". */
6975 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006976 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006977 if (!akl)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006978 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006979 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006980 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006981 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006982
6983 /* Fill fcn. */
6984 fcn->name = strdup(name);
6985 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006986 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006987 fcn->function_ref = ref;
6988
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006989 /* Set the expected number od arguments. */
6990 fcn->nargs = nargs;
6991
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006992 /* List head */
6993 akl->list.n = akl->list.p = NULL;
6994
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006995 /* action keyword. */
6996 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006997 akl->kw[0].kw = calloc(1, len);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006998 if (!akl->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006999 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007000
7001 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
7002
7003 akl->kw[0].match_pfx = 0;
7004 akl->kw[0].private = fcn;
7005 akl->kw[0].parse = action_register_lua;
7006
7007 /* select the action registering point. */
7008 if (strcmp(lua_tostring(L, -1), "tcp-req") == 0)
7009 tcp_req_cont_keywords_register(akl);
7010 else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0)
7011 tcp_res_cont_keywords_register(akl);
7012 else if (strcmp(lua_tostring(L, -1), "http-req") == 0)
7013 http_req_keywords_register(akl);
7014 else if (strcmp(lua_tostring(L, -1), "http-res") == 0)
7015 http_res_keywords_register(akl);
7016 else
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007017 WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. "
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007018 "'tcp-req', 'tcp-res', 'http-req' or 'http-res' "
7019 "are expected.", lua_tostring(L, -1)));
7020
7021 /* pop the environment string. */
7022 lua_pop(L, 1);
7023 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007024 return ACT_RET_PRS_OK;
7025}
7026
7027static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px,
7028 struct act_rule *rule, char **err)
7029{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007030 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007031
Christopher Fauletafd8f102018-11-08 11:34:21 +01007032 if (px->options2 & PR_O2_USE_HTX) {
7033 memprintf(err, "Lua services cannot be used when the HTX internal representation is enabled");
7034 return ACT_RET_PRS_ERR;
7035 }
7036
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007037 /* Memory for the rule. */
7038 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
7039 if (!rule->arg.hlua_rule) {
7040 memprintf(err, "out of memory error");
7041 return ACT_RET_PRS_ERR;
7042 }
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007043
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007044 /* Reference the Lua function and store the reference. */
7045 rule->arg.hlua_rule->fcn = *fcn;
7046
7047 /* TODO: later accept arguments. */
7048 rule->arg.hlua_rule->args = NULL;
7049
7050 /* Add applet pointer in the rule. */
7051 rule->applet.obj_type = OBJ_TYPE_APPLET;
7052 rule->applet.name = fcn->name;
7053 rule->applet.init = hlua_applet_tcp_init;
7054 rule->applet.fct = hlua_applet_tcp_fct;
7055 rule->applet.release = hlua_applet_tcp_release;
7056 rule->applet.timeout = hlua_timeout_applet;
7057
7058 return 0;
7059}
7060
7061/* This function is an LUA binding used for registering
7062 * "sample-conv" functions. It expects a converter name used
7063 * in the haproxy configuration file, and an LUA function.
7064 */
7065__LJMP static int hlua_register_service(lua_State *L)
7066{
7067 struct action_kw_list *akl;
7068 const char *name;
7069 const char *env;
7070 int ref;
7071 int len;
7072 struct hlua_function *fcn;
7073
7074 MAY_LJMP(check_args(L, 3, "register_service"));
7075
7076 /* First argument : converter name. */
7077 name = MAY_LJMP(luaL_checkstring(L, 1));
7078
7079 /* Second argument : environment. */
7080 env = MAY_LJMP(luaL_checkstring(L, 2));
7081
7082 /* Third argument : lua function. */
7083 ref = MAY_LJMP(hlua_checkfunction(L, 3));
7084
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007085 /* Allocate and fill the sample fetch keyword struct. */
7086 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
7087 if (!akl)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007088 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007089 fcn = calloc(1, sizeof(*fcn));
7090 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007091 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007092
7093 /* Fill fcn. */
7094 len = strlen("<lua.>") + strlen(name) + 1;
7095 fcn->name = calloc(1, len);
7096 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007097 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007098 snprintf((char *)fcn->name, len, "<lua.%s>", name);
7099 fcn->function_ref = ref;
7100
7101 /* List head */
7102 akl->list.n = akl->list.p = NULL;
7103
7104 /* converter keyword. */
7105 len = strlen("lua.") + strlen(name) + 1;
7106 akl->kw[0].kw = calloc(1, len);
7107 if (!akl->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007108 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007109
7110 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
7111
Thierry FOURNIER / OZON.IO02564fd2016-11-12 11:07:05 +01007112 /* Check required environment. Only accepted "http" or "tcp". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007113 if (strcmp(env, "tcp") == 0)
7114 akl->kw[0].parse = action_register_service_tcp;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007115 else if (strcmp(env, "http") == 0)
7116 akl->kw[0].parse = action_register_service_http;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007117 else
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007118 WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. "
Eric Salamafe7456f2017-12-21 14:30:07 +01007119 "'tcp' or 'http' are expected.", env));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007120
7121 akl->kw[0].match_pfx = 0;
7122 akl->kw[0].private = fcn;
7123
7124 /* End of array. */
7125 memset(&akl->kw[1], 0, sizeof(*akl->kw));
7126
7127 /* Register this new converter */
7128 service_keywords_register(akl);
7129
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007130 return 0;
7131}
7132
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007133/* This function initialises Lua cli handler. It copies the
7134 * arguments in the Lua stack and create channel IO objects.
7135 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02007136static int hlua_cli_parse_fct(char **args, char *payload, struct appctx *appctx, void *private)
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007137{
7138 struct hlua *hlua;
7139 struct hlua_function *fcn;
7140 int i;
7141 const char *error;
7142
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007143 fcn = private;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007144 appctx->ctx.hlua_cli.fcn = private;
7145
Willy Tarreaubafbe012017-11-24 17:34:44 +01007146 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007147 if (!hlua) {
7148 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007149 return 1;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007150 }
7151 HLUA_INIT(hlua);
7152 appctx->ctx.hlua_cli.hlua = hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007153
7154 /* Create task used by signal to wakeup applets.
7155 * We use the same wakeup fonction than the Lua applet_tcp and
7156 * applet_http. It is absolutely compatible.
7157 */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01007158 appctx->ctx.hlua_cli.task = task_new(tid_bit);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007159 if (!appctx->ctx.hlua_cli.task) {
Thierry FOURNIERffbf5692016-12-16 11:14:06 +01007160 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007161 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007162 }
7163 appctx->ctx.hlua_cli.task->nice = 0;
7164 appctx->ctx.hlua_cli.task->context = appctx;
7165 appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup;
7166
7167 /* Initialises the Lua context */
7168 if (!hlua_ctx_init(hlua, appctx->ctx.hlua_cli.task)) {
7169 SEND_ERR(NULL, "Lua cli '%s': can't initialize Lua context.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007170 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007171 }
7172
7173 /* The following Lua calls can fail. */
7174 if (!SET_SAFE_LJMP(hlua->T)) {
7175 if (lua_type(hlua->T, -1) == LUA_TSTRING)
7176 error = lua_tostring(hlua->T, -1);
7177 else
7178 error = "critical error";
7179 SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error);
7180 goto error;
7181 }
7182
7183 /* Check stack available size. */
7184 if (!lua_checkstack(hlua->T, 2)) {
7185 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7186 goto error;
7187 }
7188
7189 /* Restore the function in the stack. */
7190 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
7191
7192 /* Once the arguments parsed, the CLI is like an AppletTCP,
7193 * so push AppletTCP in the stack.
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007194 */
7195 if (!hlua_applet_tcp_new(hlua->T, appctx)) {
7196 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7197 goto error;
7198 }
7199 hlua->nargs = 1;
7200
7201 /* push keywords in the stack. */
7202 for (i = 0; *args[i]; i++) {
7203 /* Check stack available size. */
7204 if (!lua_checkstack(hlua->T, 1)) {
7205 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7206 goto error;
7207 }
7208 lua_pushstring(hlua->T, args[i]);
7209 hlua->nargs++;
7210 }
7211
7212 /* We must initialize the execution timeouts. */
7213 hlua->max_time = hlua_timeout_session;
7214
7215 /* At this point the execution is safe. */
7216 RESET_SAFE_LJMP(hlua->T);
7217
7218 /* It's ok */
7219 return 0;
7220
7221 /* It's not ok. */
7222error:
7223 RESET_SAFE_LJMP(hlua->T);
7224 hlua_ctx_destroy(hlua);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007225 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007226 return 1;
7227}
7228
7229static int hlua_cli_io_handler_fct(struct appctx *appctx)
7230{
7231 struct hlua *hlua;
7232 struct stream_interface *si;
7233 struct hlua_function *fcn;
7234
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007235 hlua = appctx->ctx.hlua_cli.hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007236 si = appctx->owner;
Willy Tarreau8ae4f752016-12-14 15:41:45 +01007237 fcn = appctx->ctx.hlua_cli.fcn;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007238
7239 /* If the stream is disconnect or closed, ldo nothing. */
7240 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
7241 return 1;
7242
7243 /* Execute the function. */
7244 switch (hlua_ctx_resume(hlua, 1)) {
7245
7246 /* finished. */
7247 case HLUA_E_OK:
7248 return 1;
7249
7250 /* yield. */
7251 case HLUA_E_AGAIN:
7252 /* We want write. */
7253 if (HLUA_IS_WAKERESWR(hlua))
Willy Tarreaudb398432018-11-15 11:08:52 +01007254 si_rx_room_blk(si);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007255 /* Set the timeout. */
7256 if (hlua->wake_time != TICK_ETERNITY)
7257 task_schedule(hlua->task, hlua->wake_time);
7258 return 0;
7259
7260 /* finished with error. */
7261 case HLUA_E_ERRMSG:
7262 /* Display log. */
7263 SEND_ERR(NULL, "Lua cli '%s': %s.\n",
7264 fcn->name, lua_tostring(hlua->T, -1));
7265 lua_pop(hlua->T, 1);
7266 return 1;
7267
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007268 case HLUA_E_ETMOUT:
7269 SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n",
7270 fcn->name);
7271 return 1;
7272
7273 case HLUA_E_NOMEM:
7274 SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n",
7275 fcn->name);
7276 return 1;
7277
7278 case HLUA_E_YIELD: /* unexpected */
7279 SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n",
7280 fcn->name);
7281 return 1;
7282
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007283 case HLUA_E_ERR:
7284 /* Display log. */
7285 SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n",
7286 fcn->name);
7287 return 1;
7288
7289 default:
7290 return 1;
7291 }
7292
7293 return 1;
7294}
7295
7296static void hlua_cli_io_release_fct(struct appctx *appctx)
7297{
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007298 hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007299 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007300}
7301
7302/* This function is an LUA binding used for registering
7303 * new keywords in the cli. It expects a list of keywords
7304 * which are the "path". It is limited to 5 keywords. A
7305 * description of the command, a function to be executed
7306 * for the parsing and a function for io handlers.
7307 */
7308__LJMP static int hlua_register_cli(lua_State *L)
7309{
7310 struct cli_kw_list *cli_kws;
7311 const char *message;
7312 int ref_io;
7313 int len;
7314 struct hlua_function *fcn;
7315 int index;
7316 int i;
7317
7318 MAY_LJMP(check_args(L, 3, "register_cli"));
7319
7320 /* First argument : an array of maximum 5 keywords. */
7321 if (!lua_istable(L, 1))
7322 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table"));
7323
7324 /* Second argument : string with contextual message. */
7325 message = MAY_LJMP(luaL_checkstring(L, 2));
7326
7327 /* Third and fourth argument : lua function. */
7328 ref_io = MAY_LJMP(hlua_checkfunction(L, 3));
7329
7330 /* Allocate and fill the sample fetch keyword struct. */
7331 cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2);
7332 if (!cli_kws)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007333 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007334 fcn = calloc(1, sizeof(*fcn));
7335 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007336 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007337
7338 /* Fill path. */
7339 index = 0;
7340 lua_pushnil(L);
7341 while(lua_next(L, 1) != 0) {
7342 if (index >= 5)
7343 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries"));
7344 if (lua_type(L, -1) != LUA_TSTRING)
7345 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings"));
7346 cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1));
7347 if (!cli_kws->kw[0].str_kw[index])
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007348 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007349 index++;
7350 lua_pop(L, 1);
7351 }
7352
7353 /* Copy help message. */
7354 cli_kws->kw[0].usage = strdup(message);
7355 if (!cli_kws->kw[0].usage)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007356 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007357
7358 /* Fill fcn io handler. */
7359 len = strlen("<lua.cli>") + 1;
7360 for (i = 0; i < index; i++)
7361 len += strlen(cli_kws->kw[0].str_kw[i]) + 1;
7362 fcn->name = calloc(1, len);
7363 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007364 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007365 strncat((char *)fcn->name, "<lua.cli", len);
7366 for (i = 0; i < index; i++) {
7367 strncat((char *)fcn->name, ".", len);
7368 strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len);
7369 }
7370 strncat((char *)fcn->name, ">", len);
7371 fcn->function_ref = ref_io;
7372
7373 /* Fill last entries. */
7374 cli_kws->kw[0].private = fcn;
7375 cli_kws->kw[0].parse = hlua_cli_parse_fct;
7376 cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct;
7377 cli_kws->kw[0].io_release = hlua_cli_io_release_fct;
7378
7379 /* Register this new converter */
7380 cli_register_kw(cli_kws);
7381
7382 return 0;
7383}
7384
Thierry FOURNIERbd413492015-03-03 16:52:26 +01007385static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx,
7386 struct proxy *defpx, const char *file, int line,
7387 char **err, unsigned int *timeout)
7388{
7389 const char *error;
7390
7391 error = parse_time_err(args[1], timeout, TIME_UNIT_MS);
7392 if (error && *error != '\0') {
7393 memprintf(err, "%s: invalid timeout", args[0]);
7394 return -1;
7395 }
7396 return 0;
7397}
7398
7399static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx,
7400 struct proxy *defpx, const char *file, int line,
7401 char **err)
7402{
7403 return hlua_read_timeout(args, section_type, curpx, defpx,
7404 file, line, err, &hlua_timeout_session);
7405}
7406
7407static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx,
7408 struct proxy *defpx, const char *file, int line,
7409 char **err)
7410{
7411 return hlua_read_timeout(args, section_type, curpx, defpx,
7412 file, line, err, &hlua_timeout_task);
7413}
7414
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007415static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx,
7416 struct proxy *defpx, const char *file, int line,
7417 char **err)
7418{
7419 return hlua_read_timeout(args, section_type, curpx, defpx,
7420 file, line, err, &hlua_timeout_applet);
7421}
7422
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01007423static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx,
7424 struct proxy *defpx, const char *file, int line,
7425 char **err)
7426{
7427 char *error;
7428
7429 hlua_nb_instruction = strtoll(args[1], &error, 10);
7430 if (*error != '\0') {
7431 memprintf(err, "%s: invalid number", args[0]);
7432 return -1;
7433 }
7434 return 0;
7435}
7436
Willy Tarreau32f61e22015-03-18 17:54:59 +01007437static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx,
7438 struct proxy *defpx, const char *file, int line,
7439 char **err)
7440{
7441 char *error;
7442
7443 if (*(args[1]) == 0) {
7444 memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]);
7445 return -1;
7446 }
7447 hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L;
7448 if (*error != '\0') {
7449 memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error);
7450 return -1;
7451 }
7452 return 0;
7453}
7454
7455
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007456/* This function is called by the main configuration key "lua-load". It loads and
7457 * execute an lua file during the parsing of the HAProxy configuration file. It is
7458 * the main lua entry point.
7459 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007460 * This function runs with the HAProxy keywords API. It returns -1 if an error
7461 * occurs, otherwise it returns 0.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007462 *
7463 * In some error case, LUA set an error message in top of the stack. This function
7464 * returns this error message in the HAProxy logs and pop it from the stack.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007465 *
7466 * This function can fail with an abort() due to an Lua critical error.
7467 * We are in the configuration parsing process of HAProxy, this abort() is
7468 * tolerated.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007469 */
7470static int hlua_load(char **args, int section_type, struct proxy *curpx,
7471 struct proxy *defpx, const char *file, int line,
7472 char **err)
7473{
7474 int error;
7475
7476 /* Just load and compile the file. */
7477 error = luaL_loadfile(gL.T, args[1]);
7478 if (error) {
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007479 memprintf(err, "error in Lua file '%s': %s", args[1], lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007480 lua_pop(gL.T, 1);
7481 return -1;
7482 }
7483
7484 /* If no syntax error where detected, execute the code. */
7485 error = lua_pcall(gL.T, 0, LUA_MULTRET, 0);
7486 switch (error) {
7487 case LUA_OK:
7488 break;
7489 case LUA_ERRRUN:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007490 memprintf(err, "Lua runtime error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007491 lua_pop(gL.T, 1);
7492 return -1;
7493 case LUA_ERRMEM:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007494 memprintf(err, "Lua out of memory error.n");
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007495 return -1;
7496 case LUA_ERRERR:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007497 memprintf(err, "Lua message handler error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007498 lua_pop(gL.T, 1);
7499 return -1;
7500 case LUA_ERRGCMM:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007501 memprintf(err, "Lua garbage collector error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007502 lua_pop(gL.T, 1);
7503 return -1;
7504 default:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007505 memprintf(err, "Lua unknonwn error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007506 lua_pop(gL.T, 1);
7507 return -1;
7508 }
7509
7510 return 0;
7511}
7512
7513/* configuration keywords declaration */
7514static struct cfg_kw_list cfg_kws = {{ },{
Thierry FOURNIERbd413492015-03-03 16:52:26 +01007515 { CFG_GLOBAL, "lua-load", hlua_load },
7516 { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout },
7517 { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout },
Thierry FOURNIER56da1012015-10-01 08:42:31 +02007518 { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout },
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01007519 { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield },
Willy Tarreau32f61e22015-03-18 17:54:59 +01007520 { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem },
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007521 { 0, NULL, NULL },
7522}};
7523
Willy Tarreau0108d902018-11-25 19:14:37 +01007524INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
7525
Christopher Fauletafd8f102018-11-08 11:34:21 +01007526static int hlua_check_config()
7527{
7528 struct proxy *px;
7529 struct acl *acl;
7530 struct acl_expr *expr;
7531 struct sample_fetch *fetch;
7532 int err = 0;
7533
7534 for (px = proxies_list; px; px = px->next) {
7535 if (!(px->options2 & PR_O2_USE_HTX))
7536 continue;
7537
7538 list_for_each_entry(acl, &px->acl, list) {
7539 list_for_each_entry(expr, &acl->expr, list) {
7540 fetch = expr->smp->fetch;
7541 if (fetch->process != hlua_sample_fetch_wrapper)
7542 continue;
7543
7544 ha_alert("config: %s '%s': sample-fetch '%s' cannot be used used "
7545 "when the HTX internal representation is enabled.\n",
7546 proxy_type_str(px), px->id, fetch->kw);
7547 err++;
7548 }
7549 }
7550 }
7551 return err;
7552}
7553
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007554/* This function can fail with an abort() due to an Lua critical error.
7555 * We are in the initialisation process of HAProxy, this abort() is
7556 * tolerated.
7557 */
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007558int hlua_post_init()
7559{
7560 struct hlua_init_function *init;
7561 const char *msg;
7562 enum hlua_exec ret;
Thierry Fournierfd107a22016-02-19 19:57:23 +01007563 const char *error;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007564
Thierry Fournier3d4a6752016-02-19 20:53:30 +01007565 /* Call post initialisation function in safe environement. */
7566 if (!SET_SAFE_LJMP(gL.T)) {
7567 if (lua_type(gL.T, -1) == LUA_TSTRING)
7568 error = lua_tostring(gL.T, -1);
7569 else
7570 error = "critical error";
7571 fprintf(stderr, "Lua post-init: %s.\n", error);
7572 exit(1);
7573 }
Frédéric Lécaille54f2bcf2018-08-29 13:46:24 +02007574
7575#if USE_OPENSSL
7576 /* Initialize SSL server. */
7577 if (socket_ssl.xprt->prepare_srv) {
7578 int saved_used_backed = global.ssl_used_backend;
7579 // don't affect maxconn automatic computation
7580 socket_ssl.xprt->prepare_srv(&socket_ssl);
7581 global.ssl_used_backend = saved_used_backed;
7582 }
7583#endif
7584
Thierry Fournier3d4a6752016-02-19 20:53:30 +01007585 hlua_fcn_post_init(gL.T);
7586 RESET_SAFE_LJMP(gL.T);
7587
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007588 list_for_each_entry(init, &hlua_init_functions, l) {
7589 lua_rawgeti(gL.T, LUA_REGISTRYINDEX, init->function_ref);
7590 ret = hlua_ctx_resume(&gL, 0);
7591 switch (ret) {
7592 case HLUA_E_OK:
7593 lua_pop(gL.T, -1);
7594 return 1;
7595 case HLUA_E_AGAIN:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007596 ha_alert("Lua init: yield not allowed.\n");
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007597 return 0;
7598 case HLUA_E_ERRMSG:
7599 msg = lua_tostring(gL.T, -1);
Christopher Faulet767a84b2017-11-24 16:50:31 +01007600 ha_alert("lua init: %s.\n", msg);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007601 return 0;
7602 case HLUA_E_ERR:
7603 default:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007604 ha_alert("Lua init: unknown runtime error.\n");
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007605 return 0;
7606 }
7607 }
7608 return 1;
7609}
7610
Willy Tarreau32f61e22015-03-18 17:54:59 +01007611/* The memory allocator used by the Lua stack. <ud> is a pointer to the
7612 * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize>
7613 * is the previously allocated size or the kind of object in case of a new
7614 * allocation. <nsize> is the requested new size.
7615 */
7616static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
7617{
7618 struct hlua_mem_allocator *zone = ud;
7619
7620 if (nsize == 0) {
7621 /* it's a free */
7622 if (ptr)
7623 zone->allocated -= osize;
7624 free(ptr);
7625 return NULL;
7626 }
7627
7628 if (!ptr) {
7629 /* it's a new allocation */
7630 if (zone->limit && zone->allocated + nsize > zone->limit)
7631 return NULL;
7632
7633 ptr = malloc(nsize);
7634 if (ptr)
7635 zone->allocated += nsize;
7636 return ptr;
7637 }
7638
7639 /* it's a realloc */
7640 if (zone->limit && zone->allocated + nsize - osize > zone->limit)
7641 return NULL;
7642
7643 ptr = realloc(ptr, nsize);
7644 if (ptr)
7645 zone->allocated += nsize - osize;
7646 return ptr;
7647}
7648
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007649/* Ithis function can fail with an abort() due to an Lua critical error.
7650 * We are in the initialisation process of HAProxy, this abort() is
7651 * tolerated.
7652 */
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01007653void hlua_init(void)
7654{
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007655 int i;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01007656 int idx;
7657 struct sample_fetch *sf;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007658 struct sample_conv *sc;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01007659 char *p;
Thierry Fournierfd107a22016-02-19 19:57:23 +01007660 const char *error_msg;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01007661#ifdef USE_OPENSSL
Willy Tarreau80f5fae2015-02-27 16:38:20 +01007662 struct srv_kw *kw;
7663 int tmp_error;
7664 char *error;
Thierry FOURNIER36d13742015-03-17 16:48:53 +01007665 char *args[] = { /* SSL client configuration. */
7666 "ssl",
7667 "verify",
7668 "none",
Thierry FOURNIER36d13742015-03-17 16:48:53 +01007669 NULL
7670 };
Willy Tarreau80f5fae2015-02-27 16:38:20 +01007671#endif
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007672
Thierry FOURNIER380d0932015-01-23 14:27:52 +01007673 /* Init main lua stack. */
7674 gL.Mref = LUA_REFNIL;
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01007675 gL.flags = 0;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01007676 LIST_INIT(&gL.com);
Willy Tarreau42ef75f2017-04-12 21:40:29 +02007677 gL.T = lua_newstate(hlua_alloc, &hlua_global_allocator);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01007678 hlua_sethlua(&gL);
7679 gL.Tref = LUA_REFNIL;
7680 gL.task = NULL;
7681
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007682 /* From this point, until the end of the initialisation function,
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007683 * the Lua function can fail with an abort. We are in the initialisation
7684 * process of HAProxy, this abort() is tolerated.
7685 */
7686
Thierry FOURNIER380d0932015-01-23 14:27:52 +01007687 /* Initialise lua. */
7688 luaL_openlibs(gL.T);
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007689
Thierry Fournier75933d42016-01-21 09:30:18 +01007690 /* Set safe environment for the initialisation. */
7691 if (!SET_SAFE_LJMP(gL.T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01007692 if (lua_type(gL.T, -1) == LUA_TSTRING)
7693 error_msg = lua_tostring(gL.T, -1);
7694 else
7695 error_msg = "critical error";
7696 fprintf(stderr, "Lua init: %s.\n", error_msg);
Thierry Fournier75933d42016-01-21 09:30:18 +01007697 exit(1);
7698 }
7699
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007700 /*
7701 *
7702 * Create "core" object.
7703 *
7704 */
7705
Thierry FOURNIERa2d8c652015-03-11 17:29:39 +01007706 /* This table entry is the object "core" base. */
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007707 lua_newtable(gL.T);
7708
7709 /* Push the loglevel constants. */
Willy Tarreau80f5fae2015-02-27 16:38:20 +01007710 for (i = 0; i < NB_LOG_LEVELS; i++)
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007711 hlua_class_const_int(gL.T, log_levels[i], i);
7712
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007713 /* Register special functions. */
7714 hlua_class_function(gL.T, "register_init", hlua_register_init);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01007715 hlua_class_function(gL.T, "register_task", hlua_register_task);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01007716 hlua_class_function(gL.T, "register_fetches", hlua_register_fetches);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01007717 hlua_class_function(gL.T, "register_converters", hlua_register_converters);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007718 hlua_class_function(gL.T, "register_action", hlua_register_action);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007719 hlua_class_function(gL.T, "register_service", hlua_register_service);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007720 hlua_class_function(gL.T, "register_cli", hlua_register_cli);
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01007721 hlua_class_function(gL.T, "yield", hlua_yield);
Willy Tarreau59551662015-03-10 14:23:13 +01007722 hlua_class_function(gL.T, "set_nice", hlua_set_nice);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01007723 hlua_class_function(gL.T, "sleep", hlua_sleep);
7724 hlua_class_function(gL.T, "msleep", hlua_msleep);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01007725 hlua_class_function(gL.T, "add_acl", hlua_add_acl);
7726 hlua_class_function(gL.T, "del_acl", hlua_del_acl);
7727 hlua_class_function(gL.T, "set_map", hlua_set_map);
7728 hlua_class_function(gL.T, "del_map", hlua_del_map);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007729 hlua_class_function(gL.T, "tcp", hlua_socket_new);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01007730 hlua_class_function(gL.T, "log", hlua_log);
7731 hlua_class_function(gL.T, "Debug", hlua_log_debug);
7732 hlua_class_function(gL.T, "Info", hlua_log_info);
7733 hlua_class_function(gL.T, "Warning", hlua_log_warning);
7734 hlua_class_function(gL.T, "Alert", hlua_log_alert);
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02007735 hlua_class_function(gL.T, "done", hlua_done);
Thierry Fournierfb0b5462016-01-21 09:28:58 +01007736 hlua_fcn_reg_core_fcn(gL.T);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007737
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007738 lua_setglobal(gL.T, "core");
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01007739
7740 /*
7741 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02007742 * Register class Map
7743 *
7744 */
7745
7746 /* This table entry is the object "Map" base. */
7747 lua_newtable(gL.T);
7748
7749 /* register pattern types. */
7750 for (i=0; i<PAT_MATCH_NUM; i++)
7751 hlua_class_const_int(gL.T, pat_match_names[i], i);
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01007752 for (i=0; i<PAT_MATCH_NUM; i++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007753 snprintf(trash.area, trash.size, "_%s", pat_match_names[i]);
7754 hlua_class_const_int(gL.T, trash.area, i);
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01007755 }
Thierry FOURNIER3def3932015-04-07 11:27:54 +02007756
7757 /* register constructor. */
7758 hlua_class_function(gL.T, "new", hlua_map_new);
7759
7760 /* Create and fill the metatable. */
7761 lua_newtable(gL.T);
7762
7763 /* Create and fille the __index entry. */
7764 lua_pushstring(gL.T, "__index");
7765 lua_newtable(gL.T);
7766
7767 /* Register . */
7768 hlua_class_function(gL.T, "lookup", hlua_map_lookup);
7769 hlua_class_function(gL.T, "slookup", hlua_map_slookup);
7770
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007771 lua_rawset(gL.T, -3);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02007772
Thierry Fournier45e78d72016-02-19 18:34:46 +01007773 /* Register previous table in the registry with reference and named entry.
7774 * The function hlua_register_metatable() pops the stack, so we
7775 * previously create a copy of the table.
7776 */
Thierry FOURNIER3def3932015-04-07 11:27:54 +02007777 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007778 class_map_ref = hlua_register_metatable(gL.T, CLASS_MAP);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02007779
7780 /* Assign the metatable to the mai Map object. */
7781 lua_setmetatable(gL.T, -2);
7782
7783 /* Set a name to the table. */
7784 lua_setglobal(gL.T, "Map");
7785
7786 /*
7787 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01007788 * Register class Channel
7789 *
7790 */
7791
7792 /* Create and fill the metatable. */
7793 lua_newtable(gL.T);
7794
7795 /* Create and fille the __index entry. */
7796 lua_pushstring(gL.T, "__index");
7797 lua_newtable(gL.T);
7798
7799 /* Register . */
7800 hlua_class_function(gL.T, "get", hlua_channel_get);
7801 hlua_class_function(gL.T, "dup", hlua_channel_dup);
7802 hlua_class_function(gL.T, "getline", hlua_channel_getline);
7803 hlua_class_function(gL.T, "set", hlua_channel_set);
7804 hlua_class_function(gL.T, "append", hlua_channel_append);
7805 hlua_class_function(gL.T, "send", hlua_channel_send);
7806 hlua_class_function(gL.T, "forward", hlua_channel_forward);
7807 hlua_class_function(gL.T, "get_in_len", hlua_channel_get_in_len);
7808 hlua_class_function(gL.T, "get_out_len", hlua_channel_get_out_len);
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01007809 hlua_class_function(gL.T, "is_full", hlua_channel_is_full);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01007810
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007811 lua_rawset(gL.T, -3);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01007812
7813 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007814 class_channel_ref = hlua_register_metatable(gL.T, CLASS_CHANNEL);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01007815
7816 /*
7817 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01007818 * Register class Fetches
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01007819 *
7820 */
7821
7822 /* Create and fill the metatable. */
7823 lua_newtable(gL.T);
7824
7825 /* Create and fille the __index entry. */
7826 lua_pushstring(gL.T, "__index");
7827 lua_newtable(gL.T);
7828
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01007829 /* Browse existing fetches and create the associated
7830 * object method.
7831 */
7832 sf = NULL;
7833 while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) {
7834
7835 /* Dont register the keywork if the arguments check function are
7836 * not safe during the runtime.
7837 */
7838 if ((sf->val_args != NULL) &&
7839 (sf->val_args != val_payload_lv) &&
7840 (sf->val_args != val_hdr))
7841 continue;
7842
7843 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
7844 * by an underscore.
7845 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007846 strncpy(trash.area, sf->kw, trash.size);
7847 trash.area[trash.size - 1] = '\0';
7848 for (p = trash.area; *p; p++)
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01007849 if (*p == '.' || *p == '-' || *p == '+')
7850 *p = '_';
7851
7852 /* Register the function. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007853 lua_pushstring(gL.T, trash.area);
Willy Tarreau2ec22742015-03-10 14:27:20 +01007854 lua_pushlightuserdata(gL.T, sf);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01007855 lua_pushcclosure(gL.T, hlua_run_sample_fetch, 1);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007856 lua_rawset(gL.T, -3);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01007857 }
7858
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007859 lua_rawset(gL.T, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01007860
7861 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007862 class_fetches_ref = hlua_register_metatable(gL.T, CLASS_FETCHES);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01007863
7864 /*
7865 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007866 * Register class Converters
7867 *
7868 */
7869
7870 /* Create and fill the metatable. */
7871 lua_newtable(gL.T);
7872
7873 /* Create and fill the __index entry. */
7874 lua_pushstring(gL.T, "__index");
7875 lua_newtable(gL.T);
7876
7877 /* Browse existing converters and create the associated
7878 * object method.
7879 */
7880 sc = NULL;
7881 while ((sc = sample_conv_getnext(sc, &idx)) != NULL) {
7882 /* Dont register the keywork if the arguments check function are
7883 * not safe during the runtime.
7884 */
7885 if (sc->val_args != NULL)
7886 continue;
7887
7888 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
7889 * by an underscore.
7890 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007891 strncpy(trash.area, sc->kw, trash.size);
7892 trash.area[trash.size - 1] = '\0';
7893 for (p = trash.area; *p; p++)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007894 if (*p == '.' || *p == '-' || *p == '+')
7895 *p = '_';
7896
7897 /* Register the function. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007898 lua_pushstring(gL.T, trash.area);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007899 lua_pushlightuserdata(gL.T, sc);
7900 lua_pushcclosure(gL.T, hlua_run_sample_conv, 1);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007901 lua_rawset(gL.T, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007902 }
7903
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007904 lua_rawset(gL.T, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007905
7906 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007907 class_converters_ref = hlua_register_metatable(gL.T, CLASS_CONVERTERS);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007908
7909 /*
7910 *
Thierry FOURNIER08504f42015-03-16 14:17:08 +01007911 * Register class HTTP
7912 *
7913 */
7914
7915 /* Create and fill the metatable. */
7916 lua_newtable(gL.T);
7917
7918 /* Create and fille the __index entry. */
7919 lua_pushstring(gL.T, "__index");
7920 lua_newtable(gL.T);
7921
7922 /* Register Lua functions. */
7923 hlua_class_function(gL.T, "req_get_headers",hlua_http_req_get_headers);
7924 hlua_class_function(gL.T, "req_del_header", hlua_http_req_del_hdr);
7925 hlua_class_function(gL.T, "req_rep_header", hlua_http_req_rep_hdr);
7926 hlua_class_function(gL.T, "req_rep_value", hlua_http_req_rep_val);
7927 hlua_class_function(gL.T, "req_add_header", hlua_http_req_add_hdr);
7928 hlua_class_function(gL.T, "req_set_header", hlua_http_req_set_hdr);
7929 hlua_class_function(gL.T, "req_set_method", hlua_http_req_set_meth);
7930 hlua_class_function(gL.T, "req_set_path", hlua_http_req_set_path);
7931 hlua_class_function(gL.T, "req_set_query", hlua_http_req_set_query);
7932 hlua_class_function(gL.T, "req_set_uri", hlua_http_req_set_uri);
7933
7934 hlua_class_function(gL.T, "res_get_headers",hlua_http_res_get_headers);
7935 hlua_class_function(gL.T, "res_del_header", hlua_http_res_del_hdr);
7936 hlua_class_function(gL.T, "res_rep_header", hlua_http_res_rep_hdr);
7937 hlua_class_function(gL.T, "res_rep_value", hlua_http_res_rep_val);
7938 hlua_class_function(gL.T, "res_add_header", hlua_http_res_add_hdr);
7939 hlua_class_function(gL.T, "res_set_header", hlua_http_res_set_hdr);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02007940 hlua_class_function(gL.T, "res_set_status", hlua_http_res_set_status);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01007941
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007942 lua_rawset(gL.T, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01007943
7944 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007945 class_http_ref = hlua_register_metatable(gL.T, CLASS_HTTP);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01007946
7947 /*
7948 *
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007949 * Register class AppletTCP
7950 *
7951 */
7952
7953 /* Create and fill the metatable. */
7954 lua_newtable(gL.T);
7955
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007956 /* Create and fille the __index entry. */
7957 lua_pushstring(gL.T, "__index");
7958 lua_newtable(gL.T);
7959
7960 /* Register Lua functions. */
Thierry FOURNIER / OZON.IO3e1d7912016-12-12 12:29:34 +01007961 hlua_class_function(gL.T, "getline", hlua_applet_tcp_getline);
7962 hlua_class_function(gL.T, "receive", hlua_applet_tcp_recv);
7963 hlua_class_function(gL.T, "send", hlua_applet_tcp_send);
7964 hlua_class_function(gL.T, "set_priv", hlua_applet_tcp_set_priv);
7965 hlua_class_function(gL.T, "get_priv", hlua_applet_tcp_get_priv);
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01007966 hlua_class_function(gL.T, "set_var", hlua_applet_tcp_set_var);
7967 hlua_class_function(gL.T, "unset_var", hlua_applet_tcp_unset_var);
7968 hlua_class_function(gL.T, "get_var", hlua_applet_tcp_get_var);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007969
7970 lua_settable(gL.T, -3);
7971
7972 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007973 class_applet_tcp_ref = hlua_register_metatable(gL.T, CLASS_APPLET_TCP);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007974
7975 /*
7976 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007977 * Register class AppletHTTP
7978 *
7979 */
7980
7981 /* Create and fill the metatable. */
7982 lua_newtable(gL.T);
7983
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007984 /* Create and fille the __index entry. */
7985 lua_pushstring(gL.T, "__index");
7986 lua_newtable(gL.T);
7987
7988 /* Register Lua functions. */
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01007989 hlua_class_function(gL.T, "set_priv", hlua_applet_http_set_priv);
7990 hlua_class_function(gL.T, "get_priv", hlua_applet_http_get_priv);
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01007991 hlua_class_function(gL.T, "set_var", hlua_applet_http_set_var);
7992 hlua_class_function(gL.T, "unset_var", hlua_applet_http_unset_var);
7993 hlua_class_function(gL.T, "get_var", hlua_applet_http_get_var);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007994 hlua_class_function(gL.T, "getline", hlua_applet_http_getline);
7995 hlua_class_function(gL.T, "receive", hlua_applet_http_recv);
7996 hlua_class_function(gL.T, "send", hlua_applet_http_send);
7997 hlua_class_function(gL.T, "add_header", hlua_applet_http_addheader);
7998 hlua_class_function(gL.T, "set_status", hlua_applet_http_status);
7999 hlua_class_function(gL.T, "start_response", hlua_applet_http_start_response);
8000
8001 lua_settable(gL.T, -3);
8002
8003 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008004 class_applet_http_ref = hlua_register_metatable(gL.T, CLASS_APPLET_HTTP);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02008005
8006 /*
8007 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008008 * Register class TXN
8009 *
8010 */
8011
8012 /* Create and fill the metatable. */
8013 lua_newtable(gL.T);
8014
8015 /* Create and fille the __index entry. */
8016 lua_pushstring(gL.T, "__index");
8017 lua_newtable(gL.T);
8018
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01008019 /* Register Lua functions. */
Patrick Hemmer268a7072018-05-11 12:52:31 -04008020 hlua_class_function(gL.T, "set_priv", hlua_set_priv);
8021 hlua_class_function(gL.T, "get_priv", hlua_get_priv);
8022 hlua_class_function(gL.T, "set_var", hlua_set_var);
8023 hlua_class_function(gL.T, "unset_var", hlua_unset_var);
8024 hlua_class_function(gL.T, "get_var", hlua_get_var);
8025 hlua_class_function(gL.T, "done", hlua_txn_done);
8026 hlua_class_function(gL.T, "set_loglevel", hlua_txn_set_loglevel);
8027 hlua_class_function(gL.T, "set_tos", hlua_txn_set_tos);
8028 hlua_class_function(gL.T, "set_mark", hlua_txn_set_mark);
8029 hlua_class_function(gL.T, "set_priority_class", hlua_txn_set_priority_class);
8030 hlua_class_function(gL.T, "set_priority_offset", hlua_txn_set_priority_offset);
8031 hlua_class_function(gL.T, "deflog", hlua_txn_deflog);
8032 hlua_class_function(gL.T, "log", hlua_txn_log);
8033 hlua_class_function(gL.T, "Debug", hlua_txn_log_debug);
8034 hlua_class_function(gL.T, "Info", hlua_txn_log_info);
8035 hlua_class_function(gL.T, "Warning", hlua_txn_log_warning);
8036 hlua_class_function(gL.T, "Alert", hlua_txn_log_alert);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01008037
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008038 lua_rawset(gL.T, -3);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008039
8040 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008041 class_txn_ref = hlua_register_metatable(gL.T, CLASS_TXN);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008042
8043 /*
8044 *
8045 * Register class Socket
8046 *
8047 */
8048
8049 /* Create and fill the metatable. */
8050 lua_newtable(gL.T);
8051
8052 /* Create and fille the __index entry. */
8053 lua_pushstring(gL.T, "__index");
8054 lua_newtable(gL.T);
8055
Baptiste Assmann84bb4932015-03-02 21:40:06 +01008056#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008057 hlua_class_function(gL.T, "connect_ssl", hlua_socket_connect_ssl);
Baptiste Assmann84bb4932015-03-02 21:40:06 +01008058#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008059 hlua_class_function(gL.T, "connect", hlua_socket_connect);
8060 hlua_class_function(gL.T, "send", hlua_socket_send);
8061 hlua_class_function(gL.T, "receive", hlua_socket_receive);
8062 hlua_class_function(gL.T, "close", hlua_socket_close);
8063 hlua_class_function(gL.T, "getpeername", hlua_socket_getpeername);
8064 hlua_class_function(gL.T, "getsockname", hlua_socket_getsockname);
8065 hlua_class_function(gL.T, "setoption", hlua_socket_setoption);
8066 hlua_class_function(gL.T, "settimeout", hlua_socket_settimeout);
8067
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008068 lua_rawset(gL.T, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008069
8070 /* Register the garbage collector entry. */
8071 lua_pushstring(gL.T, "__gc");
8072 lua_pushcclosure(gL.T, hlua_socket_gc, 0);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008073 lua_rawset(gL.T, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008074
8075 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008076 class_socket_ref = hlua_register_metatable(gL.T, CLASS_SOCKET);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008077
8078 /* Proxy and server configuration initialisation. */
8079 memset(&socket_proxy, 0, sizeof(socket_proxy));
8080 init_new_proxy(&socket_proxy);
8081 socket_proxy.parent = NULL;
8082 socket_proxy.last_change = now.tv_sec;
8083 socket_proxy.id = "LUA-SOCKET";
8084 socket_proxy.cap = PR_CAP_FE | PR_CAP_BE;
8085 socket_proxy.maxconn = 0;
8086 socket_proxy.accept = NULL;
8087 socket_proxy.options2 |= PR_O2_INDEPSTR;
8088 socket_proxy.srv = NULL;
8089 socket_proxy.conn_retries = 0;
8090 socket_proxy.timeout.connect = 5000; /* By default the timeout connection is 5s. */
8091
8092 /* Init TCP server: unchanged parameters */
8093 memset(&socket_tcp, 0, sizeof(socket_tcp));
8094 socket_tcp.next = NULL;
8095 socket_tcp.proxy = &socket_proxy;
8096 socket_tcp.obj_type = OBJ_TYPE_SERVER;
8097 LIST_INIT(&socket_tcp.actconns);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04008098 socket_tcp.pendconns = EB_ROOT;
Christopher Faulet40a007c2017-07-03 15:41:01 +02008099 socket_tcp.priv_conns = NULL;
8100 socket_tcp.idle_conns = NULL;
8101 socket_tcp.safe_conns = NULL;
Emeric Brun52a91d32017-08-31 14:41:55 +02008102 socket_tcp.next_state = SRV_ST_RUNNING; /* early server setup */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008103 socket_tcp.last_change = 0;
8104 socket_tcp.id = "LUA-TCP-CONN";
8105 socket_tcp.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8106 socket_tcp.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8107 socket_tcp.pp_opts = 0; /* Remove proxy protocol. */
8108
8109 /* XXX: Copy default parameter from default server,
8110 * but the default server is not initialized.
8111 */
8112 socket_tcp.maxqueue = socket_proxy.defsrv.maxqueue;
8113 socket_tcp.minconn = socket_proxy.defsrv.minconn;
8114 socket_tcp.maxconn = socket_proxy.defsrv.maxconn;
8115 socket_tcp.slowstart = socket_proxy.defsrv.slowstart;
8116 socket_tcp.onerror = socket_proxy.defsrv.onerror;
8117 socket_tcp.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
8118 socket_tcp.onmarkedup = socket_proxy.defsrv.onmarkedup;
8119 socket_tcp.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
8120 socket_tcp.uweight = socket_proxy.defsrv.iweight;
8121 socket_tcp.iweight = socket_proxy.defsrv.iweight;
8122
8123 socket_tcp.check.status = HCHK_STATUS_INI;
8124 socket_tcp.check.rise = socket_proxy.defsrv.check.rise;
8125 socket_tcp.check.fall = socket_proxy.defsrv.check.fall;
8126 socket_tcp.check.health = socket_tcp.check.rise; /* socket, but will fall down at first failure */
8127 socket_tcp.check.server = &socket_tcp;
8128
8129 socket_tcp.agent.status = HCHK_STATUS_INI;
8130 socket_tcp.agent.rise = socket_proxy.defsrv.agent.rise;
8131 socket_tcp.agent.fall = socket_proxy.defsrv.agent.fall;
8132 socket_tcp.agent.health = socket_tcp.agent.rise; /* socket, but will fall down at first failure */
8133 socket_tcp.agent.server = &socket_tcp;
8134
Willy Tarreaua261e9b2016-12-22 20:44:00 +01008135 socket_tcp.xprt = xprt_get(XPRT_RAW);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008136
8137#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008138 /* Init TCP server: unchanged parameters */
8139 memset(&socket_ssl, 0, sizeof(socket_ssl));
8140 socket_ssl.next = NULL;
8141 socket_ssl.proxy = &socket_proxy;
8142 socket_ssl.obj_type = OBJ_TYPE_SERVER;
8143 LIST_INIT(&socket_ssl.actconns);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04008144 socket_ssl.pendconns = EB_ROOT;
Christopher Faulet40a007c2017-07-03 15:41:01 +02008145 socket_tcp.priv_conns = NULL;
8146 socket_tcp.idle_conns = NULL;
8147 socket_tcp.safe_conns = NULL;
Emeric Brun52a91d32017-08-31 14:41:55 +02008148 socket_ssl.next_state = SRV_ST_RUNNING; /* early server setup */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008149 socket_ssl.last_change = 0;
8150 socket_ssl.id = "LUA-SSL-CONN";
8151 socket_ssl.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8152 socket_ssl.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8153 socket_ssl.pp_opts = 0; /* Remove proxy protocol. */
8154
8155 /* XXX: Copy default parameter from default server,
8156 * but the default server is not initialized.
8157 */
8158 socket_ssl.maxqueue = socket_proxy.defsrv.maxqueue;
8159 socket_ssl.minconn = socket_proxy.defsrv.minconn;
8160 socket_ssl.maxconn = socket_proxy.defsrv.maxconn;
8161 socket_ssl.slowstart = socket_proxy.defsrv.slowstart;
8162 socket_ssl.onerror = socket_proxy.defsrv.onerror;
8163 socket_ssl.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
8164 socket_ssl.onmarkedup = socket_proxy.defsrv.onmarkedup;
8165 socket_ssl.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
8166 socket_ssl.uweight = socket_proxy.defsrv.iweight;
8167 socket_ssl.iweight = socket_proxy.defsrv.iweight;
8168
8169 socket_ssl.check.status = HCHK_STATUS_INI;
8170 socket_ssl.check.rise = socket_proxy.defsrv.check.rise;
8171 socket_ssl.check.fall = socket_proxy.defsrv.check.fall;
8172 socket_ssl.check.health = socket_ssl.check.rise; /* socket, but will fall down at first failure */
8173 socket_ssl.check.server = &socket_ssl;
8174
8175 socket_ssl.agent.status = HCHK_STATUS_INI;
8176 socket_ssl.agent.rise = socket_proxy.defsrv.agent.rise;
8177 socket_ssl.agent.fall = socket_proxy.defsrv.agent.fall;
8178 socket_ssl.agent.health = socket_ssl.agent.rise; /* socket, but will fall down at first failure */
8179 socket_ssl.agent.server = &socket_ssl;
8180
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008181 socket_ssl.use_ssl = 1;
Willy Tarreaua261e9b2016-12-22 20:44:00 +01008182 socket_ssl.xprt = xprt_get(XPRT_SSL);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008183
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008184 for (idx = 0; args[idx] != NULL; idx++) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008185 if ((kw = srv_find_kw(args[idx])) != NULL) { /* Maybe it's registered server keyword */
8186 /*
8187 *
8188 * If the keyword is not known, we can search in the registered
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08008189 * server keywords. This is useful to configure special SSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008190 * features like client certificates and ssl_verify.
8191 *
8192 */
8193 tmp_error = kw->parse(args, &idx, &socket_proxy, &socket_ssl, &error);
8194 if (tmp_error != 0) {
8195 fprintf(stderr, "INTERNAL ERROR: %s\n", error);
8196 abort(); /* This must be never arrives because the command line
8197 not editable by the user. */
8198 }
8199 idx += kw->skip;
8200 }
8201 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008202#endif
Thierry Fournier75933d42016-01-21 09:30:18 +01008203
8204 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01008205}
Willy Tarreaubb57d942016-12-21 19:04:56 +01008206
Willy Tarreau80713382018-11-26 10:19:54 +01008207static void hlua_register_build_options(void)
8208{
Willy Tarreaubb57d942016-12-21 19:04:56 +01008209 char *ptr = NULL;
Willy Tarreau80713382018-11-26 10:19:54 +01008210
Willy Tarreaubb57d942016-12-21 19:04:56 +01008211 memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE);
8212 hap_register_build_opts(ptr, 1);
8213}
Willy Tarreau80713382018-11-26 10:19:54 +01008214
8215INITCALL0(STG_REGISTER, hlua_register_build_options);
Willy Tarreaue6552512018-11-26 11:33:13 +01008216REGISTER_CONFIG_POSTPARSER("hlua", hlua_check_config);