blob: 5dd88f2f19454b21b2ec5c8fd59167fa2a3cc350 [file] [log] [blame]
Thierry Fourniere726b142016-02-11 17:57:57 +01001/*
2 * Lua unsafe core engine
3 *
4 * Copyright 2015-2016 Thierry Fournier <tfournier@arpalert.org>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +010013#include <ctype.h>
Mark Lakes56cc1252018-03-27 09:48:06 +020014#include <limits.h>
Thierry FOURNIERbabae282015-09-17 11:36:37 +020015#include <setjmp.h>
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +010016
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +010017#include <lauxlib.h>
18#include <lua.h>
19#include <lualib.h>
20
Thierry FOURNIER463119c2015-03-10 00:35:36 +010021#if !defined(LUA_VERSION_NUM) || LUA_VERSION_NUM < 503
22#error "Requires Lua 5.3 or later."
Cyril Bontédc0306e2015-03-02 00:08:40 +010023#endif
24
Thierry FOURNIER380d0932015-01-23 14:27:52 +010025#include <ebpttree.h>
26
27#include <common/cfgparse.h>
Willy Tarreaub059b892018-10-16 17:57:36 +020028#include <common/compiler.h>
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +020029#include <common/hathreads.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010030#include <common/initcall.h>
31#include <common/xref.h>
Thierry FOURNIER380d0932015-01-23 14:27:52 +010032
William Lallemand9ed62032016-11-21 17:49:11 +010033#include <types/cli.h>
Thierry FOURNIER380d0932015-01-23 14:27:52 +010034#include <types/hlua.h>
35#include <types/proxy.h>
William Lallemand9ed62032016-11-21 17:49:11 +010036#include <types/stats.h>
Thierry FOURNIER380d0932015-01-23 14:27:52 +010037
Thierry FOURNIER55da1652015-01-23 11:36:30 +010038#include <proto/arg.h>
Willy Tarreau8a8d83b2015-04-13 13:24:54 +020039#include <proto/applet.h>
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010040#include <proto/channel.h>
William Lallemand9ed62032016-11-21 17:49:11 +010041#include <proto/cli.h>
Willy Tarreaua71f6422016-11-16 17:00:14 +010042#include <proto/connection.h>
William Lallemand9ed62032016-11-21 17:49:11 +010043#include <proto/stats.h>
Thierry FOURNIER9a819e72015-02-16 20:22:55 +010044#include <proto/hdr_idx.h>
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +010045#include <proto/hlua.h>
Thierry Fournierfb0b5462016-01-21 09:28:58 +010046#include <proto/hlua_fcn.h>
Willy Tarreau79e57332018-10-02 16:01:16 +020047#include <proto/http_fetch.h>
Willy Tarreau61c112a2018-10-02 16:43:32 +020048#include <proto/http_rules.h>
Thierry FOURNIER3def3932015-04-07 11:27:54 +020049#include <proto/map.h>
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010050#include <proto/obj_type.h>
Patrick Hemmer268a7072018-05-11 12:52:31 -040051#include <proto/queue.h>
Thierry FOURNIER83758bb2015-02-04 13:21:04 +010052#include <proto/pattern.h>
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +010053#include <proto/payload.h>
54#include <proto/proto_http.h>
55#include <proto/sample.h>
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010056#include <proto/server.h>
Willy Tarreaufeb76402015-04-03 14:10:06 +020057#include <proto/session.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020058#include <proto/stream.h>
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010059#include <proto/stream_interface.h>
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +010060#include <proto/task.h>
Willy Tarreau39713102016-11-25 15:49:32 +010061#include <proto/tcp_rules.h>
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +020062#include <proto/vars.h>
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +010063
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +010064/* Lua uses longjmp to perform yield or throwing errors. This
65 * macro is used only for identifying the function that can
66 * not return because a longjmp is executed.
67 * __LJMP marks a prototype of hlua file that can use longjmp.
68 * WILL_LJMP() marks an lua function that will use longjmp.
69 * MAY_LJMP() marks an lua function that may use longjmp.
70 */
71#define __LJMP
Willy Tarreau4e7cc332018-10-20 17:45:48 +020072#define WILL_LJMP(func) do { func; my_unreachable(); } while(0)
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +010073#define MAY_LJMP(func) func
74
Thierry FOURNIERbabae282015-09-17 11:36:37 +020075/* This couple of function executes securely some Lua calls outside of
76 * the lua runtime environment. Each Lua call can return a longjmp
77 * if it encounter a memory error.
78 *
79 * Lua documentation extract:
80 *
81 * If an error happens outside any protected environment, Lua calls
82 * a panic function (see lua_atpanic) and then calls abort, thus
83 * exiting the host application. Your panic function can avoid this
84 * exit by never returning (e.g., doing a long jump to your own
85 * recovery point outside Lua).
86 *
87 * The panic function runs as if it were a message handler (see
88 * §2.3); in particular, the error message is at the top of the
89 * stack. However, there is no guarantee about stack space. To push
90 * anything on the stack, the panic function must first check the
91 * available space (see §4.2).
92 *
93 * We must check all the Lua entry point. This includes:
94 * - The include/proto/hlua.h exported functions
95 * - the task wrapper function
96 * - The action wrapper function
97 * - The converters wrapper function
98 * - The sample-fetch wrapper functions
99 *
100 * It is tolerated that the initilisation function returns an abort.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800101 * Before each Lua abort, an error message is written on stderr.
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200102 *
103 * The macro SET_SAFE_LJMP initialise the longjmp. The Macro
104 * RESET_SAFE_LJMP reset the longjmp. These function must be macro
105 * because they must be exists in the program stack when the longjmp
106 * is called.
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +0200107 *
108 * Note that the Lua processing is not really thread safe. It provides
109 * heavy system which consists to add our own lock function in the Lua
110 * code and recompile the library. This system will probably not accepted
111 * by maintainers of various distribs.
112 *
113 * Our main excution point of the Lua is the function lua_resume(). A
114 * quick looking on the Lua sources displays a lua_lock() a the start
115 * of function and a lua_unlock() at the end of the function. So I
116 * conclude that the Lua thread safe mode just perform a mutex around
117 * all execution. So I prefer to do this in the HAProxy code, it will be
118 * easier for distro maintainers.
119 *
120 * Note that the HAProxy lua functions rounded by the macro SET_SAFE_LJMP
121 * and RESET_SAFE_LJMP manipulates the Lua stack, so it will be careful
122 * to set mutex around these functions.
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200123 */
Willy Tarreau86abe442018-11-25 20:12:18 +0100124__decl_spinlock(hlua_global_lock);
Thierry FOURNIERffbad792017-07-12 11:39:04 +0200125THREAD_LOCAL jmp_buf safe_ljmp_env;
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200126static int hlua_panic_safe(lua_State *L) { return 0; }
127static int hlua_panic_ljmp(lua_State *L) { longjmp(safe_ljmp_env, 1); }
128
129#define SET_SAFE_LJMP(__L) \
130 ({ \
131 int ret; \
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100132 HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200133 if (setjmp(safe_ljmp_env) != 0) { \
134 lua_atpanic(__L, hlua_panic_safe); \
135 ret = 0; \
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100136 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200137 } else { \
138 lua_atpanic(__L, hlua_panic_ljmp); \
139 ret = 1; \
140 } \
141 ret; \
142 })
143
144/* If we are the last function catching Lua errors, we
145 * must reset the panic function.
146 */
147#define RESET_SAFE_LJMP(__L) \
148 do { \
149 lua_atpanic(__L, hlua_panic_safe); \
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100150 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200151 } while(0)
152
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200153/* Applet status flags */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200154#define APPLET_DONE 0x01 /* applet processing is done. */
155#define APPLET_100C 0x02 /* 100 continue expected. */
156#define APPLET_HDR_SENT 0x04 /* Response header sent. */
157#define APPLET_CHUNKED 0x08 /* Use transfer encoding chunked. */
158#define APPLET_LAST_CHK 0x10 /* Last chunk sent. */
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +0100159#define APPLET_HTTP11 0x20 /* Last chunk sent. */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200160
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100161/* The main Lua execution context. */
162struct hlua gL;
163
Thierry FOURNIERebed6e92016-12-16 11:54:07 +0100164/* This is the memory pool containing struct lua for applets
165 * (including cli).
166 */
Willy Tarreau8ceae722018-11-26 11:58:30 +0100167DECLARE_STATIC_POOL(pool_head_hlua, "hlua", sizeof(struct hlua));
Thierry FOURNIERebed6e92016-12-16 11:54:07 +0100168
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100169/* Used for Socket connection. */
170static struct proxy socket_proxy;
171static struct server socket_tcp;
172#ifdef USE_OPENSSL
173static struct server socket_ssl;
174#endif
175
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +0100176/* List head of the function called at the initialisation time. */
177struct list hlua_init_functions = LIST_HEAD_INIT(hlua_init_functions);
178
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +0100179/* The following variables contains the reference of the different
180 * Lua classes. These references are useful for identify metadata
181 * associated with an object.
182 */
Thierry FOURNIER65f34c62015-02-16 20:11:43 +0100183static int class_txn_ref;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100184static int class_socket_ref;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +0100185static int class_channel_ref;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +0100186static int class_fetches_ref;
Thierry FOURNIER594afe72015-03-10 23:58:30 +0100187static int class_converters_ref;
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100188static int class_http_ref;
Thierry FOURNIER3def3932015-04-07 11:27:54 +0200189static int class_map_ref;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200190static int class_applet_tcp_ref;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200191static int class_applet_http_ref;
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +0100192
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100193/* Global Lua execution timeout. By default Lua, execution linked
Willy Tarreau87b09662015-04-03 00:22:06 +0200194 * with stream (actions, sample-fetches and converters) have a
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100195 * short timeout. Lua linked with tasks doesn't have a timeout
196 * because a task may remain alive during all the haproxy execution.
197 */
198static unsigned int hlua_timeout_session = 4000; /* session timeout. */
199static unsigned int hlua_timeout_task = TICK_ETERNITY; /* task timeout. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200200static unsigned int hlua_timeout_applet = 4000; /* applet timeout. */
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100201
Thierry FOURNIERee9f8022015-03-03 17:37:37 +0100202/* Interrupts the Lua processing each "hlua_nb_instruction" instructions.
203 * it is used for preventing infinite loops.
204 *
205 * I test the scheer with an infinite loop containing one incrementation
206 * and one test. I run this loop between 10 seconds, I raise a ceil of
207 * 710M loops from one interrupt each 9000 instructions, so I fix the value
208 * to one interrupt each 10 000 instructions.
209 *
210 * configured | Number of
211 * instructions | loops executed
212 * between two | in milions
213 * forced yields |
214 * ---------------+---------------
215 * 10 | 160
216 * 500 | 670
217 * 1000 | 680
218 * 5000 | 700
219 * 7000 | 700
220 * 8000 | 700
221 * 9000 | 710 <- ceil
222 * 10000 | 710
223 * 100000 | 710
224 * 1000000 | 710
225 *
226 */
227static unsigned int hlua_nb_instruction = 10000;
228
Willy Tarreau32f61e22015-03-18 17:54:59 +0100229/* Descriptor for the memory allocation state. If limit is not null, it will
230 * be enforced on any memory allocation.
231 */
232struct hlua_mem_allocator {
233 size_t allocated;
234 size_t limit;
235};
236
237static struct hlua_mem_allocator hlua_global_allocator;
238
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200239static const char error_500[] =
Jarno Huuskonen16ad94a2017-01-09 14:17:10 +0200240 "HTTP/1.0 500 Internal Server Error\r\n"
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200241 "Cache-Control: no-cache\r\n"
242 "Connection: close\r\n"
243 "Content-Type: text/html\r\n"
244 "\r\n"
Joseph Herlant7fe15772018-11-15 10:07:32 -0800245 "<html><body><h1>500 Internal Server Error</h1>\nAn internal server error occurred.\n</body></html>\n";
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200246
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100247/* These functions converts types between HAProxy internal args or
248 * sample and LUA types. Another function permits to check if the
249 * LUA stack contains arguments according with an required ARG_T
250 * format.
251 */
252static int hlua_arg2lua(lua_State *L, const struct arg *arg);
253static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100254__LJMP static int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp,
David Carlier0c437f42016-04-27 16:21:56 +0100255 uint64_t mask, struct proxy *p);
Willy Tarreau5eadada2015-03-10 17:28:54 +0100256static int hlua_smp2lua(lua_State *L, struct sample *smp);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100257static int hlua_smp2lua_str(lua_State *L, struct sample *smp);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100258static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp);
259
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200260__LJMP static int hlua_http_get_headers(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg);
261
Thierry FOURNIER23bc3752015-09-11 19:15:43 +0200262#define SEND_ERR(__be, __fmt, __args...) \
263 do { \
264 send_log(__be, LOG_ERR, __fmt, ## __args); \
265 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) \
Christopher Faulet767a84b2017-11-24 16:50:31 +0100266 ha_alert(__fmt, ## __args); \
Thierry FOURNIER23bc3752015-09-11 19:15:43 +0200267 } while (0)
268
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100269/* Used to check an Lua function type in the stack. It creates and
270 * returns a reference of the function. This function throws an
271 * error if the rgument is not a "function".
272 */
273__LJMP unsigned int hlua_checkfunction(lua_State *L, int argno)
274{
275 if (!lua_isfunction(L, argno)) {
Thierry FOURNIERfd1e9552018-02-23 18:41:18 +0100276 const char *msg = lua_pushfstring(L, "function expected, got %s", luaL_typename(L, argno));
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100277 WILL_LJMP(luaL_argerror(L, argno, msg));
278 }
279 lua_pushvalue(L, argno);
280 return luaL_ref(L, LUA_REGISTRYINDEX);
281}
282
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200283/* Return the string that is of the top of the stack. */
284const char *hlua_get_top_error_string(lua_State *L)
285{
286 if (lua_gettop(L) < 1)
287 return "unknown error";
288 if (lua_type(L, -1) != LUA_TSTRING)
289 return "unknown error";
290 return lua_tostring(L, -1);
291}
292
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200293__LJMP static const char *hlua_traceback(lua_State *L)
294{
295 lua_Debug ar;
296 int level = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +0200297 struct buffer *msg = get_trash_chunk();
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200298 int filled = 0;
299
300 while (lua_getstack(L, level++, &ar)) {
301
302 /* Add separator */
303 if (filled)
304 chunk_appendf(msg, ", ");
305 filled = 1;
306
307 /* Fill fields:
308 * 'S': fills in the fields source, short_src, linedefined, lastlinedefined, and what;
309 * 'l': fills in the field currentline;
310 * 'n': fills in the field name and namewhat;
311 * 't': fills in the field istailcall;
312 */
313 lua_getinfo(L, "Slnt", &ar);
314
315 /* Append code localisation */
316 if (ar.currentline > 0)
317 chunk_appendf(msg, "%s:%d ", ar.short_src, ar.currentline);
318 else
319 chunk_appendf(msg, "%s ", ar.short_src);
320
321 /*
322 * Get function name
323 *
324 * if namewhat is no empty, name is defined.
325 * what contains "Lua" for Lua function, "C" for C function,
326 * or "main" for main code.
327 */
328 if (*ar.namewhat != '\0' && ar.name != NULL) /* is there a name from code? */
329 chunk_appendf(msg, "%s '%s'", ar.namewhat, ar.name); /* use it */
330
331 else if (*ar.what == 'm') /* "main", the code is not executed in a function */
332 chunk_appendf(msg, "main chunk");
333
334 else if (*ar.what != 'C') /* for Lua functions, use <file:line> */
335 chunk_appendf(msg, "C function line %d", ar.linedefined);
336
337 else /* nothing left... */
338 chunk_appendf(msg, "?");
339
340
341 /* Display tailed call */
342 if (ar.istailcall)
343 chunk_appendf(msg, " ...");
344 }
345
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200346 return msg->area;
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200347}
348
349
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100350/* This function check the number of arguments available in the
351 * stack. If the number of arguments available is not the same
352 * then <nb> an error is throwed.
353 */
354__LJMP static inline void check_args(lua_State *L, int nb, char *fcn)
355{
356 if (lua_gettop(L) == nb)
357 return;
358 WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb));
359}
360
Mark Lakes22154b42018-01-29 14:38:40 -0800361/* This function pushes an error string prefixed by the file name
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100362 * and the line number where the error is encountered.
363 */
364static int hlua_pusherror(lua_State *L, const char *fmt, ...)
365{
366 va_list argp;
367 va_start(argp, fmt);
368 luaL_where(L, 1);
369 lua_pushvfstring(L, fmt, argp);
370 va_end(argp);
371 lua_concat(L, 2);
372 return 1;
373}
374
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100375/* This functions is used with sample fetch and converters. It
376 * converts the HAProxy configuration argument in a lua stack
377 * values.
378 *
379 * It takes an array of "arg", and each entry of the array is
380 * converted and pushed in the LUA stack.
381 */
382static int hlua_arg2lua(lua_State *L, const struct arg *arg)
383{
384 switch (arg->type) {
385 case ARGT_SINT:
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100386 case ARGT_TIME:
387 case ARGT_SIZE:
Thierry FOURNIERf90838b2015-03-06 13:48:32 +0100388 lua_pushinteger(L, arg->data.sint);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100389 break;
390
391 case ARGT_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200392 lua_pushlstring(L, arg->data.str.area, arg->data.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100393 break;
394
395 case ARGT_IPV4:
396 case ARGT_IPV6:
397 case ARGT_MSK4:
398 case ARGT_MSK6:
399 case ARGT_FE:
400 case ARGT_BE:
401 case ARGT_TAB:
402 case ARGT_SRV:
403 case ARGT_USR:
404 case ARGT_MAP:
405 default:
406 lua_pushnil(L);
407 break;
408 }
409 return 1;
410}
411
412/* This function take one entrie in an LUA stack at the index "ud",
413 * and try to convert it in an HAProxy argument entry. This is useful
414 * with sample fetch wrappers. The input arguments are gived to the
415 * lua wrapper and converted as arg list by thi function.
416 */
417static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg)
418{
419 switch (lua_type(L, ud)) {
420
421 case LUA_TNUMBER:
422 case LUA_TBOOLEAN:
423 arg->type = ARGT_SINT;
424 arg->data.sint = lua_tointeger(L, ud);
425 break;
426
427 case LUA_TSTRING:
428 arg->type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200429 arg->data.str.area = (char *)lua_tolstring(L, ud, (size_t *)&arg->data.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100430 break;
431
432 case LUA_TUSERDATA:
433 case LUA_TNIL:
434 case LUA_TTABLE:
435 case LUA_TFUNCTION:
436 case LUA_TTHREAD:
437 case LUA_TLIGHTUSERDATA:
438 arg->type = ARGT_SINT;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +0200439 arg->data.sint = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100440 break;
441 }
442 return 1;
443}
444
445/* the following functions are used to convert a struct sample
446 * in Lua type. This useful to convert the return of the
447 * fetchs or converters.
448 */
Willy Tarreau5eadada2015-03-10 17:28:54 +0100449static int hlua_smp2lua(lua_State *L, struct sample *smp)
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100450{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200451 switch (smp->data.type) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100452 case SMP_T_SINT:
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100453 case SMP_T_BOOL:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200454 lua_pushinteger(L, smp->data.u.sint);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100455 break;
456
457 case SMP_T_BIN:
458 case SMP_T_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200459 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100460 break;
461
462 case SMP_T_METH:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200463 switch (smp->data.u.meth.meth) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100464 case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break;
465 case HTTP_METH_GET: lua_pushstring(L, "GET"); break;
466 case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break;
467 case HTTP_METH_POST: lua_pushstring(L, "POST"); break;
468 case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break;
469 case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break;
470 case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break;
471 case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break;
472 case HTTP_METH_OTHER:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200473 lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100474 break;
475 default:
476 lua_pushnil(L);
477 break;
478 }
479 break;
480
481 case SMP_T_IPV4:
482 case SMP_T_IPV6:
483 case SMP_T_ADDR: /* This type is never used to qualify a sample. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200484 if (sample_casts[smp->data.type][SMP_T_STR] &&
485 sample_casts[smp->data.type][SMP_T_STR](smp))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200486 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Willy Tarreau5eadada2015-03-10 17:28:54 +0100487 else
488 lua_pushnil(L);
489 break;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100490 default:
491 lua_pushnil(L);
492 break;
493 }
494 return 1;
495}
496
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100497/* the following functions are used to convert a struct sample
498 * in Lua strings. This is useful to convert the return of the
499 * fetchs or converters.
500 */
501static int hlua_smp2lua_str(lua_State *L, struct sample *smp)
502{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200503 switch (smp->data.type) {
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100504
505 case SMP_T_BIN:
506 case SMP_T_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200507 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100508 break;
509
510 case SMP_T_METH:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200511 switch (smp->data.u.meth.meth) {
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100512 case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break;
513 case HTTP_METH_GET: lua_pushstring(L, "GET"); break;
514 case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break;
515 case HTTP_METH_POST: lua_pushstring(L, "POST"); break;
516 case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break;
517 case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break;
518 case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break;
519 case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break;
520 case HTTP_METH_OTHER:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200521 lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100522 break;
523 default:
524 lua_pushstring(L, "");
525 break;
526 }
527 break;
528
529 case SMP_T_SINT:
530 case SMP_T_BOOL:
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100531 case SMP_T_IPV4:
532 case SMP_T_IPV6:
533 case SMP_T_ADDR: /* This type is never used to qualify a sample. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200534 if (sample_casts[smp->data.type][SMP_T_STR] &&
535 sample_casts[smp->data.type][SMP_T_STR](smp))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200536 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100537 else
538 lua_pushstring(L, "");
539 break;
540 default:
541 lua_pushstring(L, "");
542 break;
543 }
544 return 1;
545}
546
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100547/* the following functions are used to convert an Lua type in a
548 * struct sample. This is useful to provide data from a converter
549 * to the LUA code.
550 */
551static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp)
552{
553 switch (lua_type(L, ud)) {
554
555 case LUA_TNUMBER:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200556 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200557 smp->data.u.sint = lua_tointeger(L, ud);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100558 break;
559
560
561 case LUA_TBOOLEAN:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200562 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200563 smp->data.u.sint = lua_toboolean(L, ud);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100564 break;
565
566 case LUA_TSTRING:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200567 smp->data.type = SMP_T_STR;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100568 smp->flags |= SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200569 smp->data.u.str.area = (char *)lua_tolstring(L, ud, (size_t *)&smp->data.u.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100570 break;
571
572 case LUA_TUSERDATA:
573 case LUA_TNIL:
574 case LUA_TTABLE:
575 case LUA_TFUNCTION:
576 case LUA_TTHREAD:
577 case LUA_TLIGHTUSERDATA:
Thierry FOURNIER93405e12015-08-26 14:19:03 +0200578 case LUA_TNONE:
579 default:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200580 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200581 smp->data.u.sint = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100582 break;
583 }
584 return 1;
585}
586
587/* This function check the "argp" builded by another conversion function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800588 * is in accord with the expected argp defined by the "mask". The function
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100589 * returns true or false. It can be adjust the types if there compatibles.
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100590 *
591 * This function assumes thant the argp argument contains ARGM_NBARGS + 1
592 * entries.
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100593 */
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100594__LJMP int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp,
David Carlier0c437f42016-04-27 16:21:56 +0100595 uint64_t mask, struct proxy *p)
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100596{
597 int min_arg;
598 int idx;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100599 struct proxy *px;
600 char *sname, *pname;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100601
602 idx = 0;
603 min_arg = ARGM(mask);
604 mask >>= ARGM_BITS;
605
606 while (1) {
607
608 /* Check oversize. */
609 if (idx >= ARGM_NBARGS && argp[idx].type != ARGT_STOP) {
Cyril Bonté577a36a2015-03-02 00:08:38 +0100610 WILL_LJMP(luaL_argerror(L, first + idx, "Malformed argument mask"));
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100611 }
612
613 /* Check for mandatory arguments. */
614 if (argp[idx].type == ARGT_STOP) {
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100615 if (idx < min_arg) {
616
617 /* If miss other argument than the first one, we return an error. */
618 if (idx > 0)
619 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
620
621 /* If first argument have a certain type, some default values
622 * may be used. See the function smp_resolve_args().
623 */
624 switch (mask & ARGT_MASK) {
625
626 case ARGT_FE:
627 if (!(p->cap & PR_CAP_FE))
628 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
629 argp[idx].data.prx = p;
630 argp[idx].type = ARGT_FE;
631 argp[idx+1].type = ARGT_STOP;
632 break;
633
634 case ARGT_BE:
635 if (!(p->cap & PR_CAP_BE))
636 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
637 argp[idx].data.prx = p;
638 argp[idx].type = ARGT_BE;
639 argp[idx+1].type = ARGT_STOP;
640 break;
641
642 case ARGT_TAB:
643 argp[idx].data.prx = p;
644 argp[idx].type = ARGT_TAB;
645 argp[idx+1].type = ARGT_STOP;
646 break;
647
648 default:
649 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
650 break;
651 }
652 }
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100653 return 0;
654 }
655
656 /* Check for exceed the number of requiered argument. */
657 if ((mask & ARGT_MASK) == ARGT_STOP &&
658 argp[idx].type != ARGT_STOP) {
659 WILL_LJMP(luaL_argerror(L, first + idx, "Last argument expected"));
660 }
661
662 if ((mask & ARGT_MASK) == ARGT_STOP &&
663 argp[idx].type == ARGT_STOP) {
664 return 0;
665 }
666
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100667 /* Convert some argument types. */
668 switch (mask & ARGT_MASK) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100669 case ARGT_SINT:
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100670 if (argp[idx].type != ARGT_SINT)
671 WILL_LJMP(luaL_argerror(L, first + idx, "integer expected"));
672 argp[idx].type = ARGT_SINT;
673 break;
674
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100675 case ARGT_TIME:
676 if (argp[idx].type != ARGT_SINT)
677 WILL_LJMP(luaL_argerror(L, first + idx, "integer expected"));
Thierry FOURNIER29176f32015-07-07 00:41:29 +0200678 argp[idx].type = ARGT_TIME;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100679 break;
680
681 case ARGT_SIZE:
682 if (argp[idx].type != ARGT_SINT)
683 WILL_LJMP(luaL_argerror(L, first + idx, "integer expected"));
Thierry FOURNIER29176f32015-07-07 00:41:29 +0200684 argp[idx].type = ARGT_SIZE;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100685 break;
686
687 case ARGT_FE:
688 if (argp[idx].type != ARGT_STR)
689 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200690 memcpy(trash.area, argp[idx].data.str.area,
691 argp[idx].data.str.data);
692 trash.area[argp[idx].data.str.data] = 0;
693 argp[idx].data.prx = proxy_fe_by_name(trash.area);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100694 if (!argp[idx].data.prx)
695 WILL_LJMP(luaL_argerror(L, first + idx, "frontend doesn't exist"));
696 argp[idx].type = ARGT_FE;
697 break;
698
699 case ARGT_BE:
700 if (argp[idx].type != ARGT_STR)
701 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200702 memcpy(trash.area, argp[idx].data.str.area,
703 argp[idx].data.str.data);
704 trash.area[argp[idx].data.str.data] = 0;
705 argp[idx].data.prx = proxy_be_by_name(trash.area);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100706 if (!argp[idx].data.prx)
707 WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist"));
708 argp[idx].type = ARGT_BE;
709 break;
710
711 case ARGT_TAB:
712 if (argp[idx].type != ARGT_STR)
713 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200714 memcpy(trash.area, argp[idx].data.str.area,
715 argp[idx].data.str.data);
716 trash.area[argp[idx].data.str.data] = 0;
717 argp[idx].data.prx = proxy_tbl_by_name(trash.area);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100718 if (!argp[idx].data.prx)
719 WILL_LJMP(luaL_argerror(L, first + idx, "table doesn't exist"));
720 argp[idx].type = ARGT_TAB;
721 break;
722
723 case ARGT_SRV:
724 if (argp[idx].type != ARGT_STR)
725 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200726 memcpy(trash.area, argp[idx].data.str.area,
727 argp[idx].data.str.data);
728 trash.area[argp[idx].data.str.data] = 0;
729 sname = strrchr(trash.area, '/');
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100730 if (sname) {
731 *sname++ = '\0';
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200732 pname = trash.area;
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200733 px = proxy_be_by_name(pname);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100734 if (!px)
735 WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist"));
736 }
737 else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200738 sname = trash.area;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100739 px = p;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100740 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100741 argp[idx].data.srv = findserver(px, sname);
742 if (!argp[idx].data.srv)
743 WILL_LJMP(luaL_argerror(L, first + idx, "server doesn't exist"));
744 argp[idx].type = ARGT_SRV;
745 break;
746
747 case ARGT_IPV4:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200748 memcpy(trash.area, argp[idx].data.str.area,
749 argp[idx].data.str.data);
750 trash.area[argp[idx].data.str.data] = 0;
751 if (inet_pton(AF_INET, trash.area, &argp[idx].data.ipv4))
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100752 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 address"));
753 argp[idx].type = ARGT_IPV4;
754 break;
755
756 case ARGT_MSK4:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200757 memcpy(trash.area, argp[idx].data.str.area,
758 argp[idx].data.str.data);
759 trash.area[argp[idx].data.str.data] = 0;
760 if (!str2mask(trash.area, &argp[idx].data.ipv4))
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100761 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 mask"));
762 argp[idx].type = ARGT_MSK4;
763 break;
764
765 case ARGT_IPV6:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200766 memcpy(trash.area, argp[idx].data.str.area,
767 argp[idx].data.str.data);
768 trash.area[argp[idx].data.str.data] = 0;
769 if (inet_pton(AF_INET6, trash.area, &argp[idx].data.ipv6))
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100770 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 address"));
771 argp[idx].type = ARGT_IPV6;
772 break;
773
774 case ARGT_MSK6:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200775 memcpy(trash.area, argp[idx].data.str.area,
776 argp[idx].data.str.data);
777 trash.area[argp[idx].data.str.data] = 0;
778 if (!str2mask6(trash.area, &argp[idx].data.ipv6))
Tim Duesterhusb814da62018-01-25 16:24:50 +0100779 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 mask"));
780 argp[idx].type = ARGT_MSK6;
781 break;
782
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100783 case ARGT_MAP:
784 case ARGT_REG:
785 case ARGT_USR:
786 WILL_LJMP(luaL_argerror(L, first + idx, "type not yet supported"));
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100787 break;
788 }
789
790 /* Check for type of argument. */
791 if ((mask & ARGT_MASK) != argp[idx].type) {
792 const char *msg = lua_pushfstring(L, "'%s' expected, got '%s'",
793 arg_type_names[(mask & ARGT_MASK)],
794 arg_type_names[argp[idx].type & ARGT_MASK]);
795 WILL_LJMP(luaL_argerror(L, first + idx, msg));
796 }
797
798 /* Next argument. */
799 mask >>= ARGT_BITS;
800 idx++;
801 }
802}
803
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100804/*
805 * The following functions are used to make correspondance between the the
806 * executed lua pointer and the "struct hlua *" that contain the context.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100807 *
808 * - hlua_gethlua : return the hlua context associated with an lua_State.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100809 * - hlua_sethlua : create the association between hlua context and lua_state.
810 */
811static inline struct hlua *hlua_gethlua(lua_State *L)
812{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +0100813 struct hlua **hlua = lua_getextraspace(L);
814 return *hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100815}
816static inline void hlua_sethlua(struct hlua *hlua)
817{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +0100818 struct hlua **hlua_store = lua_getextraspace(hlua->T);
819 *hlua_store = hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100820}
821
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100822/* This function is used to send logs. It try to send on screen (stderr)
823 * and on the default syslog server.
824 */
825static inline void hlua_sendlog(struct proxy *px, int level, const char *msg)
826{
827 struct tm tm;
828 char *p;
829
830 /* Cleanup the log message. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200831 p = trash.area;
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100832 for (; *msg != '\0'; msg++, p++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200833 if (p >= trash.area + trash.size - 1) {
Thierry FOURNIERccf00632015-09-16 12:47:03 +0200834 /* Break the message if exceed the buffer size. */
835 *(p-4) = ' ';
836 *(p-3) = '.';
837 *(p-2) = '.';
838 *(p-1) = '.';
839 break;
840 }
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100841 if (isprint(*msg))
842 *p = *msg;
843 else
844 *p = '.';
845 }
846 *p = '\0';
847
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200848 send_log(px, level, "%s\n", trash.area);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100849 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
Willy Tarreaua678b432015-08-28 10:14:59 +0200850 get_localtime(date.tv_sec, &tm);
851 fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n",
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100852 log_levels[level], tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec,
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200853 (int)getpid(), trash.area);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100854 fflush(stderr);
855 }
856}
857
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100858/* This function just ensure that the yield will be always
859 * returned with a timeout and permit to set some flags
860 */
861__LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx,
Thierry FOURNIERf90838b2015-03-06 13:48:32 +0100862 lua_KFunction k, int timeout, unsigned int flags)
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100863{
864 struct hlua *hlua = hlua_gethlua(L);
865
866 /* Set the wake timeout. If timeout is required, we set
867 * the expiration time.
868 */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +0200869 hlua->wake_time = timeout;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100870
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +0100871 hlua->flags |= flags;
872
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100873 /* Process the yield. */
Willy Tarreau9635e032018-10-16 17:52:55 +0200874 MAY_LJMP(lua_yieldk(L, nresults, ctx, k));
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100875}
876
Willy Tarreau87b09662015-04-03 00:22:06 +0200877/* This function initialises the Lua environment stored in the stream.
878 * It must be called at the start of the stream. This function creates
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100879 * an LUA coroutine. It can not be use to crete the main LUA context.
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200880 *
881 * This function is particular. it initialises a new Lua thread. If the
882 * initialisation fails (example: out of memory error), the lua function
883 * throws an error (longjmp).
884 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800885 * This function manipulates two Lua stacks: the main and the thread. Only
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200886 * the main stack can fail. The thread is not manipulated. This function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800887 * MUST NOT manipulate the created thread stack state, because it is not
888 * proctected against errors thrown by the thread stack.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100889 */
890int hlua_ctx_init(struct hlua *lua, struct task *task)
891{
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200892 if (!SET_SAFE_LJMP(gL.T)) {
893 lua->Tref = LUA_REFNIL;
894 return 0;
895 }
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100896 lua->Mref = LUA_REFNIL;
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +0100897 lua->flags = 0;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100898 LIST_INIT(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100899 lua->T = lua_newthread(gL.T);
900 if (!lua->T) {
901 lua->Tref = LUA_REFNIL;
Thierry FOURNIER0a976202017-07-12 11:18:00 +0200902 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100903 return 0;
904 }
905 hlua_sethlua(lua);
906 lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
907 lua->task = task;
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200908 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100909 return 1;
910}
911
Willy Tarreau87b09662015-04-03 00:22:06 +0200912/* Used to destroy the Lua coroutine when the attached stream or task
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100913 * is destroyed. The destroy also the memory context. The struct "lua"
914 * is not freed.
915 */
916void hlua_ctx_destroy(struct hlua *lua)
917{
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100918 if (!lua)
Thierry FOURNIERa718b292015-03-04 16:48:34 +0100919 return;
920
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100921 if (!lua->T)
922 goto end;
923
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100924 /* Purge all the pending signals. */
Thierry FOURNIERd6975962017-07-12 14:31:10 +0200925 notification_purge(&lua->com);
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100926
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200927 if (!SET_SAFE_LJMP(lua->T))
928 return;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100929 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200930 RESET_SAFE_LJMP(lua->T);
Thierry FOURNIER5a50a852015-09-23 16:59:28 +0200931
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200932 if (!SET_SAFE_LJMP(gL.T))
933 return;
934 luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref);
935 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER5a50a852015-09-23 16:59:28 +0200936 /* Forces a garbage collecting process. If the Lua program is finished
937 * without error, we run the GC on the thread pointer. Its freed all
938 * the unused memory.
939 * If the thread is finnish with an error or is currently yielded,
940 * it seems that the GC applied on the thread doesn't clean anything,
941 * so e run the GC on the main thread.
942 * NOTE: maybe this action locks all the Lua threads untiml the en of
943 * the garbage collection.
944 */
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +0200945 if (lua->flags & HLUA_MUST_GC) {
Thierry FOURNIER7bd10d52017-07-17 00:44:40 +0200946 if (!SET_SAFE_LJMP(gL.T))
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200947 return;
Thierry FOURNIER7bd10d52017-07-17 00:44:40 +0200948 lua_gc(gL.T, LUA_GCCOLLECT, 0);
949 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +0200950 }
Thierry FOURNIER5a50a852015-09-23 16:59:28 +0200951
Thierry FOURNIERa7b536b2015-09-21 22:50:24 +0200952 lua->T = NULL;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100953
954end:
Willy Tarreaubafbe012017-11-24 17:34:44 +0100955 pool_free(pool_head_hlua, lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100956}
957
958/* This function is used to restore the Lua context when a coroutine
959 * fails. This function copy the common memory between old coroutine
960 * and the new coroutine. The old coroutine is destroyed, and its
961 * replaced by the new coroutine.
962 * If the flag "keep_msg" is set, the last entry of the old is assumed
963 * as string error message and it is copied in the new stack.
964 */
965static int hlua_ctx_renew(struct hlua *lua, int keep_msg)
966{
967 lua_State *T;
968 int new_ref;
969
970 /* Renew the main LUA stack doesn't have sense. */
971 if (lua == &gL)
972 return 0;
973
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100974 /* New Lua coroutine. */
975 T = lua_newthread(gL.T);
976 if (!T)
977 return 0;
978
979 /* Copy last error message. */
980 if (keep_msg)
981 lua_xmove(lua->T, T, 1);
982
983 /* Copy data between the coroutines. */
984 lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref);
985 lua_xmove(lua->T, T, 1);
986 new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Valur poped. */
987
988 /* Destroy old data. */
989 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
990
991 /* The thread is garbage collected by Lua. */
992 luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref);
993
994 /* Fill the struct with the new coroutine values. */
995 lua->Mref = new_ref;
996 lua->T = T;
997 lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
998
999 /* Set context. */
1000 hlua_sethlua(lua);
1001
1002 return 1;
1003}
1004
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001005void hlua_hook(lua_State *L, lua_Debug *ar)
1006{
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001007 struct hlua *hlua = hlua_gethlua(L);
1008
1009 /* Lua cannot yield when its returning from a function,
1010 * so, we can fix the interrupt hook to 1 instruction,
1011 * expecting that the function is finnished.
1012 */
1013 if (lua_gethookmask(L) & LUA_MASKRET) {
1014 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1);
1015 return;
1016 }
1017
1018 /* restore the interrupt condition. */
1019 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
1020
1021 /* If we interrupt the Lua processing in yieldable state, we yield.
1022 * If the state is not yieldable, trying yield causes an error.
1023 */
1024 if (lua_isyieldable(L))
Willy Tarreau9635e032018-10-16 17:52:55 +02001025 MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD));
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001026
Thierry FOURNIERa85cfb12015-03-13 14:50:06 +01001027 /* If we cannot yield, update the clock and check the timeout. */
1028 tv_update_date(0, 1);
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001029 hlua->run_time += now_ms - hlua->start_time;
1030 if (hlua->max_time && hlua->run_time >= hlua->max_time) {
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001031 lua_pushfstring(L, "execution timeout");
1032 WILL_LJMP(lua_error(L));
1033 }
1034
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001035 /* Update the start time. */
1036 hlua->start_time = now_ms;
1037
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001038 /* Try to interrupt the process at the end of the current
1039 * unyieldable function.
1040 */
1041 lua_sethook(hlua->T, hlua_hook, LUA_MASKRET|LUA_MASKCOUNT, hlua_nb_instruction);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001042}
1043
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001044/* This function start or resumes the Lua stack execution. If the flag
1045 * "yield_allowed" if no set and the LUA stack execution returns a yield
1046 * The function return an error.
1047 *
1048 * The function can returns 4 values:
1049 * - HLUA_E_OK : The execution is terminated without any errors.
1050 * - HLUA_E_AGAIN : The execution must continue at the next associated
1051 * task wakeup.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001052 * - HLUA_E_ERRMSG : An error has occurred, an error message is set in
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001053 * the top of the stack.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001054 * - HLUA_E_ERR : An error has occurred without error message.
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001055 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001056 * If an error occurred, the stack is renewed and it is ready to run new
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001057 * LUA code.
1058 */
1059static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed)
1060{
1061 int ret;
1062 const char *msg;
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001063 const char *trace;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001064
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001065 /* Initialise run time counter. */
1066 if (!HLUA_IS_RUNNING(lua))
1067 lua->run_time = 0;
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001068
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001069 /* Lock the whole Lua execution. This lock must be before the
1070 * label "resume_execution".
1071 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001072 HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock);
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001073
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001074resume_execution:
1075
1076 /* This hook interrupts the Lua processing each 'hlua_nb_instruction'
1077 * instructions. it is used for preventing infinite loops.
1078 */
1079 lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
1080
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +01001081 /* Remove all flags except the running flags. */
Thierry FOURNIER2f3867f2015-09-28 01:02:01 +02001082 HLUA_SET_RUN(lua);
1083 HLUA_CLR_CTRLYIELD(lua);
1084 HLUA_CLR_WAKERESWR(lua);
1085 HLUA_CLR_WAKEREQWR(lua);
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +01001086
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001087 /* Update the start time. */
1088 lua->start_time = now_ms;
1089
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001090 /* Call the function. */
1091 ret = lua_resume(lua->T, gL.T, lua->nargs);
1092 switch (ret) {
1093
1094 case LUA_OK:
1095 ret = HLUA_E_OK;
1096 break;
1097
1098 case LUA_YIELD:
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001099 /* Check if the execution timeout is expired. It it is the case, we
1100 * break the Lua execution.
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001101 */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001102 tv_update_date(0, 1);
1103 lua->run_time += now_ms - lua->start_time;
1104 if (lua->max_time && lua->run_time > lua->max_time) {
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001105 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001106 ret = HLUA_E_ETMOUT;
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001107 break;
1108 }
1109 /* Process the forced yield. if the general yield is not allowed or
1110 * if no task were associated this the current Lua execution
1111 * coroutine, we resume the execution. Else we want to return in the
1112 * scheduler and we want to be waked up again, to continue the
1113 * current Lua execution. So we schedule our own task.
1114 */
1115 if (HLUA_IS_CTRLYIELDING(lua)) {
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001116 if (!yield_allowed || !lua->task)
1117 goto resume_execution;
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001118 task_wakeup(lua->task, TASK_WOKEN_MSG);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001119 }
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001120 if (!yield_allowed) {
1121 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001122 ret = HLUA_E_YIELD;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001123 break;
1124 }
1125 ret = HLUA_E_AGAIN;
1126 break;
1127
1128 case LUA_ERRRUN:
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001129
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001130 /* Special exit case. The traditional exit is returned as an error
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001131 * because the errors ares the only one mean to return immediately
1132 * from and lua execution.
1133 */
1134 if (lua->flags & HLUA_EXIT) {
1135 ret = HLUA_E_OK;
Thierry FOURNIERe1587b32015-08-28 09:54:13 +02001136 hlua_ctx_renew(lua, 0);
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001137 break;
1138 }
1139
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001140 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001141 if (!lua_checkstack(lua->T, 1)) {
1142 ret = HLUA_E_ERR;
1143 break;
1144 }
1145 msg = lua_tostring(lua->T, -1);
1146 lua_settop(lua->T, 0); /* Empty the stack. */
1147 lua_pop(lua->T, 1);
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001148 trace = hlua_traceback(lua->T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001149 if (msg)
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001150 lua_pushfstring(lua->T, "runtime error: %s from %s", msg, trace);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001151 else
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001152 lua_pushfstring(lua->T, "unknown runtime error from %s", trace);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001153 ret = HLUA_E_ERRMSG;
1154 break;
1155
1156 case LUA_ERRMEM:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001157 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001158 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001159 ret = HLUA_E_NOMEM;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001160 break;
1161
1162 case LUA_ERRERR:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001163 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001164 if (!lua_checkstack(lua->T, 1)) {
1165 ret = HLUA_E_ERR;
1166 break;
1167 }
1168 msg = lua_tostring(lua->T, -1);
1169 lua_settop(lua->T, 0); /* Empty the stack. */
1170 lua_pop(lua->T, 1);
1171 if (msg)
1172 lua_pushfstring(lua->T, "message handler error: %s", msg);
1173 else
1174 lua_pushfstring(lua->T, "message handler error");
1175 ret = HLUA_E_ERRMSG;
1176 break;
1177
1178 default:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001179 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001180 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001181 ret = HLUA_E_ERR;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001182 break;
1183 }
1184
Thierry FOURNIER6ab4d8e2015-09-27 22:17:19 +02001185 /* This GC permits to destroy some object when a Lua timeout strikes. */
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +02001186 if (lua->flags & HLUA_MUST_GC &&
1187 ret != HLUA_E_AGAIN)
Thierry FOURNIER6ab4d8e2015-09-27 22:17:19 +02001188 lua_gc(lua->T, LUA_GCCOLLECT, 0);
1189
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001190 switch (ret) {
1191 case HLUA_E_AGAIN:
1192 break;
1193
1194 case HLUA_E_ERRMSG:
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001195 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001196 hlua_ctx_renew(lua, 1);
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001197 HLUA_CLR_RUN(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001198 break;
1199
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001200 case HLUA_E_ETMOUT:
1201 case HLUA_E_NOMEM:
1202 case HLUA_E_YIELD:
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001203 case HLUA_E_ERR:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001204 HLUA_CLR_RUN(lua);
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001205 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001206 hlua_ctx_renew(lua, 0);
1207 break;
1208
1209 case HLUA_E_OK:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001210 HLUA_CLR_RUN(lua);
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001211 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001212 break;
1213 }
1214
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001215 /* This is the main exit point, remove the Lua lock. */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001216 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock);
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001217
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001218 return ret;
1219}
1220
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001221/* This function exit the current code. */
1222__LJMP static int hlua_done(lua_State *L)
1223{
1224 struct hlua *hlua = hlua_gethlua(L);
1225
1226 hlua->flags |= HLUA_EXIT;
1227 WILL_LJMP(lua_error(L));
1228
1229 return 0;
1230}
1231
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001232/* This function is an LUA binding. It provides a function
1233 * for deleting ACL from a referenced ACL file.
1234 */
1235__LJMP static int hlua_del_acl(lua_State *L)
1236{
1237 const char *name;
1238 const char *key;
1239 struct pat_ref *ref;
1240
1241 MAY_LJMP(check_args(L, 2, "del_acl"));
1242
1243 name = MAY_LJMP(luaL_checkstring(L, 1));
1244 key = MAY_LJMP(luaL_checkstring(L, 2));
1245
1246 ref = pat_ref_lookup(name);
1247 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001248 WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001249
1250 pat_ref_delete(ref, key);
1251 return 0;
1252}
1253
1254/* This function is an LUA binding. It provides a function
1255 * for deleting map entry from a referenced map file.
1256 */
1257static int hlua_del_map(lua_State *L)
1258{
1259 const char *name;
1260 const char *key;
1261 struct pat_ref *ref;
1262
1263 MAY_LJMP(check_args(L, 2, "del_map"));
1264
1265 name = MAY_LJMP(luaL_checkstring(L, 1));
1266 key = MAY_LJMP(luaL_checkstring(L, 2));
1267
1268 ref = pat_ref_lookup(name);
1269 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001270 WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001271
1272 pat_ref_delete(ref, key);
1273 return 0;
1274}
1275
1276/* This function is an LUA binding. It provides a function
1277 * for adding ACL pattern from a referenced ACL file.
1278 */
1279static int hlua_add_acl(lua_State *L)
1280{
1281 const char *name;
1282 const char *key;
1283 struct pat_ref *ref;
1284
1285 MAY_LJMP(check_args(L, 2, "add_acl"));
1286
1287 name = MAY_LJMP(luaL_checkstring(L, 1));
1288 key = MAY_LJMP(luaL_checkstring(L, 2));
1289
1290 ref = pat_ref_lookup(name);
1291 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001292 WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001293
1294 if (pat_ref_find_elt(ref, key) == NULL)
1295 pat_ref_add(ref, key, NULL, NULL);
1296 return 0;
1297}
1298
1299/* This function is an LUA binding. It provides a function
1300 * for setting map pattern and sample from a referenced map
1301 * file.
1302 */
1303static int hlua_set_map(lua_State *L)
1304{
1305 const char *name;
1306 const char *key;
1307 const char *value;
1308 struct pat_ref *ref;
1309
1310 MAY_LJMP(check_args(L, 3, "set_map"));
1311
1312 name = MAY_LJMP(luaL_checkstring(L, 1));
1313 key = MAY_LJMP(luaL_checkstring(L, 2));
1314 value = MAY_LJMP(luaL_checkstring(L, 3));
1315
1316 ref = pat_ref_lookup(name);
1317 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001318 WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001319
1320 if (pat_ref_find_elt(ref, key) != NULL)
1321 pat_ref_set(ref, key, value, NULL);
1322 else
1323 pat_ref_add(ref, key, value, NULL);
1324 return 0;
1325}
1326
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001327/* A class is a lot of memory that contain data. This data can be a table,
1328 * an integer or user data. This data is associated with a metatable. This
1329 * metatable have an original version registred in the global context with
1330 * the name of the object (_G[<name>] = <metable> ).
1331 *
1332 * A metable is a table that modify the standard behavior of a standard
1333 * access to the associated data. The entries of this new metatable are
1334 * defined as is:
1335 *
1336 * http://lua-users.org/wiki/MetatableEvents
1337 *
1338 * __index
1339 *
1340 * we access an absent field in a table, the result is nil. This is
1341 * true, but it is not the whole truth. Actually, such access triggers
1342 * the interpreter to look for an __index metamethod: If there is no
1343 * such method, as usually happens, then the access results in nil;
1344 * otherwise, the metamethod will provide the result.
1345 *
1346 * Control 'prototype' inheritance. When accessing "myTable[key]" and
1347 * the key does not appear in the table, but the metatable has an __index
1348 * property:
1349 *
1350 * - if the value is a function, the function is called, passing in the
1351 * table and the key; the return value of that function is returned as
1352 * the result.
1353 *
1354 * - if the value is another table, the value of the key in that table is
1355 * asked for and returned (and if it doesn't exist in that table, but that
1356 * table's metatable has an __index property, then it continues on up)
1357 *
1358 * - Use "rawget(myTable,key)" to skip this metamethod.
1359 *
1360 * http://www.lua.org/pil/13.4.1.html
1361 *
1362 * __newindex
1363 *
1364 * Like __index, but control property assignment.
1365 *
1366 * __mode - Control weak references. A string value with one or both
1367 * of the characters 'k' and 'v' which specifies that the the
1368 * keys and/or values in the table are weak references.
1369 *
1370 * __call - Treat a table like a function. When a table is followed by
1371 * parenthesis such as "myTable( 'foo' )" and the metatable has
1372 * a __call key pointing to a function, that function is invoked
1373 * (passing any specified arguments) and the return value is
1374 * returned.
1375 *
1376 * __metatable - Hide the metatable. When "getmetatable( myTable )" is
1377 * called, if the metatable for myTable has a __metatable
1378 * key, the value of that key is returned instead of the
1379 * actual metatable.
1380 *
1381 * __tostring - Control string representation. When the builtin
1382 * "tostring( myTable )" function is called, if the metatable
1383 * for myTable has a __tostring property set to a function,
1384 * that function is invoked (passing myTable to it) and the
1385 * return value is used as the string representation.
1386 *
1387 * __len - Control table length. When the table length is requested using
1388 * the length operator ( '#' ), if the metatable for myTable has
1389 * a __len key pointing to a function, that function is invoked
1390 * (passing myTable to it) and the return value used as the value
1391 * of "#myTable".
1392 *
1393 * __gc - Userdata finalizer code. When userdata is set to be garbage
1394 * collected, if the metatable has a __gc field pointing to a
1395 * function, that function is first invoked, passing the userdata
1396 * to it. The __gc metamethod is not called for tables.
1397 * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html)
1398 *
1399 * Special metamethods for redefining standard operators:
1400 * http://www.lua.org/pil/13.1.html
1401 *
1402 * __add "+"
1403 * __sub "-"
1404 * __mul "*"
1405 * __div "/"
1406 * __unm "!"
1407 * __pow "^"
1408 * __concat ".."
1409 *
1410 * Special methods for redfining standar relations
1411 * http://www.lua.org/pil/13.2.html
1412 *
1413 * __eq "=="
1414 * __lt "<"
1415 * __le "<="
1416 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001417
1418/*
1419 *
1420 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001421 * Class Map
1422 *
1423 *
1424 */
1425
1426/* Returns a struct hlua_map if the stack entry "ud" is
1427 * a class session, otherwise it throws an error.
1428 */
1429__LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud)
1430{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001431 return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001432}
1433
1434/* This function is the map constructor. It don't need
1435 * the class Map object. It creates and return a new Map
1436 * object. It must be called only during "body" or "init"
1437 * context because it process some filesystem accesses.
1438 */
1439__LJMP static int hlua_map_new(struct lua_State *L)
1440{
1441 const char *fn;
1442 int match = PAT_MATCH_STR;
1443 struct sample_conv conv;
1444 const char *file = "";
1445 int line = 0;
1446 lua_Debug ar;
1447 char *err = NULL;
1448 struct arg args[2];
1449
1450 if (lua_gettop(L) < 1 || lua_gettop(L) > 2)
1451 WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument."));
1452
1453 fn = MAY_LJMP(luaL_checkstring(L, 1));
1454
1455 if (lua_gettop(L) >= 2) {
1456 match = MAY_LJMP(luaL_checkinteger(L, 2));
1457 if (match < 0 || match >= PAT_MATCH_NUM)
1458 WILL_LJMP(luaL_error(L, "'new' needs a valid match method."));
1459 }
1460
1461 /* Get Lua filename and line number. */
1462 if (lua_getstack(L, 1, &ar)) { /* check function at level */
1463 lua_getinfo(L, "Sl", &ar); /* get info about it */
1464 if (ar.currentline > 0) { /* is there info? */
1465 file = ar.short_src;
1466 line = ar.currentline;
1467 }
1468 }
1469
1470 /* fill fake sample_conv struct. */
1471 conv.kw = ""; /* unused. */
1472 conv.process = NULL; /* unused. */
1473 conv.arg_mask = 0; /* unused. */
1474 conv.val_args = NULL; /* unused. */
1475 conv.out_type = SMP_T_STR;
1476 conv.private = (void *)(long)match;
1477 switch (match) {
1478 case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break;
1479 case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break;
1480 case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break;
1481 case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break;
1482 case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break;
1483 case PAT_MATCH_END: conv.in_type = SMP_T_STR; break;
1484 case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001485 case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001486 case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break;
1487 default:
1488 WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode."));
1489 }
1490
1491 /* fill fake args. */
1492 args[0].type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001493 args[0].data.str.area = (char *)fn;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001494 args[1].type = ARGT_STOP;
1495
1496 /* load the map. */
1497 if (!sample_load_map(args, &conv, file, line, &err)) {
1498 /* error case: we cant use luaL_error because we must
1499 * free the err variable.
1500 */
1501 luaL_where(L, 1);
1502 lua_pushfstring(L, "'new': %s.", err);
1503 lua_concat(L, 2);
1504 free(err);
1505 WILL_LJMP(lua_error(L));
1506 }
1507
1508 /* create the lua object. */
1509 lua_newtable(L);
1510 lua_pushlightuserdata(L, args[0].data.map);
1511 lua_rawseti(L, -2, 0);
1512
1513 /* Pop a class Map metatable and affect it to the userdata. */
1514 lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref);
1515 lua_setmetatable(L, -2);
1516
1517
1518 return 1;
1519}
1520
1521__LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str)
1522{
1523 struct map_descriptor *desc;
1524 struct pattern *pat;
1525 struct sample smp;
1526
1527 MAY_LJMP(check_args(L, 2, "lookup"));
1528 desc = MAY_LJMP(hlua_checkmap(L, 1));
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001529 if (desc->pat.expect_type == SMP_T_SINT) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001530 smp.data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001531 smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001532 }
1533 else {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001534 smp.data.type = SMP_T_STR;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001535 smp.flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001536 smp.data.u.str.area = (char *)MAY_LJMP(luaL_checklstring(L, 2, (size_t *)&smp.data.u.str.data));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001537 }
1538
1539 pat = pattern_exec_match(&desc->pat, &smp, 1);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001540 if (!pat || !pat->data) {
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001541 if (str)
1542 lua_pushstring(L, "");
1543 else
1544 lua_pushnil(L);
1545 return 1;
1546 }
1547
1548 /* The Lua pattern must return a string, so we can't check the returned type */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001549 lua_pushlstring(L, pat->data->u.str.area, pat->data->u.str.data);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001550 return 1;
1551}
1552
1553__LJMP static int hlua_map_lookup(struct lua_State *L)
1554{
1555 return _hlua_map_lookup(L, 0);
1556}
1557
1558__LJMP static int hlua_map_slookup(struct lua_State *L)
1559{
1560 return _hlua_map_lookup(L, 1);
1561}
1562
1563/*
1564 *
1565 *
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001566 * Class Socket
1567 *
1568 *
1569 */
1570
1571__LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud)
1572{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001573 return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001574}
1575
1576/* This function is the handler called for each I/O on the established
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001577 * connection. It is used for notify space available to send or data
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001578 * received.
1579 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001580static void hlua_socket_handler(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001581{
Willy Tarreau00a37f02015-04-13 12:05:19 +02001582 struct stream_interface *si = appctx->owner;
Olivier Houchard9aaf7782017-09-13 18:30:23 +02001583 struct connection *c = cs_conn(objt_cs(si_opposite(si)->end));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001584
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001585 if (appctx->ctx.hlua_cosocket.die) {
1586 si_shutw(si);
1587 si_shutr(si);
1588 si_ic(si)->flags |= CF_READ_NULL;
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001589 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1590 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001591 stream_shutdown(si_strm(si), SF_ERR_KILLED);
1592 }
1593
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001594 /* If the connection object is not available, close all the
1595 * streams and wakeup everything waiting for.
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001596 */
1597 if (!c) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001598 si_shutw(si);
1599 si_shutr(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01001600 si_ic(si)->flags |= CF_READ_NULL;
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001601 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1602 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Willy Tarreaud4da1962015-04-20 01:31:23 +02001603 return;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001604 }
1605
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001606 /* If we cant write, wakeup the pending write signals. */
1607 if (channel_output_closed(si_ic(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001608 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001609
1610 /* If we cant read, wakeup the pending read signals. */
1611 if (channel_input_closed(si_oc(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001612 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001613
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001614 /* if the connection is not estabkished, inform the stream that we want
1615 * to be notified whenever the connection completes.
1616 */
1617 if (!(c->flags & CO_FL_CONNECTED)) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01001618 si_cant_get(si);
Willy Tarreau12c24232018-12-06 15:29:50 +01001619 si_rx_conn_blk(si);
Willy Tarreau3367d412018-11-15 10:57:41 +01001620 si_rx_endp_more(si);
Willy Tarreaud4da1962015-04-20 01:31:23 +02001621 return;
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001622 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001623
1624 /* This function is called after the connect. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01001625 appctx->ctx.hlua_cosocket.connected = 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001626
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001627 /* Wake the tasks which wants to write if the buffer have available space. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001628 if (channel_may_recv(si_ic(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001629 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001630
1631 /* Wake the tasks which wants to read if the buffer contains data. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001632 if (!channel_is_empty(si_oc(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001633 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001634
1635 /* Some data were injected in the buffer, notify the stream
1636 * interface.
1637 */
1638 if (!channel_is_empty(si_ic(si)))
1639 stream_int_update(si);
1640
1641 /* If write notifications are registered, we considers we want
Willy Tarreau3367d412018-11-15 10:57:41 +01001642 * to write, so we clear the blocking flag.
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001643 */
1644 if (notification_registered(&appctx->ctx.hlua_cosocket.wake_on_write))
Willy Tarreau3367d412018-11-15 10:57:41 +01001645 si_rx_endp_more(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001646}
1647
Willy Tarreau87b09662015-04-03 00:22:06 +02001648/* This function is called when the "struct stream" is destroyed.
1649 * Remove the link from the object to this stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001650 * Wake all the pending signals.
1651 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001652static void hlua_socket_release(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001653{
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001654 struct xref *peer;
1655
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001656 /* Remove my link in the original object. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001657 peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref);
1658 if (peer)
1659 xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001660
1661 /* Wake all the task waiting for me. */
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001662 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1663 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001664}
1665
1666/* If the garbage collectio of the object is launch, nobody
Willy Tarreau87b09662015-04-03 00:22:06 +02001667 * uses this object. If the stream does not exists, just quit.
1668 * Send the shutdown signal to the stream. In some cases,
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001669 * pending signal can rest in the read and write lists. destroy
1670 * it.
1671 */
1672__LJMP static int hlua_socket_gc(lua_State *L)
1673{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001674 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001675 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001676 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001677
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001678 MAY_LJMP(check_args(L, 1, "__gc"));
1679
1680 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001681 peer = xref_get_peer_and_lock(&socket->xref);
1682 if (!peer)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001683 return 0;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001684 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001685
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001686 /* Set the flag which destroy the session. */
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001687 appctx->ctx.hlua_cosocket.die = 1;
1688 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001689
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001690 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001691 xref_disconnect(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001692 return 0;
1693}
1694
1695/* The close function send shutdown signal and break the
Willy Tarreau87b09662015-04-03 00:22:06 +02001696 * links between the stream and the object.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001697 */
sada05ed3302018-05-11 11:48:18 -07001698__LJMP static int hlua_socket_close_helper(lua_State *L)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001699{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001700 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001701 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001702 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001703
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001704 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001705
1706 /* Check if we run on the same thread than the xreator thread.
1707 * We cannot access to the socket if the thread is different.
1708 */
1709 if (socket->tid != tid)
1710 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1711
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001712 peer = xref_get_peer_and_lock(&socket->xref);
1713 if (!peer)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001714 return 0;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001715 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001716
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001717 /* Set the flag which destroy the session. */
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001718 appctx->ctx.hlua_cosocket.die = 1;
1719 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001720
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001721 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001722 xref_disconnect(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001723 return 0;
1724}
1725
sada05ed3302018-05-11 11:48:18 -07001726/* The close function calls close_helper.
1727 */
1728__LJMP static int hlua_socket_close(lua_State *L)
1729{
1730 MAY_LJMP(check_args(L, 1, "close"));
1731 return hlua_socket_close_helper(L);
1732}
1733
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001734/* This Lua function assumes that the stack contain three parameters.
1735 * 1 - USERDATA containing a struct socket
1736 * 2 - INTEGER with values of the macro defined below
1737 * If the integer is -1, we must read at most one line.
1738 * If the integer is -2, we ust read all the data until the
1739 * end of the stream.
1740 * If the integer is positive value, we must read a number of
1741 * bytes corresponding to this value.
1742 */
1743#define HLSR_READ_LINE (-1)
1744#define HLSR_READ_ALL (-2)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001745__LJMP static int hlua_socket_receive_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001746{
1747 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
1748 int wanted = lua_tointeger(L, 2);
1749 struct hlua *hlua = hlua_gethlua(L);
1750 struct appctx *appctx;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001751 size_t len;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001752 int nblk;
Willy Tarreau206ba832018-06-14 15:27:31 +02001753 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001754 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02001755 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001756 size_t len2;
Thierry FOURNIER00543922015-03-09 18:35:06 +01001757 int skip_at_end = 0;
Willy Tarreau81389672015-03-10 12:03:52 +01001758 struct channel *oc;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001759 struct stream_interface *si;
1760 struct stream *s;
1761 struct xref *peer;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001762 int missing_bytes;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001763
1764 /* Check if this lua stack is schedulable. */
1765 if (!hlua || !hlua->task)
1766 WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in "
1767 "'frontend', 'backend' or 'task'"));
1768
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001769 /* Check if we run on the same thread than the xreator thread.
1770 * We cannot access to the socket if the thread is different.
1771 */
1772 if (socket->tid != tid)
1773 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1774
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001775 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001776 peer = xref_get_peer_and_lock(&socket->xref);
1777 if (!peer)
1778 goto no_peer;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001779 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
1780 si = appctx->owner;
1781 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001782
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001783 oc = &s->res;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001784 if (wanted == HLSR_READ_LINE) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001785 /* Read line. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001786 nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001787 if (nblk < 0) /* Connection close. */
1788 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001789 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001790 goto connection_empty;
Thierry FOURNIER00543922015-03-09 18:35:06 +01001791
1792 /* remove final \r\n. */
1793 if (nblk == 1) {
1794 if (blk1[len1-1] == '\n') {
1795 len1--;
1796 skip_at_end++;
1797 if (blk1[len1-1] == '\r') {
1798 len1--;
1799 skip_at_end++;
1800 }
1801 }
1802 }
1803 else {
1804 if (blk2[len2-1] == '\n') {
1805 len2--;
1806 skip_at_end++;
1807 if (blk2[len2-1] == '\r') {
1808 len2--;
1809 skip_at_end++;
1810 }
1811 }
1812 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001813 }
1814
1815 else if (wanted == HLSR_READ_ALL) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001816 /* Read all the available data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001817 nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001818 if (nblk < 0) /* Connection close. */
1819 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001820 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001821 goto connection_empty;
1822 }
1823
1824 else {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001825 /* Read a block of data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001826 nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001827 if (nblk < 0) /* Connection close. */
1828 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001829 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001830 goto connection_empty;
1831
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001832 missing_bytes = wanted - socket->b.n;
1833 if (len1 > missing_bytes) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001834 nblk = 1;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001835 len1 = missing_bytes;
1836 } if (nblk == 2 && len1 + len2 > missing_bytes)
1837 len2 = missing_bytes - len1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001838 }
1839
1840 len = len1;
1841
1842 luaL_addlstring(&socket->b, blk1, len1);
1843 if (nblk == 2) {
1844 len += len2;
1845 luaL_addlstring(&socket->b, blk2, len2);
1846 }
1847
1848 /* Consume data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001849 co_skip(oc, len + skip_at_end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001850
1851 /* Don't wait anything. */
Thierry FOURNIER7e4ee472018-05-25 15:03:50 +02001852 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001853
1854 /* If the pattern reclaim to read all the data
1855 * in the connection, got out.
1856 */
1857 if (wanted == HLSR_READ_ALL)
1858 goto connection_empty;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001859 else if (wanted >= 0 && socket->b.n < wanted)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001860 goto connection_empty;
1861
1862 /* Return result. */
1863 luaL_pushresult(&socket->b);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001864 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001865 return 1;
1866
1867connection_closed:
1868
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001869 xref_unlock(&socket->xref, peer);
1870
1871no_peer:
1872
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001873 /* If the buffer containds data. */
1874 if (socket->b.n > 0) {
1875 luaL_pushresult(&socket->b);
1876 return 1;
1877 }
1878 lua_pushnil(L);
1879 lua_pushstring(L, "connection closed.");
1880 return 2;
1881
1882connection_empty:
1883
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001884 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) {
1885 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001886 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001887 }
1888 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02001889 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_receive_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001890 return 0;
1891}
1892
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001893/* This Lua function gets two parameters. The first one can be string
1894 * or a number. If the string is "*l", the user requires one line. If
1895 * the string is "*a", the user requires all the contents of the stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001896 * If the value is a number, the user require a number of bytes equal
1897 * to the value. The default value is "*l" (a line).
1898 *
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001899 * This parameter with a variable type is converted in integer. This
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001900 * integer takes this values:
1901 * -1 : read a line
1902 * -2 : read all the stream
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001903 * >0 : amount of bytes.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001904 *
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001905 * The second parameter is optional. It contains a string that must be
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001906 * concatenated with the read data.
1907 */
1908__LJMP static int hlua_socket_receive(struct lua_State *L)
1909{
1910 int wanted = HLSR_READ_LINE;
1911 const char *pattern;
1912 int type;
1913 char *error;
1914 size_t len;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001915 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001916
1917 if (lua_gettop(L) < 1 || lua_gettop(L) > 3)
1918 WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments."));
1919
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001920 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001921
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001922 /* Check if we run on the same thread than the xreator thread.
1923 * We cannot access to the socket if the thread is different.
1924 */
1925 if (socket->tid != tid)
1926 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1927
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001928 /* check for pattern. */
1929 if (lua_gettop(L) >= 2) {
1930 type = lua_type(L, 2);
1931 if (type == LUA_TSTRING) {
1932 pattern = lua_tostring(L, 2);
1933 if (strcmp(pattern, "*a") == 0)
1934 wanted = HLSR_READ_ALL;
1935 else if (strcmp(pattern, "*l") == 0)
1936 wanted = HLSR_READ_LINE;
1937 else {
1938 wanted = strtoll(pattern, &error, 10);
1939 if (*error != '\0')
1940 WILL_LJMP(luaL_error(L, "Unsupported pattern."));
1941 }
1942 }
1943 else if (type == LUA_TNUMBER) {
1944 wanted = lua_tointeger(L, 2);
1945 if (wanted < 0)
1946 WILL_LJMP(luaL_error(L, "Unsupported size."));
1947 }
1948 }
1949
1950 /* Set pattern. */
1951 lua_pushinteger(L, wanted);
Tim Duesterhusc6e377e2018-01-04 19:32:13 +01001952
1953 /* Check if we would replace the top by itself. */
1954 if (lua_gettop(L) != 2)
1955 lua_replace(L, 2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001956
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001957 /* init buffer, and fill it with prefix. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001958 luaL_buffinit(L, &socket->b);
1959
1960 /* Check prefix. */
1961 if (lua_gettop(L) >= 3) {
1962 if (lua_type(L, 3) != LUA_TSTRING)
1963 WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix"));
1964 pattern = lua_tolstring(L, 3, &len);
1965 luaL_addlstring(&socket->b, pattern, len);
1966 }
1967
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001968 return __LJMP(hlua_socket_receive_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001969}
1970
1971/* Write the Lua input string in the output buffer.
Mark Lakes22154b42018-01-29 14:38:40 -08001972 * This function returns a yield if no space is available.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001973 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001974static int hlua_socket_write_yield(struct lua_State *L,int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001975{
1976 struct hlua_socket *socket;
1977 struct hlua *hlua = hlua_gethlua(L);
1978 struct appctx *appctx;
1979 size_t buf_len;
1980 const char *buf;
1981 int len;
1982 int send_len;
1983 int sent;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001984 struct xref *peer;
1985 struct stream_interface *si;
1986 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001987
1988 /* Check if this lua stack is schedulable. */
1989 if (!hlua || !hlua->task)
1990 WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in "
1991 "'frontend', 'backend' or 'task'"));
1992
1993 /* Get object */
1994 socket = MAY_LJMP(hlua_checksocket(L, 1));
1995 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001996 sent = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001997
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001998 /* Check if we run on the same thread than the xreator thread.
1999 * We cannot access to the socket if the thread is different.
2000 */
2001 if (socket->tid != tid)
2002 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2003
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002004 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002005 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002006 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002007 lua_pushinteger(L, -1);
2008 return 1;
2009 }
2010 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2011 si = appctx->owner;
2012 s = si_strm(si);
2013
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002014 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002015 if (channel_output_closed(&s->req)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002016 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002017 lua_pushinteger(L, -1);
2018 return 1;
2019 }
2020
2021 /* Update the input buffer data. */
2022 buf += sent;
2023 send_len = buf_len - sent;
2024
2025 /* All the data are sent. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002026 if (sent >= buf_len) {
2027 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002028 return 1; /* Implicitly return the length sent. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002029 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002030
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002031 /* Check if the buffer is available because HAProxy doesn't allocate
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01002032 * the request buffer if its not required.
2033 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002034 if (s->req.buf.size == 0) {
Willy Tarreau581abd32018-10-25 10:21:41 +02002035 if (!si_alloc_ibuf(si, &appctx->buffer_wait))
Christopher Faulet33834b12016-12-19 09:29:06 +01002036 goto hlua_socket_write_yield_return;
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01002037 }
2038
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002039 /* Check for available space. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002040 len = b_room(&s->req.buf);
Christopher Faulet33834b12016-12-19 09:29:06 +01002041 if (len <= 0) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002042 goto hlua_socket_write_yield_return;
Christopher Faulet33834b12016-12-19 09:29:06 +01002043 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002044
2045 /* send data */
2046 if (len < send_len)
2047 send_len = len;
Thierry FOURNIER66b89192018-05-27 01:14:47 +02002048 len = ci_putblk(&s->req, buf, send_len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002049
2050 /* "Not enough space" (-1), "Buffer too little to contain
2051 * the data" (-2) are not expected because the available length
2052 * is tested.
2053 * Other unknown error are also not expected.
2054 */
2055 if (len <= 0) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01002056 if (len == -1)
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002057 s->req.flags |= CF_WAKE_WRITE;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002058
sada05ed3302018-05-11 11:48:18 -07002059 MAY_LJMP(hlua_socket_close_helper(L));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002060 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002061 lua_pushinteger(L, -1);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002062 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002063 return 1;
2064 }
2065
2066 /* update buffers. */
Thierry FOURNIER101b9762018-05-27 01:27:40 +02002067 appctx_wakeup(appctx);
Willy Tarreaude70fa12015-09-26 11:25:05 +02002068
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002069 s->req.rex = TICK_ETERNITY;
2070 s->res.wex = TICK_ETERNITY;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002071
2072 /* Update length sent. */
2073 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002074 lua_pushinteger(L, sent + len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002075
2076 /* All the data buffer is sent ? */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002077 if (sent + len >= buf_len) {
2078 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002079 return 1;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002080 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002081
2082hlua_socket_write_yield_return:
Thierry FOURNIERba42fcd2018-05-27 00:59:48 +02002083 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2084 xref_unlock(&socket->xref, peer);
2085 WILL_LJMP(luaL_error(L, "out of memory"));
2086 }
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002087 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002088 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_write_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002089 return 0;
2090}
2091
2092/* This function initiate the send of data. It just check the input
2093 * parameters and push an integer in the Lua stack that contain the
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002094 * amount of data written to the buffer. This is used by the function
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002095 * "hlua_socket_write_yield" that can yield.
2096 *
2097 * The Lua function gets between 3 and 4 parameters. The first one is
2098 * the associated object. The second is a string buffer. The third is
2099 * a facultative integer that represents where is the buffer position
2100 * of the start of the data that can send. The first byte is the
2101 * position "1". The default value is "1". The fourth argument is a
2102 * facultative integer that represents where is the buffer position
2103 * of the end of the data that can send. The default is the last byte.
2104 */
2105static int hlua_socket_send(struct lua_State *L)
2106{
2107 int i;
2108 int j;
2109 const char *buf;
2110 size_t buf_len;
2111
2112 /* Check number of arguments. */
2113 if (lua_gettop(L) < 2 || lua_gettop(L) > 4)
2114 WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments"));
2115
2116 /* Get the string. */
2117 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
2118
2119 /* Get and check j. */
2120 if (lua_gettop(L) == 4) {
2121 j = MAY_LJMP(luaL_checkinteger(L, 4));
2122 if (j < 0)
2123 j = buf_len + j + 1;
2124 if (j > buf_len)
2125 j = buf_len + 1;
2126 lua_pop(L, 1);
2127 }
2128 else
2129 j = buf_len;
2130
2131 /* Get and check i. */
2132 if (lua_gettop(L) == 3) {
2133 i = MAY_LJMP(luaL_checkinteger(L, 3));
2134 if (i < 0)
2135 i = buf_len + i + 1;
2136 if (i > buf_len)
2137 i = buf_len + 1;
2138 lua_pop(L, 1);
2139 } else
2140 i = 1;
2141
2142 /* Check bth i and j. */
2143 if (i > j) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002144 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002145 return 1;
2146 }
2147 if (i == 0 && j == 0) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002148 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002149 return 1;
2150 }
2151 if (i == 0)
2152 i = 1;
2153 if (j == 0)
2154 j = 1;
2155
2156 /* Pop the string. */
2157 lua_pop(L, 1);
2158
2159 /* Update the buffer length. */
2160 buf += i - 1;
2161 buf_len = j - i + 1;
2162 lua_pushlstring(L, buf, buf_len);
2163
2164 /* This unsigned is used to remember the amount of sent data. */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002165 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002166
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002167 return MAY_LJMP(hlua_socket_write_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002168}
2169
Willy Tarreau22b0a682015-06-17 19:43:49 +02002170#define SOCKET_INFO_MAX_LEN sizeof("[0000:0000:0000:0000:0000:0000:0000:0000]:12345")
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002171__LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr)
2172{
2173 static char buffer[SOCKET_INFO_MAX_LEN];
2174 int ret;
2175 int len;
2176 char *p;
2177
2178 ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1);
2179 if (ret <= 0) {
2180 lua_pushnil(L);
2181 return 1;
2182 }
2183
2184 if (ret == AF_UNIX) {
2185 lua_pushstring(L, buffer+1);
2186 return 1;
2187 }
2188 else if (ret == AF_INET6) {
2189 buffer[0] = '[';
2190 len = strlen(buffer);
2191 buffer[len] = ']';
2192 len++;
2193 buffer[len] = ':';
2194 len++;
2195 p = buffer;
2196 }
2197 else if (ret == AF_INET) {
2198 p = buffer + 1;
2199 len = strlen(p);
2200 p[len] = ':';
2201 len++;
2202 }
2203 else {
2204 lua_pushnil(L);
2205 return 1;
2206 }
2207
2208 if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) {
2209 lua_pushnil(L);
2210 return 1;
2211 }
2212
2213 lua_pushstring(L, p);
2214 return 1;
2215}
2216
2217/* Returns information about the peer of the connection. */
2218__LJMP static int hlua_socket_getpeername(struct lua_State *L)
2219{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002220 struct hlua_socket *socket;
2221 struct connection *conn;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002222 struct xref *peer;
2223 struct appctx *appctx;
2224 struct stream_interface *si;
2225 struct stream *s;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002226 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002227
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002228 MAY_LJMP(check_args(L, 1, "getpeername"));
2229
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002230 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002231
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002232 /* Check if we run on the same thread than the xreator thread.
2233 * We cannot access to the socket if the thread is different.
2234 */
2235 if (socket->tid != tid)
2236 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2237
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002238 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002239 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002240 if (!peer) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002241 lua_pushnil(L);
2242 return 1;
2243 }
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002244 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2245 si = appctx->owner;
2246 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002247
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002248 conn = cs_conn(objt_cs(s->si[1].end));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002249 if (!conn) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002250 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002251 lua_pushnil(L);
2252 return 1;
2253 }
2254
Willy Tarreaua71f6422016-11-16 17:00:14 +01002255 conn_get_to_addr(conn);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002256 if (!(conn->flags & CO_FL_ADDR_TO_SET)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002257 xref_unlock(&socket->xref, peer);
Willy Tarreaua71f6422016-11-16 17:00:14 +01002258 lua_pushnil(L);
2259 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002260 }
2261
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002262 ret = MAY_LJMP(hlua_socket_info(L, &conn->addr.to));
2263 xref_unlock(&socket->xref, peer);
2264 return ret;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002265}
2266
2267/* Returns information about my connection side. */
2268static int hlua_socket_getsockname(struct lua_State *L)
2269{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002270 struct hlua_socket *socket;
2271 struct connection *conn;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002272 struct appctx *appctx;
2273 struct xref *peer;
2274 struct stream_interface *si;
2275 struct stream *s;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002276 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002277
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002278 MAY_LJMP(check_args(L, 1, "getsockname"));
2279
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002280 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002281
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002282 /* Check if we run on the same thread than the xreator thread.
2283 * We cannot access to the socket if the thread is different.
2284 */
2285 if (socket->tid != tid)
2286 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2287
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002288 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002289 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002290 if (!peer) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002291 lua_pushnil(L);
2292 return 1;
2293 }
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002294 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2295 si = appctx->owner;
2296 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002297
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002298 conn = cs_conn(objt_cs(s->si[1].end));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002299 if (!conn) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002300 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002301 lua_pushnil(L);
2302 return 1;
2303 }
2304
Willy Tarreaua71f6422016-11-16 17:00:14 +01002305 conn_get_from_addr(conn);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002306 if (!(conn->flags & CO_FL_ADDR_FROM_SET)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002307 xref_unlock(&socket->xref, peer);
Willy Tarreaua71f6422016-11-16 17:00:14 +01002308 lua_pushnil(L);
2309 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002310 }
2311
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002312 ret = hlua_socket_info(L, &conn->addr.from);
2313 xref_unlock(&socket->xref, peer);
2314 return ret;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002315}
2316
2317/* This struct define the applet. */
Willy Tarreau30576452015-04-13 13:50:30 +02002318static struct applet update_applet = {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002319 .obj_type = OBJ_TYPE_APPLET,
2320 .name = "<LUA_TCP>",
2321 .fct = hlua_socket_handler,
2322 .release = hlua_socket_release,
2323};
2324
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002325__LJMP static int hlua_socket_connect_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002326{
2327 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
2328 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002329 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002330 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002331 struct stream_interface *si;
2332 struct stream *s;
2333
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002334 /* Check if we run on the same thread than the xreator thread.
2335 * We cannot access to the socket if the thread is different.
2336 */
2337 if (socket->tid != tid)
2338 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2339
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002340 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002341 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002342 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002343 lua_pushnil(L);
2344 lua_pushstring(L, "Can't connect");
2345 return 2;
2346 }
2347 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2348 si = appctx->owner;
2349 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002350
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002351 /* Check if we run on the same thread than the xreator thread.
2352 * We cannot access to the socket if the thread is different.
2353 */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002354 if (socket->tid != tid) {
2355 xref_unlock(&socket->xref, peer);
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002356 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002357 }
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002358
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002359 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002360 if (!hlua || channel_output_closed(&s->req)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002361 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002362 lua_pushnil(L);
2363 lua_pushstring(L, "Can't connect");
2364 return 2;
2365 }
2366
Willy Tarreaue09101e2018-10-16 17:37:12 +02002367 appctx = __objt_appctx(s->si[0].end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002368
2369 /* Check for connection established. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002370 if (appctx->ctx.hlua_cosocket.connected) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002371 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002372 lua_pushinteger(L, 1);
2373 return 1;
2374 }
2375
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002376 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2377 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002378 WILL_LJMP(luaL_error(L, "out of memory error"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002379 }
2380 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002381 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002382 return 0;
2383}
2384
2385/* This function fail or initite the connection. */
2386__LJMP static int hlua_socket_connect(struct lua_State *L)
2387{
2388 struct hlua_socket *socket;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002389 int port = -1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002390 const char *ip;
2391 struct connection *conn;
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002392 struct hlua *hlua;
2393 struct appctx *appctx;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002394 int low, high;
2395 struct sockaddr_storage *addr;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002396 struct xref *peer;
2397 struct stream_interface *si;
2398 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002399
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002400 if (lua_gettop(L) < 2)
2401 WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments"));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002402
2403 /* Get args. */
2404 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002405
2406 /* Check if we run on the same thread than the xreator thread.
2407 * We cannot access to the socket if the thread is different.
2408 */
2409 if (socket->tid != tid)
2410 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2411
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002412 ip = MAY_LJMP(luaL_checkstring(L, 2));
Tim Duesterhus6edab862018-01-06 19:04:45 +01002413 if (lua_gettop(L) >= 3) {
2414 luaL_Buffer b;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002415 port = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002416
Tim Duesterhus6edab862018-01-06 19:04:45 +01002417 /* Force the ip to end with a colon, to support IPv6 addresses
2418 * that are not enclosed within square brackets.
2419 */
2420 if (port > 0) {
2421 luaL_buffinit(L, &b);
2422 luaL_addstring(&b, ip);
2423 luaL_addchar(&b, ':');
2424 luaL_pushresult(&b);
2425 ip = lua_tolstring(L, lua_gettop(L), NULL);
2426 }
2427 }
2428
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002429 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002430 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002431 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002432 lua_pushnil(L);
2433 return 1;
2434 }
2435 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2436 si = appctx->owner;
2437 s = si_strm(si);
2438
2439 /* Initialise connection. */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002440 conn = cs_conn(si_alloc_cs(&s->si[1], NULL));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002441 if (!conn) {
2442 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002443 WILL_LJMP(luaL_error(L, "connect: internal error"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002444 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002445
Willy Tarreau3adac082015-09-26 17:51:09 +02002446 /* needed for the connection not to be closed */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002447 conn->target = s->target;
Willy Tarreau3adac082015-09-26 17:51:09 +02002448
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002449 /* Parse ip address. */
Willy Tarreau48ef4c92017-01-06 18:32:38 +01002450 addr = str2sa_range(ip, NULL, &low, &high, NULL, NULL, NULL, 0);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002451 if (!addr) {
2452 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002453 WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002454 }
2455 if (low != high) {
2456 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002457 WILL_LJMP(luaL_error(L, "connect: port ranges not supported : address '%s'", ip));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002458 }
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002459 memcpy(&conn->addr.to, addr, sizeof(struct sockaddr_storage));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002460
2461 /* Set port. */
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002462 if (low == 0) {
2463 if (conn->addr.to.ss_family == AF_INET) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002464 if (port == -1) {
2465 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002466 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002467 }
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002468 ((struct sockaddr_in *)&conn->addr.to)->sin_port = htons(port);
2469 } else if (conn->addr.to.ss_family == AF_INET6) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002470 if (port == -1) {
2471 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002472 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002473 }
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002474 ((struct sockaddr_in6 *)&conn->addr.to)->sin6_port = htons(port);
2475 }
2476 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002477
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002478 hlua = hlua_gethlua(L);
Willy Tarreaue09101e2018-10-16 17:37:12 +02002479 appctx = __objt_appctx(s->si[0].end);
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002480
2481 /* inform the stream that we want to be notified whenever the
2482 * connection completes.
2483 */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01002484 si_cant_get(&s->si[0]);
Willy Tarreau3367d412018-11-15 10:57:41 +01002485 si_rx_endp_more(&s->si[0]);
Thierry FOURNIER8c8fbbe2015-09-26 17:02:35 +02002486 appctx_wakeup(appctx);
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002487
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +02002488 hlua->flags |= HLUA_MUST_GC;
2489
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002490 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2491 xref_unlock(&socket->xref, peer);
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002492 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002493 }
2494 xref_unlock(&socket->xref, peer);
PiBa-NL706d5ee2018-05-05 23:51:42 +02002495
2496 task_wakeup(s->task, TASK_WOKEN_INIT);
2497 /* Return yield waiting for connection. */
2498
Willy Tarreau9635e032018-10-16 17:52:55 +02002499 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002500
2501 return 0;
2502}
2503
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002504#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002505__LJMP static int hlua_socket_connect_ssl(struct lua_State *L)
2506{
2507 struct hlua_socket *socket;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002508 struct xref *peer;
2509 struct appctx *appctx;
2510 struct stream_interface *si;
2511 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002512
2513 MAY_LJMP(check_args(L, 3, "connect_ssl"));
2514 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002515
2516 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002517 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002518 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002519 lua_pushnil(L);
2520 return 1;
2521 }
2522 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2523 si = appctx->owner;
2524 s = si_strm(si);
2525
2526 s->target = &socket_ssl.obj_type;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002527 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002528 return MAY_LJMP(hlua_socket_connect(L));
2529}
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002530#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002531
2532__LJMP static int hlua_socket_setoption(struct lua_State *L)
2533{
2534 return 0;
2535}
2536
2537__LJMP static int hlua_socket_settimeout(struct lua_State *L)
2538{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002539 struct hlua_socket *socket;
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002540 int tmout;
Mark Lakes56cc1252018-03-27 09:48:06 +02002541 double dtmout;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002542 struct xref *peer;
2543 struct appctx *appctx;
2544 struct stream_interface *si;
2545 struct stream *s;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002546
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002547 MAY_LJMP(check_args(L, 2, "settimeout"));
2548
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002549 socket = MAY_LJMP(hlua_checksocket(L, 1));
Mark Lakes56cc1252018-03-27 09:48:06 +02002550
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002551 /* convert the timeout to millis */
2552 dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002553
Thierry Fournier17a921b2018-03-08 09:59:02 +01002554 /* Check for negative values */
Mark Lakes56cc1252018-03-27 09:48:06 +02002555 if (dtmout < 0)
Thierry Fournier17a921b2018-03-08 09:59:02 +01002556 WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values"));
2557
Mark Lakes56cc1252018-03-27 09:48:06 +02002558 if (dtmout > INT_MAX) /* overflow check */
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002559 WILL_LJMP(luaL_error(L, "settimeout: cannot set values larger than %d ms", INT_MAX));
Mark Lakes56cc1252018-03-27 09:48:06 +02002560
2561 tmout = MS_TO_TICKS((int)dtmout);
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002562 if (tmout == 0)
2563 tmout++; /* very small timeouts are adjusted to a minium of 1ms */
Mark Lakes56cc1252018-03-27 09:48:06 +02002564
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002565 /* Check if we run on the same thread than the xreator thread.
2566 * We cannot access to the socket if the thread is different.
2567 */
2568 if (socket->tid != tid)
2569 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2570
Mark Lakes56cc1252018-03-27 09:48:06 +02002571 /* check for connection break. If some data were read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002572 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002573 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002574 hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts.");
2575 WILL_LJMP(lua_error(L));
2576 return 0;
2577 }
2578 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2579 si = appctx->owner;
2580 s = si_strm(si);
2581
Cyril Bonté7bb63452018-08-17 23:51:02 +02002582 s->sess->fe->timeout.connect = tmout;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002583 s->req.rto = tmout;
2584 s->req.wto = tmout;
2585 s->res.rto = tmout;
2586 s->res.wto = tmout;
Cyril Bonté7bb63452018-08-17 23:51:02 +02002587 s->req.rex = tick_add_ifset(now_ms, tmout);
2588 s->req.wex = tick_add_ifset(now_ms, tmout);
2589 s->res.rex = tick_add_ifset(now_ms, tmout);
2590 s->res.wex = tick_add_ifset(now_ms, tmout);
2591
2592 s->task->expire = tick_add_ifset(now_ms, tmout);
2593 task_queue(s->task);
2594
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002595 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002596
Thierry Fourniere9636f12018-03-08 09:54:32 +01002597 lua_pushinteger(L, 1);
Tim Duesterhus119a5f12018-01-06 19:16:25 +01002598 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002599}
2600
2601__LJMP static int hlua_socket_new(lua_State *L)
2602{
2603 struct hlua_socket *socket;
2604 struct appctx *appctx;
Willy Tarreau15b5e142015-04-04 14:38:25 +02002605 struct session *sess;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002606 struct stream *strm;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002607
2608 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002609 if (!lua_checkstack(L, 3)) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002610 hlua_pusherror(L, "socket: full stack");
2611 goto out_fail_conf;
2612 }
2613
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002614 /* Create the object: obj[0] = userdata. */
2615 lua_newtable(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002616 socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket)));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002617 lua_rawseti(L, -2, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002618 memset(socket, 0, sizeof(*socket));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002619 socket->tid = tid;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002620
Thierry FOURNIER4a6170c2015-03-09 17:07:10 +01002621 /* Check if the various memory pools are intialized. */
Willy Tarreaubafbe012017-11-24 17:34:44 +01002622 if (!pool_head_stream || !pool_head_buffer) {
Thierry FOURNIER4a6170c2015-03-09 17:07:10 +01002623 hlua_pusherror(L, "socket: uninitialized pools.");
2624 goto out_fail_conf;
2625 }
2626
Willy Tarreau87b09662015-04-03 00:22:06 +02002627 /* Pop a class stream metatable and affect it to the userdata. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002628 lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref);
2629 lua_setmetatable(L, -2);
2630
Willy Tarreaud420a972015-04-06 00:39:18 +02002631 /* Create the applet context */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01002632 appctx = appctx_new(&update_applet, tid_bit);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002633 if (!appctx) {
2634 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaufeb76402015-04-03 14:10:06 +02002635 goto out_fail_conf;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002636 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002637
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002638 appctx->ctx.hlua_cosocket.connected = 0;
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02002639 appctx->ctx.hlua_cosocket.die = 0;
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002640 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write);
2641 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read);
Willy Tarreaub2bf8332015-04-04 15:58:58 +02002642
Willy Tarreaud420a972015-04-06 00:39:18 +02002643 /* Now create a session, task and stream for this applet */
2644 sess = session_new(&socket_proxy, NULL, &appctx->obj_type);
2645 if (!sess) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002646 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002647 goto out_fail_sess;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002648 }
2649
Willy Tarreau87787ac2017-08-28 16:22:54 +02002650 strm = stream_new(sess, &appctx->obj_type);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002651 if (!strm) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002652 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002653 goto out_fail_stream;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002654 }
2655
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002656 /* Initialise cross reference between stream and Lua socket object. */
2657 xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002658
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002659 /* Configure "right" stream interface. this "si" is used to connect
2660 * and retrieve data from the server. The connection is initialized
2661 * with the "struct server".
2662 */
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002663 si_set_state(&strm->si[1], SI_ST_ASS);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002664
2665 /* Force destination server. */
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002666 strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET | SF_BE_ASSIGNED;
2667 strm->target = &socket_tcp.obj_type;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002668
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002669 return 1;
2670
Willy Tarreaud420a972015-04-06 00:39:18 +02002671 out_fail_stream:
Willy Tarreau11c36242015-04-04 15:54:03 +02002672 session_free(sess);
Willy Tarreaud420a972015-04-06 00:39:18 +02002673 out_fail_sess:
2674 appctx_free(appctx);
2675 out_fail_conf:
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002676 WILL_LJMP(lua_error(L));
2677 return 0;
2678}
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01002679
2680/*
2681 *
2682 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002683 * Class Channel
2684 *
2685 *
2686 */
2687
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002688/* The state between the channel data and the HTTP parser state can be
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002689 * inconsistent, so reset the parser and call it again. Warning, this
2690 * action does not revalidate the request and does not send a 400 if the modified
2691 * request is not valid.
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002692 *
Thierry FOURNIER6e01f382015-11-02 09:52:54 +01002693 * This function never fails. The direction is set using dir, which equals
2694 * either SMP_OPT_DIR_REQ or SMP_OPT_DIR_RES.
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002695 */
2696static void hlua_resynchonize_proto(struct stream *stream, int dir)
2697{
2698 /* Protocol HTTP. */
2699 if (stream->be->mode == PR_MODE_HTTP) {
2700
Thierry FOURNIER6e01f382015-11-02 09:52:54 +01002701 if (dir == SMP_OPT_DIR_REQ)
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002702 http_txn_reset_req(stream->txn);
Thierry FOURNIER6e01f382015-11-02 09:52:54 +01002703 else if (dir == SMP_OPT_DIR_RES)
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002704 http_txn_reset_res(stream->txn);
2705
2706 if (stream->txn->hdr_idx.v)
2707 hdr_idx_init(&stream->txn->hdr_idx);
2708
Thierry FOURNIER6e01f382015-11-02 09:52:54 +01002709 if (dir == SMP_OPT_DIR_REQ)
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002710 http_msg_analyzer(&stream->txn->req, &stream->txn->hdr_idx);
Thierry FOURNIER6e01f382015-11-02 09:52:54 +01002711 else if (dir == SMP_OPT_DIR_RES)
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002712 http_msg_analyzer(&stream->txn->rsp, &stream->txn->hdr_idx);
2713 }
2714}
2715
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002716/* This function is called before the Lua execution. It stores
2717 * the differents parsers state before executing some Lua code.
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002718 */
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002719static inline void consistency_set(struct stream *stream, int opt, struct hlua_consistency *c)
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002720{
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002721 c->mode = stream->be->mode;
2722 switch (c->mode) {
2723 case PR_MODE_HTTP:
2724 c->data.http.dir = opt & SMP_OPT_DIR;
2725 if (c->data.http.dir == SMP_OPT_DIR_REQ)
2726 c->data.http.state = stream->txn->req.msg_state;
2727 else
2728 c->data.http.state = stream->txn->rsp.msg_state;
2729 break;
2730 default:
2731 break;
2732 }
2733}
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002734
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002735/* This function is called after the Lua execution. it
2736 * returns true if the parser state is consistent, otherwise,
2737 * it return false.
2738 *
2739 * In HTTP mode, the parser state must be in the same state
2740 * or greater when we exit the function. Even if we do a
2741 * control yield. This prevent to break the HTTP message
2742 * from the Lua code.
2743 */
2744static inline int consistency_check(struct stream *stream, int opt, struct hlua_consistency *c)
2745{
2746 if (c->mode != stream->be->mode)
2747 return 0;
2748
2749 switch (c->mode) {
2750 case PR_MODE_HTTP:
2751 if (c->data.http.dir != (opt & SMP_OPT_DIR))
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002752 return 0;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002753 if (c->data.http.dir == SMP_OPT_DIR_REQ)
2754 return stream->txn->req.msg_state >= c->data.http.state;
2755 else
2756 return stream->txn->rsp.msg_state >= c->data.http.state;
2757 default:
2758 return 1;
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002759 }
2760 return 1;
2761}
2762
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002763/* Returns the struct hlua_channel join to the class channel in the
2764 * stack entry "ud" or throws an argument error.
2765 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002766__LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002767{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02002768 return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002769}
2770
Willy Tarreau47860ed2015-03-10 14:07:50 +01002771/* Pushes the channel onto the top of the stack. If the stask does not have a
2772 * free slots, the function fails and returns 0;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002773 */
Willy Tarreau2a71af42015-03-10 13:51:50 +01002774static int hlua_channel_new(lua_State *L, struct channel *channel)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002775{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002776 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002777 if (!lua_checkstack(L, 3))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002778 return 0;
2779
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002780 lua_newtable(L);
Willy Tarreau47860ed2015-03-10 14:07:50 +01002781 lua_pushlightuserdata(L, channel);
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002782 lua_rawseti(L, -2, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002783
2784 /* Pop a class sesison metatable and affect it to the userdata. */
2785 lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref);
2786 lua_setmetatable(L, -2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002787 return 1;
2788}
2789
2790/* Duplicate all the data present in the input channel and put it
2791 * in a string LUA variables. Returns -1 and push a nil value in
2792 * the stack if the channel is closed and all the data are consumed,
2793 * returns 0 if no data are available, otherwise it returns the length
2794 * of the builded string.
2795 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002796static inline int _hlua_channel_dup(struct channel *chn, lua_State *L)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002797{
2798 char *blk1;
2799 char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02002800 size_t len1;
2801 size_t len2;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002802 int ret;
2803 luaL_Buffer b;
2804
Willy Tarreau06d80a92017-10-19 14:32:15 +02002805 ret = ci_getblk_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002806 if (unlikely(ret == 0))
2807 return 0;
2808
2809 if (unlikely(ret < 0)) {
2810 lua_pushnil(L);
2811 return -1;
2812 }
2813
2814 luaL_buffinit(L, &b);
2815 luaL_addlstring(&b, blk1, len1);
2816 if (unlikely(ret == 2))
2817 luaL_addlstring(&b, blk2, len2);
2818 luaL_pushresult(&b);
2819
2820 if (unlikely(ret == 2))
2821 return len1 + len2;
2822 return len1;
2823}
2824
2825/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2826 * a yield. This function keep the data in the buffer.
2827 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002828__LJMP static int hlua_channel_dup_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002829{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002830 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002831
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002832 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2833
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002834 if (_hlua_channel_dup(chn, L) == 0)
Willy Tarreau9635e032018-10-16 17:52:55 +02002835 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_dup_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002836 return 1;
2837}
2838
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002839/* Check arguments for the function "hlua_channel_dup_yield". */
2840__LJMP static int hlua_channel_dup(lua_State *L)
2841{
2842 MAY_LJMP(check_args(L, 1, "dup"));
2843 MAY_LJMP(hlua_checkchannel(L, 1));
2844 return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0));
2845}
2846
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002847/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2848 * a yield. This function consumes the data in the buffer. It returns
2849 * a string containing the data or a nil pointer if no data are available
2850 * and the channel is closed.
2851 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002852__LJMP static int hlua_channel_get_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002853{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002854 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002855 int ret;
2856
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002857 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002858
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002859 ret = _hlua_channel_dup(chn, L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002860 if (unlikely(ret == 0))
Willy Tarreau9635e032018-10-16 17:52:55 +02002861 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002862
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002863 if (unlikely(ret == -1))
2864 return 1;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002865
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002866 b_sub(&chn->buf, ret);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002867 hlua_resynchonize_proto(chn_strm(chn), !!(chn->flags & CF_ISRESP));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002868 return 1;
2869}
2870
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002871/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002872__LJMP static int hlua_channel_get(lua_State *L)
2873{
2874 MAY_LJMP(check_args(L, 1, "get"));
2875 MAY_LJMP(hlua_checkchannel(L, 1));
2876 return MAY_LJMP(hlua_channel_get_yield(L, 0, 0));
2877}
2878
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002879/* This functions consumes and returns one line. If the channel is closed,
2880 * and the last data does not contains a final '\n', the data are returned
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002881 * without the final '\n'. When no more data are available, it returns nil
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002882 * value.
2883 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002884__LJMP static int hlua_channel_getline_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002885{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002886 char *blk1;
2887 char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02002888 size_t len1;
2889 size_t len2;
2890 size_t len;
Willy Tarreau47860ed2015-03-10 14:07:50 +01002891 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002892 int ret;
2893 luaL_Buffer b;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002894
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002895 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2896
Willy Tarreau06d80a92017-10-19 14:32:15 +02002897 ret = ci_getline_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002898 if (ret == 0)
Willy Tarreau9635e032018-10-16 17:52:55 +02002899 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_getline_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002900
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002901 if (ret == -1) {
2902 lua_pushnil(L);
2903 return 1;
2904 }
2905
2906 luaL_buffinit(L, &b);
2907 luaL_addlstring(&b, blk1, len1);
2908 len = len1;
2909 if (unlikely(ret == 2)) {
2910 luaL_addlstring(&b, blk2, len2);
2911 len += len2;
2912 }
2913 luaL_pushresult(&b);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002914 b_rep_blk(&chn->buf, ci_head(chn), ci_head(chn) + len, NULL, 0);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002915 hlua_resynchonize_proto(chn_strm(chn), !!(chn->flags & CF_ISRESP));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002916 return 1;
2917}
2918
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002919/* Check arguments for the function "hlua_channel_getline_yield". */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002920__LJMP static int hlua_channel_getline(lua_State *L)
2921{
2922 MAY_LJMP(check_args(L, 1, "getline"));
2923 MAY_LJMP(hlua_checkchannel(L, 1));
2924 return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0));
2925}
2926
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002927/* This function takes a string as input, and append it at the
2928 * input side of channel. If the data is too big, but a space
2929 * is probably available after sending some data, the function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002930 * yields. If the data is bigger than the buffer, or if the
2931 * channel is closed, it returns -1. Otherwise, it returns the
2932 * amount of data written.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002933 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002934__LJMP static int hlua_channel_append_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002935{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002936 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002937 size_t len;
2938 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
2939 int l = MAY_LJMP(luaL_checkinteger(L, 3));
2940 int ret;
2941 int max;
2942
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002943 /* Check if the buffer is available because HAProxy doesn't allocate
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002944 * the request buffer if its not required.
2945 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002946 if (chn->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +01002947 si_rx_buff_blk(chn_prod(chn));
Willy Tarreau9635e032018-10-16 17:52:55 +02002948 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002949 }
2950
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002951 max = channel_recv_limit(chn) - b_data(&chn->buf);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002952 if (max > len - l)
2953 max = len - l;
2954
Willy Tarreau06d80a92017-10-19 14:32:15 +02002955 ret = ci_putblk(chn, str + l, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002956 if (ret == -2 || ret == -3) {
2957 lua_pushinteger(L, -1);
2958 return 1;
2959 }
Willy Tarreaubc18da12015-03-13 14:00:47 +01002960 if (ret == -1) {
2961 chn->flags |= CF_WAKE_WRITE;
Willy Tarreau9635e032018-10-16 17:52:55 +02002962 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Willy Tarreaubc18da12015-03-13 14:00:47 +01002963 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002964 l += ret;
2965 lua_pop(L, 1);
2966 lua_pushinteger(L, l);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002967 hlua_resynchonize_proto(chn_strm(chn), !!(chn->flags & CF_ISRESP));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002968
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002969 max = channel_recv_limit(chn) - b_data(&chn->buf);
Willy Tarreaua79021a2018-06-15 18:07:57 +02002970 if (max == 0 && co_data(chn) == 0) {
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002971 /* There are no space available, and the output buffer is empty.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002972 * in this case, we cannot add more data, so we cannot yield,
2973 * we return the amount of copyied data.
2974 */
2975 return 1;
2976 }
2977 if (l < len)
Willy Tarreau9635e032018-10-16 17:52:55 +02002978 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002979 return 1;
2980}
2981
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002982/* Just a wrapper of "hlua_channel_append_yield". It returns the length
2983 * of the written string, or -1 if the channel is closed or if the
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002984 * buffer size is too little for the data.
2985 */
2986__LJMP static int hlua_channel_append(lua_State *L)
2987{
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002988 size_t len;
2989
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002990 MAY_LJMP(check_args(L, 2, "append"));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002991 MAY_LJMP(hlua_checkchannel(L, 1));
2992 MAY_LJMP(luaL_checklstring(L, 2, &len));
2993 MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002994 lua_pushinteger(L, 0);
2995
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002996 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002997}
2998
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002999/* Just a wrapper of "hlua_channel_append_yield". This wrapper starts
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003000 * his process by cleaning the buffer. The result is a replacement
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003001 * of the current data. It returns the length of the written string,
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003002 * or -1 if the channel is closed or if the buffer size is too
3003 * little for the data.
3004 */
3005__LJMP static int hlua_channel_set(lua_State *L)
3006{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003007 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003008
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003009 MAY_LJMP(check_args(L, 2, "set"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003010 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003011 lua_pushinteger(L, 0);
3012
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003013 b_set_data(&chn->buf, co_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003014
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003015 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003016}
3017
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003018/* Append data in the output side of the buffer. This data is immediately
3019 * sent. The function returns the amount of data written. If the buffer
3020 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003021 * if the channel is closed.
3022 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003023__LJMP static int hlua_channel_send_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003024{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003025 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003026 size_t len;
3027 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
3028 int l = MAY_LJMP(luaL_checkinteger(L, 3));
3029 int max;
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003030 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003031
Willy Tarreau47860ed2015-03-10 14:07:50 +01003032 if (unlikely(channel_output_closed(chn))) {
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003033 lua_pushinteger(L, -1);
3034 return 1;
3035 }
3036
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003037 /* Check if the buffer is available because HAProxy doesn't allocate
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01003038 * the request buffer if its not required.
3039 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003040 if (chn->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +01003041 si_rx_buff_blk(chn_prod(chn));
Willy Tarreau9635e032018-10-16 17:52:55 +02003042 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01003043 }
3044
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003045 /* The written data will be immediately sent, so we can check
3046 * the available space without taking in account the reserve.
3047 * The reserve is guaranteed for the processing of incoming
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003048 * data, because the buffer will be flushed.
3049 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003050 max = b_room(&chn->buf);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003051
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003052 /* If there is no space available, and the output buffer is empty.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003053 * in this case, we cannot add more data, so we cannot yield,
3054 * we return the amount of copyied data.
3055 */
Willy Tarreaua79021a2018-06-15 18:07:57 +02003056 if (max == 0 && co_data(chn) == 0)
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003057 return 1;
3058
3059 /* Adjust the real required length. */
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003060 if (max > len - l)
3061 max = len - l;
3062
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003063 /* The buffer available size may be not contiguous. This test
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003064 * detects a non contiguous buffer and realign it.
3065 */
Willy Tarreau3f679992018-06-15 15:06:42 +02003066 if (ci_space_for_replace(chn) < max)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003067 channel_slow_realign(chn, trash.area);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003068
3069 /* Copy input data in the buffer. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003070 max = b_rep_blk(&chn->buf, ci_head(chn), ci_head(chn), str + l, max);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003071
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003072 /* buffer replace considers that the input part is filled.
3073 * so, I must forward these new data in the output part.
3074 */
Willy Tarreaubcbd3932018-06-06 07:13:22 +02003075 c_adv(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003076
3077 l += max;
3078 lua_pop(L, 1);
3079 lua_pushinteger(L, l);
3080
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003081 /* If there is no space available, and the output buffer is empty.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003082 * in this case, we cannot add more data, so we cannot yield,
3083 * we return the amount of copyied data.
3084 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003085 max = b_room(&chn->buf);
Willy Tarreaua79021a2018-06-15 18:07:57 +02003086 if (max == 0 && co_data(chn) == 0)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003087 return 1;
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003088
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003089 if (l < len) {
3090 /* If we are waiting for space in the response buffer, we
3091 * must set the flag WAKERESWR. This flag required the task
3092 * wake up if any activity is detected on the response buffer.
3093 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003094 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003095 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01003096 else
3097 HLUA_SET_WAKEREQWR(hlua);
Willy Tarreau9635e032018-10-16 17:52:55 +02003098 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003099 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003100
3101 return 1;
3102}
3103
3104/* Just a wraper of "_hlua_channel_send". This wrapper permits
3105 * yield the LUA process, and resume it without checking the
3106 * input arguments.
3107 */
3108__LJMP static int hlua_channel_send(lua_State *L)
3109{
3110 MAY_LJMP(check_args(L, 2, "send"));
3111 lua_pushinteger(L, 0);
3112
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003113 return MAY_LJMP(hlua_channel_send_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003114}
3115
3116/* This function forward and amount of butes. The data pass from
3117 * the input side of the buffer to the output side, and can be
3118 * forwarded. This function never fails.
3119 *
3120 * The Lua function takes an amount of bytes to be forwarded in
3121 * imput. It returns the number of bytes forwarded.
3122 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003123__LJMP static int hlua_channel_forward_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003124{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003125 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003126 int len;
3127 int l;
3128 int max;
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003129 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003130
3131 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3132 len = MAY_LJMP(luaL_checkinteger(L, 2));
3133 l = MAY_LJMP(luaL_checkinteger(L, -1));
3134
3135 max = len - l;
Willy Tarreaua79021a2018-06-15 18:07:57 +02003136 if (max > ci_data(chn))
3137 max = ci_data(chn);
Willy Tarreau47860ed2015-03-10 14:07:50 +01003138 channel_forward(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003139 l += max;
3140
3141 lua_pop(L, 1);
3142 lua_pushinteger(L, l);
3143
3144 /* Check if it miss bytes to forward. */
3145 if (l < len) {
3146 /* The the input channel or the output channel are closed, we
3147 * must return the amount of data forwarded.
3148 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003149 if (channel_input_closed(chn) || channel_output_closed(chn))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003150 return 1;
3151
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003152 /* If we are waiting for space data in the response buffer, we
3153 * must set the flag WAKERESWR. This flag required the task
3154 * wake up if any activity is detected on the response buffer.
3155 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003156 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003157 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01003158 else
3159 HLUA_SET_WAKEREQWR(hlua);
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003160
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003161 /* Otherwise, we can yield waiting for new data in the inpout side. */
Willy Tarreau9635e032018-10-16 17:52:55 +02003162 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_forward_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003163 }
3164
3165 return 1;
3166}
3167
3168/* Just check the input and prepare the stack for the previous
3169 * function "hlua_channel_forward_yield"
3170 */
3171__LJMP static int hlua_channel_forward(lua_State *L)
3172{
3173 MAY_LJMP(check_args(L, 2, "forward"));
3174 MAY_LJMP(hlua_checkchannel(L, 1));
3175 MAY_LJMP(luaL_checkinteger(L, 2));
3176
3177 lua_pushinteger(L, 0);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003178 return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003179}
3180
3181/* Just returns the number of bytes available in the input
3182 * side of the buffer. This function never fails.
3183 */
3184__LJMP static int hlua_channel_get_in_len(lua_State *L)
3185{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003186 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003187
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003188 MAY_LJMP(check_args(L, 1, "get_in_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003189 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Willy Tarreaua79021a2018-06-15 18:07:57 +02003190 lua_pushinteger(L, ci_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003191 return 1;
3192}
3193
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003194/* Returns true if the channel is full. */
3195__LJMP static int hlua_channel_is_full(lua_State *L)
3196{
3197 struct channel *chn;
3198 int rem;
3199
3200 MAY_LJMP(check_args(L, 1, "is_full"));
3201 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3202
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003203 rem = b_room(&chn->buf);
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003204 rem -= global.tune.maxrewrite; /* Rewrite reserved size */
3205
3206 lua_pushboolean(L, rem <= 0);
3207 return 1;
3208}
3209
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003210/* Just returns the number of bytes available in the output
3211 * side of the buffer. This function never fails.
3212 */
3213__LJMP static int hlua_channel_get_out_len(lua_State *L)
3214{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003215 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003216
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003217 MAY_LJMP(check_args(L, 1, "get_out_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003218 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Willy Tarreaua79021a2018-06-15 18:07:57 +02003219 lua_pushinteger(L, co_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003220 return 1;
3221}
3222
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003223/*
3224 *
3225 *
3226 * Class Fetches
3227 *
3228 *
3229 */
3230
3231/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003232 * a class stream, otherwise it throws an error.
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003233 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003234__LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003235{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003236 return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003237}
3238
3239/* This function creates and push in the stack a fetch object according
3240 * with a current TXN.
3241 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003242static int hlua_fetches_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003243{
Willy Tarreau7073c472015-04-06 11:15:40 +02003244 struct hlua_smp *hsmp;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003245
3246 /* Check stack size. */
3247 if (!lua_checkstack(L, 3))
3248 return 0;
3249
3250 /* Create the object: obj[0] = userdata.
3251 * Note that the base of the Fetches object is the
3252 * transaction object.
3253 */
3254 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003255 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003256 lua_rawseti(L, -2, 0);
3257
Willy Tarreau7073c472015-04-06 11:15:40 +02003258 hsmp->s = txn->s;
3259 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003260 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003261 hsmp->flags = flags;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003262
3263 /* Pop a class sesison metatable and affect it to the userdata. */
3264 lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref);
3265 lua_setmetatable(L, -2);
3266
3267 return 1;
3268}
3269
3270/* This function is an LUA binding. It is called with each sample-fetch.
3271 * It uses closure argument to store the associated sample-fetch. It
3272 * returns only one argument or throws an error. An error is thrown
3273 * only if an error is encountered during the argument parsing. If
3274 * the "sample-fetch" function fails, nil is returned.
3275 */
3276__LJMP static int hlua_run_sample_fetch(lua_State *L)
3277{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003278 struct hlua_smp *hsmp;
Willy Tarreau2ec22742015-03-10 14:27:20 +01003279 struct sample_fetch *f;
Frédéric Lécaillef874a832018-06-15 13:56:04 +02003280 struct arg args[ARGM_NBARGS + 1] = {{0}};
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003281 int i;
3282 struct sample smp;
3283
3284 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003285 f = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003286
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003287 /* Get traditional arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003288 hsmp = MAY_LJMP(hlua_checkfetches(L, 1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003289
Thierry FOURNIERca988662015-12-20 18:43:03 +01003290 /* Check execution authorization. */
3291 if (f->use & SMP_USE_HTTP_ANY &&
3292 !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) {
3293 lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which "
3294 "is not available in Lua services", f->kw);
3295 WILL_LJMP(lua_error(L));
3296 }
3297
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003298 /* Get extra arguments. */
3299 for (i = 0; i < lua_gettop(L) - 1; i++) {
3300 if (i >= ARGM_NBARGS)
3301 break;
3302 hlua_lua2arg(L, i + 2, &args[i]);
3303 }
3304 args[i].type = ARGT_STOP;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003305 args[i].data.str.area = NULL;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003306
3307 /* Check arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003308 MAY_LJMP(hlua_lua2arg_check(L, 2, args, f->arg_mask, hsmp->p));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003309
3310 /* Run the special args checker. */
Willy Tarreau2ec22742015-03-10 14:27:20 +01003311 if (f->val_args && !f->val_args(args, NULL)) {
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003312 lua_pushfstring(L, "error in arguments");
3313 WILL_LJMP(lua_error(L));
3314 }
3315
3316 /* Initialise the sample. */
3317 memset(&smp, 0, sizeof(smp));
3318
3319 /* Run the sample fetch process. */
Willy Tarreau1777ea62016-03-10 16:15:46 +01003320 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003321 if (!f->process(args, &smp, f->kw, f->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003322 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003323 lua_pushstring(L, "");
3324 else
3325 lua_pushnil(L);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003326 return 1;
3327 }
3328
3329 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003330 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003331 hlua_smp2lua_str(L, &smp);
3332 else
3333 hlua_smp2lua(L, &smp);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003334 return 1;
3335}
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003336
3337/*
3338 *
3339 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003340 * Class Converters
3341 *
3342 *
3343 */
3344
3345/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003346 * a class stream, otherwise it throws an error.
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003347 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003348__LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003349{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003350 return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003351}
3352
3353/* This function creates and push in the stack a Converters object
3354 * according with a current TXN.
3355 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003356static int hlua_converters_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003357{
Willy Tarreau7073c472015-04-06 11:15:40 +02003358 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003359
3360 /* Check stack size. */
3361 if (!lua_checkstack(L, 3))
3362 return 0;
3363
3364 /* Create the object: obj[0] = userdata.
3365 * Note that the base of the Converters object is the
3366 * same than the TXN object.
3367 */
3368 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003369 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003370 lua_rawseti(L, -2, 0);
3371
Willy Tarreau7073c472015-04-06 11:15:40 +02003372 hsmp->s = txn->s;
3373 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003374 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003375 hsmp->flags = flags;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003376
Willy Tarreau87b09662015-04-03 00:22:06 +02003377 /* Pop a class stream metatable and affect it to the table. */
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003378 lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref);
3379 lua_setmetatable(L, -2);
3380
3381 return 1;
3382}
3383
3384/* This function is an LUA binding. It is called with each converter.
3385 * It uses closure argument to store the associated converter. It
3386 * returns only one argument or throws an error. An error is thrown
3387 * only if an error is encountered during the argument parsing. If
3388 * the converter function function fails, nil is returned.
3389 */
3390__LJMP static int hlua_run_sample_conv(lua_State *L)
3391{
Willy Tarreauda5f1082015-04-06 11:17:13 +02003392 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003393 struct sample_conv *conv;
Frédéric Lécaillef874a832018-06-15 13:56:04 +02003394 struct arg args[ARGM_NBARGS + 1] = {{0}};
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003395 int i;
3396 struct sample smp;
3397
3398 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003399 conv = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003400
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003401 /* Get traditional arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003402 hsmp = MAY_LJMP(hlua_checkconverters(L, 1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003403
3404 /* Get extra arguments. */
3405 for (i = 0; i < lua_gettop(L) - 2; i++) {
3406 if (i >= ARGM_NBARGS)
3407 break;
3408 hlua_lua2arg(L, i + 3, &args[i]);
3409 }
3410 args[i].type = ARGT_STOP;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003411 args[i].data.str.area = NULL;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003412
3413 /* Check arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003414 MAY_LJMP(hlua_lua2arg_check(L, 3, args, conv->arg_mask, hsmp->p));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003415
3416 /* Run the special args checker. */
3417 if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) {
3418 hlua_pusherror(L, "error in arguments");
3419 WILL_LJMP(lua_error(L));
3420 }
3421
3422 /* Initialise the sample. */
3423 if (!hlua_lua2smp(L, 2, &smp)) {
3424 hlua_pusherror(L, "error in the input argument");
3425 WILL_LJMP(lua_error(L));
3426 }
3427
Willy Tarreau1777ea62016-03-10 16:15:46 +01003428 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
3429
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003430 /* Apply expected cast. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003431 if (!sample_casts[smp.data.type][conv->in_type]) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003432 hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'",
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003433 smp_to_type[smp.data.type], smp_to_type[conv->in_type]);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003434 WILL_LJMP(lua_error(L));
3435 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003436 if (sample_casts[smp.data.type][conv->in_type] != c_none &&
3437 !sample_casts[smp.data.type][conv->in_type](&smp)) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003438 hlua_pusherror(L, "error during the input argument casting");
3439 WILL_LJMP(lua_error(L));
3440 }
3441
3442 /* Run the sample conversion process. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003443 if (!conv->process(args, &smp, conv->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003444 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003445 lua_pushstring(L, "");
3446 else
Willy Tarreaua678b432015-08-28 10:14:59 +02003447 lua_pushnil(L);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003448 return 1;
3449 }
3450
3451 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003452 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003453 hlua_smp2lua_str(L, &smp);
3454 else
3455 hlua_smp2lua(L, &smp);
Willy Tarreaua678b432015-08-28 10:14:59 +02003456 return 1;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003457}
3458
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003459/*
3460 *
3461 *
3462 * Class AppletTCP
3463 *
3464 *
3465 */
3466
3467/* Returns a struct hlua_txn if the stack entry "ud" is
3468 * a class stream, otherwise it throws an error.
3469 */
3470__LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud)
3471{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003472 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003473}
3474
3475/* This function creates and push in the stack an Applet object
3476 * according with a current TXN.
3477 */
3478static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx)
3479{
3480 struct hlua_appctx *appctx;
3481 struct stream_interface *si = ctx->owner;
3482 struct stream *s = si_strm(si);
3483 struct proxy *p = s->be;
3484
3485 /* Check stack size. */
3486 if (!lua_checkstack(L, 3))
3487 return 0;
3488
3489 /* Create the object: obj[0] = userdata.
3490 * Note that the base of the Converters object is the
3491 * same than the TXN object.
3492 */
3493 lua_newtable(L);
3494 appctx = lua_newuserdata(L, sizeof(*appctx));
3495 lua_rawseti(L, -2, 0);
3496 appctx->appctx = ctx;
3497 appctx->htxn.s = s;
3498 appctx->htxn.p = p;
3499
3500 /* Create the "f" field that contains a list of fetches. */
3501 lua_pushstring(L, "f");
3502 if (!hlua_fetches_new(L, &appctx->htxn, 0))
3503 return 0;
3504 lua_settable(L, -3);
3505
3506 /* Create the "sf" field that contains a list of stringsafe fetches. */
3507 lua_pushstring(L, "sf");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003508 if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003509 return 0;
3510 lua_settable(L, -3);
3511
3512 /* Create the "c" field that contains a list of converters. */
3513 lua_pushstring(L, "c");
3514 if (!hlua_converters_new(L, &appctx->htxn, 0))
3515 return 0;
3516 lua_settable(L, -3);
3517
3518 /* Create the "sc" field that contains a list of stringsafe converters. */
3519 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003520 if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003521 return 0;
3522 lua_settable(L, -3);
3523
3524 /* Pop a class stream metatable and affect it to the table. */
3525 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref);
3526 lua_setmetatable(L, -2);
3527
3528 return 1;
3529}
3530
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003531__LJMP static int hlua_applet_tcp_set_var(lua_State *L)
3532{
3533 struct hlua_appctx *appctx;
3534 struct stream *s;
3535 const char *name;
3536 size_t len;
3537 struct sample smp;
3538
3539 MAY_LJMP(check_args(L, 3, "set_var"));
3540
3541 /* It is useles to retrieve the stream, but this function
3542 * runs only in a stream context.
3543 */
3544 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3545 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3546 s = appctx->htxn.s;
3547
3548 /* Converts the third argument in a sample. */
3549 hlua_lua2smp(L, 3, &smp);
3550
3551 /* Store the sample in a variable. */
3552 smp_set_owner(&smp, s->be, s->sess, s, 0);
3553 vars_set_by_name(name, len, &smp);
3554 return 0;
3555}
3556
3557__LJMP static int hlua_applet_tcp_unset_var(lua_State *L)
3558{
3559 struct hlua_appctx *appctx;
3560 struct stream *s;
3561 const char *name;
3562 size_t len;
3563 struct sample smp;
3564
3565 MAY_LJMP(check_args(L, 2, "unset_var"));
3566
3567 /* It is useles to retrieve the stream, but this function
3568 * runs only in a stream context.
3569 */
3570 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3571 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3572 s = appctx->htxn.s;
3573
3574 /* Unset the variable. */
3575 smp_set_owner(&smp, s->be, s->sess, s, 0);
3576 vars_unset_by_name(name, len, &smp);
3577 return 0;
3578}
3579
3580__LJMP static int hlua_applet_tcp_get_var(lua_State *L)
3581{
3582 struct hlua_appctx *appctx;
3583 struct stream *s;
3584 const char *name;
3585 size_t len;
3586 struct sample smp;
3587
3588 MAY_LJMP(check_args(L, 2, "get_var"));
3589
3590 /* It is useles to retrieve the stream, but this function
3591 * runs only in a stream context.
3592 */
3593 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3594 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3595 s = appctx->htxn.s;
3596
3597 smp_set_owner(&smp, s->be, s->sess, s, 0);
3598 if (!vars_get_by_name(name, len, &smp)) {
3599 lua_pushnil(L);
3600 return 1;
3601 }
3602
3603 return hlua_smp2lua(L, &smp);
3604}
3605
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003606__LJMP static int hlua_applet_tcp_set_priv(lua_State *L)
3607{
3608 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3609 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003610 struct hlua *hlua;
3611
3612 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003613 if (!s->hlua)
3614 return 0;
3615 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003616
3617 MAY_LJMP(check_args(L, 2, "set_priv"));
3618
3619 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02003620 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003621
3622 /* Get and store new value. */
3623 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
3624 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
3625
3626 return 0;
3627}
3628
3629__LJMP static int hlua_applet_tcp_get_priv(lua_State *L)
3630{
3631 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3632 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003633 struct hlua *hlua;
3634
3635 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003636 if (!s->hlua) {
3637 lua_pushnil(L);
3638 return 1;
3639 }
3640 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003641
3642 /* Push configuration index in the stack. */
3643 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
3644
3645 return 1;
3646}
3647
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003648/* If expected data not yet available, it returns a yield. This function
3649 * consumes the data in the buffer. It returns a string containing the
3650 * data. This string can be empty.
3651 */
3652__LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx)
3653{
3654 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3655 struct stream_interface *si = appctx->appctx->owner;
3656 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02003657 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003658 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02003659 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003660 size_t len2;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003661
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003662 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003663 ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003664
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003665 /* Data not yet available. return yield. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003666 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003667 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003668 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_getline_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003669 }
3670
3671 /* End of data: commit the total strings and return. */
3672 if (ret < 0) {
3673 luaL_pushresult(&appctx->b);
3674 return 1;
3675 }
3676
3677 /* Ensure that the block 2 length is usable. */
3678 if (ret == 1)
3679 len2 = 0;
3680
3681 /* dont check the max length read and dont check. */
3682 luaL_addlstring(&appctx->b, blk1, len1);
3683 luaL_addlstring(&appctx->b, blk2, len2);
3684
3685 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003686 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003687 luaL_pushresult(&appctx->b);
3688 return 1;
3689}
3690
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003691/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003692__LJMP static int hlua_applet_tcp_getline(lua_State *L)
3693{
3694 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3695
3696 /* Initialise the string catenation. */
3697 luaL_buffinit(L, &appctx->b);
3698
3699 return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0));
3700}
3701
3702/* If expected data not yet available, it returns a yield. This function
3703 * consumes the data in the buffer. It returns a string containing the
3704 * data. This string can be empty.
3705 */
3706__LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx)
3707{
3708 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3709 struct stream_interface *si = appctx->appctx->owner;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003710 size_t len = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003711 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02003712 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003713 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02003714 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003715 size_t len2;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003716
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003717 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003718 ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003719
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003720 /* Data not yet available. return yield. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003721 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003722 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003723 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003724 }
3725
3726 /* End of data: commit the total strings and return. */
3727 if (ret < 0) {
3728 luaL_pushresult(&appctx->b);
3729 return 1;
3730 }
3731
3732 /* Ensure that the block 2 length is usable. */
3733 if (ret == 1)
3734 len2 = 0;
3735
3736 if (len == -1) {
3737
3738 /* If len == -1, catenate all the data avalaile and
3739 * yield because we want to get all the data until
3740 * the end of data stream.
3741 */
3742 luaL_addlstring(&appctx->b, blk1, len1);
3743 luaL_addlstring(&appctx->b, blk2, len2);
Willy Tarreau06d80a92017-10-19 14:32:15 +02003744 co_skip(si_oc(si), len1 + len2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003745 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003746 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003747
3748 } else {
3749
3750 /* Copy the fisrt block caping to the length required. */
3751 if (len1 > len)
3752 len1 = len;
3753 luaL_addlstring(&appctx->b, blk1, len1);
3754 len -= len1;
3755
3756 /* Copy the second block. */
3757 if (len2 > len)
3758 len2 = len;
3759 luaL_addlstring(&appctx->b, blk2, len2);
3760 len -= len2;
3761
3762 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003763 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003764
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003765 /* If there is no other data available, yield waiting for new data. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003766 if (len > 0) {
3767 lua_pushinteger(L, len);
3768 lua_replace(L, 2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003769 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003770 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003771 }
3772
3773 /* return the result. */
3774 luaL_pushresult(&appctx->b);
3775 return 1;
3776 }
3777
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003778 /* we never execute this */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003779 hlua_pusherror(L, "Lua: internal error");
3780 WILL_LJMP(lua_error(L));
3781 return 0;
3782}
3783
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003784/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003785__LJMP static int hlua_applet_tcp_recv(lua_State *L)
3786{
3787 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3788 int len = -1;
3789
3790 if (lua_gettop(L) > 2)
3791 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
3792 if (lua_gettop(L) >= 2) {
3793 len = MAY_LJMP(luaL_checkinteger(L, 2));
3794 lua_pop(L, 1);
3795 }
3796
3797 /* Confirm or set the required length */
3798 lua_pushinteger(L, len);
3799
3800 /* Initialise the string catenation. */
3801 luaL_buffinit(L, &appctx->b);
3802
3803 return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0));
3804}
3805
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003806/* Append data in the output side of the buffer. This data is immediately
3807 * sent. The function returns the amount of data written. If the buffer
3808 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003809 * if the channel is closed.
3810 */
3811__LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx)
3812{
3813 size_t len;
3814 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3815 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
3816 int l = MAY_LJMP(luaL_checkinteger(L, 3));
3817 struct stream_interface *si = appctx->appctx->owner;
3818 struct channel *chn = si_ic(si);
3819 int max;
3820
3821 /* Get the max amount of data which can write as input in the channel. */
3822 max = channel_recv_max(chn);
3823 if (max > (len - l))
3824 max = len - l;
3825
3826 /* Copy data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003827 ci_putblk(chn, str + l, max);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003828
3829 /* update counters. */
3830 l += max;
3831 lua_pop(L, 1);
3832 lua_pushinteger(L, l);
3833
3834 /* If some data is not send, declares the situation to the
3835 * applet, and returns a yield.
3836 */
3837 if (l < len) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003838 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003839 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003840 }
3841
3842 return 1;
3843}
3844
3845/* Just a wraper of "hlua_applet_tcp_send_yield". This wrapper permits
3846 * yield the LUA process, and resume it without checking the
3847 * input arguments.
3848 */
3849__LJMP static int hlua_applet_tcp_send(lua_State *L)
3850{
3851 MAY_LJMP(check_args(L, 2, "send"));
3852 lua_pushinteger(L, 0);
3853
3854 return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0));
3855}
3856
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003857/*
3858 *
3859 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003860 * Class AppletHTTP
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003861 *
3862 *
3863 */
3864
3865/* Returns a struct hlua_txn if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003866 * a class stream, otherwise it throws an error.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003867 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003868__LJMP static struct hlua_appctx *hlua_checkapplet_http(lua_State *L, int ud)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003869{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003870 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003871}
3872
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003873/* This function creates and push in the stack an Applet object
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003874 * according with a current TXN.
3875 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003876static int hlua_applet_http_new(lua_State *L, struct appctx *ctx)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003877{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003878 struct hlua_appctx *appctx;
Thierry FOURNIER841475e2015-12-11 17:10:09 +01003879 struct hlua_txn htxn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003880 struct stream_interface *si = ctx->owner;
3881 struct stream *s = si_strm(si);
3882 struct proxy *px = s->be;
3883 struct http_txn *txn = s->txn;
3884 const char *path;
3885 const char *end;
3886 const char *p;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003887
3888 /* Check stack size. */
3889 if (!lua_checkstack(L, 3))
3890 return 0;
3891
3892 /* Create the object: obj[0] = userdata.
3893 * Note that the base of the Converters object is the
3894 * same than the TXN object.
3895 */
3896 lua_newtable(L);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003897 appctx = lua_newuserdata(L, sizeof(*appctx));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003898 lua_rawseti(L, -2, 0);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003899 appctx->appctx = ctx;
3900 appctx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */
Robin H. Johnson52f5db22017-01-01 13:10:52 -08003901 appctx->appctx->ctx.hlua_apphttp.reason = NULL; /* Use default reason based on status */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003902 appctx->htxn.s = s;
3903 appctx->htxn.p = px;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003904
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003905 /* Create the "f" field that contains a list of fetches. */
3906 lua_pushstring(L, "f");
3907 if (!hlua_fetches_new(L, &appctx->htxn, 0))
3908 return 0;
3909 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003910
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003911 /* Create the "sf" field that contains a list of stringsafe fetches. */
3912 lua_pushstring(L, "sf");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003913 if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003914 return 0;
3915 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003916
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003917 /* Create the "c" field that contains a list of converters. */
3918 lua_pushstring(L, "c");
3919 if (!hlua_converters_new(L, &appctx->htxn, 0))
3920 return 0;
3921 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003922
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003923 /* Create the "sc" field that contains a list of stringsafe converters. */
3924 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003925 if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003926 return 0;
3927 lua_settable(L, -3);
Willy Tarreaueee5b512015-04-03 23:46:31 +02003928
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003929 /* Stores the request method. */
3930 lua_pushstring(L, "method");
Willy Tarreaua79021a2018-06-15 18:07:57 +02003931 lua_pushlstring(L, ci_head(txn->req.chn), txn->req.sl.rq.m_l);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003932 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003933
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003934 /* Stores the http version. */
3935 lua_pushstring(L, "version");
Willy Tarreaua79021a2018-06-15 18:07:57 +02003936 lua_pushlstring(L, ci_head(txn->req.chn) + txn->req.sl.rq.v, txn->req.sl.rq.v_l);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003937 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003938
Thierry FOURNIER841475e2015-12-11 17:10:09 +01003939 /* creates an array of headers. hlua_http_get_headers() crates and push
3940 * the array on the top of the stack.
3941 */
3942 lua_pushstring(L, "headers");
3943 htxn.s = s;
3944 htxn.p = px;
3945 htxn.dir = SMP_OPT_DIR_REQ;
3946 if (!hlua_http_get_headers(L, &htxn, &htxn.s->txn->req))
3947 return 0;
3948 lua_settable(L, -3);
3949
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003950 /* Get path and qs */
Willy Tarreau6b952c82018-09-10 17:45:34 +02003951 path = http_txn_get_path(txn);
Thierry FOURNIER7d388632017-02-22 02:06:16 +01003952 if (path) {
Willy Tarreaua79021a2018-06-15 18:07:57 +02003953 end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Thierry FOURNIER7d388632017-02-22 02:06:16 +01003954 p = path;
3955 while (p < end && *p != '?')
3956 p++;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003957
Thierry FOURNIER7d388632017-02-22 02:06:16 +01003958 /* Stores the request path. */
3959 lua_pushstring(L, "path");
3960 lua_pushlstring(L, path, p - path);
3961 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003962
Thierry FOURNIER7d388632017-02-22 02:06:16 +01003963 /* Stores the query string. */
3964 lua_pushstring(L, "qs");
3965 if (*p == '?')
3966 p++;
3967 lua_pushlstring(L, p, end - p);
3968 lua_settable(L, -3);
3969 }
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003970
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003971 /* Stores the request path. */
3972 lua_pushstring(L, "length");
3973 lua_pushinteger(L, txn->req.body_len);
3974 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003975
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003976 /* Create an array of HTTP request headers. */
3977 lua_pushstring(L, "headers");
3978 MAY_LJMP(hlua_http_get_headers(L, &appctx->htxn, &appctx->htxn.s->txn->req));
3979 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003980
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003981 /* Create an empty array of HTTP request headers. */
3982 lua_pushstring(L, "response");
3983 lua_newtable(L);
3984 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003985
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003986 /* Pop a class stream metatable and affect it to the table. */
3987 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref);
3988 lua_setmetatable(L, -2);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003989
3990 return 1;
3991}
3992
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003993__LJMP static int hlua_applet_http_set_var(lua_State *L)
3994{
3995 struct hlua_appctx *appctx;
3996 struct stream *s;
3997 const char *name;
3998 size_t len;
3999 struct sample smp;
4000
4001 MAY_LJMP(check_args(L, 3, "set_var"));
4002
4003 /* It is useles to retrieve the stream, but this function
4004 * runs only in a stream context.
4005 */
4006 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4007 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4008 s = appctx->htxn.s;
4009
4010 /* Converts the third argument in a sample. */
4011 hlua_lua2smp(L, 3, &smp);
4012
4013 /* Store the sample in a variable. */
4014 smp_set_owner(&smp, s->be, s->sess, s, 0);
4015 vars_set_by_name(name, len, &smp);
4016 return 0;
4017}
4018
4019__LJMP static int hlua_applet_http_unset_var(lua_State *L)
4020{
4021 struct hlua_appctx *appctx;
4022 struct stream *s;
4023 const char *name;
4024 size_t len;
4025 struct sample smp;
4026
4027 MAY_LJMP(check_args(L, 2, "unset_var"));
4028
4029 /* It is useles to retrieve the stream, but this function
4030 * runs only in a stream context.
4031 */
4032 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4033 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4034 s = appctx->htxn.s;
4035
4036 /* Unset the variable. */
4037 smp_set_owner(&smp, s->be, s->sess, s, 0);
4038 vars_unset_by_name(name, len, &smp);
4039 return 0;
4040}
4041
4042__LJMP static int hlua_applet_http_get_var(lua_State *L)
4043{
4044 struct hlua_appctx *appctx;
4045 struct stream *s;
4046 const char *name;
4047 size_t len;
4048 struct sample smp;
4049
4050 MAY_LJMP(check_args(L, 2, "get_var"));
4051
4052 /* It is useles to retrieve the stream, but this function
4053 * runs only in a stream context.
4054 */
4055 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4056 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4057 s = appctx->htxn.s;
4058
4059 smp_set_owner(&smp, s->be, s->sess, s, 0);
4060 if (!vars_get_by_name(name, len, &smp)) {
4061 lua_pushnil(L);
4062 return 1;
4063 }
4064
4065 return hlua_smp2lua(L, &smp);
4066}
4067
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004068__LJMP static int hlua_applet_http_set_priv(lua_State *L)
4069{
4070 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4071 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004072 struct hlua *hlua;
4073
4074 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004075 if (!s->hlua)
4076 return 0;
4077 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004078
4079 MAY_LJMP(check_args(L, 2, "set_priv"));
4080
4081 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02004082 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004083
4084 /* Get and store new value. */
4085 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
4086 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
4087
4088 return 0;
4089}
4090
4091__LJMP static int hlua_applet_http_get_priv(lua_State *L)
4092{
4093 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4094 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004095 struct hlua *hlua;
4096
4097 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004098 if (!s->hlua) {
4099 lua_pushnil(L);
4100 return 1;
4101 }
4102 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004103
4104 /* Push configuration index in the stack. */
4105 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
4106
4107 return 1;
4108}
4109
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004110/* If expected data not yet available, it returns a yield. This function
4111 * consumes the data in the buffer. It returns a string containing the
4112 * data. This string can be empty.
4113 */
4114__LJMP static int hlua_applet_http_getline_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004115{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004116 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4117 struct stream_interface *si = appctx->appctx->owner;
4118 struct channel *chn = si_ic(si);
4119 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02004120 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02004121 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02004122 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02004123 size_t len2;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004124
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004125 /* Maybe we cant send a 100-continue ? */
4126 if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_100C) {
Willy Tarreau04f1e2d2018-09-10 18:04:24 +02004127 ret = ci_putblk(chn, HTTP_100.ptr, HTTP_100.len);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004128 /* if ret == -2 or -3 the channel closed or the message si too
4129 * big for the buffers. We cant send anything. So, we ignoring
4130 * the error, considers that the 100-continue is sent, and try
4131 * to receive.
4132 * If ret is -1, we dont have room in the buffer, so we yield.
4133 */
4134 if (ret == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01004135 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004136 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_getline_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004137 }
4138 appctx->appctx->ctx.hlua_apphttp.flags &= ~APPLET_100C;
4139 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004140
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004141 /* Check for the end of the data. */
4142 if (appctx->appctx->ctx.hlua_apphttp.left_bytes <= 0) {
4143 luaL_pushresult(&appctx->b);
4144 return 1;
4145 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004146
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004147 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004148 ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004149
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004150 /* Data not yet available. return yield. */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004151 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01004152 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004153 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_getline_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004154 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004155
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004156 /* End of data: commit the total strings and return. */
4157 if (ret < 0) {
4158 luaL_pushresult(&appctx->b);
4159 return 1;
4160 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004161
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004162 /* Ensure that the block 2 length is usable. */
4163 if (ret == 1)
4164 len2 = 0;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004165
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004166 /* Copy the fisrt block caping to the length required. */
4167 if (len1 > appctx->appctx->ctx.hlua_apphttp.left_bytes)
4168 len1 = appctx->appctx->ctx.hlua_apphttp.left_bytes;
4169 luaL_addlstring(&appctx->b, blk1, len1);
4170 appctx->appctx->ctx.hlua_apphttp.left_bytes -= len1;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004171
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004172 /* Copy the second block. */
4173 if (len2 > appctx->appctx->ctx.hlua_apphttp.left_bytes)
4174 len2 = appctx->appctx->ctx.hlua_apphttp.left_bytes;
4175 luaL_addlstring(&appctx->b, blk2, len2);
4176 appctx->appctx->ctx.hlua_apphttp.left_bytes -= len2;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004177
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004178 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004179 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004180 luaL_pushresult(&appctx->b);
4181 return 1;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004182}
4183
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004184/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004185__LJMP static int hlua_applet_http_getline(lua_State *L)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004186{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004187 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004188
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004189 /* Initialise the string catenation. */
4190 luaL_buffinit(L, &appctx->b);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004191
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004192 return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004193}
4194
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004195/* If expected data not yet available, it returns a yield. This function
4196 * consumes the data in the buffer. It returns a string containing the
4197 * data. This string can be empty.
4198 */
4199__LJMP static int hlua_applet_http_recv_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004200{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004201 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4202 struct stream_interface *si = appctx->appctx->owner;
4203 int len = MAY_LJMP(luaL_checkinteger(L, 2));
4204 struct channel *chn = si_ic(si);
4205 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02004206 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02004207 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02004208 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02004209 size_t len2;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004210
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004211 /* Maybe we cant send a 100-continue ? */
4212 if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_100C) {
Willy Tarreau04f1e2d2018-09-10 18:04:24 +02004213 ret = ci_putblk(chn, HTTP_100.ptr, HTTP_100.len);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004214 /* if ret == -2 or -3 the channel closed or the message si too
4215 * big for the buffers. We cant send anything. So, we ignoring
4216 * the error, considers that the 100-continue is sent, and try
4217 * to receive.
4218 * If ret is -1, we dont have room in the buffer, so we yield.
4219 */
4220 if (ret == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01004221 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004222 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004223 }
4224 appctx->appctx->ctx.hlua_apphttp.flags &= ~APPLET_100C;
4225 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004226
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004227 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004228 ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004229
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004230 /* Data not yet available. return yield. */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004231 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01004232 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004233 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004234 }
4235
4236 /* End of data: commit the total strings and return. */
4237 if (ret < 0) {
4238 luaL_pushresult(&appctx->b);
4239 return 1;
4240 }
4241
4242 /* Ensure that the block 2 length is usable. */
4243 if (ret == 1)
4244 len2 = 0;
4245
4246 /* Copy the fisrt block caping to the length required. */
4247 if (len1 > len)
4248 len1 = len;
4249 luaL_addlstring(&appctx->b, blk1, len1);
4250 len -= len1;
4251
4252 /* Copy the second block. */
4253 if (len2 > len)
4254 len2 = len;
4255 luaL_addlstring(&appctx->b, blk2, len2);
4256 len -= len2;
4257
4258 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004259 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004260 if (appctx->appctx->ctx.hlua_apphttp.left_bytes != -1)
4261 appctx->appctx->ctx.hlua_apphttp.left_bytes -= len;
4262
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004263 /* If we are no other data available, yield waiting for new data. */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004264 if (len > 0) {
4265 lua_pushinteger(L, len);
4266 lua_replace(L, 2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01004267 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004268 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004269 }
4270
4271 /* return the result. */
4272 luaL_pushresult(&appctx->b);
4273 return 1;
4274}
4275
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004276/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004277__LJMP static int hlua_applet_http_recv(lua_State *L)
4278{
4279 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4280 int len = -1;
4281
4282 /* Check arguments. */
4283 if (lua_gettop(L) > 2)
4284 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
4285 if (lua_gettop(L) >= 2) {
4286 len = MAY_LJMP(luaL_checkinteger(L, 2));
4287 lua_pop(L, 1);
4288 }
4289
4290 /* Check the required length */
4291 if (len == -1 || len > appctx->appctx->ctx.hlua_apphttp.left_bytes)
4292 len = appctx->appctx->ctx.hlua_apphttp.left_bytes;
4293 lua_pushinteger(L, len);
4294
4295 /* Initialise the string catenation. */
4296 luaL_buffinit(L, &appctx->b);
4297
4298 return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0));
4299}
4300
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004301/* Append data in the output side of the buffer. This data is immediately
4302 * sent. The function returns the amount of data written. If the buffer
4303 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004304 * if the channel is closed.
4305 */
4306__LJMP static int hlua_applet_http_send_yield(lua_State *L, int status, lua_KContext ctx)
4307{
4308 size_t len;
4309 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4310 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
4311 int l = MAY_LJMP(luaL_checkinteger(L, 3));
4312 struct stream_interface *si = appctx->appctx->owner;
4313 struct channel *chn = si_ic(si);
4314 int max;
4315
4316 /* Get the max amount of data which can write as input in the channel. */
4317 max = channel_recv_max(chn);
4318 if (max > (len - l))
4319 max = len - l;
4320
4321 /* Copy data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004322 ci_putblk(chn, str + l, max);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004323
4324 /* update counters. */
4325 l += max;
4326 lua_pop(L, 1);
4327 lua_pushinteger(L, l);
4328
4329 /* If some data is not send, declares the situation to the
4330 * applet, and returns a yield.
4331 */
4332 if (l < len) {
Willy Tarreaudb398432018-11-15 11:08:52 +01004333 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004334 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004335 }
4336
4337 return 1;
4338}
4339
4340/* Just a wraper of "hlua_applet_send_yield". This wrapper permits
4341 * yield the LUA process, and resume it without checking the
4342 * input arguments.
4343 */
4344__LJMP static int hlua_applet_http_send(lua_State *L)
4345{
4346 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4347 size_t len;
4348 char hex[10];
4349
4350 MAY_LJMP(luaL_checklstring(L, 2, &len));
4351
4352 /* If transfer encoding chunked is selected, we surround the data
4353 * by chunk data.
4354 */
4355 if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_CHUNKED) {
4356 snprintf(hex, 9, "%x", (unsigned int)len);
4357 lua_pushfstring(L, "%s\r\n", hex);
4358 lua_insert(L, 2); /* swap the last 2 entries. */
4359 lua_pushstring(L, "\r\n");
4360 lua_concat(L, 3);
4361 }
4362
4363 /* This interger is used for followinf the amount of data sent. */
4364 lua_pushinteger(L, 0);
4365
4366 /* We want to send some data. Headers must be sent. */
4367 if (!(appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
4368 hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data.");
4369 WILL_LJMP(lua_error(L));
4370 }
4371
4372 return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0));
4373}
4374
4375__LJMP static int hlua_applet_http_addheader(lua_State *L)
4376{
4377 const char *name;
4378 int ret;
4379
4380 MAY_LJMP(hlua_checkapplet_http(L, 1));
4381 name = MAY_LJMP(luaL_checkstring(L, 2));
4382 MAY_LJMP(luaL_checkstring(L, 3));
4383
4384 /* Push in the stack the "response" entry. */
4385 ret = lua_getfield(L, 1, "response");
4386 if (ret != LUA_TTABLE) {
4387 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] "
4388 "is expected as an array. %s found", lua_typename(L, ret));
4389 WILL_LJMP(lua_error(L));
4390 }
4391
4392 /* check if the header is already registered if it is not
4393 * the case, register it.
4394 */
4395 ret = lua_getfield(L, -1, name);
4396 if (ret == LUA_TNIL) {
4397
4398 /* Entry not found. */
4399 lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */
4400
4401 /* Insert the new header name in the array in the top of the stack.
4402 * It left the new array in the top of the stack.
4403 */
4404 lua_newtable(L);
4405 lua_pushvalue(L, 2);
4406 lua_pushvalue(L, -2);
4407 lua_settable(L, -4);
4408
4409 } else if (ret != LUA_TTABLE) {
4410
4411 /* corruption error. */
4412 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] "
4413 "is expected as an array. %s found", name, lua_typename(L, ret));
4414 WILL_LJMP(lua_error(L));
4415 }
4416
4417 /* Now the top od thestack is an array of values. We push
4418 * the header value as new entry.
4419 */
4420 lua_pushvalue(L, 3);
4421 ret = lua_rawlen(L, -2);
4422 lua_rawseti(L, -2, ret + 1);
4423 lua_pushboolean(L, 1);
4424 return 1;
4425}
4426
4427__LJMP static int hlua_applet_http_status(lua_State *L)
4428{
4429 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4430 int status = MAY_LJMP(luaL_checkinteger(L, 2));
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004431 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004432
4433 if (status < 100 || status > 599) {
4434 lua_pushboolean(L, 0);
4435 return 1;
4436 }
4437
4438 appctx->appctx->ctx.hlua_apphttp.status = status;
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004439 appctx->appctx->ctx.hlua_apphttp.reason = reason;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004440 lua_pushboolean(L, 1);
4441 return 1;
4442}
4443
4444/* We will build the status line and the headers of the HTTP response.
4445 * We will try send at once if its not possible, we give back the hand
4446 * waiting for more room.
4447 */
4448__LJMP static int hlua_applet_http_start_response_yield(lua_State *L, int status, lua_KContext ctx)
4449{
4450 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4451 struct stream_interface *si = appctx->appctx->owner;
4452 struct channel *chn = si_ic(si);
4453 int ret;
4454 size_t len;
4455 const char *msg;
4456
4457 /* Get the message as the first argument on the stack. */
4458 msg = MAY_LJMP(luaL_checklstring(L, 2, &len));
4459
4460 /* Send the message at once. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004461 ret = ci_putblk(chn, msg, len);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004462
4463 /* if ret == -2 or -3 the channel closed or the message si too
4464 * big for the buffers.
4465 */
4466 if (ret == -2 || ret == -3) {
4467 hlua_pusherror(L, "Lua: 'start_response': response header block too big");
4468 WILL_LJMP(lua_error(L));
4469 }
4470
4471 /* If ret is -1, we dont have room in the buffer, so we yield. */
4472 if (ret == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01004473 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004474 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_start_response_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004475 }
4476
4477 /* Headers sent, set the flag. */
4478 appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT;
4479 return 0;
4480}
4481
4482__LJMP static int hlua_applet_http_start_response(lua_State *L)
4483{
Willy Tarreau83061a82018-07-13 11:56:34 +02004484 struct buffer *tmp = get_trash_chunk();
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004485 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004486 const char *name;
Willy Tarreaua3294632017-08-23 11:24:47 +02004487 size_t name_len;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004488 const char *value;
Willy Tarreaua3294632017-08-23 11:24:47 +02004489 size_t value_len;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004490 int id;
Willy Tarreauc9f4ea02017-08-23 09:32:06 +02004491 long long hdr_contentlength = -1;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004492 int hdr_chunked = 0;
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004493 const char *reason = appctx->appctx->ctx.hlua_apphttp.reason;
4494
4495 if (reason == NULL)
Willy Tarreau04f1e2d2018-09-10 18:04:24 +02004496 reason = http_get_reason(appctx->appctx->ctx.hlua_apphttp.status);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004497
4498 /* Use the same http version than the request. */
4499 chunk_appendf(tmp, "HTTP/1.%c %d %s\r\n",
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +01004500 appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11 ? '1' : '0',
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004501 appctx->appctx->ctx.hlua_apphttp.status,
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004502 reason);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004503
4504 /* Get the array associated to the field "response" in the object AppletHTTP. */
4505 lua_pushvalue(L, 0);
4506 if (lua_getfield(L, 1, "response") != LUA_TTABLE) {
4507 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n",
4508 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4509 WILL_LJMP(lua_error(L));
4510 }
4511
4512 /* Browse the list of headers. */
4513 lua_pushnil(L);
4514 while(lua_next(L, -2) != 0) {
4515
4516 /* We expect a string as -2. */
4517 if (lua_type(L, -2) != LUA_TSTRING) {
4518 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n",
4519 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4520 lua_typename(L, lua_type(L, -2)));
4521 WILL_LJMP(lua_error(L));
4522 }
Willy Tarreaua3294632017-08-23 11:24:47 +02004523 name = lua_tolstring(L, -2, &name_len);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004524
4525 /* We expect an array as -1. */
4526 if (lua_type(L, -1) != LUA_TTABLE) {
4527 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n",
4528 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4529 name,
4530 lua_typename(L, lua_type(L, -1)));
4531 WILL_LJMP(lua_error(L));
4532 }
4533
4534 /* Browse the table who is on the top of the stack. */
4535 lua_pushnil(L);
4536 while(lua_next(L, -2) != 0) {
4537
4538 /* We expect a number as -2. */
4539 if (lua_type(L, -2) != LUA_TNUMBER) {
4540 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n",
4541 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4542 name,
4543 lua_typename(L, lua_type(L, -2)));
4544 WILL_LJMP(lua_error(L));
4545 }
4546 id = lua_tointeger(L, -2);
4547
4548 /* We expect a string as -2. */
4549 if (lua_type(L, -1) != LUA_TSTRING) {
4550 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n",
4551 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4552 name, id,
4553 lua_typename(L, lua_type(L, -1)));
4554 WILL_LJMP(lua_error(L));
4555 }
Willy Tarreaua3294632017-08-23 11:24:47 +02004556 value = lua_tolstring(L, -1, &value_len);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004557
4558 /* Catenate a new header. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004559 if (tmp->data + name_len + 2 + value_len + 2 < tmp->size) {
4560 memcpy(tmp->area + tmp->data, name, name_len);
4561 tmp->data += name_len;
4562 tmp->area[tmp->data++] = ':';
4563 tmp->area[tmp->data++] = ' ';
Willy Tarreaua3294632017-08-23 11:24:47 +02004564
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004565 memcpy(tmp->area + tmp->data, value,
4566 value_len);
4567 tmp->data += value_len;
4568 tmp->area[tmp->data++] = '\r';
4569 tmp->area[tmp->data++] = '\n';
Willy Tarreaua3294632017-08-23 11:24:47 +02004570 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004571
4572 /* Protocol checks. */
4573
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004574 /* Copy the header content length. The length conversion
Willy Tarreauc9f4ea02017-08-23 09:32:06 +02004575 * is done without control. If it contains a bad value,
4576 * the content-length remains negative so that we can
4577 * switch to either chunked encoding or close.
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004578 */
Willy Tarreaua3294632017-08-23 11:24:47 +02004579 if (name_len == 14 && strcasecmp("content-length", name) == 0)
Willy Tarreauc9f4ea02017-08-23 09:32:06 +02004580 strl2llrc(value, strlen(value), &hdr_contentlength);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004581
4582 /* Check if the client annouces a transfer-encoding chunked it self. */
Willy Tarreaua3294632017-08-23 11:24:47 +02004583 if (name_len == 17 && value_len == 7 &&
4584 strcasecmp("transfer-encoding", name) == 0 &&
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004585 strcasecmp("chunked", value) == 0)
4586 hdr_chunked = 1;
4587
4588 /* Remove the array from the stack, and get next element with a remaining string. */
4589 lua_pop(L, 1);
4590 }
4591
4592 /* Remove the array from the stack, and get next element with a remaining string. */
4593 lua_pop(L, 1);
4594 }
4595
Willy Tarreau06c75fe2017-08-23 09:10:38 +02004596 /* If we dont have a content-length set, and the HTTP version is 1.1
4597 * and the status code implies the presence of a message body, we must
4598 * announce a transfer encoding chunked. This is required by haproxy
4599 * for the keepalive compliance. If the applet annouces a transfer-encoding
4600 * chunked itslef, don't do anything.
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004601 */
Willy Tarreauc9f4ea02017-08-23 09:32:06 +02004602 if (hdr_contentlength < 0 && hdr_chunked == 0 &&
Willy Tarreau06c75fe2017-08-23 09:10:38 +02004603 (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) &&
4604 appctx->appctx->ctx.hlua_apphttp.status >= 200 &&
4605 appctx->appctx->ctx.hlua_apphttp.status != 204 &&
4606 appctx->appctx->ctx.hlua_apphttp.status != 304) {
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004607 chunk_appendf(tmp, "Transfer-encoding: chunked\r\n");
4608 appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_CHUNKED;
4609 }
4610
4611 /* Finalize headers. */
4612 chunk_appendf(tmp, "\r\n");
4613
4614 /* Remove the last entry and the array of headers */
4615 lua_pop(L, 2);
4616
4617 /* Push the headers block. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004618 lua_pushlstring(L, tmp->area, tmp->data);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004619
4620 return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0));
4621}
4622
4623/*
4624 *
4625 *
4626 * Class HTTP
4627 *
4628 *
4629 */
4630
4631/* Returns a struct hlua_txn if the stack entry "ud" is
4632 * a class stream, otherwise it throws an error.
4633 */
4634__LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud)
4635{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02004636 return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004637}
4638
4639/* This function creates and push in the stack a HTTP object
4640 * according with a current TXN.
4641 */
4642static int hlua_http_new(lua_State *L, struct hlua_txn *txn)
4643{
4644 struct hlua_txn *htxn;
4645
4646 /* Check stack size. */
4647 if (!lua_checkstack(L, 3))
4648 return 0;
4649
4650 /* Create the object: obj[0] = userdata.
4651 * Note that the base of the Converters object is the
4652 * same than the TXN object.
4653 */
4654 lua_newtable(L);
4655 htxn = lua_newuserdata(L, sizeof(*htxn));
4656 lua_rawseti(L, -2, 0);
4657
4658 htxn->s = txn->s;
4659 htxn->p = txn->p;
4660
4661 /* Pop a class stream metatable and affect it to the table. */
4662 lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref);
4663 lua_setmetatable(L, -2);
4664
4665 return 1;
4666}
4667
4668/* This function creates ans returns an array of HTTP headers.
4669 * This function does not fails. It is used as wrapper with the
4670 * 2 following functions.
4671 */
4672__LJMP static int hlua_http_get_headers(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg)
4673{
4674 const char *cur_ptr, *cur_next, *p;
4675 int old_idx, cur_idx;
4676 struct hdr_idx_elem *cur_hdr;
4677 const char *hn, *hv;
4678 int hnl, hvl;
4679 int type;
4680 const char *in;
4681 char *out;
4682 int len;
4683
4684 /* Create the table. */
4685 lua_newtable(L);
4686
4687 if (!htxn->s->txn)
4688 return 1;
4689
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004690 /* Check if a valid response is parsed */
4691 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4692 return 1;
4693
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004694 /* Build array of headers. */
4695 old_idx = 0;
Willy Tarreaua79021a2018-06-15 18:07:57 +02004696 cur_next = ci_head(msg->chn) + hdr_idx_first_pos(&htxn->s->txn->hdr_idx);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004697
4698 while (1) {
4699 cur_idx = htxn->s->txn->hdr_idx.v[old_idx].next;
4700 if (!cur_idx)
4701 break;
4702 old_idx = cur_idx;
4703
4704 cur_hdr = &htxn->s->txn->hdr_idx.v[cur_idx];
4705 cur_ptr = cur_next;
4706 cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1;
4707
4708 /* Now we have one full header at cur_ptr of len cur_hdr->len,
4709 * and the next header starts at cur_next. We'll check
4710 * this header in the list as well as against the default
4711 * rule.
4712 */
4713
4714 /* look for ': *'. */
4715 hn = cur_ptr;
4716 for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++);
4717 if (p >= cur_ptr+cur_hdr->len)
4718 continue;
4719 hnl = p - hn;
4720 p++;
4721 while (p < cur_ptr+cur_hdr->len && ( *p == ' ' || *p == '\t' ))
4722 p++;
4723 if (p >= cur_ptr+cur_hdr->len)
4724 continue;
4725 hv = p;
4726 hvl = cur_ptr+cur_hdr->len-p;
4727
4728 /* Lowercase the key. Don't check the size of trash, it have
4729 * the size of one buffer and the input data contains in one
4730 * buffer.
4731 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004732 out = trash.area;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004733 for (in=hn; in<hn+hnl; in++, out++)
4734 *out = tolower(*in);
4735 *out = '\0';
4736
4737 /* Check for existing entry:
4738 * assume that the table is on the top of the stack, and
4739 * push the key in the stack, the function lua_gettable()
4740 * perform the lookup.
4741 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004742 lua_pushlstring(L, trash.area, hnl);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004743 lua_gettable(L, -2);
4744 type = lua_type(L, -1);
4745
4746 switch (type) {
4747 case LUA_TNIL:
4748 /* Table not found, create it. */
4749 lua_pop(L, 1); /* remove the nil value. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004750 lua_pushlstring(L, trash.area, hnl); /* push the header name as key. */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004751 lua_newtable(L); /* create and push empty table. */
4752 lua_pushlstring(L, hv, hvl); /* push header value. */
4753 lua_rawseti(L, -2, 0); /* index header value (pop it). */
4754 lua_rawset(L, -3); /* index new table with header name (pop the values). */
4755 break;
4756
4757 case LUA_TTABLE:
4758 /* Entry found: push the value in the table. */
4759 len = lua_rawlen(L, -1);
4760 lua_pushlstring(L, hv, hvl); /* push header value. */
4761 lua_rawseti(L, -2, len+1); /* index header value (pop it). */
4762 lua_pop(L, 1); /* remove the table (it is stored in the main table). */
4763 break;
4764
4765 default:
4766 /* Other cases are errors. */
4767 hlua_pusherror(L, "internal error during the parsing of headers.");
4768 WILL_LJMP(lua_error(L));
4769 }
4770 }
4771
4772 return 1;
4773}
4774
4775__LJMP static int hlua_http_req_get_headers(lua_State *L)
4776{
4777 struct hlua_txn *htxn;
4778
4779 MAY_LJMP(check_args(L, 1, "req_get_headers"));
4780 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4781
4782 return hlua_http_get_headers(L, htxn, &htxn->s->txn->req);
4783}
4784
4785__LJMP static int hlua_http_res_get_headers(lua_State *L)
4786{
4787 struct hlua_txn *htxn;
4788
4789 MAY_LJMP(check_args(L, 1, "res_get_headers"));
4790 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4791
4792 return hlua_http_get_headers(L, htxn, &htxn->s->txn->rsp);
4793}
4794
4795/* This function replace full header, or just a value in
4796 * the request or in the response. It is a wrapper fir the
4797 * 4 following functions.
4798 */
4799__LJMP static inline int hlua_http_rep_hdr(lua_State *L, struct hlua_txn *htxn,
4800 struct http_msg *msg, int action)
4801{
4802 size_t name_len;
4803 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
4804 const char *reg = MAY_LJMP(luaL_checkstring(L, 3));
4805 const char *value = MAY_LJMP(luaL_checkstring(L, 4));
4806 struct my_regex re;
4807
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004808 /* Check if a valid response is parsed */
4809 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4810 return 0;
4811
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004812 if (!regex_comp(reg, &re, 1, 1, NULL))
4813 WILL_LJMP(luaL_argerror(L, 3, "invalid regex"));
4814
4815 http_transform_header_str(htxn->s, msg, name, name_len, value, &re, action);
4816 regex_free(&re);
4817 return 0;
4818}
4819
4820__LJMP static int hlua_http_req_rep_hdr(lua_State *L)
4821{
4822 struct hlua_txn *htxn;
4823
4824 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
4825 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4826
4827 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_HDR));
4828}
4829
4830__LJMP static int hlua_http_res_rep_hdr(lua_State *L)
4831{
4832 struct hlua_txn *htxn;
4833
4834 MAY_LJMP(check_args(L, 4, "res_rep_hdr"));
4835 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4836
4837 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->rsp, ACT_HTTP_REPLACE_HDR));
4838}
4839
4840__LJMP static int hlua_http_req_rep_val(lua_State *L)
4841{
4842 struct hlua_txn *htxn;
4843
4844 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
4845 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4846
4847 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_VAL));
4848}
4849
4850__LJMP static int hlua_http_res_rep_val(lua_State *L)
4851{
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004852 struct hlua_txn *htxn;
4853
4854 MAY_LJMP(check_args(L, 4, "res_rep_val"));
4855 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4856
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02004857 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->rsp, ACT_HTTP_REPLACE_VAL));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004858}
4859
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004860/* This function deletes all the occurrences of an header.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004861 * It is a wrapper for the 2 following functions.
4862 */
4863__LJMP static inline int hlua_http_del_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg)
4864{
4865 size_t len;
4866 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4867 struct hdr_ctx ctx;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004868 struct http_txn *txn = htxn->s->txn;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004869
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004870 /* Check if a valid response is parsed */
4871 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4872 return 0;
4873
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004874 ctx.idx = 0;
Willy Tarreaua79021a2018-06-15 18:07:57 +02004875 while (http_find_header2(name, len, ci_head(msg->chn), &txn->hdr_idx, &ctx))
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004876 http_remove_header2(msg, &txn->hdr_idx, &ctx);
4877 return 0;
4878}
4879
4880__LJMP static int hlua_http_req_del_hdr(lua_State *L)
4881{
4882 struct hlua_txn *htxn;
4883
4884 MAY_LJMP(check_args(L, 2, "req_del_hdr"));
4885 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4886
Willy Tarreaueee5b512015-04-03 23:46:31 +02004887 return hlua_http_del_hdr(L, htxn, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004888}
4889
4890__LJMP static int hlua_http_res_del_hdr(lua_State *L)
4891{
4892 struct hlua_txn *htxn;
4893
4894 MAY_LJMP(check_args(L, 2, "req_del_hdr"));
4895 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4896
Willy Tarreaueee5b512015-04-03 23:46:31 +02004897 return hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004898}
4899
4900/* This function adds an header. It is a wrapper used by
4901 * the 2 following functions.
4902 */
4903__LJMP static inline int hlua_http_add_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg)
4904{
4905 size_t name_len;
4906 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
4907 size_t value_len;
4908 const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len));
4909 char *p;
4910
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004911 /* Check if a valid message is parsed */
4912 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4913 return 0;
4914
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004915 /* Check length. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004916 trash.data = value_len + name_len + 2;
4917 if (trash.data > trash.size)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004918 return 0;
4919
4920 /* Creates the header string. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004921 p = trash.area;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004922 memcpy(p, name, name_len);
4923 p += name_len;
4924 *p = ':';
4925 p++;
4926 *p = ' ';
4927 p++;
4928 memcpy(p, value, value_len);
4929
Willy Tarreaueee5b512015-04-03 23:46:31 +02004930 lua_pushboolean(L, http_header_add_tail2(msg, &htxn->s->txn->hdr_idx,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004931 trash.area, trash.data) != 0);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004932
4933 return 0;
4934}
4935
4936__LJMP static int hlua_http_req_add_hdr(lua_State *L)
4937{
4938 struct hlua_txn *htxn;
4939
4940 MAY_LJMP(check_args(L, 3, "req_add_hdr"));
4941 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4942
Willy Tarreaueee5b512015-04-03 23:46:31 +02004943 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004944}
4945
4946__LJMP static int hlua_http_res_add_hdr(lua_State *L)
4947{
4948 struct hlua_txn *htxn;
4949
4950 MAY_LJMP(check_args(L, 3, "res_add_hdr"));
4951 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4952
Willy Tarreaueee5b512015-04-03 23:46:31 +02004953 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004954}
4955
4956static int hlua_http_req_set_hdr(lua_State *L)
4957{
4958 struct hlua_txn *htxn;
4959
4960 MAY_LJMP(check_args(L, 3, "req_set_hdr"));
4961 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4962
Willy Tarreaueee5b512015-04-03 23:46:31 +02004963 hlua_http_del_hdr(L, htxn, &htxn->s->txn->req);
4964 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004965}
4966
4967static int hlua_http_res_set_hdr(lua_State *L)
4968{
4969 struct hlua_txn *htxn;
4970
4971 MAY_LJMP(check_args(L, 3, "res_set_hdr"));
4972 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4973
Willy Tarreaueee5b512015-04-03 23:46:31 +02004974 hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp);
4975 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004976}
4977
4978/* This function set the method. */
4979static int hlua_http_req_set_meth(lua_State *L)
4980{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02004981 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004982 size_t name_len;
4983 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004984
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004985 /* Check if a valid request is parsed */
4986 if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) {
4987 lua_pushboolean(L, 0);
4988 return 1;
4989 }
4990
Willy Tarreau987e3fb2015-04-04 01:09:08 +02004991 lua_pushboolean(L, http_replace_req_line(0, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004992 return 1;
4993}
4994
4995/* This function set the method. */
4996static int hlua_http_req_set_path(lua_State *L)
4997{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02004998 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004999 size_t name_len;
5000 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02005001
5002 /* Check if a valid request is parsed */
5003 if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) {
5004 lua_pushboolean(L, 0);
5005 return 1;
5006 }
5007
Willy Tarreau987e3fb2015-04-04 01:09:08 +02005008 lua_pushboolean(L, http_replace_req_line(1, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005009 return 1;
5010}
5011
5012/* This function set the query-string. */
5013static int hlua_http_req_set_query(lua_State *L)
5014{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005015 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005016 size_t name_len;
5017 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005018
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02005019 /* Check if a valid request is parsed */
5020 if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) {
5021 lua_pushboolean(L, 0);
5022 return 1;
5023 }
5024
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005025 /* Check length. */
5026 if (name_len > trash.size - 1) {
5027 lua_pushboolean(L, 0);
5028 return 1;
5029 }
5030
5031 /* Add the mark question as prefix. */
5032 chunk_reset(&trash);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005033 trash.area[trash.data++] = '?';
5034 memcpy(trash.area + trash.data, name, name_len);
5035 trash.data += name_len;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005036
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005037 lua_pushboolean(L,
5038 http_replace_req_line(2, trash.area, trash.data, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005039 return 1;
5040}
5041
5042/* This function set the uri. */
5043static int hlua_http_req_set_uri(lua_State *L)
5044{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005045 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005046 size_t name_len;
5047 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005048
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02005049 /* Check if a valid request is parsed */
5050 if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) {
5051 lua_pushboolean(L, 0);
5052 return 1;
5053 }
5054
Willy Tarreau987e3fb2015-04-04 01:09:08 +02005055 lua_pushboolean(L, http_replace_req_line(3, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005056 return 1;
5057}
5058
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005059/* This function set the response code & optionally reason. */
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005060static int hlua_http_res_set_status(lua_State *L)
5061{
5062 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5063 unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2));
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005064 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005065
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02005066 /* Check if a valid response is parsed */
5067 if (unlikely(htxn->s->txn->rsp.msg_state < HTTP_MSG_BODY))
5068 return 0;
5069
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005070 http_set_status(code, reason, htxn->s);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005071 return 0;
5072}
5073
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005074/*
5075 *
5076 *
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005077 * Class TXN
5078 *
5079 *
5080 */
5081
5082/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02005083 * a class stream, otherwise it throws an error.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005084 */
5085__LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud)
5086{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02005087 return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref));
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005088}
5089
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005090__LJMP static int hlua_set_var(lua_State *L)
5091{
5092 struct hlua_txn *htxn;
5093 const char *name;
5094 size_t len;
5095 struct sample smp;
5096
5097 MAY_LJMP(check_args(L, 3, "set_var"));
5098
5099 /* It is useles to retrieve the stream, but this function
5100 * runs only in a stream context.
5101 */
5102 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5103 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5104
5105 /* Converts the third argument in a sample. */
5106 hlua_lua2smp(L, 3, &smp);
5107
5108 /* Store the sample in a variable. */
Willy Tarreau7560dd42016-03-10 16:28:58 +01005109 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Willy Tarreau6204cd92016-03-10 16:33:04 +01005110 vars_set_by_name(name, len, &smp);
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005111 return 0;
5112}
5113
Christopher Faulet85d79c92016-11-09 16:54:56 +01005114__LJMP static int hlua_unset_var(lua_State *L)
5115{
5116 struct hlua_txn *htxn;
5117 const char *name;
5118 size_t len;
5119 struct sample smp;
5120
5121 MAY_LJMP(check_args(L, 2, "unset_var"));
5122
5123 /* It is useles to retrieve the stream, but this function
5124 * runs only in a stream context.
5125 */
5126 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5127 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5128
5129 /* Unset the variable. */
5130 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
5131 vars_unset_by_name(name, len, &smp);
5132 return 0;
5133}
5134
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005135__LJMP static int hlua_get_var(lua_State *L)
5136{
5137 struct hlua_txn *htxn;
5138 const char *name;
5139 size_t len;
5140 struct sample smp;
5141
5142 MAY_LJMP(check_args(L, 2, "get_var"));
5143
5144 /* It is useles to retrieve the stream, but this function
5145 * runs only in a stream context.
5146 */
5147 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5148 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5149
Willy Tarreau7560dd42016-03-10 16:28:58 +01005150 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Willy Tarreau6204cd92016-03-10 16:33:04 +01005151 if (!vars_get_by_name(name, len, &smp)) {
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005152 lua_pushnil(L);
5153 return 1;
5154 }
5155
5156 return hlua_smp2lua(L, &smp);
5157}
5158
Willy Tarreau59551662015-03-10 14:23:13 +01005159__LJMP static int hlua_set_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005160{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005161 struct hlua *hlua;
5162
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005163 MAY_LJMP(check_args(L, 2, "set_priv"));
5164
Willy Tarreau87b09662015-04-03 00:22:06 +02005165 /* It is useles to retrieve the stream, but this function
5166 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005167 */
5168 MAY_LJMP(hlua_checktxn(L, 1));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005169 hlua = hlua_gethlua(L);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005170
5171 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02005172 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005173
5174 /* Get and store new value. */
5175 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
5176 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
5177
5178 return 0;
5179}
5180
Willy Tarreau59551662015-03-10 14:23:13 +01005181__LJMP static int hlua_get_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005182{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005183 struct hlua *hlua;
5184
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005185 MAY_LJMP(check_args(L, 1, "get_priv"));
5186
Willy Tarreau87b09662015-04-03 00:22:06 +02005187 /* It is useles to retrieve the stream, but this function
5188 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005189 */
5190 MAY_LJMP(hlua_checktxn(L, 1));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005191 hlua = hlua_gethlua(L);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005192
5193 /* Push configuration index in the stack. */
5194 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
5195
5196 return 1;
5197}
5198
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005199/* Create stack entry containing a class TXN. This function
5200 * return 0 if the stack does not contains free slots,
5201 * otherwise it returns 1.
5202 */
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005203static int hlua_txn_new(lua_State *L, struct stream *s, struct proxy *p, int dir, int flags)
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005204{
Willy Tarreaude491382015-04-06 11:04:28 +02005205 struct hlua_txn *htxn;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005206
5207 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005208 if (!lua_checkstack(L, 3))
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005209 return 0;
5210
5211 /* NOTE: The allocation never fails. The failure
5212 * throw an error, and the function never returns.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08005213 * if the throw is not available, the process is aborted.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005214 */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005215 /* Create the object: obj[0] = userdata. */
5216 lua_newtable(L);
Willy Tarreaude491382015-04-06 11:04:28 +02005217 htxn = lua_newuserdata(L, sizeof(*htxn));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005218 lua_rawseti(L, -2, 0);
5219
Willy Tarreaude491382015-04-06 11:04:28 +02005220 htxn->s = s;
5221 htxn->p = p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01005222 htxn->dir = dir;
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005223 htxn->flags = flags;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005224
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005225 /* Create the "f" field that contains a list of fetches. */
5226 lua_pushstring(L, "f");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005227 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005228 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005229 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005230
5231 /* Create the "sf" field that contains a list of stringsafe fetches. */
5232 lua_pushstring(L, "sf");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005233 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP | HLUA_F_AS_STRING))
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005234 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005235 lua_rawset(L, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005236
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005237 /* Create the "c" field that contains a list of converters. */
5238 lua_pushstring(L, "c");
Willy Tarreaude491382015-04-06 11:04:28 +02005239 if (!hlua_converters_new(L, htxn, 0))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005240 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005241 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005242
5243 /* Create the "sc" field that contains a list of stringsafe converters. */
5244 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01005245 if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING))
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005246 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005247 lua_rawset(L, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005248
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005249 /* Create the "req" field that contains the request channel object. */
5250 lua_pushstring(L, "req");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005251 if (!hlua_channel_new(L, &s->req))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005252 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005253 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005254
5255 /* Create the "res" field that contains the response channel object. */
5256 lua_pushstring(L, "res");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005257 if (!hlua_channel_new(L, &s->res))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005258 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005259 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005260
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005261 /* Creates the HTTP object is the current proxy allows http. */
5262 lua_pushstring(L, "http");
5263 if (p->mode == PR_MODE_HTTP) {
Willy Tarreaude491382015-04-06 11:04:28 +02005264 if (!hlua_http_new(L, htxn))
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005265 return 0;
5266 }
5267 else
5268 lua_pushnil(L);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005269 lua_rawset(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005270
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005271 /* Pop a class sesison metatable and affect it to the userdata. */
5272 lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref);
5273 lua_setmetatable(L, -2);
5274
5275 return 1;
5276}
5277
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005278__LJMP static int hlua_txn_deflog(lua_State *L)
5279{
5280 const char *msg;
5281 struct hlua_txn *htxn;
5282
5283 MAY_LJMP(check_args(L, 2, "deflog"));
5284 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5285 msg = MAY_LJMP(luaL_checkstring(L, 2));
5286
5287 hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg);
5288 return 0;
5289}
5290
5291__LJMP static int hlua_txn_log(lua_State *L)
5292{
5293 int level;
5294 const char *msg;
5295 struct hlua_txn *htxn;
5296
5297 MAY_LJMP(check_args(L, 3, "log"));
5298 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5299 level = MAY_LJMP(luaL_checkinteger(L, 2));
5300 msg = MAY_LJMP(luaL_checkstring(L, 3));
5301
5302 if (level < 0 || level >= NB_LOG_LEVELS)
5303 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
5304
5305 hlua_sendlog(htxn->s->be, level, msg);
5306 return 0;
5307}
5308
5309__LJMP static int hlua_txn_log_debug(lua_State *L)
5310{
5311 const char *msg;
5312 struct hlua_txn *htxn;
5313
5314 MAY_LJMP(check_args(L, 2, "Debug"));
5315 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5316 msg = MAY_LJMP(luaL_checkstring(L, 2));
5317 hlua_sendlog(htxn->s->be, LOG_DEBUG, msg);
5318 return 0;
5319}
5320
5321__LJMP static int hlua_txn_log_info(lua_State *L)
5322{
5323 const char *msg;
5324 struct hlua_txn *htxn;
5325
5326 MAY_LJMP(check_args(L, 2, "Info"));
5327 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5328 msg = MAY_LJMP(luaL_checkstring(L, 2));
5329 hlua_sendlog(htxn->s->be, LOG_INFO, msg);
5330 return 0;
5331}
5332
5333__LJMP static int hlua_txn_log_warning(lua_State *L)
5334{
5335 const char *msg;
5336 struct hlua_txn *htxn;
5337
5338 MAY_LJMP(check_args(L, 2, "Warning"));
5339 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5340 msg = MAY_LJMP(luaL_checkstring(L, 2));
5341 hlua_sendlog(htxn->s->be, LOG_WARNING, msg);
5342 return 0;
5343}
5344
5345__LJMP static int hlua_txn_log_alert(lua_State *L)
5346{
5347 const char *msg;
5348 struct hlua_txn *htxn;
5349
5350 MAY_LJMP(check_args(L, 2, "Alert"));
5351 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5352 msg = MAY_LJMP(luaL_checkstring(L, 2));
5353 hlua_sendlog(htxn->s->be, LOG_ALERT, msg);
5354 return 0;
5355}
5356
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005357__LJMP static int hlua_txn_set_loglevel(lua_State *L)
5358{
5359 struct hlua_txn *htxn;
5360 int ll;
5361
5362 MAY_LJMP(check_args(L, 2, "set_loglevel"));
5363 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5364 ll = MAY_LJMP(luaL_checkinteger(L, 2));
5365
5366 if (ll < 0 || ll > 7)
5367 WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7"));
5368
5369 htxn->s->logs.level = ll;
5370 return 0;
5371}
5372
5373__LJMP static int hlua_txn_set_tos(lua_State *L)
5374{
5375 struct hlua_txn *htxn;
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005376 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 Tarreau1a18b542018-12-11 16:37:42 +01005382 conn_set_tos(objt_conn(htxn->s->sess->origin), tos);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005383 return 0;
5384}
5385
5386__LJMP static int hlua_txn_set_mark(lua_State *L)
5387{
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005388 struct hlua_txn *htxn;
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005389 int mark;
5390
5391 MAY_LJMP(check_args(L, 2, "set_mark"));
5392 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5393 mark = MAY_LJMP(luaL_checkinteger(L, 2));
5394
Willy Tarreau1a18b542018-12-11 16:37:42 +01005395 conn_set_tos(objt_conn(htxn->s->sess->origin), mark);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005396 return 0;
5397}
5398
Patrick Hemmer268a7072018-05-11 12:52:31 -04005399__LJMP static int hlua_txn_set_priority_class(lua_State *L)
5400{
5401 struct hlua_txn *htxn;
5402
5403 MAY_LJMP(check_args(L, 2, "set_priority_class"));
5404 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5405 htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2)));
5406 return 0;
5407}
5408
5409__LJMP static int hlua_txn_set_priority_offset(lua_State *L)
5410{
5411 struct hlua_txn *htxn;
5412
5413 MAY_LJMP(check_args(L, 2, "set_priority_offset"));
5414 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5415 htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2)));
5416 return 0;
5417}
5418
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005419/* This function is an Lua binding that send pending data
5420 * to the client, and close the stream interface.
5421 */
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005422__LJMP static int hlua_txn_done(lua_State *L)
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005423{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005424 struct hlua_txn *htxn;
Thierry FOURNIER9bd52d42016-07-14 11:45:33 +02005425 struct hlua *hlua;
Willy Tarreau81389672015-03-10 12:03:52 +01005426 struct channel *ic, *oc;
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005427
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005428 MAY_LJMP(check_args(L, 1, "close"));
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005429 htxn = MAY_LJMP(hlua_checktxn(L, 1));
Thierry FOURNIER9bd52d42016-07-14 11:45:33 +02005430 hlua = hlua_gethlua(L);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005431
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005432 /* If the flags NOTERM is set, we cannot terminate the http
5433 * session, so we just end the execution of the current
5434 * lua code.
5435 */
5436 if (htxn->flags & HLUA_TXN_NOTERM) {
5437 WILL_LJMP(hlua_done(L));
5438 return 0;
5439 }
5440
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005441 ic = &htxn->s->req;
5442 oc = &htxn->s->res;
Willy Tarreau81389672015-03-10 12:03:52 +01005443
Willy Tarreau630ef452015-08-28 10:06:15 +02005444 if (htxn->s->txn) {
5445 /* HTTP mode, let's stay in sync with the stream */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005446 b_del(&ic->buf, htxn->s->txn->req.sov);
Willy Tarreau630ef452015-08-28 10:06:15 +02005447 htxn->s->txn->req.next -= htxn->s->txn->req.sov;
5448 htxn->s->txn->req.sov = 0;
5449 ic->analysers &= AN_REQ_HTTP_XFER_BODY;
5450 oc->analysers = AN_RES_HTTP_XFER_BODY;
5451 htxn->s->txn->req.msg_state = HTTP_MSG_CLOSED;
5452 htxn->s->txn->rsp.msg_state = HTTP_MSG_DONE;
5453
Willy Tarreau630ef452015-08-28 10:06:15 +02005454 /* Note that if we want to support keep-alive, we need
5455 * to bypass the close/shutr_now calls below, but that
5456 * may only be done if the HTTP request was already
5457 * processed and the connection header is known (ie
5458 * not during TCP rules).
5459 */
5460 }
5461
Thierry FOURNIER10ec2142015-08-24 17:23:45 +02005462 channel_auto_read(ic);
Willy Tarreau81389672015-03-10 12:03:52 +01005463 channel_abort(ic);
5464 channel_auto_close(ic);
5465 channel_erase(ic);
Thierry FOURNIER10ec2142015-08-24 17:23:45 +02005466
5467 oc->wex = tick_add_ifset(now_ms, oc->wto);
Willy Tarreau81389672015-03-10 12:03:52 +01005468 channel_auto_read(oc);
5469 channel_auto_close(oc);
5470 channel_shutr_now(oc);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005471
Willy Tarreau0458b082015-08-28 09:40:04 +02005472 ic->analysers = 0;
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005473
Thierry FOURNIER9bd52d42016-07-14 11:45:33 +02005474 hlua->flags |= HLUA_STOP;
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005475 WILL_LJMP(hlua_done(L));
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005476 return 0;
5477}
5478
5479__LJMP static int hlua_log(lua_State *L)
5480{
5481 int level;
5482 const char *msg;
5483
5484 MAY_LJMP(check_args(L, 2, "log"));
5485 level = MAY_LJMP(luaL_checkinteger(L, 1));
5486 msg = MAY_LJMP(luaL_checkstring(L, 2));
5487
5488 if (level < 0 || level >= NB_LOG_LEVELS)
5489 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
5490
5491 hlua_sendlog(NULL, level, msg);
5492 return 0;
5493}
5494
5495__LJMP static int hlua_log_debug(lua_State *L)
5496{
5497 const char *msg;
5498
5499 MAY_LJMP(check_args(L, 1, "debug"));
5500 msg = MAY_LJMP(luaL_checkstring(L, 1));
5501 hlua_sendlog(NULL, LOG_DEBUG, msg);
5502 return 0;
5503}
5504
5505__LJMP static int hlua_log_info(lua_State *L)
5506{
5507 const char *msg;
5508
5509 MAY_LJMP(check_args(L, 1, "info"));
5510 msg = MAY_LJMP(luaL_checkstring(L, 1));
5511 hlua_sendlog(NULL, LOG_INFO, msg);
5512 return 0;
5513}
5514
5515__LJMP static int hlua_log_warning(lua_State *L)
5516{
5517 const char *msg;
5518
5519 MAY_LJMP(check_args(L, 1, "warning"));
5520 msg = MAY_LJMP(luaL_checkstring(L, 1));
5521 hlua_sendlog(NULL, LOG_WARNING, msg);
5522 return 0;
5523}
5524
5525__LJMP static int hlua_log_alert(lua_State *L)
5526{
5527 const char *msg;
5528
5529 MAY_LJMP(check_args(L, 1, "alert"));
5530 msg = MAY_LJMP(luaL_checkstring(L, 1));
5531 hlua_sendlog(NULL, LOG_ALERT, msg);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005532 return 0;
5533}
5534
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005535__LJMP static int hlua_sleep_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005536{
5537 int wakeup_ms = lua_tointeger(L, -1);
5538 if (now_ms < wakeup_ms)
Willy Tarreau9635e032018-10-16 17:52:55 +02005539 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005540 return 0;
5541}
5542
5543__LJMP static int hlua_sleep(lua_State *L)
5544{
5545 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005546 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005547
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005548 MAY_LJMP(check_args(L, 1, "sleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005549
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005550 delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005551 wakeup_ms = tick_add(now_ms, delay);
5552 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005553
Willy Tarreau9635e032018-10-16 17:52:55 +02005554 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005555 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005556}
5557
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005558__LJMP static int hlua_msleep(lua_State *L)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005559{
5560 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005561 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005562
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005563 MAY_LJMP(check_args(L, 1, "msleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005564
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005565 delay = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005566 wakeup_ms = tick_add(now_ms, delay);
5567 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005568
Willy Tarreau9635e032018-10-16 17:52:55 +02005569 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005570 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005571}
5572
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01005573/* This functionis an LUA binding. it permits to give back
5574 * the hand at the HAProxy scheduler. It is used when the
5575 * LUA processing consumes a lot of time.
5576 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005577__LJMP static int hlua_yield_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005578{
5579 return 0;
5580}
5581
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01005582__LJMP static int hlua_yield(lua_State *L)
5583{
Willy Tarreau9635e032018-10-16 17:52:55 +02005584 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_yield_yield, TICK_ETERNITY, HLUA_CTRLYIELD));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005585 return 0;
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01005586}
5587
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005588/* This function change the nice of the currently executed
5589 * task. It is used set low or high priority at the current
5590 * task.
5591 */
Willy Tarreau59551662015-03-10 14:23:13 +01005592__LJMP static int hlua_set_nice(lua_State *L)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005593{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005594 struct hlua *hlua;
5595 int nice;
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005596
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005597 MAY_LJMP(check_args(L, 1, "set_nice"));
5598 hlua = hlua_gethlua(L);
5599 nice = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005600
5601 /* If he task is not set, I'm in a start mode. */
5602 if (!hlua || !hlua->task)
5603 return 0;
5604
5605 if (nice < -1024)
5606 nice = -1024;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005607 else if (nice > 1024)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005608 nice = 1024;
5609
5610 hlua->task->nice = nice;
5611 return 0;
5612}
5613
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08005614/* This function is used as a callback of a task. It is called by the
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005615 * HAProxy task subsystem when the task is awaked. The LUA runtime can
5616 * return an E_AGAIN signal, the emmiter of this signal must set a
5617 * signal to wake the task.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005618 *
5619 * Task wrapper are longjmp safe because the only one Lua code
5620 * executed is the safe hlua_ctx_resume();
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005621 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02005622static struct task *hlua_process_task(struct task *task, void *context, unsigned short state)
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005623{
Olivier Houchard9f6af332018-05-25 14:04:04 +02005624 struct hlua *hlua = context;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005625 enum hlua_exec status;
5626
Christopher Faulet5bc99722018-04-25 10:34:45 +02005627 if (task->thread_mask == MAX_THREADS_MASK)
5628 task_set_affinity(task, tid_bit);
5629
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005630 /* If it is the first call to the task, we must initialize the
5631 * execution timeouts.
5632 */
5633 if (!HLUA_IS_RUNNING(hlua))
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02005634 hlua->max_time = hlua_timeout_task;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005635
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005636 /* Execute the Lua code. */
5637 status = hlua_ctx_resume(hlua, 1);
5638
5639 switch (status) {
5640 /* finished or yield */
5641 case HLUA_E_OK:
5642 hlua_ctx_destroy(hlua);
5643 task_delete(task);
5644 task_free(task);
Tim Duesterhuscd235c62018-04-24 13:56:01 +02005645 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005646 break;
5647
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01005648 case HLUA_E_AGAIN: /* co process or timeout wake me later. */
Thierry FOURNIERcb146882017-12-10 17:10:57 +01005649 notification_gc(&hlua->com);
PiBa-NLfe971b32018-05-02 22:27:14 +02005650 task->expire = hlua->wake_time;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005651 break;
5652
5653 /* finished with error. */
5654 case HLUA_E_ERRMSG:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005655 SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005656 hlua_ctx_destroy(hlua);
5657 task_delete(task);
5658 task_free(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02005659 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005660 break;
5661
5662 case HLUA_E_ERR:
5663 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005664 SEND_ERR(NULL, "Lua task: unknown error.\n");
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005665 hlua_ctx_destroy(hlua);
5666 task_delete(task);
5667 task_free(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02005668 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005669 break;
5670 }
Emeric Brun253e53e2017-10-17 18:58:40 +02005671 return task;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005672}
5673
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01005674/* This function is an LUA binding that register LUA function to be
5675 * executed after the HAProxy configuration parsing and before the
5676 * HAProxy scheduler starts. This function expect only one LUA
5677 * argument that is a function. This function returns nothing, but
5678 * throws if an error is encountered.
5679 */
5680__LJMP static int hlua_register_init(lua_State *L)
5681{
5682 struct hlua_init_function *init;
5683 int ref;
5684
5685 MAY_LJMP(check_args(L, 1, "register_init"));
5686
5687 ref = MAY_LJMP(hlua_checkfunction(L, 1));
5688
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02005689 init = calloc(1, sizeof(*init));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01005690 if (!init)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01005691 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01005692
5693 init->function_ref = ref;
5694 LIST_ADDQ(&hlua_init_functions, &init->l);
5695 return 0;
5696}
5697
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005698/* This functio is an LUA binding. It permits to register a task
5699 * executed in parallel of the main HAroxy activity. The task is
5700 * created and it is set in the HAProxy scheduler. It can be called
5701 * from the "init" section, "post init" or during the runtime.
5702 *
5703 * Lua prototype:
5704 *
5705 * <none> core.register_task(<function>)
5706 */
5707static int hlua_register_task(lua_State *L)
5708{
5709 struct hlua *hlua;
5710 struct task *task;
5711 int ref;
5712
5713 MAY_LJMP(check_args(L, 1, "register_task"));
5714
5715 ref = MAY_LJMP(hlua_checkfunction(L, 1));
5716
Willy Tarreaubafbe012017-11-24 17:34:44 +01005717 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005718 if (!hlua)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01005719 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005720
Emeric Brunc60def82017-09-27 14:59:38 +02005721 task = task_new(MAX_THREADS_MASK);
Willy Tarreaue09101e2018-10-16 17:37:12 +02005722 if (!task)
5723 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
5724
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005725 task->context = hlua;
5726 task->process = hlua_process_task;
5727
5728 if (!hlua_ctx_init(hlua, task))
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01005729 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005730
5731 /* Restore the function in the stack. */
5732 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref);
5733 hlua->nargs = 0;
5734
5735 /* Schedule task. */
5736 task_schedule(task, now_ms);
5737
5738 return 0;
5739}
5740
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005741/* Wrapper called by HAProxy to execute an LUA converter. This wrapper
5742 * doesn't allow "yield" functions because the HAProxy engine cannot
5743 * resume converters.
5744 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02005745static int hlua_sample_conv_wrapper(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005746{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02005747 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02005748 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01005749 const char *error;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005750
Willy Tarreaube508f12016-03-10 11:47:01 +01005751 if (!stream)
5752 return 0;
5753
Christopher Fauletafd8f102018-11-08 11:34:21 +01005754 if (IS_HTX_STRM(stream)) {
5755 SEND_ERR(stream->be, "Lua converter '%s': Lua fetches cannot be used when the"
5756 " HTX internal representation is enabled.\n", fcn->name);
5757 return 0;
5758 }
Willy Tarreau87b09662015-04-03 00:22:06 +02005759 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01005760 * Lua context can be not initialized. This behavior
5761 * permits to save performances because a systematic
5762 * Lua initialization cause 5% performances loss.
5763 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005764 if (!stream->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01005765 stream->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005766 if (!stream->hlua) {
5767 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
5768 return 0;
5769 }
5770 if (!hlua_ctx_init(stream->hlua, stream->task)) {
5771 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
5772 return 0;
5773 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01005774 }
5775
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005776 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005777 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005778
5779 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005780 if (!SET_SAFE_LJMP(stream->hlua->T)) {
5781 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
5782 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01005783 else
5784 error = "critical error";
5785 SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005786 return 0;
5787 }
5788
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005789 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005790 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005791 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005792 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005793 return 0;
5794 }
5795
5796 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005797 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005798
5799 /* convert input sample and pust-it in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005800 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005801 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005802 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005803 return 0;
5804 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005805 hlua_smp2lua(stream->hlua->T, smp);
5806 stream->hlua->nargs = 1;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005807
5808 /* push keywords in the stack. */
5809 if (arg_p) {
5810 for (; arg_p->type != ARGT_STOP; arg_p++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005811 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005812 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005813 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005814 return 0;
5815 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005816 hlua_arg2lua(stream->hlua->T, arg_p);
5817 stream->hlua->nargs++;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005818 }
5819 }
5820
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005821 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005822 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005823
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005824 /* At this point the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005825 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005826 }
5827
5828 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005829 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005830 /* finished. */
5831 case HLUA_E_OK:
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02005832 /* If the stack is empty, the function fails. */
5833 if (lua_gettop(stream->hlua->T) <= 0)
5834 return 0;
5835
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005836 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005837 hlua_lua2smp(stream->hlua->T, -1, smp);
5838 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005839 return 1;
5840
5841 /* yield. */
5842 case HLUA_E_AGAIN:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005843 SEND_ERR(stream->be, "Lua converter '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005844 return 0;
5845
5846 /* finished with error. */
5847 case HLUA_E_ERRMSG:
5848 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005849 SEND_ERR(stream->be, "Lua converter '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005850 fcn->name, lua_tostring(stream->hlua->T, -1));
5851 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005852 return 0;
5853
Thierry Fournierd5b073c2018-05-21 19:42:47 +02005854 case HLUA_E_ETMOUT:
5855 SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name);
5856 return 0;
5857
5858 case HLUA_E_NOMEM:
5859 SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name);
5860 return 0;
5861
5862 case HLUA_E_YIELD:
5863 SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name);
5864 return 0;
5865
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005866 case HLUA_E_ERR:
5867 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005868 SEND_ERR(stream->be, "Lua converter '%s' returns an unknown error.\n", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005869
5870 default:
5871 return 0;
5872 }
5873}
5874
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005875/* Wrapper called by HAProxy to execute a sample-fetch. this wrapper
5876 * doesn't allow "yield" functions because the HAProxy engine cannot
Willy Tarreaube508f12016-03-10 11:47:01 +01005877 * resume sample-fetches. This function will be called by the sample
5878 * fetch engine to call lua-based fetch operations.
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005879 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +02005880static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp,
5881 const char *kw, void *private)
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005882{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02005883 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02005884 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01005885 const char *error;
Willy Tarreau83061a82018-07-13 11:56:34 +02005886 const struct buffer msg = { };
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005887
Willy Tarreaube508f12016-03-10 11:47:01 +01005888 if (!stream)
5889 return 0;
Christopher Fauletafd8f102018-11-08 11:34:21 +01005890
5891 if (IS_HTX_STRM(stream)) {
5892 SEND_ERR(stream->be, "Lua sample-fetch '%s': Lua fetches cannot be used when the"
5893 " HTX internal representation is enabled.\n", fcn->name);
5894 return 0;
5895 }
Willy Tarreaube508f12016-03-10 11:47:01 +01005896
Willy Tarreau87b09662015-04-03 00:22:06 +02005897 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01005898 * Lua context can be not initialized. This behavior
5899 * permits to save performances because a systematic
5900 * Lua initialization cause 5% performances loss.
5901 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005902 if (!stream->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01005903 stream->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005904 if (!stream->hlua) {
5905 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
5906 return 0;
5907 }
5908 if (!hlua_ctx_init(stream->hlua, stream->task)) {
5909 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
5910 return 0;
5911 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01005912 }
5913
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005914 consistency_set(stream, smp->opt, &stream->hlua->cons);
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005915
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005916 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005917 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005918
5919 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005920 if (!SET_SAFE_LJMP(stream->hlua->T)) {
5921 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
5922 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01005923 else
5924 error = "critical error";
5925 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005926 return 0;
5927 }
5928
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005929 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005930 if (!lua_checkstack(stream->hlua->T, 2)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005931 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005932 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005933 return 0;
5934 }
5935
5936 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005937 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005938
5939 /* push arguments in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005940 if (!hlua_txn_new(stream->hlua->T, stream, smp->px, smp->opt & SMP_OPT_DIR,
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005941 HLUA_TXN_NOTERM)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005942 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005943 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005944 return 0;
5945 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005946 stream->hlua->nargs = 1;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005947
5948 /* push keywords in the stack. */
5949 for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) {
5950 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005951 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005952 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005953 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005954 return 0;
5955 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005956 hlua_arg2lua(stream->hlua->T, arg_p);
5957 stream->hlua->nargs++;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005958 }
5959
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005960 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005961 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005962
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005963 /* At this point the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005964 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005965 }
5966
5967 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005968 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005969 /* finished. */
5970 case HLUA_E_OK:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005971 if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) {
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005972 stream_int_retnclose(&stream->si[0], &msg);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02005973 return 0;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005974 }
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02005975 /* If the stack is empty, the function fails. */
5976 if (lua_gettop(stream->hlua->T) <= 0)
5977 return 0;
5978
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005979 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005980 hlua_lua2smp(stream->hlua->T, -1, smp);
5981 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005982
5983 /* Set the end of execution flag. */
5984 smp->flags &= ~SMP_F_MAY_CHANGE;
5985 return 1;
5986
5987 /* yield. */
5988 case HLUA_E_AGAIN:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005989 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005990 stream_int_retnclose(&stream->si[0], &msg);
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005991 SEND_ERR(smp->px, "Lua sample-fetch '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005992 return 0;
5993
5994 /* finished with error. */
5995 case HLUA_E_ERRMSG:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005996 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005997 stream_int_retnclose(&stream->si[0], &msg);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005998 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005999 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006000 fcn->name, lua_tostring(stream->hlua->T, -1));
6001 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006002 return 0;
6003
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006004 case HLUA_E_ETMOUT:
6005 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
6006 stream_int_retnclose(&stream->si[0], &msg);
6007 SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name);
6008 return 0;
6009
6010 case HLUA_E_NOMEM:
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': out of memory error.\n", fcn->name);
6014 return 0;
6015
6016 case HLUA_E_YIELD:
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': yield not allowed.\n", fcn->name);
6020 return 0;
6021
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006022 case HLUA_E_ERR:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006023 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006024 stream_int_retnclose(&stream->si[0], &msg);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006025 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006026 SEND_ERR(smp->px, "Lua sample-fetch '%s' returns an unknown error.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006027
6028 default:
6029 return 0;
6030 }
6031}
6032
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006033/* This function is an LUA binding used for registering
6034 * "sample-conv" functions. It expects a converter name used
6035 * in the haproxy configuration file, and an LUA function.
6036 */
6037__LJMP static int hlua_register_converters(lua_State *L)
6038{
6039 struct sample_conv_kw_list *sck;
6040 const char *name;
6041 int ref;
6042 int len;
6043 struct hlua_function *fcn;
6044
6045 MAY_LJMP(check_args(L, 2, "register_converters"));
6046
6047 /* First argument : converter name. */
6048 name = MAY_LJMP(luaL_checkstring(L, 1));
6049
6050 /* Second argument : lua function. */
6051 ref = MAY_LJMP(hlua_checkfunction(L, 2));
6052
6053 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006054 sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006055 if (!sck)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006056 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006057 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006058 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006059 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006060
6061 /* Fill fcn. */
6062 fcn->name = strdup(name);
6063 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006064 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006065 fcn->function_ref = ref;
6066
6067 /* List head */
6068 sck->list.n = sck->list.p = NULL;
6069
6070 /* converter keyword. */
6071 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006072 sck->kw[0].kw = calloc(1, len);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006073 if (!sck->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006074 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006075
6076 snprintf((char *)sck->kw[0].kw, len, "lua.%s", name);
6077 sck->kw[0].process = hlua_sample_conv_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01006078 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 +01006079 sck->kw[0].val_args = NULL;
6080 sck->kw[0].in_type = SMP_T_STR;
6081 sck->kw[0].out_type = SMP_T_STR;
6082 sck->kw[0].private = fcn;
6083
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006084 /* Register this new converter */
6085 sample_register_convs(sck);
6086
6087 return 0;
6088}
6089
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006090/* This function is an LUA binding used for registering
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006091 * "sample-fetch" functions. It expects a converter name used
6092 * in the haproxy configuration file, and an LUA function.
6093 */
6094__LJMP static int hlua_register_fetches(lua_State *L)
6095{
6096 const char *name;
6097 int ref;
6098 int len;
6099 struct sample_fetch_kw_list *sfk;
6100 struct hlua_function *fcn;
6101
6102 MAY_LJMP(check_args(L, 2, "register_fetches"));
6103
6104 /* First argument : sample-fetch name. */
6105 name = MAY_LJMP(luaL_checkstring(L, 1));
6106
6107 /* Second argument : lua function. */
6108 ref = MAY_LJMP(hlua_checkfunction(L, 2));
6109
6110 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006111 sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006112 if (!sfk)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006113 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006114 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006115 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006116 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006117
6118 /* Fill fcn. */
6119 fcn->name = strdup(name);
6120 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006121 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006122 fcn->function_ref = ref;
6123
6124 /* List head */
6125 sfk->list.n = sfk->list.p = NULL;
6126
6127 /* sample-fetch keyword. */
6128 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006129 sfk->kw[0].kw = calloc(1, len);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006130 if (!sfk->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006131 return luaL_error(L, "Lua out of memory error.");
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006132
6133 snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name);
6134 sfk->kw[0].process = hlua_sample_fetch_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01006135 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 +01006136 sfk->kw[0].val_args = NULL;
6137 sfk->kw[0].out_type = SMP_T_STR;
6138 sfk->kw[0].use = SMP_USE_HTTP_ANY;
6139 sfk->kw[0].val = 0;
6140 sfk->kw[0].private = fcn;
6141
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006142 /* Register this new fetch. */
6143 sample_register_fetches(sfk);
6144
6145 return 0;
6146}
6147
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006148/* This function is a wrapper to execute each LUA function declared
6149 * as an action wrapper during the initialisation period. This function
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006150 * return ACT_RET_CONT if the processing is finished (with or without
6151 * error) and return ACT_RET_YIELD if the function must be called again
6152 * because the LUA returns a yield.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006153 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006154static enum act_return hlua_action(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +02006155 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006156{
6157 char **arg;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006158 unsigned int analyzer;
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02006159 int dir;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006160 const char *error;
Willy Tarreau83061a82018-07-13 11:56:34 +02006161 const struct buffer msg = { };
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006162
6163 switch (rule->from) {
Thierry FOURNIER6e01f382015-11-02 09:52:54 +01006164 case ACT_F_TCP_REQ_CNT: analyzer = AN_REQ_INSPECT_FE ; dir = SMP_OPT_DIR_REQ; break;
6165 case ACT_F_TCP_RES_CNT: analyzer = AN_RES_INSPECT ; dir = SMP_OPT_DIR_RES; break;
6166 case ACT_F_HTTP_REQ: analyzer = AN_REQ_HTTP_PROCESS_FE; dir = SMP_OPT_DIR_REQ; break;
6167 case ACT_F_HTTP_RES: analyzer = AN_RES_HTTP_PROCESS_BE; dir = SMP_OPT_DIR_RES; break;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006168 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006169 SEND_ERR(px, "Lua: internal error while execute action.\n");
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006170 return ACT_RET_CONT;
6171 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006172
Willy Tarreau87b09662015-04-03 00:22:06 +02006173 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006174 * Lua context can be not initialized. This behavior
6175 * permits to save performances because a systematic
6176 * Lua initialization cause 5% performances loss.
6177 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006178 if (!s->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006179 s->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006180 if (!s->hlua) {
6181 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
6182 rule->arg.hlua_rule->fcn.name);
6183 return ACT_RET_CONT;
6184 }
6185 if (!hlua_ctx_init(s->hlua, s->task)) {
6186 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
6187 rule->arg.hlua_rule->fcn.name);
6188 return ACT_RET_CONT;
6189 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006190 }
6191
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006192 consistency_set(s, dir, &s->hlua->cons);
6193
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006194 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006195 if (!HLUA_IS_RUNNING(s->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006196
6197 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006198 if (!SET_SAFE_LJMP(s->hlua->T)) {
6199 if (lua_type(s->hlua->T, -1) == LUA_TSTRING)
6200 error = lua_tostring(s->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006201 else
6202 error = "critical error";
6203 SEND_ERR(px, "Lua function '%s': %s.\n",
6204 rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006205 return ACT_RET_CONT;
6206 }
6207
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006208 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006209 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006210 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006211 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006212 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006213 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006214 }
6215
6216 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006217 lua_rawgeti(s->hlua->T, LUA_REGISTRYINDEX, rule->arg.hlua_rule->fcn.function_ref);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006218
Willy Tarreau87b09662015-04-03 00:22:06 +02006219 /* Create and and push object stream in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006220 if (!hlua_txn_new(s->hlua->T, s, px, dir, 0)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006221 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006222 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006223 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006224 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006225 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006226 s->hlua->nargs = 1;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006227
6228 /* push keywords in the stack. */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006229 for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006230 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006231 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006232 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006233 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006234 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006235 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006236 lua_pushstring(s->hlua->T, *arg);
6237 s->hlua->nargs++;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006238 }
6239
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006240 /* Now the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006241 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006242
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006243 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006244 s->hlua->max_time = hlua_timeout_session;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006245 }
6246
6247 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006248 switch (hlua_ctx_resume(s->hlua, !(flags & ACT_FLAG_FINAL))) {
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006249 /* finished. */
6250 case HLUA_E_OK:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006251 if (!consistency_check(s, dir, &s->hlua->cons)) {
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006252 stream_int_retnclose(&s->si[0], &msg);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02006253 return ACT_RET_ERR;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006254 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006255 if (s->hlua->flags & HLUA_STOP)
Thierry FOURNIER9bd52d42016-07-14 11:45:33 +02006256 return ACT_RET_STOP;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006257 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006258
6259 /* yield. */
6260 case HLUA_E_AGAIN:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006261 /* Set timeout in the required channel. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006262 if (s->hlua->wake_time != TICK_ETERNITY) {
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006263 if (analyzer & (AN_REQ_INSPECT_FE|AN_REQ_HTTP_PROCESS_FE))
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006264 s->req.analyse_exp = s->hlua->wake_time;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006265 else if (analyzer & (AN_RES_INSPECT|AN_RES_HTTP_PROCESS_BE))
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006266 s->res.analyse_exp = s->hlua->wake_time;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006267 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006268 /* Some actions can be wake up when a "write" event
6269 * is detected on a response channel. This is useful
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006270 * only for actions targeted on the requests.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006271 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006272 if (HLUA_IS_WAKERESWR(s->hlua)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006273 s->res.flags |= CF_WAKE_WRITE;
Willy Tarreau76bd97f2015-03-10 17:16:10 +01006274 if ((analyzer & (AN_REQ_INSPECT_FE|AN_REQ_HTTP_PROCESS_FE)))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006275 s->res.analysers |= analyzer;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006276 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006277 if (HLUA_IS_WAKEREQWR(s->hlua))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006278 s->req.flags |= CF_WAKE_WRITE;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006279 /* We can quit the function without consistency check
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006280 * because HAProxy is not able to manipulate data, it
6281 * is only allowed to call me again. */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006282 return ACT_RET_YIELD;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006283
6284 /* finished with error. */
6285 case HLUA_E_ERRMSG:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006286 if (!consistency_check(s, dir, &s->hlua->cons)) {
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006287 stream_int_retnclose(&s->si[0], &msg);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02006288 return ACT_RET_ERR;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006289 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006290 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006291 SEND_ERR(px, "Lua function '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006292 rule->arg.hlua_rule->fcn.name, lua_tostring(s->hlua->T, -1));
6293 lua_pop(s->hlua->T, 1);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006294 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006295
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006296 case HLUA_E_ETMOUT:
6297 if (!consistency_check(s, dir, &s->hlua->cons)) {
6298 stream_int_retnclose(&s->si[0], &msg);
6299 return ACT_RET_ERR;
6300 }
6301 SEND_ERR(px, "Lua function '%s': execution timeout.\n", rule->arg.hlua_rule->fcn.name);
6302 return 0;
6303
6304 case HLUA_E_NOMEM:
6305 if (!consistency_check(s, dir, &s->hlua->cons)) {
6306 stream_int_retnclose(&s->si[0], &msg);
6307 return ACT_RET_ERR;
6308 }
6309 SEND_ERR(px, "Lua function '%s': out of memory error.\n", rule->arg.hlua_rule->fcn.name);
6310 return 0;
6311
6312 case HLUA_E_YIELD:
6313 if (!consistency_check(s, dir, &s->hlua->cons)) {
6314 stream_int_retnclose(&s->si[0], &msg);
6315 return ACT_RET_ERR;
6316 }
6317 SEND_ERR(px, "Lua function '%s': aborting Lua processing on expired timeout.\n",
6318 rule->arg.hlua_rule->fcn.name);
6319 return 0;
6320
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006321 case HLUA_E_ERR:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006322 if (!consistency_check(s, dir, &s->hlua->cons)) {
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006323 stream_int_retnclose(&s->si[0], &msg);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02006324 return ACT_RET_ERR;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006325 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006326 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006327 SEND_ERR(px, "Lua function '%s' return an unknown error.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006328 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006329
6330 default:
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006331 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006332 }
6333}
6334
Olivier Houchard9f6af332018-05-25 14:04:04 +02006335struct task *hlua_applet_wakeup(struct task *t, void *context, unsigned short state)
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006336{
Olivier Houchard9f6af332018-05-25 14:04:04 +02006337 struct appctx *ctx = context;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006338
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006339 appctx_wakeup(ctx);
Willy Tarreaud9587412017-08-23 16:07:33 +02006340 t->expire = TICK_ETERNITY;
Willy Tarreaud1aa41f2017-07-21 16:41:56 +02006341 return t;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006342}
6343
6344static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
6345{
6346 struct stream_interface *si = ctx->owner;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006347 struct hlua *hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006348 struct task *task;
6349 char **arg;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006350 const char *error;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006351
Willy Tarreaubafbe012017-11-24 17:34:44 +01006352 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006353 if (!hlua) {
6354 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
6355 ctx->rule->arg.hlua_rule->fcn.name);
6356 return 0;
6357 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006358 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006359 ctx->ctx.hlua_apptcp.hlua = hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006360 ctx->ctx.hlua_apptcp.flags = 0;
6361
6362 /* Create task used by signal to wakeup applets. */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01006363 task = task_new(tid_bit);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006364 if (!task) {
6365 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
6366 ctx->rule->arg.hlua_rule->fcn.name);
6367 return 0;
6368 }
6369 task->nice = 0;
6370 task->context = ctx;
6371 task->process = hlua_applet_wakeup;
6372 ctx->ctx.hlua_apptcp.task = task;
6373
6374 /* In the execution wrappers linked with a stream, the
6375 * Lua context can be not initialized. This behavior
6376 * permits to save performances because a systematic
6377 * Lua initialization cause 5% performances loss.
6378 */
6379 if (!hlua_ctx_init(hlua, task)) {
6380 SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n",
6381 ctx->rule->arg.hlua_rule->fcn.name);
6382 return 0;
6383 }
6384
6385 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02006386 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006387
6388 /* The following Lua calls can fail. */
6389 if (!SET_SAFE_LJMP(hlua->T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01006390 if (lua_type(hlua->T, -1) == LUA_TSTRING)
6391 error = lua_tostring(hlua->T, -1);
6392 else
6393 error = "critical error";
6394 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
6395 ctx->rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006396 return 0;
6397 }
6398
6399 /* Check stack available size. */
6400 if (!lua_checkstack(hlua->T, 1)) {
6401 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
6402 ctx->rule->arg.hlua_rule->fcn.name);
6403 RESET_SAFE_LJMP(hlua->T);
6404 return 0;
6405 }
6406
6407 /* Restore the function in the stack. */
6408 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref);
6409
6410 /* Create and and push object stream in the stack. */
6411 if (!hlua_applet_tcp_new(hlua->T, ctx)) {
6412 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
6413 ctx->rule->arg.hlua_rule->fcn.name);
6414 RESET_SAFE_LJMP(hlua->T);
6415 return 0;
6416 }
6417 hlua->nargs = 1;
6418
6419 /* push keywords in the stack. */
6420 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
6421 if (!lua_checkstack(hlua->T, 1)) {
6422 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
6423 ctx->rule->arg.hlua_rule->fcn.name);
6424 RESET_SAFE_LJMP(hlua->T);
6425 return 0;
6426 }
6427 lua_pushstring(hlua->T, *arg);
6428 hlua->nargs++;
6429 }
6430
6431 RESET_SAFE_LJMP(hlua->T);
6432
6433 /* Wakeup the applet ASAP. */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01006434 si_cant_get(si);
Willy Tarreau3367d412018-11-15 10:57:41 +01006435 si_rx_endp_more(si);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006436
6437 return 1;
6438}
6439
6440static void hlua_applet_tcp_fct(struct appctx *ctx)
6441{
6442 struct stream_interface *si = ctx->owner;
6443 struct stream *strm = si_strm(si);
6444 struct channel *res = si_ic(si);
6445 struct act_rule *rule = ctx->rule;
6446 struct proxy *px = strm->be;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006447 struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006448
6449 /* The applet execution is already done. */
Olivier Houchard594c8c52018-08-28 14:41:31 +02006450 if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) {
6451 /* eat the whole request */
6452 co_skip(si_oc(si), co_data(si_oc(si)));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006453 return;
Olivier Houchard594c8c52018-08-28 14:41:31 +02006454 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006455
6456 /* If the stream is disconnect or closed, ldo nothing. */
6457 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
6458 return;
6459
6460 /* Execute the function. */
6461 switch (hlua_ctx_resume(hlua, 1)) {
6462 /* finished. */
6463 case HLUA_E_OK:
6464 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
6465
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006466 /* eat the whole request */
Willy Tarreaua79021a2018-06-15 18:07:57 +02006467 co_skip(si_oc(si), co_data(si_oc(si)));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006468 res->flags |= CF_READ_NULL;
6469 si_shutr(si);
6470 return;
6471
6472 /* yield. */
6473 case HLUA_E_AGAIN:
Thierry Fournier0164f202016-02-20 17:47:43 +01006474 if (hlua->wake_time != TICK_ETERNITY)
6475 task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006476 return;
6477
6478 /* finished with error. */
6479 case HLUA_E_ERRMSG:
6480 /* Display log. */
6481 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
6482 rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1));
6483 lua_pop(hlua->T, 1);
6484 goto error;
6485
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006486 case HLUA_E_ETMOUT:
6487 SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n",
6488 rule->arg.hlua_rule->fcn.name);
6489 goto error;
6490
6491 case HLUA_E_NOMEM:
6492 SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n",
6493 rule->arg.hlua_rule->fcn.name);
6494 goto error;
6495
6496 case HLUA_E_YIELD: /* unexpected */
6497 SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n",
6498 rule->arg.hlua_rule->fcn.name);
6499 goto error;
6500
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006501 case HLUA_E_ERR:
6502 /* Display log. */
6503 SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n",
6504 rule->arg.hlua_rule->fcn.name);
6505 goto error;
6506
6507 default:
6508 goto error;
6509 }
6510
6511error:
6512
6513 /* For all other cases, just close the stream. */
6514 si_shutw(si);
6515 si_shutr(si);
6516 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
6517}
6518
6519static void hlua_applet_tcp_release(struct appctx *ctx)
6520{
Willy Tarreaubd7fc952017-07-24 17:35:27 +02006521 task_delete(ctx->ctx.hlua_apptcp.task);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006522 task_free(ctx->ctx.hlua_apptcp.task);
6523 ctx->ctx.hlua_apptcp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006524 hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006525 ctx->ctx.hlua_apptcp.hlua = NULL;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006526}
6527
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006528/* The function returns 1 if the initialisation is complete, 0 if
6529 * an errors occurs and -1 if more data are required for initializing
6530 * the applet.
6531 */
6532static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
6533{
6534 struct stream_interface *si = ctx->owner;
6535 struct channel *req = si_oc(si);
6536 struct http_msg *msg;
6537 struct http_txn *txn;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006538 struct hlua *hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006539 char **arg;
6540 struct hdr_ctx hdr;
6541 struct task *task;
6542 struct sample smp; /* just used for a valid call to smp_prefetch_http. */
Thierry Fournierfd107a22016-02-19 19:57:23 +01006543 const char *error;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006544
6545 /* Wait for a full HTTP request. */
6546 if (!smp_prefetch_http(px, strm, 0, NULL, &smp, 0)) {
6547 if (smp.flags & SMP_F_MAY_CHANGE)
6548 return -1;
6549 return 0;
6550 }
6551 txn = strm->txn;
6552 msg = &txn->req;
6553
Willy Tarreau0078bfc2015-10-07 20:20:28 +02006554 /* We want two things in HTTP mode :
6555 * - enforce server-close mode if we were in keep-alive, so that the
6556 * applet is released after each response ;
6557 * - enable request body transfer to the applet in order to resync
6558 * with the response body.
6559 */
6560 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)
6561 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreau0078bfc2015-10-07 20:20:28 +02006562
Willy Tarreaubafbe012017-11-24 17:34:44 +01006563 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006564 if (!hlua) {
6565 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
6566 ctx->rule->arg.hlua_rule->fcn.name);
6567 return 0;
6568 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006569 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006570 ctx->ctx.hlua_apphttp.hlua = hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006571 ctx->ctx.hlua_apphttp.left_bytes = -1;
6572 ctx->ctx.hlua_apphttp.flags = 0;
6573
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +01006574 if (txn->req.flags & HTTP_MSGF_VER_11)
6575 ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11;
6576
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006577 /* Create task used by signal to wakeup applets. */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01006578 task = task_new(tid_bit);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006579 if (!task) {
6580 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
6581 ctx->rule->arg.hlua_rule->fcn.name);
6582 return 0;
6583 }
6584 task->nice = 0;
6585 task->context = ctx;
6586 task->process = hlua_applet_wakeup;
6587 ctx->ctx.hlua_apphttp.task = task;
6588
6589 /* In the execution wrappers linked with a stream, the
6590 * Lua context can be not initialized. This behavior
6591 * permits to save performances because a systematic
6592 * Lua initialization cause 5% performances loss.
6593 */
6594 if (!hlua_ctx_init(hlua, task)) {
6595 SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n",
6596 ctx->rule->arg.hlua_rule->fcn.name);
6597 return 0;
6598 }
6599
6600 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02006601 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006602
6603 /* The following Lua calls can fail. */
6604 if (!SET_SAFE_LJMP(hlua->T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01006605 if (lua_type(hlua->T, -1) == LUA_TSTRING)
6606 error = lua_tostring(hlua->T, -1);
6607 else
6608 error = "critical error";
6609 SEND_ERR(px, "Lua applet http '%s': %s.\n",
6610 ctx->rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006611 return 0;
6612 }
6613
6614 /* Check stack available size. */
6615 if (!lua_checkstack(hlua->T, 1)) {
6616 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
6617 ctx->rule->arg.hlua_rule->fcn.name);
6618 RESET_SAFE_LJMP(hlua->T);
6619 return 0;
6620 }
6621
6622 /* Restore the function in the stack. */
6623 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref);
6624
6625 /* Create and and push object stream in the stack. */
6626 if (!hlua_applet_http_new(hlua->T, ctx)) {
6627 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
6628 ctx->rule->arg.hlua_rule->fcn.name);
6629 RESET_SAFE_LJMP(hlua->T);
6630 return 0;
6631 }
6632 hlua->nargs = 1;
6633
6634 /* Look for a 100-continue expected. */
6635 if (msg->flags & HTTP_MSGF_VER_11) {
6636 hdr.idx = 0;
Willy Tarreaua79021a2018-06-15 18:07:57 +02006637 if (http_find_header2("Expect", 6, ci_head(req), &txn->hdr_idx, &hdr) &&
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006638 unlikely(hdr.vlen == 12 && strncasecmp(hdr.line+hdr.val, "100-continue", 12) == 0))
6639 ctx->ctx.hlua_apphttp.flags |= APPLET_100C;
6640 }
6641
6642 /* push keywords in the stack. */
6643 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
6644 if (!lua_checkstack(hlua->T, 1)) {
6645 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
6646 ctx->rule->arg.hlua_rule->fcn.name);
6647 RESET_SAFE_LJMP(hlua->T);
6648 return 0;
6649 }
6650 lua_pushstring(hlua->T, *arg);
6651 hlua->nargs++;
6652 }
6653
6654 RESET_SAFE_LJMP(hlua->T);
6655
6656 /* Wakeup the applet when data is ready for read. */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01006657 si_cant_get(si);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006658
6659 return 1;
6660}
6661
6662static void hlua_applet_http_fct(struct appctx *ctx)
6663{
6664 struct stream_interface *si = ctx->owner;
6665 struct stream *strm = si_strm(si);
6666 struct channel *res = si_ic(si);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006667 struct act_rule *rule = ctx->rule;
6668 struct proxy *px = strm->be;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006669 struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua;
Willy Tarreau206ba832018-06-14 15:27:31 +02006670 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02006671 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02006672 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02006673 size_t len2;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006674 int ret;
6675
6676 /* If the stream is disconnect or closed, ldo nothing. */
6677 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
6678 return;
6679
6680 /* Set the currently running flag. */
6681 if (!HLUA_IS_RUNNING(hlua) &&
6682 !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
6683
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006684 /* Wait for full HTTP analysys. */
6685 if (unlikely(strm->txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01006686 si_cant_get(si);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006687 return;
6688 }
6689
6690 /* Store the max amount of bytes that we can read. */
6691 ctx->ctx.hlua_apphttp.left_bytes = strm->txn->req.body_len;
6692
6693 /* We need to flush the request header. This left the body
6694 * for the Lua.
6695 */
6696
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006697 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02006698 ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006699 if (ret == -1)
6700 return;
6701
6702 /* No data available, ask for more data. */
6703 if (ret == 1)
6704 len2 = 0;
6705 if (ret == 0)
6706 len1 = 0;
Thierry FOURNIER70d318c2018-06-30 10:37:33 +02006707 if (len1 + len2 < strm->txn->req.eoh + strm->txn->req.eol) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01006708 si_cant_get(si);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006709 return;
6710 }
6711
6712 /* skip the requests bytes. */
Thierry FOURNIER70d318c2018-06-30 10:37:33 +02006713 co_skip(si_oc(si), strm->txn->req.eoh + strm->txn->req.eol);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006714 }
6715
6716 /* Executes The applet if it is not done. */
6717 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
6718
6719 /* Execute the function. */
6720 switch (hlua_ctx_resume(hlua, 1)) {
6721 /* finished. */
6722 case HLUA_E_OK:
6723 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
6724 break;
6725
6726 /* yield. */
6727 case HLUA_E_AGAIN:
Thierry Fournier0164f202016-02-20 17:47:43 +01006728 if (hlua->wake_time != TICK_ETERNITY)
6729 task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006730 return;
6731
6732 /* finished with error. */
6733 case HLUA_E_ERRMSG:
6734 /* Display log. */
6735 SEND_ERR(px, "Lua applet http '%s': %s.\n",
6736 rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1));
6737 lua_pop(hlua->T, 1);
6738 goto error;
6739
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006740 case HLUA_E_ETMOUT:
6741 SEND_ERR(px, "Lua applet http '%s': execution timeout.\n",
6742 rule->arg.hlua_rule->fcn.name);
6743 goto error;
6744
6745 case HLUA_E_NOMEM:
6746 SEND_ERR(px, "Lua applet http '%s': out of memory error.\n",
6747 rule->arg.hlua_rule->fcn.name);
6748 goto error;
6749
6750 case HLUA_E_YIELD: /* unexpected */
6751 SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n",
6752 rule->arg.hlua_rule->fcn.name);
6753 goto error;
6754
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006755 case HLUA_E_ERR:
6756 /* Display log. */
6757 SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n",
6758 rule->arg.hlua_rule->fcn.name);
6759 goto error;
6760
6761 default:
6762 goto error;
6763 }
6764 }
6765
6766 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
6767
6768 /* We must send the final chunk. */
6769 if (ctx->ctx.hlua_apphttp.flags & APPLET_CHUNKED &&
6770 !(ctx->ctx.hlua_apphttp.flags & APPLET_LAST_CHK)) {
6771
6772 /* sent last chunk at once. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02006773 ret = ci_putblk(res, "0\r\n\r\n", 5);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006774
6775 /* critical error. */
6776 if (ret == -2 || ret == -3) {
6777 SEND_ERR(px, "Lua applet http '%s'cannont send last chunk.\n",
6778 rule->arg.hlua_rule->fcn.name);
6779 goto error;
6780 }
6781
6782 /* no enough space error. */
6783 if (ret == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01006784 si_rx_room_blk(si);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006785 return;
6786 }
6787
6788 /* set the last chunk sent. */
6789 ctx->ctx.hlua_apphttp.flags |= APPLET_LAST_CHK;
6790 }
6791
6792 /* close the connection. */
6793
Frédéric Lécaille83ed5d52018-07-18 14:25:26 +02006794 /* status */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006795 strm->txn->status = ctx->ctx.hlua_apphttp.status;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006796
6797 /* eat the whole request */
Willy Tarreaua79021a2018-06-15 18:07:57 +02006798 co_skip(si_oc(si), co_data(si_oc(si)));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006799 res->flags |= CF_READ_NULL;
6800 si_shutr(si);
6801
6802 return;
6803 }
6804
6805error:
6806
6807 /* If we are in HTTP mode, and we are not send any
6808 * data, return a 500 server error in best effort:
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006809 * if there is no room available in the buffer,
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006810 * just close the connection.
6811 */
Willy Tarreau06d80a92017-10-19 14:32:15 +02006812 ci_putblk(res, error_500, strlen(error_500));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006813 if (!(strm->flags & SF_ERR_MASK))
6814 strm->flags |= SF_ERR_RESOURCE;
6815 si_shutw(si);
6816 si_shutr(si);
6817 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
6818}
6819
6820static void hlua_applet_http_release(struct appctx *ctx)
6821{
Willy Tarreaubd7fc952017-07-24 17:35:27 +02006822 task_delete(ctx->ctx.hlua_apphttp.task);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006823 task_free(ctx->ctx.hlua_apphttp.task);
6824 ctx->ctx.hlua_apphttp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006825 hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006826 ctx->ctx.hlua_apphttp.hlua = NULL;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006827}
6828
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006829/* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in
6830 * succes case, else return ACT_RET_PRS_ERR.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006831 *
6832 * This function can fail with an abort() due to an Lua critical error.
6833 * We are in the configuration parsing process of HAProxy, this abort() is
6834 * tolerated.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006835 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006836static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px,
6837 struct act_rule *rule, char **err)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006838{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006839 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006840 int i;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006841
Christopher Fauletafd8f102018-11-08 11:34:21 +01006842 if (px->options2 & PR_O2_USE_HTX) {
6843 memprintf(err, "Lua actions cannot be used when the HTX internal representation is enabled");
6844 return ACT_RET_PRS_ERR;
6845 }
6846
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006847 /* Memory for the rule. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006848 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006849 if (!rule->arg.hlua_rule) {
6850 memprintf(err, "out of memory error");
Thierry FOURNIERafa80492015-08-19 09:04:15 +02006851 return ACT_RET_PRS_ERR;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006852 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006853
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006854 /* Memory for arguments. */
6855 rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, sizeof(char *));
6856 if (!rule->arg.hlua_rule->args) {
6857 memprintf(err, "out of memory error");
6858 return ACT_RET_PRS_ERR;
6859 }
6860
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006861 /* Reference the Lua function and store the reference. */
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006862 rule->arg.hlua_rule->fcn = *fcn;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006863
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006864 /* Expect some arguments */
6865 for (i = 0; i < fcn->nargs; i++) {
6866 if (*args[i+1] == '\0') {
6867 memprintf(err, "expect %d arguments", fcn->nargs);
6868 return ACT_RET_PRS_ERR;
6869 }
6870 rule->arg.hlua_rule->args[i] = strdup(args[i + 1]);
6871 if (!rule->arg.hlua_rule->args[i]) {
6872 memprintf(err, "out of memory error");
6873 return ACT_RET_PRS_ERR;
6874 }
6875 (*cur_arg)++;
6876 }
6877 rule->arg.hlua_rule->args[i] = NULL;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006878
Thierry FOURNIER42148732015-09-02 17:17:33 +02006879 rule->action = ACT_CUSTOM;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006880 rule->action_ptr = hlua_action;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02006881 return ACT_RET_PRS_OK;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006882}
6883
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006884static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px,
6885 struct act_rule *rule, char **err)
6886{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006887 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006888
Christopher Fauletafd8f102018-11-08 11:34:21 +01006889 if (px->options2 & PR_O2_USE_HTX) {
6890 memprintf(err, "Lua services cannot be used when the HTX internal representation is enabled");
6891 return ACT_RET_PRS_ERR;
6892 }
6893
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01006894 /* HTTP applets are forbidden in tcp-request rules.
6895 * HTTP applet request requires everything initilized by
6896 * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER).
6897 * The applet will be immediately initilized, but its before
6898 * the call of this analyzer.
6899 */
6900 if (rule->from != ACT_F_HTTP_REQ) {
6901 memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets");
6902 return ACT_RET_PRS_ERR;
6903 }
6904
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006905 /* Memory for the rule. */
6906 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
6907 if (!rule->arg.hlua_rule) {
6908 memprintf(err, "out of memory error");
6909 return ACT_RET_PRS_ERR;
6910 }
6911
6912 /* Reference the Lua function and store the reference. */
6913 rule->arg.hlua_rule->fcn = *fcn;
6914
6915 /* TODO: later accept arguments. */
6916 rule->arg.hlua_rule->args = NULL;
6917
6918 /* Add applet pointer in the rule. */
6919 rule->applet.obj_type = OBJ_TYPE_APPLET;
6920 rule->applet.name = fcn->name;
6921 rule->applet.init = hlua_applet_http_init;
6922 rule->applet.fct = hlua_applet_http_fct;
6923 rule->applet.release = hlua_applet_http_release;
6924 rule->applet.timeout = hlua_timeout_applet;
6925
6926 return ACT_RET_PRS_OK;
6927}
6928
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006929/* This function is an LUA binding used for registering
6930 * "sample-conv" functions. It expects a converter name used
6931 * in the haproxy configuration file, and an LUA function.
6932 */
6933__LJMP static int hlua_register_action(lua_State *L)
6934{
6935 struct action_kw_list *akl;
6936 const char *name;
6937 int ref;
6938 int len;
6939 struct hlua_function *fcn;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006940 int nargs;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006941
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006942 /* Initialise the number of expected arguments at 0. */
6943 nargs = 0;
6944
6945 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
6946 WILL_LJMP(luaL_error(L, "'register_action' needs between 3 and 4 arguments"));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006947
6948 /* First argument : converter name. */
6949 name = MAY_LJMP(luaL_checkstring(L, 1));
6950
6951 /* Second argument : environment. */
6952 if (lua_type(L, 2) != LUA_TTABLE)
6953 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
6954
6955 /* Third argument : lua function. */
6956 ref = MAY_LJMP(hlua_checkfunction(L, 3));
6957
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006958 /* Fourth argument : number of mandatory arguments expected on the configuration line. */
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006959 if (lua_gettop(L) >= 4)
6960 nargs = MAY_LJMP(luaL_checkinteger(L, 4));
6961
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006962 /* browse the second argument as an array. */
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006963 lua_pushnil(L);
6964 while (lua_next(L, 2) != 0) {
6965 if (lua_type(L, -1) != LUA_TSTRING)
6966 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
6967
6968 /* Check required environment. Only accepted "http" or "tcp". */
6969 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006970 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006971 if (!akl)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006972 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006973 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006974 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006975 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006976
6977 /* Fill fcn. */
6978 fcn->name = strdup(name);
6979 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006980 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006981 fcn->function_ref = ref;
6982
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006983 /* Set the expected number od arguments. */
6984 fcn->nargs = nargs;
6985
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006986 /* List head */
6987 akl->list.n = akl->list.p = NULL;
6988
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006989 /* action keyword. */
6990 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006991 akl->kw[0].kw = calloc(1, len);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006992 if (!akl->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006993 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006994
6995 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
6996
6997 akl->kw[0].match_pfx = 0;
6998 akl->kw[0].private = fcn;
6999 akl->kw[0].parse = action_register_lua;
7000
7001 /* select the action registering point. */
7002 if (strcmp(lua_tostring(L, -1), "tcp-req") == 0)
7003 tcp_req_cont_keywords_register(akl);
7004 else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0)
7005 tcp_res_cont_keywords_register(akl);
7006 else if (strcmp(lua_tostring(L, -1), "http-req") == 0)
7007 http_req_keywords_register(akl);
7008 else if (strcmp(lua_tostring(L, -1), "http-res") == 0)
7009 http_res_keywords_register(akl);
7010 else
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007011 WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. "
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007012 "'tcp-req', 'tcp-res', 'http-req' or 'http-res' "
7013 "are expected.", lua_tostring(L, -1)));
7014
7015 /* pop the environment string. */
7016 lua_pop(L, 1);
7017 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007018 return ACT_RET_PRS_OK;
7019}
7020
7021static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px,
7022 struct act_rule *rule, char **err)
7023{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007024 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007025
Christopher Fauletafd8f102018-11-08 11:34:21 +01007026 if (px->options2 & PR_O2_USE_HTX) {
7027 memprintf(err, "Lua services cannot be used when the HTX internal representation is enabled");
7028 return ACT_RET_PRS_ERR;
7029 }
7030
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007031 /* Memory for the rule. */
7032 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
7033 if (!rule->arg.hlua_rule) {
7034 memprintf(err, "out of memory error");
7035 return ACT_RET_PRS_ERR;
7036 }
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007037
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007038 /* Reference the Lua function and store the reference. */
7039 rule->arg.hlua_rule->fcn = *fcn;
7040
7041 /* TODO: later accept arguments. */
7042 rule->arg.hlua_rule->args = NULL;
7043
7044 /* Add applet pointer in the rule. */
7045 rule->applet.obj_type = OBJ_TYPE_APPLET;
7046 rule->applet.name = fcn->name;
7047 rule->applet.init = hlua_applet_tcp_init;
7048 rule->applet.fct = hlua_applet_tcp_fct;
7049 rule->applet.release = hlua_applet_tcp_release;
7050 rule->applet.timeout = hlua_timeout_applet;
7051
7052 return 0;
7053}
7054
7055/* This function is an LUA binding used for registering
7056 * "sample-conv" functions. It expects a converter name used
7057 * in the haproxy configuration file, and an LUA function.
7058 */
7059__LJMP static int hlua_register_service(lua_State *L)
7060{
7061 struct action_kw_list *akl;
7062 const char *name;
7063 const char *env;
7064 int ref;
7065 int len;
7066 struct hlua_function *fcn;
7067
7068 MAY_LJMP(check_args(L, 3, "register_service"));
7069
7070 /* First argument : converter name. */
7071 name = MAY_LJMP(luaL_checkstring(L, 1));
7072
7073 /* Second argument : environment. */
7074 env = MAY_LJMP(luaL_checkstring(L, 2));
7075
7076 /* Third argument : lua function. */
7077 ref = MAY_LJMP(hlua_checkfunction(L, 3));
7078
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007079 /* Allocate and fill the sample fetch keyword struct. */
7080 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
7081 if (!akl)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007082 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007083 fcn = calloc(1, sizeof(*fcn));
7084 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007085 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007086
7087 /* Fill fcn. */
7088 len = strlen("<lua.>") + strlen(name) + 1;
7089 fcn->name = calloc(1, len);
7090 if (!fcn->name)
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 snprintf((char *)fcn->name, len, "<lua.%s>", name);
7093 fcn->function_ref = ref;
7094
7095 /* List head */
7096 akl->list.n = akl->list.p = NULL;
7097
7098 /* converter keyword. */
7099 len = strlen("lua.") + strlen(name) + 1;
7100 akl->kw[0].kw = calloc(1, len);
7101 if (!akl->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007102 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007103
7104 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
7105
Thierry FOURNIER / OZON.IO02564fd2016-11-12 11:07:05 +01007106 /* Check required environment. Only accepted "http" or "tcp". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007107 if (strcmp(env, "tcp") == 0)
7108 akl->kw[0].parse = action_register_service_tcp;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007109 else if (strcmp(env, "http") == 0)
7110 akl->kw[0].parse = action_register_service_http;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007111 else
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007112 WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. "
Eric Salamafe7456f2017-12-21 14:30:07 +01007113 "'tcp' or 'http' are expected.", env));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007114
7115 akl->kw[0].match_pfx = 0;
7116 akl->kw[0].private = fcn;
7117
7118 /* End of array. */
7119 memset(&akl->kw[1], 0, sizeof(*akl->kw));
7120
7121 /* Register this new converter */
7122 service_keywords_register(akl);
7123
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007124 return 0;
7125}
7126
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007127/* This function initialises Lua cli handler. It copies the
7128 * arguments in the Lua stack and create channel IO objects.
7129 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02007130static int hlua_cli_parse_fct(char **args, char *payload, struct appctx *appctx, void *private)
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007131{
7132 struct hlua *hlua;
7133 struct hlua_function *fcn;
7134 int i;
7135 const char *error;
7136
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007137 fcn = private;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007138 appctx->ctx.hlua_cli.fcn = private;
7139
Willy Tarreaubafbe012017-11-24 17:34:44 +01007140 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007141 if (!hlua) {
7142 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007143 return 1;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007144 }
7145 HLUA_INIT(hlua);
7146 appctx->ctx.hlua_cli.hlua = hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007147
7148 /* Create task used by signal to wakeup applets.
7149 * We use the same wakeup fonction than the Lua applet_tcp and
7150 * applet_http. It is absolutely compatible.
7151 */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01007152 appctx->ctx.hlua_cli.task = task_new(tid_bit);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007153 if (!appctx->ctx.hlua_cli.task) {
Thierry FOURNIERffbf5692016-12-16 11:14:06 +01007154 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007155 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007156 }
7157 appctx->ctx.hlua_cli.task->nice = 0;
7158 appctx->ctx.hlua_cli.task->context = appctx;
7159 appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup;
7160
7161 /* Initialises the Lua context */
7162 if (!hlua_ctx_init(hlua, appctx->ctx.hlua_cli.task)) {
7163 SEND_ERR(NULL, "Lua cli '%s': can't initialize Lua context.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007164 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007165 }
7166
7167 /* The following Lua calls can fail. */
7168 if (!SET_SAFE_LJMP(hlua->T)) {
7169 if (lua_type(hlua->T, -1) == LUA_TSTRING)
7170 error = lua_tostring(hlua->T, -1);
7171 else
7172 error = "critical error";
7173 SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error);
7174 goto error;
7175 }
7176
7177 /* Check stack available size. */
7178 if (!lua_checkstack(hlua->T, 2)) {
7179 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7180 goto error;
7181 }
7182
7183 /* Restore the function in the stack. */
7184 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
7185
7186 /* Once the arguments parsed, the CLI is like an AppletTCP,
7187 * so push AppletTCP in the stack.
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007188 */
7189 if (!hlua_applet_tcp_new(hlua->T, appctx)) {
7190 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7191 goto error;
7192 }
7193 hlua->nargs = 1;
7194
7195 /* push keywords in the stack. */
7196 for (i = 0; *args[i]; i++) {
7197 /* Check stack available size. */
7198 if (!lua_checkstack(hlua->T, 1)) {
7199 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7200 goto error;
7201 }
7202 lua_pushstring(hlua->T, args[i]);
7203 hlua->nargs++;
7204 }
7205
7206 /* We must initialize the execution timeouts. */
7207 hlua->max_time = hlua_timeout_session;
7208
7209 /* At this point the execution is safe. */
7210 RESET_SAFE_LJMP(hlua->T);
7211
7212 /* It's ok */
7213 return 0;
7214
7215 /* It's not ok. */
7216error:
7217 RESET_SAFE_LJMP(hlua->T);
7218 hlua_ctx_destroy(hlua);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007219 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007220 return 1;
7221}
7222
7223static int hlua_cli_io_handler_fct(struct appctx *appctx)
7224{
7225 struct hlua *hlua;
7226 struct stream_interface *si;
7227 struct hlua_function *fcn;
7228
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007229 hlua = appctx->ctx.hlua_cli.hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007230 si = appctx->owner;
Willy Tarreau8ae4f752016-12-14 15:41:45 +01007231 fcn = appctx->ctx.hlua_cli.fcn;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007232
7233 /* If the stream is disconnect or closed, ldo nothing. */
7234 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
7235 return 1;
7236
7237 /* Execute the function. */
7238 switch (hlua_ctx_resume(hlua, 1)) {
7239
7240 /* finished. */
7241 case HLUA_E_OK:
7242 return 1;
7243
7244 /* yield. */
7245 case HLUA_E_AGAIN:
7246 /* We want write. */
7247 if (HLUA_IS_WAKERESWR(hlua))
Willy Tarreaudb398432018-11-15 11:08:52 +01007248 si_rx_room_blk(si);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007249 /* Set the timeout. */
7250 if (hlua->wake_time != TICK_ETERNITY)
7251 task_schedule(hlua->task, hlua->wake_time);
7252 return 0;
7253
7254 /* finished with error. */
7255 case HLUA_E_ERRMSG:
7256 /* Display log. */
7257 SEND_ERR(NULL, "Lua cli '%s': %s.\n",
7258 fcn->name, lua_tostring(hlua->T, -1));
7259 lua_pop(hlua->T, 1);
7260 return 1;
7261
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007262 case HLUA_E_ETMOUT:
7263 SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n",
7264 fcn->name);
7265 return 1;
7266
7267 case HLUA_E_NOMEM:
7268 SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n",
7269 fcn->name);
7270 return 1;
7271
7272 case HLUA_E_YIELD: /* unexpected */
7273 SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n",
7274 fcn->name);
7275 return 1;
7276
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007277 case HLUA_E_ERR:
7278 /* Display log. */
7279 SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n",
7280 fcn->name);
7281 return 1;
7282
7283 default:
7284 return 1;
7285 }
7286
7287 return 1;
7288}
7289
7290static void hlua_cli_io_release_fct(struct appctx *appctx)
7291{
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007292 hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007293 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007294}
7295
7296/* This function is an LUA binding used for registering
7297 * new keywords in the cli. It expects a list of keywords
7298 * which are the "path". It is limited to 5 keywords. A
7299 * description of the command, a function to be executed
7300 * for the parsing and a function for io handlers.
7301 */
7302__LJMP static int hlua_register_cli(lua_State *L)
7303{
7304 struct cli_kw_list *cli_kws;
7305 const char *message;
7306 int ref_io;
7307 int len;
7308 struct hlua_function *fcn;
7309 int index;
7310 int i;
7311
7312 MAY_LJMP(check_args(L, 3, "register_cli"));
7313
7314 /* First argument : an array of maximum 5 keywords. */
7315 if (!lua_istable(L, 1))
7316 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table"));
7317
7318 /* Second argument : string with contextual message. */
7319 message = MAY_LJMP(luaL_checkstring(L, 2));
7320
7321 /* Third and fourth argument : lua function. */
7322 ref_io = MAY_LJMP(hlua_checkfunction(L, 3));
7323
7324 /* Allocate and fill the sample fetch keyword struct. */
7325 cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2);
7326 if (!cli_kws)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007327 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007328 fcn = calloc(1, sizeof(*fcn));
7329 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007330 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007331
7332 /* Fill path. */
7333 index = 0;
7334 lua_pushnil(L);
7335 while(lua_next(L, 1) != 0) {
7336 if (index >= 5)
7337 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries"));
7338 if (lua_type(L, -1) != LUA_TSTRING)
7339 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings"));
7340 cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1));
7341 if (!cli_kws->kw[0].str_kw[index])
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007342 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007343 index++;
7344 lua_pop(L, 1);
7345 }
7346
7347 /* Copy help message. */
7348 cli_kws->kw[0].usage = strdup(message);
7349 if (!cli_kws->kw[0].usage)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007350 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007351
7352 /* Fill fcn io handler. */
7353 len = strlen("<lua.cli>") + 1;
7354 for (i = 0; i < index; i++)
7355 len += strlen(cli_kws->kw[0].str_kw[i]) + 1;
7356 fcn->name = calloc(1, len);
7357 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007358 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007359 strncat((char *)fcn->name, "<lua.cli", len);
7360 for (i = 0; i < index; i++) {
7361 strncat((char *)fcn->name, ".", len);
7362 strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len);
7363 }
7364 strncat((char *)fcn->name, ">", len);
7365 fcn->function_ref = ref_io;
7366
7367 /* Fill last entries. */
7368 cli_kws->kw[0].private = fcn;
7369 cli_kws->kw[0].parse = hlua_cli_parse_fct;
7370 cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct;
7371 cli_kws->kw[0].io_release = hlua_cli_io_release_fct;
7372
7373 /* Register this new converter */
7374 cli_register_kw(cli_kws);
7375
7376 return 0;
7377}
7378
Thierry FOURNIERbd413492015-03-03 16:52:26 +01007379static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx,
7380 struct proxy *defpx, const char *file, int line,
7381 char **err, unsigned int *timeout)
7382{
7383 const char *error;
7384
7385 error = parse_time_err(args[1], timeout, TIME_UNIT_MS);
7386 if (error && *error != '\0') {
7387 memprintf(err, "%s: invalid timeout", args[0]);
7388 return -1;
7389 }
7390 return 0;
7391}
7392
7393static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx,
7394 struct proxy *defpx, const char *file, int line,
7395 char **err)
7396{
7397 return hlua_read_timeout(args, section_type, curpx, defpx,
7398 file, line, err, &hlua_timeout_session);
7399}
7400
7401static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx,
7402 struct proxy *defpx, const char *file, int line,
7403 char **err)
7404{
7405 return hlua_read_timeout(args, section_type, curpx, defpx,
7406 file, line, err, &hlua_timeout_task);
7407}
7408
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007409static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx,
7410 struct proxy *defpx, const char *file, int line,
7411 char **err)
7412{
7413 return hlua_read_timeout(args, section_type, curpx, defpx,
7414 file, line, err, &hlua_timeout_applet);
7415}
7416
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01007417static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx,
7418 struct proxy *defpx, const char *file, int line,
7419 char **err)
7420{
7421 char *error;
7422
7423 hlua_nb_instruction = strtoll(args[1], &error, 10);
7424 if (*error != '\0') {
7425 memprintf(err, "%s: invalid number", args[0]);
7426 return -1;
7427 }
7428 return 0;
7429}
7430
Willy Tarreau32f61e22015-03-18 17:54:59 +01007431static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx,
7432 struct proxy *defpx, const char *file, int line,
7433 char **err)
7434{
7435 char *error;
7436
7437 if (*(args[1]) == 0) {
7438 memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]);
7439 return -1;
7440 }
7441 hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L;
7442 if (*error != '\0') {
7443 memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error);
7444 return -1;
7445 }
7446 return 0;
7447}
7448
7449
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007450/* This function is called by the main configuration key "lua-load". It loads and
7451 * execute an lua file during the parsing of the HAProxy configuration file. It is
7452 * the main lua entry point.
7453 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007454 * This function runs with the HAProxy keywords API. It returns -1 if an error
7455 * occurs, otherwise it returns 0.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007456 *
7457 * In some error case, LUA set an error message in top of the stack. This function
7458 * returns this error message in the HAProxy logs and pop it from the stack.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007459 *
7460 * This function can fail with an abort() due to an Lua critical error.
7461 * We are in the configuration parsing process of HAProxy, this abort() is
7462 * tolerated.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007463 */
7464static int hlua_load(char **args, int section_type, struct proxy *curpx,
7465 struct proxy *defpx, const char *file, int line,
7466 char **err)
7467{
7468 int error;
7469
7470 /* Just load and compile the file. */
7471 error = luaL_loadfile(gL.T, args[1]);
7472 if (error) {
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007473 memprintf(err, "error in Lua file '%s': %s", args[1], lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007474 lua_pop(gL.T, 1);
7475 return -1;
7476 }
7477
7478 /* If no syntax error where detected, execute the code. */
7479 error = lua_pcall(gL.T, 0, LUA_MULTRET, 0);
7480 switch (error) {
7481 case LUA_OK:
7482 break;
7483 case LUA_ERRRUN:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007484 memprintf(err, "Lua runtime error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007485 lua_pop(gL.T, 1);
7486 return -1;
7487 case LUA_ERRMEM:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007488 memprintf(err, "Lua out of memory error.n");
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007489 return -1;
7490 case LUA_ERRERR:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007491 memprintf(err, "Lua message handler error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007492 lua_pop(gL.T, 1);
7493 return -1;
7494 case LUA_ERRGCMM:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007495 memprintf(err, "Lua garbage collector error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007496 lua_pop(gL.T, 1);
7497 return -1;
7498 default:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007499 memprintf(err, "Lua unknonwn error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007500 lua_pop(gL.T, 1);
7501 return -1;
7502 }
7503
7504 return 0;
7505}
7506
7507/* configuration keywords declaration */
7508static struct cfg_kw_list cfg_kws = {{ },{
Thierry FOURNIERbd413492015-03-03 16:52:26 +01007509 { CFG_GLOBAL, "lua-load", hlua_load },
7510 { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout },
7511 { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout },
Thierry FOURNIER56da1012015-10-01 08:42:31 +02007512 { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout },
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01007513 { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield },
Willy Tarreau32f61e22015-03-18 17:54:59 +01007514 { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem },
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007515 { 0, NULL, NULL },
7516}};
7517
Willy Tarreau0108d902018-11-25 19:14:37 +01007518INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
7519
Christopher Fauletafd8f102018-11-08 11:34:21 +01007520static int hlua_check_config()
7521{
7522 struct proxy *px;
7523 struct acl *acl;
7524 struct acl_expr *expr;
7525 struct sample_fetch *fetch;
7526 int err = 0;
7527
7528 for (px = proxies_list; px; px = px->next) {
7529 if (!(px->options2 & PR_O2_USE_HTX))
7530 continue;
7531
7532 list_for_each_entry(acl, &px->acl, list) {
7533 list_for_each_entry(expr, &acl->expr, list) {
7534 fetch = expr->smp->fetch;
7535 if (fetch->process != hlua_sample_fetch_wrapper)
7536 continue;
7537
7538 ha_alert("config: %s '%s': sample-fetch '%s' cannot be used used "
7539 "when the HTX internal representation is enabled.\n",
7540 proxy_type_str(px), px->id, fetch->kw);
7541 err++;
7542 }
7543 }
7544 }
7545 return err;
7546}
7547
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007548/* This function can fail with an abort() due to an Lua critical error.
7549 * We are in the initialisation process of HAProxy, this abort() is
7550 * tolerated.
7551 */
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007552int hlua_post_init()
7553{
7554 struct hlua_init_function *init;
7555 const char *msg;
7556 enum hlua_exec ret;
Thierry Fournierfd107a22016-02-19 19:57:23 +01007557 const char *error;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007558
Thierry Fournier3d4a6752016-02-19 20:53:30 +01007559 /* Call post initialisation function in safe environement. */
7560 if (!SET_SAFE_LJMP(gL.T)) {
7561 if (lua_type(gL.T, -1) == LUA_TSTRING)
7562 error = lua_tostring(gL.T, -1);
7563 else
7564 error = "critical error";
7565 fprintf(stderr, "Lua post-init: %s.\n", error);
7566 exit(1);
7567 }
Frédéric Lécaille54f2bcf2018-08-29 13:46:24 +02007568
7569#if USE_OPENSSL
7570 /* Initialize SSL server. */
7571 if (socket_ssl.xprt->prepare_srv) {
7572 int saved_used_backed = global.ssl_used_backend;
7573 // don't affect maxconn automatic computation
7574 socket_ssl.xprt->prepare_srv(&socket_ssl);
7575 global.ssl_used_backend = saved_used_backed;
7576 }
7577#endif
7578
Thierry Fournier3d4a6752016-02-19 20:53:30 +01007579 hlua_fcn_post_init(gL.T);
7580 RESET_SAFE_LJMP(gL.T);
7581
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007582 list_for_each_entry(init, &hlua_init_functions, l) {
7583 lua_rawgeti(gL.T, LUA_REGISTRYINDEX, init->function_ref);
7584 ret = hlua_ctx_resume(&gL, 0);
7585 switch (ret) {
7586 case HLUA_E_OK:
7587 lua_pop(gL.T, -1);
7588 return 1;
7589 case HLUA_E_AGAIN:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007590 ha_alert("Lua init: yield not allowed.\n");
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007591 return 0;
7592 case HLUA_E_ERRMSG:
7593 msg = lua_tostring(gL.T, -1);
Christopher Faulet767a84b2017-11-24 16:50:31 +01007594 ha_alert("lua init: %s.\n", msg);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007595 return 0;
7596 case HLUA_E_ERR:
7597 default:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007598 ha_alert("Lua init: unknown runtime error.\n");
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007599 return 0;
7600 }
7601 }
7602 return 1;
7603}
7604
Willy Tarreau32f61e22015-03-18 17:54:59 +01007605/* The memory allocator used by the Lua stack. <ud> is a pointer to the
7606 * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize>
7607 * is the previously allocated size or the kind of object in case of a new
7608 * allocation. <nsize> is the requested new size.
7609 */
7610static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
7611{
7612 struct hlua_mem_allocator *zone = ud;
7613
7614 if (nsize == 0) {
7615 /* it's a free */
7616 if (ptr)
7617 zone->allocated -= osize;
7618 free(ptr);
7619 return NULL;
7620 }
7621
7622 if (!ptr) {
7623 /* it's a new allocation */
7624 if (zone->limit && zone->allocated + nsize > zone->limit)
7625 return NULL;
7626
7627 ptr = malloc(nsize);
7628 if (ptr)
7629 zone->allocated += nsize;
7630 return ptr;
7631 }
7632
7633 /* it's a realloc */
7634 if (zone->limit && zone->allocated + nsize - osize > zone->limit)
7635 return NULL;
7636
7637 ptr = realloc(ptr, nsize);
7638 if (ptr)
7639 zone->allocated += nsize - osize;
7640 return ptr;
7641}
7642
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007643/* Ithis function can fail with an abort() due to an Lua critical error.
7644 * We are in the initialisation process of HAProxy, this abort() is
7645 * tolerated.
7646 */
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01007647void hlua_init(void)
7648{
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007649 int i;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01007650 int idx;
7651 struct sample_fetch *sf;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007652 struct sample_conv *sc;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01007653 char *p;
Thierry Fournierfd107a22016-02-19 19:57:23 +01007654 const char *error_msg;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01007655#ifdef USE_OPENSSL
Willy Tarreau80f5fae2015-02-27 16:38:20 +01007656 struct srv_kw *kw;
7657 int tmp_error;
7658 char *error;
Thierry FOURNIER36d13742015-03-17 16:48:53 +01007659 char *args[] = { /* SSL client configuration. */
7660 "ssl",
7661 "verify",
7662 "none",
Thierry FOURNIER36d13742015-03-17 16:48:53 +01007663 NULL
7664 };
Willy Tarreau80f5fae2015-02-27 16:38:20 +01007665#endif
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007666
Thierry FOURNIER380d0932015-01-23 14:27:52 +01007667 /* Init main lua stack. */
7668 gL.Mref = LUA_REFNIL;
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01007669 gL.flags = 0;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01007670 LIST_INIT(&gL.com);
Willy Tarreau42ef75f2017-04-12 21:40:29 +02007671 gL.T = lua_newstate(hlua_alloc, &hlua_global_allocator);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01007672 hlua_sethlua(&gL);
7673 gL.Tref = LUA_REFNIL;
7674 gL.task = NULL;
7675
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007676 /* From this point, until the end of the initialisation function,
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007677 * the Lua function can fail with an abort. We are in the initialisation
7678 * process of HAProxy, this abort() is tolerated.
7679 */
7680
Thierry FOURNIER380d0932015-01-23 14:27:52 +01007681 /* Initialise lua. */
7682 luaL_openlibs(gL.T);
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007683
Thierry Fournier75933d42016-01-21 09:30:18 +01007684 /* Set safe environment for the initialisation. */
7685 if (!SET_SAFE_LJMP(gL.T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01007686 if (lua_type(gL.T, -1) == LUA_TSTRING)
7687 error_msg = lua_tostring(gL.T, -1);
7688 else
7689 error_msg = "critical error";
7690 fprintf(stderr, "Lua init: %s.\n", error_msg);
Thierry Fournier75933d42016-01-21 09:30:18 +01007691 exit(1);
7692 }
7693
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007694 /*
7695 *
7696 * Create "core" object.
7697 *
7698 */
7699
Thierry FOURNIERa2d8c652015-03-11 17:29:39 +01007700 /* This table entry is the object "core" base. */
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007701 lua_newtable(gL.T);
7702
7703 /* Push the loglevel constants. */
Willy Tarreau80f5fae2015-02-27 16:38:20 +01007704 for (i = 0; i < NB_LOG_LEVELS; i++)
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007705 hlua_class_const_int(gL.T, log_levels[i], i);
7706
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007707 /* Register special functions. */
7708 hlua_class_function(gL.T, "register_init", hlua_register_init);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01007709 hlua_class_function(gL.T, "register_task", hlua_register_task);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01007710 hlua_class_function(gL.T, "register_fetches", hlua_register_fetches);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01007711 hlua_class_function(gL.T, "register_converters", hlua_register_converters);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007712 hlua_class_function(gL.T, "register_action", hlua_register_action);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007713 hlua_class_function(gL.T, "register_service", hlua_register_service);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007714 hlua_class_function(gL.T, "register_cli", hlua_register_cli);
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01007715 hlua_class_function(gL.T, "yield", hlua_yield);
Willy Tarreau59551662015-03-10 14:23:13 +01007716 hlua_class_function(gL.T, "set_nice", hlua_set_nice);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01007717 hlua_class_function(gL.T, "sleep", hlua_sleep);
7718 hlua_class_function(gL.T, "msleep", hlua_msleep);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01007719 hlua_class_function(gL.T, "add_acl", hlua_add_acl);
7720 hlua_class_function(gL.T, "del_acl", hlua_del_acl);
7721 hlua_class_function(gL.T, "set_map", hlua_set_map);
7722 hlua_class_function(gL.T, "del_map", hlua_del_map);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007723 hlua_class_function(gL.T, "tcp", hlua_socket_new);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01007724 hlua_class_function(gL.T, "log", hlua_log);
7725 hlua_class_function(gL.T, "Debug", hlua_log_debug);
7726 hlua_class_function(gL.T, "Info", hlua_log_info);
7727 hlua_class_function(gL.T, "Warning", hlua_log_warning);
7728 hlua_class_function(gL.T, "Alert", hlua_log_alert);
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02007729 hlua_class_function(gL.T, "done", hlua_done);
Thierry Fournierfb0b5462016-01-21 09:28:58 +01007730 hlua_fcn_reg_core_fcn(gL.T);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007731
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007732 lua_setglobal(gL.T, "core");
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01007733
7734 /*
7735 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02007736 * Register class Map
7737 *
7738 */
7739
7740 /* This table entry is the object "Map" base. */
7741 lua_newtable(gL.T);
7742
7743 /* register pattern types. */
7744 for (i=0; i<PAT_MATCH_NUM; i++)
7745 hlua_class_const_int(gL.T, pat_match_names[i], i);
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01007746 for (i=0; i<PAT_MATCH_NUM; i++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007747 snprintf(trash.area, trash.size, "_%s", pat_match_names[i]);
7748 hlua_class_const_int(gL.T, trash.area, i);
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01007749 }
Thierry FOURNIER3def3932015-04-07 11:27:54 +02007750
7751 /* register constructor. */
7752 hlua_class_function(gL.T, "new", hlua_map_new);
7753
7754 /* Create and fill the metatable. */
7755 lua_newtable(gL.T);
7756
7757 /* Create and fille the __index entry. */
7758 lua_pushstring(gL.T, "__index");
7759 lua_newtable(gL.T);
7760
7761 /* Register . */
7762 hlua_class_function(gL.T, "lookup", hlua_map_lookup);
7763 hlua_class_function(gL.T, "slookup", hlua_map_slookup);
7764
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007765 lua_rawset(gL.T, -3);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02007766
Thierry Fournier45e78d72016-02-19 18:34:46 +01007767 /* Register previous table in the registry with reference and named entry.
7768 * The function hlua_register_metatable() pops the stack, so we
7769 * previously create a copy of the table.
7770 */
Thierry FOURNIER3def3932015-04-07 11:27:54 +02007771 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007772 class_map_ref = hlua_register_metatable(gL.T, CLASS_MAP);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02007773
7774 /* Assign the metatable to the mai Map object. */
7775 lua_setmetatable(gL.T, -2);
7776
7777 /* Set a name to the table. */
7778 lua_setglobal(gL.T, "Map");
7779
7780 /*
7781 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01007782 * Register class Channel
7783 *
7784 */
7785
7786 /* Create and fill the metatable. */
7787 lua_newtable(gL.T);
7788
7789 /* Create and fille the __index entry. */
7790 lua_pushstring(gL.T, "__index");
7791 lua_newtable(gL.T);
7792
7793 /* Register . */
7794 hlua_class_function(gL.T, "get", hlua_channel_get);
7795 hlua_class_function(gL.T, "dup", hlua_channel_dup);
7796 hlua_class_function(gL.T, "getline", hlua_channel_getline);
7797 hlua_class_function(gL.T, "set", hlua_channel_set);
7798 hlua_class_function(gL.T, "append", hlua_channel_append);
7799 hlua_class_function(gL.T, "send", hlua_channel_send);
7800 hlua_class_function(gL.T, "forward", hlua_channel_forward);
7801 hlua_class_function(gL.T, "get_in_len", hlua_channel_get_in_len);
7802 hlua_class_function(gL.T, "get_out_len", hlua_channel_get_out_len);
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01007803 hlua_class_function(gL.T, "is_full", hlua_channel_is_full);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01007804
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007805 lua_rawset(gL.T, -3);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01007806
7807 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007808 class_channel_ref = hlua_register_metatable(gL.T, CLASS_CHANNEL);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01007809
7810 /*
7811 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01007812 * Register class Fetches
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01007813 *
7814 */
7815
7816 /* Create and fill the metatable. */
7817 lua_newtable(gL.T);
7818
7819 /* Create and fille the __index entry. */
7820 lua_pushstring(gL.T, "__index");
7821 lua_newtable(gL.T);
7822
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01007823 /* Browse existing fetches and create the associated
7824 * object method.
7825 */
7826 sf = NULL;
7827 while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) {
7828
7829 /* Dont register the keywork if the arguments check function are
7830 * not safe during the runtime.
7831 */
7832 if ((sf->val_args != NULL) &&
7833 (sf->val_args != val_payload_lv) &&
7834 (sf->val_args != val_hdr))
7835 continue;
7836
7837 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
7838 * by an underscore.
7839 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007840 strncpy(trash.area, sf->kw, trash.size);
7841 trash.area[trash.size - 1] = '\0';
7842 for (p = trash.area; *p; p++)
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01007843 if (*p == '.' || *p == '-' || *p == '+')
7844 *p = '_';
7845
7846 /* Register the function. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007847 lua_pushstring(gL.T, trash.area);
Willy Tarreau2ec22742015-03-10 14:27:20 +01007848 lua_pushlightuserdata(gL.T, sf);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01007849 lua_pushcclosure(gL.T, hlua_run_sample_fetch, 1);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007850 lua_rawset(gL.T, -3);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01007851 }
7852
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007853 lua_rawset(gL.T, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01007854
7855 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007856 class_fetches_ref = hlua_register_metatable(gL.T, CLASS_FETCHES);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01007857
7858 /*
7859 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007860 * Register class Converters
7861 *
7862 */
7863
7864 /* Create and fill the metatable. */
7865 lua_newtable(gL.T);
7866
7867 /* Create and fill the __index entry. */
7868 lua_pushstring(gL.T, "__index");
7869 lua_newtable(gL.T);
7870
7871 /* Browse existing converters and create the associated
7872 * object method.
7873 */
7874 sc = NULL;
7875 while ((sc = sample_conv_getnext(sc, &idx)) != NULL) {
7876 /* Dont register the keywork if the arguments check function are
7877 * not safe during the runtime.
7878 */
7879 if (sc->val_args != NULL)
7880 continue;
7881
7882 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
7883 * by an underscore.
7884 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007885 strncpy(trash.area, sc->kw, trash.size);
7886 trash.area[trash.size - 1] = '\0';
7887 for (p = trash.area; *p; p++)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007888 if (*p == '.' || *p == '-' || *p == '+')
7889 *p = '_';
7890
7891 /* Register the function. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007892 lua_pushstring(gL.T, trash.area);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007893 lua_pushlightuserdata(gL.T, sc);
7894 lua_pushcclosure(gL.T, hlua_run_sample_conv, 1);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007895 lua_rawset(gL.T, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007896 }
7897
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007898 lua_rawset(gL.T, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007899
7900 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007901 class_converters_ref = hlua_register_metatable(gL.T, CLASS_CONVERTERS);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007902
7903 /*
7904 *
Thierry FOURNIER08504f42015-03-16 14:17:08 +01007905 * Register class HTTP
7906 *
7907 */
7908
7909 /* Create and fill the metatable. */
7910 lua_newtable(gL.T);
7911
7912 /* Create and fille the __index entry. */
7913 lua_pushstring(gL.T, "__index");
7914 lua_newtable(gL.T);
7915
7916 /* Register Lua functions. */
7917 hlua_class_function(gL.T, "req_get_headers",hlua_http_req_get_headers);
7918 hlua_class_function(gL.T, "req_del_header", hlua_http_req_del_hdr);
7919 hlua_class_function(gL.T, "req_rep_header", hlua_http_req_rep_hdr);
7920 hlua_class_function(gL.T, "req_rep_value", hlua_http_req_rep_val);
7921 hlua_class_function(gL.T, "req_add_header", hlua_http_req_add_hdr);
7922 hlua_class_function(gL.T, "req_set_header", hlua_http_req_set_hdr);
7923 hlua_class_function(gL.T, "req_set_method", hlua_http_req_set_meth);
7924 hlua_class_function(gL.T, "req_set_path", hlua_http_req_set_path);
7925 hlua_class_function(gL.T, "req_set_query", hlua_http_req_set_query);
7926 hlua_class_function(gL.T, "req_set_uri", hlua_http_req_set_uri);
7927
7928 hlua_class_function(gL.T, "res_get_headers",hlua_http_res_get_headers);
7929 hlua_class_function(gL.T, "res_del_header", hlua_http_res_del_hdr);
7930 hlua_class_function(gL.T, "res_rep_header", hlua_http_res_rep_hdr);
7931 hlua_class_function(gL.T, "res_rep_value", hlua_http_res_rep_val);
7932 hlua_class_function(gL.T, "res_add_header", hlua_http_res_add_hdr);
7933 hlua_class_function(gL.T, "res_set_header", hlua_http_res_set_hdr);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02007934 hlua_class_function(gL.T, "res_set_status", hlua_http_res_set_status);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01007935
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007936 lua_rawset(gL.T, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01007937
7938 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007939 class_http_ref = hlua_register_metatable(gL.T, CLASS_HTTP);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01007940
7941 /*
7942 *
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007943 * Register class AppletTCP
7944 *
7945 */
7946
7947 /* Create and fill the metatable. */
7948 lua_newtable(gL.T);
7949
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007950 /* Create and fille the __index entry. */
7951 lua_pushstring(gL.T, "__index");
7952 lua_newtable(gL.T);
7953
7954 /* Register Lua functions. */
Thierry FOURNIER / OZON.IO3e1d7912016-12-12 12:29:34 +01007955 hlua_class_function(gL.T, "getline", hlua_applet_tcp_getline);
7956 hlua_class_function(gL.T, "receive", hlua_applet_tcp_recv);
7957 hlua_class_function(gL.T, "send", hlua_applet_tcp_send);
7958 hlua_class_function(gL.T, "set_priv", hlua_applet_tcp_set_priv);
7959 hlua_class_function(gL.T, "get_priv", hlua_applet_tcp_get_priv);
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01007960 hlua_class_function(gL.T, "set_var", hlua_applet_tcp_set_var);
7961 hlua_class_function(gL.T, "unset_var", hlua_applet_tcp_unset_var);
7962 hlua_class_function(gL.T, "get_var", hlua_applet_tcp_get_var);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007963
7964 lua_settable(gL.T, -3);
7965
7966 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007967 class_applet_tcp_ref = hlua_register_metatable(gL.T, CLASS_APPLET_TCP);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007968
7969 /*
7970 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007971 * Register class AppletHTTP
7972 *
7973 */
7974
7975 /* Create and fill the metatable. */
7976 lua_newtable(gL.T);
7977
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007978 /* Create and fille the __index entry. */
7979 lua_pushstring(gL.T, "__index");
7980 lua_newtable(gL.T);
7981
7982 /* Register Lua functions. */
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01007983 hlua_class_function(gL.T, "set_priv", hlua_applet_http_set_priv);
7984 hlua_class_function(gL.T, "get_priv", hlua_applet_http_get_priv);
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01007985 hlua_class_function(gL.T, "set_var", hlua_applet_http_set_var);
7986 hlua_class_function(gL.T, "unset_var", hlua_applet_http_unset_var);
7987 hlua_class_function(gL.T, "get_var", hlua_applet_http_get_var);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007988 hlua_class_function(gL.T, "getline", hlua_applet_http_getline);
7989 hlua_class_function(gL.T, "receive", hlua_applet_http_recv);
7990 hlua_class_function(gL.T, "send", hlua_applet_http_send);
7991 hlua_class_function(gL.T, "add_header", hlua_applet_http_addheader);
7992 hlua_class_function(gL.T, "set_status", hlua_applet_http_status);
7993 hlua_class_function(gL.T, "start_response", hlua_applet_http_start_response);
7994
7995 lua_settable(gL.T, -3);
7996
7997 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007998 class_applet_http_ref = hlua_register_metatable(gL.T, CLASS_APPLET_HTTP);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007999
8000 /*
8001 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008002 * Register class TXN
8003 *
8004 */
8005
8006 /* Create and fill the metatable. */
8007 lua_newtable(gL.T);
8008
8009 /* Create and fille the __index entry. */
8010 lua_pushstring(gL.T, "__index");
8011 lua_newtable(gL.T);
8012
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01008013 /* Register Lua functions. */
Patrick Hemmer268a7072018-05-11 12:52:31 -04008014 hlua_class_function(gL.T, "set_priv", hlua_set_priv);
8015 hlua_class_function(gL.T, "get_priv", hlua_get_priv);
8016 hlua_class_function(gL.T, "set_var", hlua_set_var);
8017 hlua_class_function(gL.T, "unset_var", hlua_unset_var);
8018 hlua_class_function(gL.T, "get_var", hlua_get_var);
8019 hlua_class_function(gL.T, "done", hlua_txn_done);
8020 hlua_class_function(gL.T, "set_loglevel", hlua_txn_set_loglevel);
8021 hlua_class_function(gL.T, "set_tos", hlua_txn_set_tos);
8022 hlua_class_function(gL.T, "set_mark", hlua_txn_set_mark);
8023 hlua_class_function(gL.T, "set_priority_class", hlua_txn_set_priority_class);
8024 hlua_class_function(gL.T, "set_priority_offset", hlua_txn_set_priority_offset);
8025 hlua_class_function(gL.T, "deflog", hlua_txn_deflog);
8026 hlua_class_function(gL.T, "log", hlua_txn_log);
8027 hlua_class_function(gL.T, "Debug", hlua_txn_log_debug);
8028 hlua_class_function(gL.T, "Info", hlua_txn_log_info);
8029 hlua_class_function(gL.T, "Warning", hlua_txn_log_warning);
8030 hlua_class_function(gL.T, "Alert", hlua_txn_log_alert);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01008031
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008032 lua_rawset(gL.T, -3);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008033
8034 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008035 class_txn_ref = hlua_register_metatable(gL.T, CLASS_TXN);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008036
8037 /*
8038 *
8039 * Register class Socket
8040 *
8041 */
8042
8043 /* Create and fill the metatable. */
8044 lua_newtable(gL.T);
8045
8046 /* Create and fille the __index entry. */
8047 lua_pushstring(gL.T, "__index");
8048 lua_newtable(gL.T);
8049
Baptiste Assmann84bb4932015-03-02 21:40:06 +01008050#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008051 hlua_class_function(gL.T, "connect_ssl", hlua_socket_connect_ssl);
Baptiste Assmann84bb4932015-03-02 21:40:06 +01008052#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008053 hlua_class_function(gL.T, "connect", hlua_socket_connect);
8054 hlua_class_function(gL.T, "send", hlua_socket_send);
8055 hlua_class_function(gL.T, "receive", hlua_socket_receive);
8056 hlua_class_function(gL.T, "close", hlua_socket_close);
8057 hlua_class_function(gL.T, "getpeername", hlua_socket_getpeername);
8058 hlua_class_function(gL.T, "getsockname", hlua_socket_getsockname);
8059 hlua_class_function(gL.T, "setoption", hlua_socket_setoption);
8060 hlua_class_function(gL.T, "settimeout", hlua_socket_settimeout);
8061
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008062 lua_rawset(gL.T, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008063
8064 /* Register the garbage collector entry. */
8065 lua_pushstring(gL.T, "__gc");
8066 lua_pushcclosure(gL.T, hlua_socket_gc, 0);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008067 lua_rawset(gL.T, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008068
8069 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008070 class_socket_ref = hlua_register_metatable(gL.T, CLASS_SOCKET);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008071
8072 /* Proxy and server configuration initialisation. */
8073 memset(&socket_proxy, 0, sizeof(socket_proxy));
8074 init_new_proxy(&socket_proxy);
8075 socket_proxy.parent = NULL;
8076 socket_proxy.last_change = now.tv_sec;
8077 socket_proxy.id = "LUA-SOCKET";
8078 socket_proxy.cap = PR_CAP_FE | PR_CAP_BE;
8079 socket_proxy.maxconn = 0;
8080 socket_proxy.accept = NULL;
8081 socket_proxy.options2 |= PR_O2_INDEPSTR;
8082 socket_proxy.srv = NULL;
8083 socket_proxy.conn_retries = 0;
8084 socket_proxy.timeout.connect = 5000; /* By default the timeout connection is 5s. */
8085
8086 /* Init TCP server: unchanged parameters */
8087 memset(&socket_tcp, 0, sizeof(socket_tcp));
8088 socket_tcp.next = NULL;
8089 socket_tcp.proxy = &socket_proxy;
8090 socket_tcp.obj_type = OBJ_TYPE_SERVER;
8091 LIST_INIT(&socket_tcp.actconns);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04008092 socket_tcp.pendconns = EB_ROOT;
Christopher Faulet40a007c2017-07-03 15:41:01 +02008093 socket_tcp.priv_conns = NULL;
8094 socket_tcp.idle_conns = NULL;
8095 socket_tcp.safe_conns = NULL;
Emeric Brun52a91d32017-08-31 14:41:55 +02008096 socket_tcp.next_state = SRV_ST_RUNNING; /* early server setup */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008097 socket_tcp.last_change = 0;
8098 socket_tcp.id = "LUA-TCP-CONN";
8099 socket_tcp.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8100 socket_tcp.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8101 socket_tcp.pp_opts = 0; /* Remove proxy protocol. */
8102
8103 /* XXX: Copy default parameter from default server,
8104 * but the default server is not initialized.
8105 */
8106 socket_tcp.maxqueue = socket_proxy.defsrv.maxqueue;
8107 socket_tcp.minconn = socket_proxy.defsrv.minconn;
8108 socket_tcp.maxconn = socket_proxy.defsrv.maxconn;
8109 socket_tcp.slowstart = socket_proxy.defsrv.slowstart;
8110 socket_tcp.onerror = socket_proxy.defsrv.onerror;
8111 socket_tcp.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
8112 socket_tcp.onmarkedup = socket_proxy.defsrv.onmarkedup;
8113 socket_tcp.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
8114 socket_tcp.uweight = socket_proxy.defsrv.iweight;
8115 socket_tcp.iweight = socket_proxy.defsrv.iweight;
8116
8117 socket_tcp.check.status = HCHK_STATUS_INI;
8118 socket_tcp.check.rise = socket_proxy.defsrv.check.rise;
8119 socket_tcp.check.fall = socket_proxy.defsrv.check.fall;
8120 socket_tcp.check.health = socket_tcp.check.rise; /* socket, but will fall down at first failure */
8121 socket_tcp.check.server = &socket_tcp;
8122
8123 socket_tcp.agent.status = HCHK_STATUS_INI;
8124 socket_tcp.agent.rise = socket_proxy.defsrv.agent.rise;
8125 socket_tcp.agent.fall = socket_proxy.defsrv.agent.fall;
8126 socket_tcp.agent.health = socket_tcp.agent.rise; /* socket, but will fall down at first failure */
8127 socket_tcp.agent.server = &socket_tcp;
8128
Willy Tarreaua261e9b2016-12-22 20:44:00 +01008129 socket_tcp.xprt = xprt_get(XPRT_RAW);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008130
8131#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008132 /* Init TCP server: unchanged parameters */
8133 memset(&socket_ssl, 0, sizeof(socket_ssl));
8134 socket_ssl.next = NULL;
8135 socket_ssl.proxy = &socket_proxy;
8136 socket_ssl.obj_type = OBJ_TYPE_SERVER;
8137 LIST_INIT(&socket_ssl.actconns);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04008138 socket_ssl.pendconns = EB_ROOT;
Christopher Faulet40a007c2017-07-03 15:41:01 +02008139 socket_tcp.priv_conns = NULL;
8140 socket_tcp.idle_conns = NULL;
8141 socket_tcp.safe_conns = NULL;
Emeric Brun52a91d32017-08-31 14:41:55 +02008142 socket_ssl.next_state = SRV_ST_RUNNING; /* early server setup */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008143 socket_ssl.last_change = 0;
8144 socket_ssl.id = "LUA-SSL-CONN";
8145 socket_ssl.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8146 socket_ssl.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8147 socket_ssl.pp_opts = 0; /* Remove proxy protocol. */
8148
8149 /* XXX: Copy default parameter from default server,
8150 * but the default server is not initialized.
8151 */
8152 socket_ssl.maxqueue = socket_proxy.defsrv.maxqueue;
8153 socket_ssl.minconn = socket_proxy.defsrv.minconn;
8154 socket_ssl.maxconn = socket_proxy.defsrv.maxconn;
8155 socket_ssl.slowstart = socket_proxy.defsrv.slowstart;
8156 socket_ssl.onerror = socket_proxy.defsrv.onerror;
8157 socket_ssl.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
8158 socket_ssl.onmarkedup = socket_proxy.defsrv.onmarkedup;
8159 socket_ssl.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
8160 socket_ssl.uweight = socket_proxy.defsrv.iweight;
8161 socket_ssl.iweight = socket_proxy.defsrv.iweight;
8162
8163 socket_ssl.check.status = HCHK_STATUS_INI;
8164 socket_ssl.check.rise = socket_proxy.defsrv.check.rise;
8165 socket_ssl.check.fall = socket_proxy.defsrv.check.fall;
8166 socket_ssl.check.health = socket_ssl.check.rise; /* socket, but will fall down at first failure */
8167 socket_ssl.check.server = &socket_ssl;
8168
8169 socket_ssl.agent.status = HCHK_STATUS_INI;
8170 socket_ssl.agent.rise = socket_proxy.defsrv.agent.rise;
8171 socket_ssl.agent.fall = socket_proxy.defsrv.agent.fall;
8172 socket_ssl.agent.health = socket_ssl.agent.rise; /* socket, but will fall down at first failure */
8173 socket_ssl.agent.server = &socket_ssl;
8174
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008175 socket_ssl.use_ssl = 1;
Willy Tarreaua261e9b2016-12-22 20:44:00 +01008176 socket_ssl.xprt = xprt_get(XPRT_SSL);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008177
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008178 for (idx = 0; args[idx] != NULL; idx++) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008179 if ((kw = srv_find_kw(args[idx])) != NULL) { /* Maybe it's registered server keyword */
8180 /*
8181 *
8182 * If the keyword is not known, we can search in the registered
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08008183 * server keywords. This is useful to configure special SSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008184 * features like client certificates and ssl_verify.
8185 *
8186 */
8187 tmp_error = kw->parse(args, &idx, &socket_proxy, &socket_ssl, &error);
8188 if (tmp_error != 0) {
8189 fprintf(stderr, "INTERNAL ERROR: %s\n", error);
8190 abort(); /* This must be never arrives because the command line
8191 not editable by the user. */
8192 }
8193 idx += kw->skip;
8194 }
8195 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008196#endif
Thierry Fournier75933d42016-01-21 09:30:18 +01008197
8198 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01008199}
Willy Tarreaubb57d942016-12-21 19:04:56 +01008200
Willy Tarreau80713382018-11-26 10:19:54 +01008201static void hlua_register_build_options(void)
8202{
Willy Tarreaubb57d942016-12-21 19:04:56 +01008203 char *ptr = NULL;
Willy Tarreau80713382018-11-26 10:19:54 +01008204
Willy Tarreaubb57d942016-12-21 19:04:56 +01008205 memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE);
8206 hap_register_build_opts(ptr, 1);
8207}
Willy Tarreau80713382018-11-26 10:19:54 +01008208
8209INITCALL0(STG_REGISTER, hlua_register_build_options);
Willy Tarreaue6552512018-11-26 11:33:13 +01008210REGISTER_CONFIG_POSTPARSER("hlua", hlua_check_config);