blob: 13e03cef732351bf1e623957b27eb2a2df24440a [file] [log] [blame]
Thierry Fourniere726b142016-02-11 17:57:57 +01001/*
2 * Lua unsafe core engine
3 *
4 * Copyright 2015-2016 Thierry Fournier <tfournier@arpalert.org>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +010013#include <ctype.h>
Mark Lakes56cc1252018-03-27 09:48:06 +020014#include <limits.h>
Thierry FOURNIERbabae282015-09-17 11:36:37 +020015#include <setjmp.h>
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +010016
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +010017#include <lauxlib.h>
18#include <lua.h>
19#include <lualib.h>
20
Thierry FOURNIER463119c2015-03-10 00:35:36 +010021#if !defined(LUA_VERSION_NUM) || LUA_VERSION_NUM < 503
22#error "Requires Lua 5.3 or later."
Cyril Bontédc0306e2015-03-02 00:08:40 +010023#endif
24
Thierry FOURNIER380d0932015-01-23 14:27:52 +010025#include <ebpttree.h>
26
27#include <common/cfgparse.h>
Willy Tarreaub059b892018-10-16 17:57:36 +020028#include <common/compiler.h>
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +020029#include <common/hathreads.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010030#include <common/initcall.h>
31#include <common/xref.h>
Christopher Faulet724a12c2018-12-13 22:12:15 +010032#include <common/h1.h>
Thierry FOURNIER380d0932015-01-23 14:27:52 +010033
William Lallemand9ed62032016-11-21 17:49:11 +010034#include <types/cli.h>
Thierry FOURNIER380d0932015-01-23 14:27:52 +010035#include <types/hlua.h>
36#include <types/proxy.h>
William Lallemand9ed62032016-11-21 17:49:11 +010037#include <types/stats.h>
Thierry FOURNIER380d0932015-01-23 14:27:52 +010038
Thierry FOURNIER55da1652015-01-23 11:36:30 +010039#include <proto/arg.h>
Willy Tarreau8a8d83b2015-04-13 13:24:54 +020040#include <proto/applet.h>
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010041#include <proto/channel.h>
William Lallemand9ed62032016-11-21 17:49:11 +010042#include <proto/cli.h>
Willy Tarreaua71f6422016-11-16 17:00:14 +010043#include <proto/connection.h>
William Lallemand9ed62032016-11-21 17:49:11 +010044#include <proto/stats.h>
Thierry FOURNIER9a819e72015-02-16 20:22:55 +010045#include <proto/hdr_idx.h>
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +010046#include <proto/hlua.h>
Thierry Fournierfb0b5462016-01-21 09:28:58 +010047#include <proto/hlua_fcn.h>
Willy Tarreau79e57332018-10-02 16:01:16 +020048#include <proto/http_fetch.h>
Christopher Faulet724a12c2018-12-13 22:12:15 +010049#include <proto/http_htx.h>
Willy Tarreau61c112a2018-10-02 16:43:32 +020050#include <proto/http_rules.h>
Thierry FOURNIER3def3932015-04-07 11:27:54 +020051#include <proto/map.h>
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010052#include <proto/obj_type.h>
Patrick Hemmer268a7072018-05-11 12:52:31 -040053#include <proto/queue.h>
Thierry FOURNIER83758bb2015-02-04 13:21:04 +010054#include <proto/pattern.h>
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +010055#include <proto/payload.h>
56#include <proto/proto_http.h>
57#include <proto/sample.h>
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010058#include <proto/server.h>
Willy Tarreaufeb76402015-04-03 14:10:06 +020059#include <proto/session.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020060#include <proto/stream.h>
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010061#include <proto/stream_interface.h>
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +010062#include <proto/task.h>
Willy Tarreau39713102016-11-25 15:49:32 +010063#include <proto/tcp_rules.h>
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +020064#include <proto/vars.h>
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +010065
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +010066/* Lua uses longjmp to perform yield or throwing errors. This
67 * macro is used only for identifying the function that can
68 * not return because a longjmp is executed.
69 * __LJMP marks a prototype of hlua file that can use longjmp.
70 * WILL_LJMP() marks an lua function that will use longjmp.
71 * MAY_LJMP() marks an lua function that may use longjmp.
72 */
73#define __LJMP
Willy Tarreau4e7cc332018-10-20 17:45:48 +020074#define WILL_LJMP(func) do { func; my_unreachable(); } while(0)
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +010075#define MAY_LJMP(func) func
76
Thierry FOURNIERbabae282015-09-17 11:36:37 +020077/* This couple of function executes securely some Lua calls outside of
78 * the lua runtime environment. Each Lua call can return a longjmp
79 * if it encounter a memory error.
80 *
81 * Lua documentation extract:
82 *
83 * If an error happens outside any protected environment, Lua calls
84 * a panic function (see lua_atpanic) and then calls abort, thus
85 * exiting the host application. Your panic function can avoid this
86 * exit by never returning (e.g., doing a long jump to your own
87 * recovery point outside Lua).
88 *
89 * The panic function runs as if it were a message handler (see
90 * §2.3); in particular, the error message is at the top of the
91 * stack. However, there is no guarantee about stack space. To push
92 * anything on the stack, the panic function must first check the
93 * available space (see §4.2).
94 *
95 * We must check all the Lua entry point. This includes:
96 * - The include/proto/hlua.h exported functions
97 * - the task wrapper function
98 * - The action wrapper function
99 * - The converters wrapper function
100 * - The sample-fetch wrapper functions
101 *
102 * It is tolerated that the initilisation function returns an abort.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800103 * Before each Lua abort, an error message is written on stderr.
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200104 *
105 * The macro SET_SAFE_LJMP initialise the longjmp. The Macro
106 * RESET_SAFE_LJMP reset the longjmp. These function must be macro
107 * because they must be exists in the program stack when the longjmp
108 * is called.
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +0200109 *
110 * Note that the Lua processing is not really thread safe. It provides
111 * heavy system which consists to add our own lock function in the Lua
112 * code and recompile the library. This system will probably not accepted
113 * by maintainers of various distribs.
114 *
115 * Our main excution point of the Lua is the function lua_resume(). A
116 * quick looking on the Lua sources displays a lua_lock() a the start
117 * of function and a lua_unlock() at the end of the function. So I
118 * conclude that the Lua thread safe mode just perform a mutex around
119 * all execution. So I prefer to do this in the HAProxy code, it will be
120 * easier for distro maintainers.
121 *
122 * Note that the HAProxy lua functions rounded by the macro SET_SAFE_LJMP
123 * and RESET_SAFE_LJMP manipulates the Lua stack, so it will be careful
124 * to set mutex around these functions.
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200125 */
Willy Tarreau86abe442018-11-25 20:12:18 +0100126__decl_spinlock(hlua_global_lock);
Thierry FOURNIERffbad792017-07-12 11:39:04 +0200127THREAD_LOCAL jmp_buf safe_ljmp_env;
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200128static int hlua_panic_safe(lua_State *L) { return 0; }
129static int hlua_panic_ljmp(lua_State *L) { longjmp(safe_ljmp_env, 1); }
130
131#define SET_SAFE_LJMP(__L) \
132 ({ \
133 int ret; \
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100134 HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200135 if (setjmp(safe_ljmp_env) != 0) { \
136 lua_atpanic(__L, hlua_panic_safe); \
137 ret = 0; \
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100138 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200139 } else { \
140 lua_atpanic(__L, hlua_panic_ljmp); \
141 ret = 1; \
142 } \
143 ret; \
144 })
145
146/* If we are the last function catching Lua errors, we
147 * must reset the panic function.
148 */
149#define RESET_SAFE_LJMP(__L) \
150 do { \
151 lua_atpanic(__L, hlua_panic_safe); \
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100152 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200153 } while(0)
154
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200155/* Applet status flags */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200156#define APPLET_DONE 0x01 /* applet processing is done. */
157#define APPLET_100C 0x02 /* 100 continue expected. */
158#define APPLET_HDR_SENT 0x04 /* Response header sent. */
159#define APPLET_CHUNKED 0x08 /* Use transfer encoding chunked. */
160#define APPLET_LAST_CHK 0x10 /* Last chunk sent. */
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +0100161#define APPLET_HTTP11 0x20 /* Last chunk sent. */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200162
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100163/* The main Lua execution context. */
164struct hlua gL;
165
Thierry FOURNIERebed6e92016-12-16 11:54:07 +0100166/* This is the memory pool containing struct lua for applets
167 * (including cli).
168 */
Willy Tarreau8ceae722018-11-26 11:58:30 +0100169DECLARE_STATIC_POOL(pool_head_hlua, "hlua", sizeof(struct hlua));
Thierry FOURNIERebed6e92016-12-16 11:54:07 +0100170
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100171/* Used for Socket connection. */
172static struct proxy socket_proxy;
173static struct server socket_tcp;
174#ifdef USE_OPENSSL
175static struct server socket_ssl;
176#endif
177
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +0100178/* List head of the function called at the initialisation time. */
179struct list hlua_init_functions = LIST_HEAD_INIT(hlua_init_functions);
180
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +0100181/* The following variables contains the reference of the different
182 * Lua classes. These references are useful for identify metadata
183 * associated with an object.
184 */
Thierry FOURNIER65f34c62015-02-16 20:11:43 +0100185static int class_txn_ref;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100186static int class_socket_ref;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +0100187static int class_channel_ref;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +0100188static int class_fetches_ref;
Thierry FOURNIER594afe72015-03-10 23:58:30 +0100189static int class_converters_ref;
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100190static int class_http_ref;
Thierry FOURNIER3def3932015-04-07 11:27:54 +0200191static int class_map_ref;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200192static int class_applet_tcp_ref;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200193static int class_applet_http_ref;
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +0100194
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100195/* Global Lua execution timeout. By default Lua, execution linked
Willy Tarreau87b09662015-04-03 00:22:06 +0200196 * with stream (actions, sample-fetches and converters) have a
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100197 * short timeout. Lua linked with tasks doesn't have a timeout
198 * because a task may remain alive during all the haproxy execution.
199 */
200static unsigned int hlua_timeout_session = 4000; /* session timeout. */
201static unsigned int hlua_timeout_task = TICK_ETERNITY; /* task timeout. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200202static unsigned int hlua_timeout_applet = 4000; /* applet timeout. */
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100203
Thierry FOURNIERee9f8022015-03-03 17:37:37 +0100204/* Interrupts the Lua processing each "hlua_nb_instruction" instructions.
205 * it is used for preventing infinite loops.
206 *
207 * I test the scheer with an infinite loop containing one incrementation
208 * and one test. I run this loop between 10 seconds, I raise a ceil of
209 * 710M loops from one interrupt each 9000 instructions, so I fix the value
210 * to one interrupt each 10 000 instructions.
211 *
212 * configured | Number of
213 * instructions | loops executed
214 * between two | in milions
215 * forced yields |
216 * ---------------+---------------
217 * 10 | 160
218 * 500 | 670
219 * 1000 | 680
220 * 5000 | 700
221 * 7000 | 700
222 * 8000 | 700
223 * 9000 | 710 <- ceil
224 * 10000 | 710
225 * 100000 | 710
226 * 1000000 | 710
227 *
228 */
229static unsigned int hlua_nb_instruction = 10000;
230
Willy Tarreau32f61e22015-03-18 17:54:59 +0100231/* Descriptor for the memory allocation state. If limit is not null, it will
232 * be enforced on any memory allocation.
233 */
234struct hlua_mem_allocator {
235 size_t allocated;
236 size_t limit;
237};
238
239static struct hlua_mem_allocator hlua_global_allocator;
240
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200241static const char error_500[] =
Jarno Huuskonen16ad94a2017-01-09 14:17:10 +0200242 "HTTP/1.0 500 Internal Server Error\r\n"
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200243 "Cache-Control: no-cache\r\n"
244 "Connection: close\r\n"
245 "Content-Type: text/html\r\n"
246 "\r\n"
Joseph Herlant7fe15772018-11-15 10:07:32 -0800247 "<html><body><h1>500 Internal Server Error</h1>\nAn internal server error occurred.\n</body></html>\n";
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200248
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100249/* These functions converts types between HAProxy internal args or
250 * sample and LUA types. Another function permits to check if the
251 * LUA stack contains arguments according with an required ARG_T
252 * format.
253 */
254static int hlua_arg2lua(lua_State *L, const struct arg *arg);
255static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100256__LJMP static int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp,
David Carlier0c437f42016-04-27 16:21:56 +0100257 uint64_t mask, struct proxy *p);
Willy Tarreau5eadada2015-03-10 17:28:54 +0100258static int hlua_smp2lua(lua_State *L, struct sample *smp);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100259static int hlua_smp2lua_str(lua_State *L, struct sample *smp);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100260static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp);
261
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200262__LJMP static int hlua_http_get_headers(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg);
263
Thierry FOURNIER23bc3752015-09-11 19:15:43 +0200264#define SEND_ERR(__be, __fmt, __args...) \
265 do { \
266 send_log(__be, LOG_ERR, __fmt, ## __args); \
267 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) \
Christopher Faulet767a84b2017-11-24 16:50:31 +0100268 ha_alert(__fmt, ## __args); \
Thierry FOURNIER23bc3752015-09-11 19:15:43 +0200269 } while (0)
270
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100271/* Used to check an Lua function type in the stack. It creates and
272 * returns a reference of the function. This function throws an
273 * error if the rgument is not a "function".
274 */
275__LJMP unsigned int hlua_checkfunction(lua_State *L, int argno)
276{
277 if (!lua_isfunction(L, argno)) {
Thierry FOURNIERfd1e9552018-02-23 18:41:18 +0100278 const char *msg = lua_pushfstring(L, "function expected, got %s", luaL_typename(L, argno));
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100279 WILL_LJMP(luaL_argerror(L, argno, msg));
280 }
281 lua_pushvalue(L, argno);
282 return luaL_ref(L, LUA_REGISTRYINDEX);
283}
284
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200285/* Return the string that is of the top of the stack. */
286const char *hlua_get_top_error_string(lua_State *L)
287{
288 if (lua_gettop(L) < 1)
289 return "unknown error";
290 if (lua_type(L, -1) != LUA_TSTRING)
291 return "unknown error";
292 return lua_tostring(L, -1);
293}
294
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200295__LJMP static const char *hlua_traceback(lua_State *L)
296{
297 lua_Debug ar;
298 int level = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +0200299 struct buffer *msg = get_trash_chunk();
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200300 int filled = 0;
301
302 while (lua_getstack(L, level++, &ar)) {
303
304 /* Add separator */
305 if (filled)
306 chunk_appendf(msg, ", ");
307 filled = 1;
308
309 /* Fill fields:
310 * 'S': fills in the fields source, short_src, linedefined, lastlinedefined, and what;
311 * 'l': fills in the field currentline;
312 * 'n': fills in the field name and namewhat;
313 * 't': fills in the field istailcall;
314 */
315 lua_getinfo(L, "Slnt", &ar);
316
317 /* Append code localisation */
318 if (ar.currentline > 0)
319 chunk_appendf(msg, "%s:%d ", ar.short_src, ar.currentline);
320 else
321 chunk_appendf(msg, "%s ", ar.short_src);
322
323 /*
324 * Get function name
325 *
326 * if namewhat is no empty, name is defined.
327 * what contains "Lua" for Lua function, "C" for C function,
328 * or "main" for main code.
329 */
330 if (*ar.namewhat != '\0' && ar.name != NULL) /* is there a name from code? */
331 chunk_appendf(msg, "%s '%s'", ar.namewhat, ar.name); /* use it */
332
333 else if (*ar.what == 'm') /* "main", the code is not executed in a function */
334 chunk_appendf(msg, "main chunk");
335
336 else if (*ar.what != 'C') /* for Lua functions, use <file:line> */
337 chunk_appendf(msg, "C function line %d", ar.linedefined);
338
339 else /* nothing left... */
340 chunk_appendf(msg, "?");
341
342
343 /* Display tailed call */
344 if (ar.istailcall)
345 chunk_appendf(msg, " ...");
346 }
347
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200348 return msg->area;
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200349}
350
351
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100352/* This function check the number of arguments available in the
353 * stack. If the number of arguments available is not the same
354 * then <nb> an error is throwed.
355 */
356__LJMP static inline void check_args(lua_State *L, int nb, char *fcn)
357{
358 if (lua_gettop(L) == nb)
359 return;
360 WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb));
361}
362
Mark Lakes22154b42018-01-29 14:38:40 -0800363/* This function pushes an error string prefixed by the file name
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100364 * and the line number where the error is encountered.
365 */
366static int hlua_pusherror(lua_State *L, const char *fmt, ...)
367{
368 va_list argp;
369 va_start(argp, fmt);
370 luaL_where(L, 1);
371 lua_pushvfstring(L, fmt, argp);
372 va_end(argp);
373 lua_concat(L, 2);
374 return 1;
375}
376
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100377/* This functions is used with sample fetch and converters. It
378 * converts the HAProxy configuration argument in a lua stack
379 * values.
380 *
381 * It takes an array of "arg", and each entry of the array is
382 * converted and pushed in the LUA stack.
383 */
384static int hlua_arg2lua(lua_State *L, const struct arg *arg)
385{
386 switch (arg->type) {
387 case ARGT_SINT:
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100388 case ARGT_TIME:
389 case ARGT_SIZE:
Thierry FOURNIERf90838b2015-03-06 13:48:32 +0100390 lua_pushinteger(L, arg->data.sint);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100391 break;
392
393 case ARGT_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200394 lua_pushlstring(L, arg->data.str.area, arg->data.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100395 break;
396
397 case ARGT_IPV4:
398 case ARGT_IPV6:
399 case ARGT_MSK4:
400 case ARGT_MSK6:
401 case ARGT_FE:
402 case ARGT_BE:
403 case ARGT_TAB:
404 case ARGT_SRV:
405 case ARGT_USR:
406 case ARGT_MAP:
407 default:
408 lua_pushnil(L);
409 break;
410 }
411 return 1;
412}
413
414/* This function take one entrie in an LUA stack at the index "ud",
415 * and try to convert it in an HAProxy argument entry. This is useful
416 * with sample fetch wrappers. The input arguments are gived to the
417 * lua wrapper and converted as arg list by thi function.
418 */
419static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg)
420{
421 switch (lua_type(L, ud)) {
422
423 case LUA_TNUMBER:
424 case LUA_TBOOLEAN:
425 arg->type = ARGT_SINT;
426 arg->data.sint = lua_tointeger(L, ud);
427 break;
428
429 case LUA_TSTRING:
430 arg->type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200431 arg->data.str.area = (char *)lua_tolstring(L, ud, (size_t *)&arg->data.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100432 break;
433
434 case LUA_TUSERDATA:
435 case LUA_TNIL:
436 case LUA_TTABLE:
437 case LUA_TFUNCTION:
438 case LUA_TTHREAD:
439 case LUA_TLIGHTUSERDATA:
440 arg->type = ARGT_SINT;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +0200441 arg->data.sint = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100442 break;
443 }
444 return 1;
445}
446
447/* the following functions are used to convert a struct sample
448 * in Lua type. This useful to convert the return of the
449 * fetchs or converters.
450 */
Willy Tarreau5eadada2015-03-10 17:28:54 +0100451static int hlua_smp2lua(lua_State *L, struct sample *smp)
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100452{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200453 switch (smp->data.type) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100454 case SMP_T_SINT:
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100455 case SMP_T_BOOL:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200456 lua_pushinteger(L, smp->data.u.sint);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100457 break;
458
459 case SMP_T_BIN:
460 case SMP_T_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200461 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100462 break;
463
464 case SMP_T_METH:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200465 switch (smp->data.u.meth.meth) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100466 case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break;
467 case HTTP_METH_GET: lua_pushstring(L, "GET"); break;
468 case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break;
469 case HTTP_METH_POST: lua_pushstring(L, "POST"); break;
470 case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break;
471 case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break;
472 case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break;
473 case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break;
474 case HTTP_METH_OTHER:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200475 lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100476 break;
477 default:
478 lua_pushnil(L);
479 break;
480 }
481 break;
482
483 case SMP_T_IPV4:
484 case SMP_T_IPV6:
485 case SMP_T_ADDR: /* This type is never used to qualify a sample. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200486 if (sample_casts[smp->data.type][SMP_T_STR] &&
487 sample_casts[smp->data.type][SMP_T_STR](smp))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200488 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Willy Tarreau5eadada2015-03-10 17:28:54 +0100489 else
490 lua_pushnil(L);
491 break;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100492 default:
493 lua_pushnil(L);
494 break;
495 }
496 return 1;
497}
498
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100499/* the following functions are used to convert a struct sample
500 * in Lua strings. This is useful to convert the return of the
501 * fetchs or converters.
502 */
503static int hlua_smp2lua_str(lua_State *L, struct sample *smp)
504{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200505 switch (smp->data.type) {
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100506
507 case SMP_T_BIN:
508 case SMP_T_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200509 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100510 break;
511
512 case SMP_T_METH:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200513 switch (smp->data.u.meth.meth) {
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100514 case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break;
515 case HTTP_METH_GET: lua_pushstring(L, "GET"); break;
516 case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break;
517 case HTTP_METH_POST: lua_pushstring(L, "POST"); break;
518 case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break;
519 case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break;
520 case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break;
521 case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break;
522 case HTTP_METH_OTHER:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200523 lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100524 break;
525 default:
526 lua_pushstring(L, "");
527 break;
528 }
529 break;
530
531 case SMP_T_SINT:
532 case SMP_T_BOOL:
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100533 case SMP_T_IPV4:
534 case SMP_T_IPV6:
535 case SMP_T_ADDR: /* This type is never used to qualify a sample. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200536 if (sample_casts[smp->data.type][SMP_T_STR] &&
537 sample_casts[smp->data.type][SMP_T_STR](smp))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200538 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100539 else
540 lua_pushstring(L, "");
541 break;
542 default:
543 lua_pushstring(L, "");
544 break;
545 }
546 return 1;
547}
548
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100549/* the following functions are used to convert an Lua type in a
550 * struct sample. This is useful to provide data from a converter
551 * to the LUA code.
552 */
553static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp)
554{
555 switch (lua_type(L, ud)) {
556
557 case LUA_TNUMBER:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200558 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200559 smp->data.u.sint = lua_tointeger(L, ud);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100560 break;
561
562
563 case LUA_TBOOLEAN:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200564 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200565 smp->data.u.sint = lua_toboolean(L, ud);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100566 break;
567
568 case LUA_TSTRING:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200569 smp->data.type = SMP_T_STR;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100570 smp->flags |= SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200571 smp->data.u.str.area = (char *)lua_tolstring(L, ud, (size_t *)&smp->data.u.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100572 break;
573
574 case LUA_TUSERDATA:
575 case LUA_TNIL:
576 case LUA_TTABLE:
577 case LUA_TFUNCTION:
578 case LUA_TTHREAD:
579 case LUA_TLIGHTUSERDATA:
Thierry FOURNIER93405e12015-08-26 14:19:03 +0200580 case LUA_TNONE:
581 default:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200582 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200583 smp->data.u.sint = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100584 break;
585 }
586 return 1;
587}
588
589/* This function check the "argp" builded by another conversion function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800590 * is in accord with the expected argp defined by the "mask". The function
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100591 * returns true or false. It can be adjust the types if there compatibles.
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100592 *
593 * This function assumes thant the argp argument contains ARGM_NBARGS + 1
594 * entries.
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100595 */
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100596__LJMP int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp,
David Carlier0c437f42016-04-27 16:21:56 +0100597 uint64_t mask, struct proxy *p)
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100598{
599 int min_arg;
600 int idx;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100601 struct proxy *px;
602 char *sname, *pname;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100603
604 idx = 0;
605 min_arg = ARGM(mask);
606 mask >>= ARGM_BITS;
607
608 while (1) {
609
610 /* Check oversize. */
611 if (idx >= ARGM_NBARGS && argp[idx].type != ARGT_STOP) {
Cyril Bonté577a36a2015-03-02 00:08:38 +0100612 WILL_LJMP(luaL_argerror(L, first + idx, "Malformed argument mask"));
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100613 }
614
615 /* Check for mandatory arguments. */
616 if (argp[idx].type == ARGT_STOP) {
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100617 if (idx < min_arg) {
618
619 /* If miss other argument than the first one, we return an error. */
620 if (idx > 0)
621 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
622
623 /* If first argument have a certain type, some default values
624 * may be used. See the function smp_resolve_args().
625 */
626 switch (mask & ARGT_MASK) {
627
628 case ARGT_FE:
629 if (!(p->cap & PR_CAP_FE))
630 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
631 argp[idx].data.prx = p;
632 argp[idx].type = ARGT_FE;
633 argp[idx+1].type = ARGT_STOP;
634 break;
635
636 case ARGT_BE:
637 if (!(p->cap & PR_CAP_BE))
638 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
639 argp[idx].data.prx = p;
640 argp[idx].type = ARGT_BE;
641 argp[idx+1].type = ARGT_STOP;
642 break;
643
644 case ARGT_TAB:
645 argp[idx].data.prx = p;
646 argp[idx].type = ARGT_TAB;
647 argp[idx+1].type = ARGT_STOP;
648 break;
649
650 default:
651 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
652 break;
653 }
654 }
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100655 return 0;
656 }
657
658 /* Check for exceed the number of requiered argument. */
659 if ((mask & ARGT_MASK) == ARGT_STOP &&
660 argp[idx].type != ARGT_STOP) {
661 WILL_LJMP(luaL_argerror(L, first + idx, "Last argument expected"));
662 }
663
664 if ((mask & ARGT_MASK) == ARGT_STOP &&
665 argp[idx].type == ARGT_STOP) {
666 return 0;
667 }
668
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100669 /* Convert some argument types. */
670 switch (mask & ARGT_MASK) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100671 case ARGT_SINT:
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100672 if (argp[idx].type != ARGT_SINT)
673 WILL_LJMP(luaL_argerror(L, first + idx, "integer expected"));
674 argp[idx].type = ARGT_SINT;
675 break;
676
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100677 case ARGT_TIME:
678 if (argp[idx].type != ARGT_SINT)
679 WILL_LJMP(luaL_argerror(L, first + idx, "integer expected"));
Thierry FOURNIER29176f32015-07-07 00:41:29 +0200680 argp[idx].type = ARGT_TIME;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100681 break;
682
683 case ARGT_SIZE:
684 if (argp[idx].type != ARGT_SINT)
685 WILL_LJMP(luaL_argerror(L, first + idx, "integer expected"));
Thierry FOURNIER29176f32015-07-07 00:41:29 +0200686 argp[idx].type = ARGT_SIZE;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100687 break;
688
689 case ARGT_FE:
690 if (argp[idx].type != ARGT_STR)
691 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200692 memcpy(trash.area, argp[idx].data.str.area,
693 argp[idx].data.str.data);
694 trash.area[argp[idx].data.str.data] = 0;
695 argp[idx].data.prx = proxy_fe_by_name(trash.area);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100696 if (!argp[idx].data.prx)
697 WILL_LJMP(luaL_argerror(L, first + idx, "frontend doesn't exist"));
698 argp[idx].type = ARGT_FE;
699 break;
700
701 case ARGT_BE:
702 if (argp[idx].type != ARGT_STR)
703 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200704 memcpy(trash.area, argp[idx].data.str.area,
705 argp[idx].data.str.data);
706 trash.area[argp[idx].data.str.data] = 0;
707 argp[idx].data.prx = proxy_be_by_name(trash.area);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100708 if (!argp[idx].data.prx)
709 WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist"));
710 argp[idx].type = ARGT_BE;
711 break;
712
713 case ARGT_TAB:
714 if (argp[idx].type != ARGT_STR)
715 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200716 memcpy(trash.area, argp[idx].data.str.area,
717 argp[idx].data.str.data);
718 trash.area[argp[idx].data.str.data] = 0;
719 argp[idx].data.prx = proxy_tbl_by_name(trash.area);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100720 if (!argp[idx].data.prx)
721 WILL_LJMP(luaL_argerror(L, first + idx, "table doesn't exist"));
722 argp[idx].type = ARGT_TAB;
723 break;
724
725 case ARGT_SRV:
726 if (argp[idx].type != ARGT_STR)
727 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200728 memcpy(trash.area, argp[idx].data.str.area,
729 argp[idx].data.str.data);
730 trash.area[argp[idx].data.str.data] = 0;
731 sname = strrchr(trash.area, '/');
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100732 if (sname) {
733 *sname++ = '\0';
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200734 pname = trash.area;
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200735 px = proxy_be_by_name(pname);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100736 if (!px)
737 WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist"));
738 }
739 else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200740 sname = trash.area;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100741 px = p;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100742 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100743 argp[idx].data.srv = findserver(px, sname);
744 if (!argp[idx].data.srv)
745 WILL_LJMP(luaL_argerror(L, first + idx, "server doesn't exist"));
746 argp[idx].type = ARGT_SRV;
747 break;
748
749 case ARGT_IPV4:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200750 memcpy(trash.area, argp[idx].data.str.area,
751 argp[idx].data.str.data);
752 trash.area[argp[idx].data.str.data] = 0;
753 if (inet_pton(AF_INET, trash.area, &argp[idx].data.ipv4))
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100754 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 address"));
755 argp[idx].type = ARGT_IPV4;
756 break;
757
758 case ARGT_MSK4:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200759 memcpy(trash.area, argp[idx].data.str.area,
760 argp[idx].data.str.data);
761 trash.area[argp[idx].data.str.data] = 0;
762 if (!str2mask(trash.area, &argp[idx].data.ipv4))
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100763 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 mask"));
764 argp[idx].type = ARGT_MSK4;
765 break;
766
767 case ARGT_IPV6:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200768 memcpy(trash.area, argp[idx].data.str.area,
769 argp[idx].data.str.data);
770 trash.area[argp[idx].data.str.data] = 0;
771 if (inet_pton(AF_INET6, trash.area, &argp[idx].data.ipv6))
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100772 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 address"));
773 argp[idx].type = ARGT_IPV6;
774 break;
775
776 case ARGT_MSK6:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200777 memcpy(trash.area, argp[idx].data.str.area,
778 argp[idx].data.str.data);
779 trash.area[argp[idx].data.str.data] = 0;
780 if (!str2mask6(trash.area, &argp[idx].data.ipv6))
Tim Duesterhusb814da62018-01-25 16:24:50 +0100781 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 mask"));
782 argp[idx].type = ARGT_MSK6;
783 break;
784
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100785 case ARGT_MAP:
786 case ARGT_REG:
787 case ARGT_USR:
788 WILL_LJMP(luaL_argerror(L, first + idx, "type not yet supported"));
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100789 break;
790 }
791
792 /* Check for type of argument. */
793 if ((mask & ARGT_MASK) != argp[idx].type) {
794 const char *msg = lua_pushfstring(L, "'%s' expected, got '%s'",
795 arg_type_names[(mask & ARGT_MASK)],
796 arg_type_names[argp[idx].type & ARGT_MASK]);
797 WILL_LJMP(luaL_argerror(L, first + idx, msg));
798 }
799
800 /* Next argument. */
801 mask >>= ARGT_BITS;
802 idx++;
803 }
804}
805
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100806/*
807 * The following functions are used to make correspondance between the the
808 * executed lua pointer and the "struct hlua *" that contain the context.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100809 *
810 * - hlua_gethlua : return the hlua context associated with an lua_State.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100811 * - hlua_sethlua : create the association between hlua context and lua_state.
812 */
813static inline struct hlua *hlua_gethlua(lua_State *L)
814{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +0100815 struct hlua **hlua = lua_getextraspace(L);
816 return *hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100817}
818static inline void hlua_sethlua(struct hlua *hlua)
819{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +0100820 struct hlua **hlua_store = lua_getextraspace(hlua->T);
821 *hlua_store = hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100822}
823
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100824/* This function is used to send logs. It try to send on screen (stderr)
825 * and on the default syslog server.
826 */
827static inline void hlua_sendlog(struct proxy *px, int level, const char *msg)
828{
829 struct tm tm;
830 char *p;
831
832 /* Cleanup the log message. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200833 p = trash.area;
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100834 for (; *msg != '\0'; msg++, p++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200835 if (p >= trash.area + trash.size - 1) {
Thierry FOURNIERccf00632015-09-16 12:47:03 +0200836 /* Break the message if exceed the buffer size. */
837 *(p-4) = ' ';
838 *(p-3) = '.';
839 *(p-2) = '.';
840 *(p-1) = '.';
841 break;
842 }
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100843 if (isprint(*msg))
844 *p = *msg;
845 else
846 *p = '.';
847 }
848 *p = '\0';
849
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200850 send_log(px, level, "%s\n", trash.area);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100851 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
Willy Tarreaua678b432015-08-28 10:14:59 +0200852 get_localtime(date.tv_sec, &tm);
853 fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n",
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100854 log_levels[level], tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec,
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200855 (int)getpid(), trash.area);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100856 fflush(stderr);
857 }
858}
859
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100860/* This function just ensure that the yield will be always
861 * returned with a timeout and permit to set some flags
862 */
863__LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx,
Thierry FOURNIERf90838b2015-03-06 13:48:32 +0100864 lua_KFunction k, int timeout, unsigned int flags)
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100865{
866 struct hlua *hlua = hlua_gethlua(L);
867
868 /* Set the wake timeout. If timeout is required, we set
869 * the expiration time.
870 */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +0200871 hlua->wake_time = timeout;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100872
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +0100873 hlua->flags |= flags;
874
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100875 /* Process the yield. */
Willy Tarreau9635e032018-10-16 17:52:55 +0200876 MAY_LJMP(lua_yieldk(L, nresults, ctx, k));
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100877}
878
Willy Tarreau87b09662015-04-03 00:22:06 +0200879/* This function initialises the Lua environment stored in the stream.
880 * It must be called at the start of the stream. This function creates
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100881 * an LUA coroutine. It can not be use to crete the main LUA context.
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200882 *
883 * This function is particular. it initialises a new Lua thread. If the
884 * initialisation fails (example: out of memory error), the lua function
885 * throws an error (longjmp).
886 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800887 * This function manipulates two Lua stacks: the main and the thread. Only
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200888 * the main stack can fail. The thread is not manipulated. This function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800889 * MUST NOT manipulate the created thread stack state, because it is not
890 * proctected against errors thrown by the thread stack.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100891 */
892int hlua_ctx_init(struct hlua *lua, struct task *task)
893{
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200894 if (!SET_SAFE_LJMP(gL.T)) {
895 lua->Tref = LUA_REFNIL;
896 return 0;
897 }
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100898 lua->Mref = LUA_REFNIL;
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +0100899 lua->flags = 0;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100900 LIST_INIT(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100901 lua->T = lua_newthread(gL.T);
902 if (!lua->T) {
903 lua->Tref = LUA_REFNIL;
Thierry FOURNIER0a976202017-07-12 11:18:00 +0200904 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100905 return 0;
906 }
907 hlua_sethlua(lua);
908 lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
909 lua->task = task;
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200910 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100911 return 1;
912}
913
Willy Tarreau87b09662015-04-03 00:22:06 +0200914/* Used to destroy the Lua coroutine when the attached stream or task
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100915 * is destroyed. The destroy also the memory context. The struct "lua"
916 * is not freed.
917 */
918void hlua_ctx_destroy(struct hlua *lua)
919{
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100920 if (!lua)
Thierry FOURNIERa718b292015-03-04 16:48:34 +0100921 return;
922
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100923 if (!lua->T)
924 goto end;
925
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100926 /* Purge all the pending signals. */
Thierry FOURNIERd6975962017-07-12 14:31:10 +0200927 notification_purge(&lua->com);
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100928
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200929 if (!SET_SAFE_LJMP(lua->T))
930 return;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100931 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200932 RESET_SAFE_LJMP(lua->T);
Thierry FOURNIER5a50a852015-09-23 16:59:28 +0200933
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200934 if (!SET_SAFE_LJMP(gL.T))
935 return;
936 luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref);
937 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER5a50a852015-09-23 16:59:28 +0200938 /* Forces a garbage collecting process. If the Lua program is finished
939 * without error, we run the GC on the thread pointer. Its freed all
940 * the unused memory.
941 * If the thread is finnish with an error or is currently yielded,
942 * it seems that the GC applied on the thread doesn't clean anything,
943 * so e run the GC on the main thread.
944 * NOTE: maybe this action locks all the Lua threads untiml the en of
945 * the garbage collection.
946 */
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +0200947 if (lua->flags & HLUA_MUST_GC) {
Thierry FOURNIER7bd10d52017-07-17 00:44:40 +0200948 if (!SET_SAFE_LJMP(gL.T))
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200949 return;
Thierry FOURNIER7bd10d52017-07-17 00:44:40 +0200950 lua_gc(gL.T, LUA_GCCOLLECT, 0);
951 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +0200952 }
Thierry FOURNIER5a50a852015-09-23 16:59:28 +0200953
Thierry FOURNIERa7b536b2015-09-21 22:50:24 +0200954 lua->T = NULL;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100955
956end:
Willy Tarreaubafbe012017-11-24 17:34:44 +0100957 pool_free(pool_head_hlua, lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100958}
959
960/* This function is used to restore the Lua context when a coroutine
961 * fails. This function copy the common memory between old coroutine
962 * and the new coroutine. The old coroutine is destroyed, and its
963 * replaced by the new coroutine.
964 * If the flag "keep_msg" is set, the last entry of the old is assumed
965 * as string error message and it is copied in the new stack.
966 */
967static int hlua_ctx_renew(struct hlua *lua, int keep_msg)
968{
969 lua_State *T;
970 int new_ref;
971
972 /* Renew the main LUA stack doesn't have sense. */
973 if (lua == &gL)
974 return 0;
975
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100976 /* New Lua coroutine. */
977 T = lua_newthread(gL.T);
978 if (!T)
979 return 0;
980
981 /* Copy last error message. */
982 if (keep_msg)
983 lua_xmove(lua->T, T, 1);
984
985 /* Copy data between the coroutines. */
986 lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref);
987 lua_xmove(lua->T, T, 1);
988 new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Valur poped. */
989
990 /* Destroy old data. */
991 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
992
993 /* The thread is garbage collected by Lua. */
994 luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref);
995
996 /* Fill the struct with the new coroutine values. */
997 lua->Mref = new_ref;
998 lua->T = T;
999 lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
1000
1001 /* Set context. */
1002 hlua_sethlua(lua);
1003
1004 return 1;
1005}
1006
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001007void hlua_hook(lua_State *L, lua_Debug *ar)
1008{
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001009 struct hlua *hlua = hlua_gethlua(L);
1010
1011 /* Lua cannot yield when its returning from a function,
1012 * so, we can fix the interrupt hook to 1 instruction,
1013 * expecting that the function is finnished.
1014 */
1015 if (lua_gethookmask(L) & LUA_MASKRET) {
1016 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1);
1017 return;
1018 }
1019
1020 /* restore the interrupt condition. */
1021 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
1022
1023 /* If we interrupt the Lua processing in yieldable state, we yield.
1024 * If the state is not yieldable, trying yield causes an error.
1025 */
1026 if (lua_isyieldable(L))
Willy Tarreau9635e032018-10-16 17:52:55 +02001027 MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD));
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001028
Thierry FOURNIERa85cfb12015-03-13 14:50:06 +01001029 /* If we cannot yield, update the clock and check the timeout. */
1030 tv_update_date(0, 1);
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001031 hlua->run_time += now_ms - hlua->start_time;
1032 if (hlua->max_time && hlua->run_time >= hlua->max_time) {
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001033 lua_pushfstring(L, "execution timeout");
1034 WILL_LJMP(lua_error(L));
1035 }
1036
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001037 /* Update the start time. */
1038 hlua->start_time = now_ms;
1039
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001040 /* Try to interrupt the process at the end of the current
1041 * unyieldable function.
1042 */
1043 lua_sethook(hlua->T, hlua_hook, LUA_MASKRET|LUA_MASKCOUNT, hlua_nb_instruction);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001044}
1045
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001046/* This function start or resumes the Lua stack execution. If the flag
1047 * "yield_allowed" if no set and the LUA stack execution returns a yield
1048 * The function return an error.
1049 *
1050 * The function can returns 4 values:
1051 * - HLUA_E_OK : The execution is terminated without any errors.
1052 * - HLUA_E_AGAIN : The execution must continue at the next associated
1053 * task wakeup.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001054 * - HLUA_E_ERRMSG : An error has occurred, an error message is set in
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001055 * the top of the stack.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001056 * - HLUA_E_ERR : An error has occurred without error message.
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001057 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001058 * If an error occurred, the stack is renewed and it is ready to run new
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001059 * LUA code.
1060 */
1061static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed)
1062{
1063 int ret;
1064 const char *msg;
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001065 const char *trace;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001066
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001067 /* Initialise run time counter. */
1068 if (!HLUA_IS_RUNNING(lua))
1069 lua->run_time = 0;
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001070
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001071 /* Lock the whole Lua execution. This lock must be before the
1072 * label "resume_execution".
1073 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001074 HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock);
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001075
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001076resume_execution:
1077
1078 /* This hook interrupts the Lua processing each 'hlua_nb_instruction'
1079 * instructions. it is used for preventing infinite loops.
1080 */
1081 lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
1082
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +01001083 /* Remove all flags except the running flags. */
Thierry FOURNIER2f3867f2015-09-28 01:02:01 +02001084 HLUA_SET_RUN(lua);
1085 HLUA_CLR_CTRLYIELD(lua);
1086 HLUA_CLR_WAKERESWR(lua);
1087 HLUA_CLR_WAKEREQWR(lua);
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +01001088
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001089 /* Update the start time. */
1090 lua->start_time = now_ms;
1091
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001092 /* Call the function. */
1093 ret = lua_resume(lua->T, gL.T, lua->nargs);
1094 switch (ret) {
1095
1096 case LUA_OK:
1097 ret = HLUA_E_OK;
1098 break;
1099
1100 case LUA_YIELD:
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001101 /* Check if the execution timeout is expired. It it is the case, we
1102 * break the Lua execution.
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001103 */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001104 tv_update_date(0, 1);
1105 lua->run_time += now_ms - lua->start_time;
1106 if (lua->max_time && lua->run_time > lua->max_time) {
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001107 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001108 ret = HLUA_E_ETMOUT;
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001109 break;
1110 }
1111 /* Process the forced yield. if the general yield is not allowed or
1112 * if no task were associated this the current Lua execution
1113 * coroutine, we resume the execution. Else we want to return in the
1114 * scheduler and we want to be waked up again, to continue the
1115 * current Lua execution. So we schedule our own task.
1116 */
1117 if (HLUA_IS_CTRLYIELDING(lua)) {
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001118 if (!yield_allowed || !lua->task)
1119 goto resume_execution;
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001120 task_wakeup(lua->task, TASK_WOKEN_MSG);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001121 }
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001122 if (!yield_allowed) {
1123 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001124 ret = HLUA_E_YIELD;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001125 break;
1126 }
1127 ret = HLUA_E_AGAIN;
1128 break;
1129
1130 case LUA_ERRRUN:
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001131
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001132 /* Special exit case. The traditional exit is returned as an error
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001133 * because the errors ares the only one mean to return immediately
1134 * from and lua execution.
1135 */
1136 if (lua->flags & HLUA_EXIT) {
1137 ret = HLUA_E_OK;
Thierry FOURNIERe1587b32015-08-28 09:54:13 +02001138 hlua_ctx_renew(lua, 0);
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001139 break;
1140 }
1141
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001142 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001143 if (!lua_checkstack(lua->T, 1)) {
1144 ret = HLUA_E_ERR;
1145 break;
1146 }
1147 msg = lua_tostring(lua->T, -1);
1148 lua_settop(lua->T, 0); /* Empty the stack. */
1149 lua_pop(lua->T, 1);
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001150 trace = hlua_traceback(lua->T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001151 if (msg)
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001152 lua_pushfstring(lua->T, "runtime error: %s from %s", msg, trace);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001153 else
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001154 lua_pushfstring(lua->T, "unknown runtime error from %s", trace);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001155 ret = HLUA_E_ERRMSG;
1156 break;
1157
1158 case LUA_ERRMEM:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001159 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001160 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001161 ret = HLUA_E_NOMEM;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001162 break;
1163
1164 case LUA_ERRERR:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001165 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001166 if (!lua_checkstack(lua->T, 1)) {
1167 ret = HLUA_E_ERR;
1168 break;
1169 }
1170 msg = lua_tostring(lua->T, -1);
1171 lua_settop(lua->T, 0); /* Empty the stack. */
1172 lua_pop(lua->T, 1);
1173 if (msg)
1174 lua_pushfstring(lua->T, "message handler error: %s", msg);
1175 else
1176 lua_pushfstring(lua->T, "message handler error");
1177 ret = HLUA_E_ERRMSG;
1178 break;
1179
1180 default:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001181 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001182 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001183 ret = HLUA_E_ERR;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001184 break;
1185 }
1186
Thierry FOURNIER6ab4d8e2015-09-27 22:17:19 +02001187 /* This GC permits to destroy some object when a Lua timeout strikes. */
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +02001188 if (lua->flags & HLUA_MUST_GC &&
1189 ret != HLUA_E_AGAIN)
Thierry FOURNIER6ab4d8e2015-09-27 22:17:19 +02001190 lua_gc(lua->T, LUA_GCCOLLECT, 0);
1191
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001192 switch (ret) {
1193 case HLUA_E_AGAIN:
1194 break;
1195
1196 case HLUA_E_ERRMSG:
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001197 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001198 hlua_ctx_renew(lua, 1);
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001199 HLUA_CLR_RUN(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001200 break;
1201
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001202 case HLUA_E_ETMOUT:
1203 case HLUA_E_NOMEM:
1204 case HLUA_E_YIELD:
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001205 case HLUA_E_ERR:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001206 HLUA_CLR_RUN(lua);
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001207 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001208 hlua_ctx_renew(lua, 0);
1209 break;
1210
1211 case HLUA_E_OK:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001212 HLUA_CLR_RUN(lua);
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001213 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001214 break;
1215 }
1216
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001217 /* This is the main exit point, remove the Lua lock. */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001218 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock);
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001219
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001220 return ret;
1221}
1222
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001223/* This function exit the current code. */
1224__LJMP static int hlua_done(lua_State *L)
1225{
1226 struct hlua *hlua = hlua_gethlua(L);
1227
1228 hlua->flags |= HLUA_EXIT;
1229 WILL_LJMP(lua_error(L));
1230
1231 return 0;
1232}
1233
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001234/* This function is an LUA binding. It provides a function
1235 * for deleting ACL from a referenced ACL file.
1236 */
1237__LJMP static int hlua_del_acl(lua_State *L)
1238{
1239 const char *name;
1240 const char *key;
1241 struct pat_ref *ref;
1242
1243 MAY_LJMP(check_args(L, 2, "del_acl"));
1244
1245 name = MAY_LJMP(luaL_checkstring(L, 1));
1246 key = MAY_LJMP(luaL_checkstring(L, 2));
1247
1248 ref = pat_ref_lookup(name);
1249 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001250 WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001251
1252 pat_ref_delete(ref, key);
1253 return 0;
1254}
1255
1256/* This function is an LUA binding. It provides a function
1257 * for deleting map entry from a referenced map file.
1258 */
1259static int hlua_del_map(lua_State *L)
1260{
1261 const char *name;
1262 const char *key;
1263 struct pat_ref *ref;
1264
1265 MAY_LJMP(check_args(L, 2, "del_map"));
1266
1267 name = MAY_LJMP(luaL_checkstring(L, 1));
1268 key = MAY_LJMP(luaL_checkstring(L, 2));
1269
1270 ref = pat_ref_lookup(name);
1271 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001272 WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001273
1274 pat_ref_delete(ref, key);
1275 return 0;
1276}
1277
1278/* This function is an LUA binding. It provides a function
1279 * for adding ACL pattern from a referenced ACL file.
1280 */
1281static int hlua_add_acl(lua_State *L)
1282{
1283 const char *name;
1284 const char *key;
1285 struct pat_ref *ref;
1286
1287 MAY_LJMP(check_args(L, 2, "add_acl"));
1288
1289 name = MAY_LJMP(luaL_checkstring(L, 1));
1290 key = MAY_LJMP(luaL_checkstring(L, 2));
1291
1292 ref = pat_ref_lookup(name);
1293 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001294 WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001295
1296 if (pat_ref_find_elt(ref, key) == NULL)
1297 pat_ref_add(ref, key, NULL, NULL);
1298 return 0;
1299}
1300
1301/* This function is an LUA binding. It provides a function
1302 * for setting map pattern and sample from a referenced map
1303 * file.
1304 */
1305static int hlua_set_map(lua_State *L)
1306{
1307 const char *name;
1308 const char *key;
1309 const char *value;
1310 struct pat_ref *ref;
1311
1312 MAY_LJMP(check_args(L, 3, "set_map"));
1313
1314 name = MAY_LJMP(luaL_checkstring(L, 1));
1315 key = MAY_LJMP(luaL_checkstring(L, 2));
1316 value = MAY_LJMP(luaL_checkstring(L, 3));
1317
1318 ref = pat_ref_lookup(name);
1319 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001320 WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001321
1322 if (pat_ref_find_elt(ref, key) != NULL)
1323 pat_ref_set(ref, key, value, NULL);
1324 else
1325 pat_ref_add(ref, key, value, NULL);
1326 return 0;
1327}
1328
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001329/* A class is a lot of memory that contain data. This data can be a table,
1330 * an integer or user data. This data is associated with a metatable. This
1331 * metatable have an original version registred in the global context with
1332 * the name of the object (_G[<name>] = <metable> ).
1333 *
1334 * A metable is a table that modify the standard behavior of a standard
1335 * access to the associated data. The entries of this new metatable are
1336 * defined as is:
1337 *
1338 * http://lua-users.org/wiki/MetatableEvents
1339 *
1340 * __index
1341 *
1342 * we access an absent field in a table, the result is nil. This is
1343 * true, but it is not the whole truth. Actually, such access triggers
1344 * the interpreter to look for an __index metamethod: If there is no
1345 * such method, as usually happens, then the access results in nil;
1346 * otherwise, the metamethod will provide the result.
1347 *
1348 * Control 'prototype' inheritance. When accessing "myTable[key]" and
1349 * the key does not appear in the table, but the metatable has an __index
1350 * property:
1351 *
1352 * - if the value is a function, the function is called, passing in the
1353 * table and the key; the return value of that function is returned as
1354 * the result.
1355 *
1356 * - if the value is another table, the value of the key in that table is
1357 * asked for and returned (and if it doesn't exist in that table, but that
1358 * table's metatable has an __index property, then it continues on up)
1359 *
1360 * - Use "rawget(myTable,key)" to skip this metamethod.
1361 *
1362 * http://www.lua.org/pil/13.4.1.html
1363 *
1364 * __newindex
1365 *
1366 * Like __index, but control property assignment.
1367 *
1368 * __mode - Control weak references. A string value with one or both
1369 * of the characters 'k' and 'v' which specifies that the the
1370 * keys and/or values in the table are weak references.
1371 *
1372 * __call - Treat a table like a function. When a table is followed by
1373 * parenthesis such as "myTable( 'foo' )" and the metatable has
1374 * a __call key pointing to a function, that function is invoked
1375 * (passing any specified arguments) and the return value is
1376 * returned.
1377 *
1378 * __metatable - Hide the metatable. When "getmetatable( myTable )" is
1379 * called, if the metatable for myTable has a __metatable
1380 * key, the value of that key is returned instead of the
1381 * actual metatable.
1382 *
1383 * __tostring - Control string representation. When the builtin
1384 * "tostring( myTable )" function is called, if the metatable
1385 * for myTable has a __tostring property set to a function,
1386 * that function is invoked (passing myTable to it) and the
1387 * return value is used as the string representation.
1388 *
1389 * __len - Control table length. When the table length is requested using
1390 * the length operator ( '#' ), if the metatable for myTable has
1391 * a __len key pointing to a function, that function is invoked
1392 * (passing myTable to it) and the return value used as the value
1393 * of "#myTable".
1394 *
1395 * __gc - Userdata finalizer code. When userdata is set to be garbage
1396 * collected, if the metatable has a __gc field pointing to a
1397 * function, that function is first invoked, passing the userdata
1398 * to it. The __gc metamethod is not called for tables.
1399 * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html)
1400 *
1401 * Special metamethods for redefining standard operators:
1402 * http://www.lua.org/pil/13.1.html
1403 *
1404 * __add "+"
1405 * __sub "-"
1406 * __mul "*"
1407 * __div "/"
1408 * __unm "!"
1409 * __pow "^"
1410 * __concat ".."
1411 *
1412 * Special methods for redfining standar relations
1413 * http://www.lua.org/pil/13.2.html
1414 *
1415 * __eq "=="
1416 * __lt "<"
1417 * __le "<="
1418 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001419
1420/*
1421 *
1422 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001423 * Class Map
1424 *
1425 *
1426 */
1427
1428/* Returns a struct hlua_map if the stack entry "ud" is
1429 * a class session, otherwise it throws an error.
1430 */
1431__LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud)
1432{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001433 return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001434}
1435
1436/* This function is the map constructor. It don't need
1437 * the class Map object. It creates and return a new Map
1438 * object. It must be called only during "body" or "init"
1439 * context because it process some filesystem accesses.
1440 */
1441__LJMP static int hlua_map_new(struct lua_State *L)
1442{
1443 const char *fn;
1444 int match = PAT_MATCH_STR;
1445 struct sample_conv conv;
1446 const char *file = "";
1447 int line = 0;
1448 lua_Debug ar;
1449 char *err = NULL;
1450 struct arg args[2];
1451
1452 if (lua_gettop(L) < 1 || lua_gettop(L) > 2)
1453 WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument."));
1454
1455 fn = MAY_LJMP(luaL_checkstring(L, 1));
1456
1457 if (lua_gettop(L) >= 2) {
1458 match = MAY_LJMP(luaL_checkinteger(L, 2));
1459 if (match < 0 || match >= PAT_MATCH_NUM)
1460 WILL_LJMP(luaL_error(L, "'new' needs a valid match method."));
1461 }
1462
1463 /* Get Lua filename and line number. */
1464 if (lua_getstack(L, 1, &ar)) { /* check function at level */
1465 lua_getinfo(L, "Sl", &ar); /* get info about it */
1466 if (ar.currentline > 0) { /* is there info? */
1467 file = ar.short_src;
1468 line = ar.currentline;
1469 }
1470 }
1471
1472 /* fill fake sample_conv struct. */
1473 conv.kw = ""; /* unused. */
1474 conv.process = NULL; /* unused. */
1475 conv.arg_mask = 0; /* unused. */
1476 conv.val_args = NULL; /* unused. */
1477 conv.out_type = SMP_T_STR;
1478 conv.private = (void *)(long)match;
1479 switch (match) {
1480 case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break;
1481 case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break;
1482 case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break;
1483 case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break;
1484 case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break;
1485 case PAT_MATCH_END: conv.in_type = SMP_T_STR; break;
1486 case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001487 case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001488 case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break;
1489 default:
1490 WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode."));
1491 }
1492
1493 /* fill fake args. */
1494 args[0].type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001495 args[0].data.str.area = (char *)fn;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001496 args[1].type = ARGT_STOP;
1497
1498 /* load the map. */
1499 if (!sample_load_map(args, &conv, file, line, &err)) {
1500 /* error case: we cant use luaL_error because we must
1501 * free the err variable.
1502 */
1503 luaL_where(L, 1);
1504 lua_pushfstring(L, "'new': %s.", err);
1505 lua_concat(L, 2);
1506 free(err);
1507 WILL_LJMP(lua_error(L));
1508 }
1509
1510 /* create the lua object. */
1511 lua_newtable(L);
1512 lua_pushlightuserdata(L, args[0].data.map);
1513 lua_rawseti(L, -2, 0);
1514
1515 /* Pop a class Map metatable and affect it to the userdata. */
1516 lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref);
1517 lua_setmetatable(L, -2);
1518
1519
1520 return 1;
1521}
1522
1523__LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str)
1524{
1525 struct map_descriptor *desc;
1526 struct pattern *pat;
1527 struct sample smp;
1528
1529 MAY_LJMP(check_args(L, 2, "lookup"));
1530 desc = MAY_LJMP(hlua_checkmap(L, 1));
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001531 if (desc->pat.expect_type == SMP_T_SINT) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001532 smp.data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001533 smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001534 }
1535 else {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001536 smp.data.type = SMP_T_STR;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001537 smp.flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001538 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 +02001539 }
1540
1541 pat = pattern_exec_match(&desc->pat, &smp, 1);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001542 if (!pat || !pat->data) {
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001543 if (str)
1544 lua_pushstring(L, "");
1545 else
1546 lua_pushnil(L);
1547 return 1;
1548 }
1549
1550 /* The Lua pattern must return a string, so we can't check the returned type */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001551 lua_pushlstring(L, pat->data->u.str.area, pat->data->u.str.data);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001552 return 1;
1553}
1554
1555__LJMP static int hlua_map_lookup(struct lua_State *L)
1556{
1557 return _hlua_map_lookup(L, 0);
1558}
1559
1560__LJMP static int hlua_map_slookup(struct lua_State *L)
1561{
1562 return _hlua_map_lookup(L, 1);
1563}
1564
1565/*
1566 *
1567 *
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001568 * Class Socket
1569 *
1570 *
1571 */
1572
1573__LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud)
1574{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001575 return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001576}
1577
1578/* This function is the handler called for each I/O on the established
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001579 * connection. It is used for notify space available to send or data
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001580 * received.
1581 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001582static void hlua_socket_handler(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001583{
Willy Tarreau00a37f02015-04-13 12:05:19 +02001584 struct stream_interface *si = appctx->owner;
Olivier Houchard9aaf7782017-09-13 18:30:23 +02001585 struct connection *c = cs_conn(objt_cs(si_opposite(si)->end));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001586
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001587 if (appctx->ctx.hlua_cosocket.die) {
1588 si_shutw(si);
1589 si_shutr(si);
1590 si_ic(si)->flags |= CF_READ_NULL;
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001591 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1592 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001593 stream_shutdown(si_strm(si), SF_ERR_KILLED);
1594 }
1595
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001596 /* If the connection object is not available, close all the
1597 * streams and wakeup everything waiting for.
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001598 */
1599 if (!c) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001600 si_shutw(si);
1601 si_shutr(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01001602 si_ic(si)->flags |= CF_READ_NULL;
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001603 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1604 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Willy Tarreaud4da1962015-04-20 01:31:23 +02001605 return;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001606 }
1607
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001608 /* If we cant write, wakeup the pending write signals. */
1609 if (channel_output_closed(si_ic(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001610 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001611
1612 /* If we cant read, wakeup the pending read signals. */
1613 if (channel_input_closed(si_oc(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001614 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001615
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001616 /* if the connection is not estabkished, inform the stream that we want
1617 * to be notified whenever the connection completes.
1618 */
1619 if (!(c->flags & CO_FL_CONNECTED)) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01001620 si_cant_get(si);
Willy Tarreau12c24232018-12-06 15:29:50 +01001621 si_rx_conn_blk(si);
Willy Tarreau3367d412018-11-15 10:57:41 +01001622 si_rx_endp_more(si);
Willy Tarreaud4da1962015-04-20 01:31:23 +02001623 return;
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001624 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001625
1626 /* This function is called after the connect. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01001627 appctx->ctx.hlua_cosocket.connected = 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001628
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001629 /* Wake the tasks which wants to write if the buffer have available space. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001630 if (channel_may_recv(si_ic(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001631 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001632
1633 /* Wake the tasks which wants to read if the buffer contains data. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001634 if (!channel_is_empty(si_oc(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001635 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001636
1637 /* Some data were injected in the buffer, notify the stream
1638 * interface.
1639 */
1640 if (!channel_is_empty(si_ic(si)))
1641 stream_int_update(si);
1642
1643 /* If write notifications are registered, we considers we want
Willy Tarreau3367d412018-11-15 10:57:41 +01001644 * to write, so we clear the blocking flag.
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001645 */
1646 if (notification_registered(&appctx->ctx.hlua_cosocket.wake_on_write))
Willy Tarreau3367d412018-11-15 10:57:41 +01001647 si_rx_endp_more(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001648}
1649
Willy Tarreau87b09662015-04-03 00:22:06 +02001650/* This function is called when the "struct stream" is destroyed.
1651 * Remove the link from the object to this stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001652 * Wake all the pending signals.
1653 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001654static void hlua_socket_release(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001655{
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001656 struct xref *peer;
1657
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001658 /* Remove my link in the original object. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001659 peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref);
1660 if (peer)
1661 xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001662
1663 /* Wake all the task waiting for me. */
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001664 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1665 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001666}
1667
1668/* If the garbage collectio of the object is launch, nobody
Willy Tarreau87b09662015-04-03 00:22:06 +02001669 * uses this object. If the stream does not exists, just quit.
1670 * Send the shutdown signal to the stream. In some cases,
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001671 * pending signal can rest in the read and write lists. destroy
1672 * it.
1673 */
1674__LJMP static int hlua_socket_gc(lua_State *L)
1675{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001676 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001677 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001678 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001679
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001680 MAY_LJMP(check_args(L, 1, "__gc"));
1681
1682 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001683 peer = xref_get_peer_and_lock(&socket->xref);
1684 if (!peer)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001685 return 0;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001686 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001687
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001688 /* Set the flag which destroy the session. */
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001689 appctx->ctx.hlua_cosocket.die = 1;
1690 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001691
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001692 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001693 xref_disconnect(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001694 return 0;
1695}
1696
1697/* The close function send shutdown signal and break the
Willy Tarreau87b09662015-04-03 00:22:06 +02001698 * links between the stream and the object.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001699 */
sada05ed3302018-05-11 11:48:18 -07001700__LJMP static int hlua_socket_close_helper(lua_State *L)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001701{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001702 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001703 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001704 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001705
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001706 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001707
1708 /* Check if we run on the same thread than the xreator thread.
1709 * We cannot access to the socket if the thread is different.
1710 */
1711 if (socket->tid != tid)
1712 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1713
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001714 peer = xref_get_peer_and_lock(&socket->xref);
1715 if (!peer)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001716 return 0;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001717 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001718
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001719 /* Set the flag which destroy the session. */
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001720 appctx->ctx.hlua_cosocket.die = 1;
1721 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001722
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001723 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001724 xref_disconnect(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001725 return 0;
1726}
1727
sada05ed3302018-05-11 11:48:18 -07001728/* The close function calls close_helper.
1729 */
1730__LJMP static int hlua_socket_close(lua_State *L)
1731{
1732 MAY_LJMP(check_args(L, 1, "close"));
1733 return hlua_socket_close_helper(L);
1734}
1735
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001736/* This Lua function assumes that the stack contain three parameters.
1737 * 1 - USERDATA containing a struct socket
1738 * 2 - INTEGER with values of the macro defined below
1739 * If the integer is -1, we must read at most one line.
1740 * If the integer is -2, we ust read all the data until the
1741 * end of the stream.
1742 * If the integer is positive value, we must read a number of
1743 * bytes corresponding to this value.
1744 */
1745#define HLSR_READ_LINE (-1)
1746#define HLSR_READ_ALL (-2)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001747__LJMP static int hlua_socket_receive_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001748{
1749 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
1750 int wanted = lua_tointeger(L, 2);
1751 struct hlua *hlua = hlua_gethlua(L);
1752 struct appctx *appctx;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001753 size_t len;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001754 int nblk;
Willy Tarreau206ba832018-06-14 15:27:31 +02001755 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001756 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02001757 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001758 size_t len2;
Thierry FOURNIER00543922015-03-09 18:35:06 +01001759 int skip_at_end = 0;
Willy Tarreau81389672015-03-10 12:03:52 +01001760 struct channel *oc;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001761 struct stream_interface *si;
1762 struct stream *s;
1763 struct xref *peer;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001764 int missing_bytes;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001765
1766 /* Check if this lua stack is schedulable. */
1767 if (!hlua || !hlua->task)
1768 WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in "
1769 "'frontend', 'backend' or 'task'"));
1770
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001771 /* Check if we run on the same thread than the xreator thread.
1772 * We cannot access to the socket if the thread is different.
1773 */
1774 if (socket->tid != tid)
1775 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1776
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001777 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001778 peer = xref_get_peer_and_lock(&socket->xref);
1779 if (!peer)
1780 goto no_peer;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001781 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
1782 si = appctx->owner;
1783 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001784
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001785 oc = &s->res;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001786 if (wanted == HLSR_READ_LINE) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001787 /* Read line. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001788 nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001789 if (nblk < 0) /* Connection close. */
1790 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001791 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001792 goto connection_empty;
Thierry FOURNIER00543922015-03-09 18:35:06 +01001793
1794 /* remove final \r\n. */
1795 if (nblk == 1) {
1796 if (blk1[len1-1] == '\n') {
1797 len1--;
1798 skip_at_end++;
1799 if (blk1[len1-1] == '\r') {
1800 len1--;
1801 skip_at_end++;
1802 }
1803 }
1804 }
1805 else {
1806 if (blk2[len2-1] == '\n') {
1807 len2--;
1808 skip_at_end++;
1809 if (blk2[len2-1] == '\r') {
1810 len2--;
1811 skip_at_end++;
1812 }
1813 }
1814 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001815 }
1816
1817 else if (wanted == HLSR_READ_ALL) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001818 /* Read all the available data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001819 nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001820 if (nblk < 0) /* Connection close. */
1821 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001822 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001823 goto connection_empty;
1824 }
1825
1826 else {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001827 /* Read a block of data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001828 nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001829 if (nblk < 0) /* Connection close. */
1830 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001831 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001832 goto connection_empty;
1833
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001834 missing_bytes = wanted - socket->b.n;
1835 if (len1 > missing_bytes) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001836 nblk = 1;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001837 len1 = missing_bytes;
1838 } if (nblk == 2 && len1 + len2 > missing_bytes)
1839 len2 = missing_bytes - len1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001840 }
1841
1842 len = len1;
1843
1844 luaL_addlstring(&socket->b, blk1, len1);
1845 if (nblk == 2) {
1846 len += len2;
1847 luaL_addlstring(&socket->b, blk2, len2);
1848 }
1849
1850 /* Consume data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001851 co_skip(oc, len + skip_at_end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001852
1853 /* Don't wait anything. */
Thierry FOURNIER7e4ee472018-05-25 15:03:50 +02001854 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001855
1856 /* If the pattern reclaim to read all the data
1857 * in the connection, got out.
1858 */
1859 if (wanted == HLSR_READ_ALL)
1860 goto connection_empty;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001861 else if (wanted >= 0 && socket->b.n < wanted)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001862 goto connection_empty;
1863
1864 /* Return result. */
1865 luaL_pushresult(&socket->b);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001866 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001867 return 1;
1868
1869connection_closed:
1870
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001871 xref_unlock(&socket->xref, peer);
1872
1873no_peer:
1874
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001875 /* If the buffer containds data. */
1876 if (socket->b.n > 0) {
1877 luaL_pushresult(&socket->b);
1878 return 1;
1879 }
1880 lua_pushnil(L);
1881 lua_pushstring(L, "connection closed.");
1882 return 2;
1883
1884connection_empty:
1885
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001886 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) {
1887 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001888 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001889 }
1890 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02001891 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_receive_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001892 return 0;
1893}
1894
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001895/* This Lua function gets two parameters. The first one can be string
1896 * or a number. If the string is "*l", the user requires one line. If
1897 * the string is "*a", the user requires all the contents of the stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001898 * If the value is a number, the user require a number of bytes equal
1899 * to the value. The default value is "*l" (a line).
1900 *
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001901 * This parameter with a variable type is converted in integer. This
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001902 * integer takes this values:
1903 * -1 : read a line
1904 * -2 : read all the stream
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001905 * >0 : amount of bytes.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001906 *
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001907 * The second parameter is optional. It contains a string that must be
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001908 * concatenated with the read data.
1909 */
1910__LJMP static int hlua_socket_receive(struct lua_State *L)
1911{
1912 int wanted = HLSR_READ_LINE;
1913 const char *pattern;
1914 int type;
1915 char *error;
1916 size_t len;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001917 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001918
1919 if (lua_gettop(L) < 1 || lua_gettop(L) > 3)
1920 WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments."));
1921
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001922 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001923
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001924 /* Check if we run on the same thread than the xreator thread.
1925 * We cannot access to the socket if the thread is different.
1926 */
1927 if (socket->tid != tid)
1928 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1929
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001930 /* check for pattern. */
1931 if (lua_gettop(L) >= 2) {
1932 type = lua_type(L, 2);
1933 if (type == LUA_TSTRING) {
1934 pattern = lua_tostring(L, 2);
1935 if (strcmp(pattern, "*a") == 0)
1936 wanted = HLSR_READ_ALL;
1937 else if (strcmp(pattern, "*l") == 0)
1938 wanted = HLSR_READ_LINE;
1939 else {
1940 wanted = strtoll(pattern, &error, 10);
1941 if (*error != '\0')
1942 WILL_LJMP(luaL_error(L, "Unsupported pattern."));
1943 }
1944 }
1945 else if (type == LUA_TNUMBER) {
1946 wanted = lua_tointeger(L, 2);
1947 if (wanted < 0)
1948 WILL_LJMP(luaL_error(L, "Unsupported size."));
1949 }
1950 }
1951
1952 /* Set pattern. */
1953 lua_pushinteger(L, wanted);
Tim Duesterhusc6e377e2018-01-04 19:32:13 +01001954
1955 /* Check if we would replace the top by itself. */
1956 if (lua_gettop(L) != 2)
1957 lua_replace(L, 2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001958
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001959 /* init buffer, and fill it with prefix. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001960 luaL_buffinit(L, &socket->b);
1961
1962 /* Check prefix. */
1963 if (lua_gettop(L) >= 3) {
1964 if (lua_type(L, 3) != LUA_TSTRING)
1965 WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix"));
1966 pattern = lua_tolstring(L, 3, &len);
1967 luaL_addlstring(&socket->b, pattern, len);
1968 }
1969
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001970 return __LJMP(hlua_socket_receive_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001971}
1972
1973/* Write the Lua input string in the output buffer.
Mark Lakes22154b42018-01-29 14:38:40 -08001974 * This function returns a yield if no space is available.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001975 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001976static int hlua_socket_write_yield(struct lua_State *L,int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001977{
1978 struct hlua_socket *socket;
1979 struct hlua *hlua = hlua_gethlua(L);
1980 struct appctx *appctx;
1981 size_t buf_len;
1982 const char *buf;
1983 int len;
1984 int send_len;
1985 int sent;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001986 struct xref *peer;
1987 struct stream_interface *si;
1988 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001989
1990 /* Check if this lua stack is schedulable. */
1991 if (!hlua || !hlua->task)
1992 WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in "
1993 "'frontend', 'backend' or 'task'"));
1994
1995 /* Get object */
1996 socket = MAY_LJMP(hlua_checksocket(L, 1));
1997 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001998 sent = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001999
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002000 /* Check if we run on the same thread than the xreator thread.
2001 * We cannot access to the socket if the thread is different.
2002 */
2003 if (socket->tid != tid)
2004 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2005
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002006 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002007 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002008 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002009 lua_pushinteger(L, -1);
2010 return 1;
2011 }
2012 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2013 si = appctx->owner;
2014 s = si_strm(si);
2015
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002016 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002017 if (channel_output_closed(&s->req)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002018 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002019 lua_pushinteger(L, -1);
2020 return 1;
2021 }
2022
2023 /* Update the input buffer data. */
2024 buf += sent;
2025 send_len = buf_len - sent;
2026
2027 /* All the data are sent. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002028 if (sent >= buf_len) {
2029 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002030 return 1; /* Implicitly return the length sent. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002031 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002032
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002033 /* Check if the buffer is available because HAProxy doesn't allocate
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01002034 * the request buffer if its not required.
2035 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002036 if (s->req.buf.size == 0) {
Willy Tarreau581abd32018-10-25 10:21:41 +02002037 if (!si_alloc_ibuf(si, &appctx->buffer_wait))
Christopher Faulet33834b12016-12-19 09:29:06 +01002038 goto hlua_socket_write_yield_return;
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01002039 }
2040
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002041 /* Check for available space. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002042 len = b_room(&s->req.buf);
Christopher Faulet33834b12016-12-19 09:29:06 +01002043 if (len <= 0) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002044 goto hlua_socket_write_yield_return;
Christopher Faulet33834b12016-12-19 09:29:06 +01002045 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002046
2047 /* send data */
2048 if (len < send_len)
2049 send_len = len;
Thierry FOURNIER66b89192018-05-27 01:14:47 +02002050 len = ci_putblk(&s->req, buf, send_len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002051
2052 /* "Not enough space" (-1), "Buffer too little to contain
2053 * the data" (-2) are not expected because the available length
2054 * is tested.
2055 * Other unknown error are also not expected.
2056 */
2057 if (len <= 0) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01002058 if (len == -1)
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002059 s->req.flags |= CF_WAKE_WRITE;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002060
sada05ed3302018-05-11 11:48:18 -07002061 MAY_LJMP(hlua_socket_close_helper(L));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002062 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002063 lua_pushinteger(L, -1);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002064 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002065 return 1;
2066 }
2067
2068 /* update buffers. */
Thierry FOURNIER101b9762018-05-27 01:27:40 +02002069 appctx_wakeup(appctx);
Willy Tarreaude70fa12015-09-26 11:25:05 +02002070
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002071 s->req.rex = TICK_ETERNITY;
2072 s->res.wex = TICK_ETERNITY;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002073
2074 /* Update length sent. */
2075 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002076 lua_pushinteger(L, sent + len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002077
2078 /* All the data buffer is sent ? */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002079 if (sent + len >= buf_len) {
2080 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002081 return 1;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002082 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002083
2084hlua_socket_write_yield_return:
Thierry FOURNIERba42fcd2018-05-27 00:59:48 +02002085 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2086 xref_unlock(&socket->xref, peer);
2087 WILL_LJMP(luaL_error(L, "out of memory"));
2088 }
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002089 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002090 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_write_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002091 return 0;
2092}
2093
2094/* This function initiate the send of data. It just check the input
2095 * parameters and push an integer in the Lua stack that contain the
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002096 * amount of data written to the buffer. This is used by the function
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002097 * "hlua_socket_write_yield" that can yield.
2098 *
2099 * The Lua function gets between 3 and 4 parameters. The first one is
2100 * the associated object. The second is a string buffer. The third is
2101 * a facultative integer that represents where is the buffer position
2102 * of the start of the data that can send. The first byte is the
2103 * position "1". The default value is "1". The fourth argument is a
2104 * facultative integer that represents where is the buffer position
2105 * of the end of the data that can send. The default is the last byte.
2106 */
2107static int hlua_socket_send(struct lua_State *L)
2108{
2109 int i;
2110 int j;
2111 const char *buf;
2112 size_t buf_len;
2113
2114 /* Check number of arguments. */
2115 if (lua_gettop(L) < 2 || lua_gettop(L) > 4)
2116 WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments"));
2117
2118 /* Get the string. */
2119 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
2120
2121 /* Get and check j. */
2122 if (lua_gettop(L) == 4) {
2123 j = MAY_LJMP(luaL_checkinteger(L, 4));
2124 if (j < 0)
2125 j = buf_len + j + 1;
2126 if (j > buf_len)
2127 j = buf_len + 1;
2128 lua_pop(L, 1);
2129 }
2130 else
2131 j = buf_len;
2132
2133 /* Get and check i. */
2134 if (lua_gettop(L) == 3) {
2135 i = MAY_LJMP(luaL_checkinteger(L, 3));
2136 if (i < 0)
2137 i = buf_len + i + 1;
2138 if (i > buf_len)
2139 i = buf_len + 1;
2140 lua_pop(L, 1);
2141 } else
2142 i = 1;
2143
2144 /* Check bth i and j. */
2145 if (i > j) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002146 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002147 return 1;
2148 }
2149 if (i == 0 && j == 0) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002150 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002151 return 1;
2152 }
2153 if (i == 0)
2154 i = 1;
2155 if (j == 0)
2156 j = 1;
2157
2158 /* Pop the string. */
2159 lua_pop(L, 1);
2160
2161 /* Update the buffer length. */
2162 buf += i - 1;
2163 buf_len = j - i + 1;
2164 lua_pushlstring(L, buf, buf_len);
2165
2166 /* This unsigned is used to remember the amount of sent data. */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002167 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002168
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002169 return MAY_LJMP(hlua_socket_write_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002170}
2171
Willy Tarreau22b0a682015-06-17 19:43:49 +02002172#define SOCKET_INFO_MAX_LEN sizeof("[0000:0000:0000:0000:0000:0000:0000:0000]:12345")
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002173__LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr)
2174{
2175 static char buffer[SOCKET_INFO_MAX_LEN];
2176 int ret;
2177 int len;
2178 char *p;
2179
2180 ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1);
2181 if (ret <= 0) {
2182 lua_pushnil(L);
2183 return 1;
2184 }
2185
2186 if (ret == AF_UNIX) {
2187 lua_pushstring(L, buffer+1);
2188 return 1;
2189 }
2190 else if (ret == AF_INET6) {
2191 buffer[0] = '[';
2192 len = strlen(buffer);
2193 buffer[len] = ']';
2194 len++;
2195 buffer[len] = ':';
2196 len++;
2197 p = buffer;
2198 }
2199 else if (ret == AF_INET) {
2200 p = buffer + 1;
2201 len = strlen(p);
2202 p[len] = ':';
2203 len++;
2204 }
2205 else {
2206 lua_pushnil(L);
2207 return 1;
2208 }
2209
2210 if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) {
2211 lua_pushnil(L);
2212 return 1;
2213 }
2214
2215 lua_pushstring(L, p);
2216 return 1;
2217}
2218
2219/* Returns information about the peer of the connection. */
2220__LJMP static int hlua_socket_getpeername(struct lua_State *L)
2221{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002222 struct hlua_socket *socket;
2223 struct connection *conn;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002224 struct xref *peer;
2225 struct appctx *appctx;
2226 struct stream_interface *si;
2227 struct stream *s;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002228 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002229
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002230 MAY_LJMP(check_args(L, 1, "getpeername"));
2231
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002232 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002233
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002234 /* Check if we run on the same thread than the xreator thread.
2235 * We cannot access to the socket if the thread is different.
2236 */
2237 if (socket->tid != tid)
2238 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2239
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002240 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002241 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002242 if (!peer) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002243 lua_pushnil(L);
2244 return 1;
2245 }
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002246 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2247 si = appctx->owner;
2248 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002249
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002250 conn = cs_conn(objt_cs(s->si[1].end));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002251 if (!conn) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002252 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002253 lua_pushnil(L);
2254 return 1;
2255 }
2256
Willy Tarreaua71f6422016-11-16 17:00:14 +01002257 conn_get_to_addr(conn);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002258 if (!(conn->flags & CO_FL_ADDR_TO_SET)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002259 xref_unlock(&socket->xref, peer);
Willy Tarreaua71f6422016-11-16 17:00:14 +01002260 lua_pushnil(L);
2261 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002262 }
2263
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002264 ret = MAY_LJMP(hlua_socket_info(L, &conn->addr.to));
2265 xref_unlock(&socket->xref, peer);
2266 return ret;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002267}
2268
2269/* Returns information about my connection side. */
2270static int hlua_socket_getsockname(struct lua_State *L)
2271{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002272 struct hlua_socket *socket;
2273 struct connection *conn;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002274 struct appctx *appctx;
2275 struct xref *peer;
2276 struct stream_interface *si;
2277 struct stream *s;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002278 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002279
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002280 MAY_LJMP(check_args(L, 1, "getsockname"));
2281
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002282 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002283
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002284 /* Check if we run on the same thread than the xreator thread.
2285 * We cannot access to the socket if the thread is different.
2286 */
2287 if (socket->tid != tid)
2288 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2289
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002290 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002291 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002292 if (!peer) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002293 lua_pushnil(L);
2294 return 1;
2295 }
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002296 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2297 si = appctx->owner;
2298 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002299
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002300 conn = cs_conn(objt_cs(s->si[1].end));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002301 if (!conn) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002302 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002303 lua_pushnil(L);
2304 return 1;
2305 }
2306
Willy Tarreaua71f6422016-11-16 17:00:14 +01002307 conn_get_from_addr(conn);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002308 if (!(conn->flags & CO_FL_ADDR_FROM_SET)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002309 xref_unlock(&socket->xref, peer);
Willy Tarreaua71f6422016-11-16 17:00:14 +01002310 lua_pushnil(L);
2311 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002312 }
2313
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002314 ret = hlua_socket_info(L, &conn->addr.from);
2315 xref_unlock(&socket->xref, peer);
2316 return ret;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002317}
2318
2319/* This struct define the applet. */
Willy Tarreau30576452015-04-13 13:50:30 +02002320static struct applet update_applet = {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002321 .obj_type = OBJ_TYPE_APPLET,
2322 .name = "<LUA_TCP>",
2323 .fct = hlua_socket_handler,
2324 .release = hlua_socket_release,
2325};
2326
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002327__LJMP static int hlua_socket_connect_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002328{
2329 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
2330 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002331 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002332 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002333 struct stream_interface *si;
2334 struct stream *s;
2335
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002336 /* Check if we run on the same thread than the xreator thread.
2337 * We cannot access to the socket if the thread is different.
2338 */
2339 if (socket->tid != tid)
2340 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2341
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002342 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002343 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002344 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002345 lua_pushnil(L);
2346 lua_pushstring(L, "Can't connect");
2347 return 2;
2348 }
2349 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2350 si = appctx->owner;
2351 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002352
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002353 /* Check if we run on the same thread than the xreator thread.
2354 * We cannot access to the socket if the thread is different.
2355 */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002356 if (socket->tid != tid) {
2357 xref_unlock(&socket->xref, peer);
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002358 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002359 }
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002360
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002361 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002362 if (!hlua || channel_output_closed(&s->req)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002363 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002364 lua_pushnil(L);
2365 lua_pushstring(L, "Can't connect");
2366 return 2;
2367 }
2368
Willy Tarreaue09101e2018-10-16 17:37:12 +02002369 appctx = __objt_appctx(s->si[0].end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002370
2371 /* Check for connection established. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002372 if (appctx->ctx.hlua_cosocket.connected) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002373 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002374 lua_pushinteger(L, 1);
2375 return 1;
2376 }
2377
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002378 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2379 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002380 WILL_LJMP(luaL_error(L, "out of memory error"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002381 }
2382 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002383 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002384 return 0;
2385}
2386
2387/* This function fail or initite the connection. */
2388__LJMP static int hlua_socket_connect(struct lua_State *L)
2389{
2390 struct hlua_socket *socket;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002391 int port = -1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002392 const char *ip;
2393 struct connection *conn;
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002394 struct hlua *hlua;
2395 struct appctx *appctx;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002396 int low, high;
2397 struct sockaddr_storage *addr;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002398 struct xref *peer;
2399 struct stream_interface *si;
2400 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002401
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002402 if (lua_gettop(L) < 2)
2403 WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments"));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002404
2405 /* Get args. */
2406 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002407
2408 /* Check if we run on the same thread than the xreator thread.
2409 * We cannot access to the socket if the thread is different.
2410 */
2411 if (socket->tid != tid)
2412 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2413
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002414 ip = MAY_LJMP(luaL_checkstring(L, 2));
Tim Duesterhus6edab862018-01-06 19:04:45 +01002415 if (lua_gettop(L) >= 3) {
2416 luaL_Buffer b;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002417 port = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002418
Tim Duesterhus6edab862018-01-06 19:04:45 +01002419 /* Force the ip to end with a colon, to support IPv6 addresses
2420 * that are not enclosed within square brackets.
2421 */
2422 if (port > 0) {
2423 luaL_buffinit(L, &b);
2424 luaL_addstring(&b, ip);
2425 luaL_addchar(&b, ':');
2426 luaL_pushresult(&b);
2427 ip = lua_tolstring(L, lua_gettop(L), NULL);
2428 }
2429 }
2430
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002431 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002432 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002433 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002434 lua_pushnil(L);
2435 return 1;
2436 }
2437 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2438 si = appctx->owner;
2439 s = si_strm(si);
2440
2441 /* Initialise connection. */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002442 conn = cs_conn(si_alloc_cs(&s->si[1], NULL));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002443 if (!conn) {
2444 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002445 WILL_LJMP(luaL_error(L, "connect: internal error"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002446 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002447
Willy Tarreau3adac082015-09-26 17:51:09 +02002448 /* needed for the connection not to be closed */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002449 conn->target = s->target;
Willy Tarreau3adac082015-09-26 17:51:09 +02002450
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002451 /* Parse ip address. */
Willy Tarreau48ef4c92017-01-06 18:32:38 +01002452 addr = str2sa_range(ip, NULL, &low, &high, NULL, NULL, NULL, 0);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002453 if (!addr) {
2454 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002455 WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002456 }
2457 if (low != high) {
2458 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002459 WILL_LJMP(luaL_error(L, "connect: port ranges not supported : address '%s'", ip));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002460 }
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002461 memcpy(&conn->addr.to, addr, sizeof(struct sockaddr_storage));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002462
2463 /* Set port. */
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002464 if (low == 0) {
2465 if (conn->addr.to.ss_family == AF_INET) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002466 if (port == -1) {
2467 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002468 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002469 }
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002470 ((struct sockaddr_in *)&conn->addr.to)->sin_port = htons(port);
2471 } else if (conn->addr.to.ss_family == AF_INET6) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002472 if (port == -1) {
2473 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002474 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002475 }
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002476 ((struct sockaddr_in6 *)&conn->addr.to)->sin6_port = htons(port);
2477 }
2478 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002479
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002480 hlua = hlua_gethlua(L);
Willy Tarreaue09101e2018-10-16 17:37:12 +02002481 appctx = __objt_appctx(s->si[0].end);
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002482
2483 /* inform the stream that we want to be notified whenever the
2484 * connection completes.
2485 */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01002486 si_cant_get(&s->si[0]);
Willy Tarreau3367d412018-11-15 10:57:41 +01002487 si_rx_endp_more(&s->si[0]);
Thierry FOURNIER8c8fbbe2015-09-26 17:02:35 +02002488 appctx_wakeup(appctx);
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002489
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +02002490 hlua->flags |= HLUA_MUST_GC;
2491
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002492 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2493 xref_unlock(&socket->xref, peer);
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002494 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002495 }
2496 xref_unlock(&socket->xref, peer);
PiBa-NL706d5ee2018-05-05 23:51:42 +02002497
2498 task_wakeup(s->task, TASK_WOKEN_INIT);
2499 /* Return yield waiting for connection. */
2500
Willy Tarreau9635e032018-10-16 17:52:55 +02002501 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002502
2503 return 0;
2504}
2505
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002506#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002507__LJMP static int hlua_socket_connect_ssl(struct lua_State *L)
2508{
2509 struct hlua_socket *socket;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002510 struct xref *peer;
2511 struct appctx *appctx;
2512 struct stream_interface *si;
2513 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002514
2515 MAY_LJMP(check_args(L, 3, "connect_ssl"));
2516 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002517
2518 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002519 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002520 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002521 lua_pushnil(L);
2522 return 1;
2523 }
2524 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2525 si = appctx->owner;
2526 s = si_strm(si);
2527
2528 s->target = &socket_ssl.obj_type;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002529 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002530 return MAY_LJMP(hlua_socket_connect(L));
2531}
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002532#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002533
2534__LJMP static int hlua_socket_setoption(struct lua_State *L)
2535{
2536 return 0;
2537}
2538
2539__LJMP static int hlua_socket_settimeout(struct lua_State *L)
2540{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002541 struct hlua_socket *socket;
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002542 int tmout;
Mark Lakes56cc1252018-03-27 09:48:06 +02002543 double dtmout;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002544 struct xref *peer;
2545 struct appctx *appctx;
2546 struct stream_interface *si;
2547 struct stream *s;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002548
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002549 MAY_LJMP(check_args(L, 2, "settimeout"));
2550
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002551 socket = MAY_LJMP(hlua_checksocket(L, 1));
Mark Lakes56cc1252018-03-27 09:48:06 +02002552
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002553 /* convert the timeout to millis */
2554 dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002555
Thierry Fournier17a921b2018-03-08 09:59:02 +01002556 /* Check for negative values */
Mark Lakes56cc1252018-03-27 09:48:06 +02002557 if (dtmout < 0)
Thierry Fournier17a921b2018-03-08 09:59:02 +01002558 WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values"));
2559
Mark Lakes56cc1252018-03-27 09:48:06 +02002560 if (dtmout > INT_MAX) /* overflow check */
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002561 WILL_LJMP(luaL_error(L, "settimeout: cannot set values larger than %d ms", INT_MAX));
Mark Lakes56cc1252018-03-27 09:48:06 +02002562
2563 tmout = MS_TO_TICKS((int)dtmout);
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002564 if (tmout == 0)
2565 tmout++; /* very small timeouts are adjusted to a minium of 1ms */
Mark Lakes56cc1252018-03-27 09:48:06 +02002566
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002567 /* Check if we run on the same thread than the xreator thread.
2568 * We cannot access to the socket if the thread is different.
2569 */
2570 if (socket->tid != tid)
2571 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2572
Mark Lakes56cc1252018-03-27 09:48:06 +02002573 /* check for connection break. If some data were read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002574 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002575 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002576 hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts.");
2577 WILL_LJMP(lua_error(L));
2578 return 0;
2579 }
2580 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2581 si = appctx->owner;
2582 s = si_strm(si);
2583
Cyril Bonté7bb63452018-08-17 23:51:02 +02002584 s->sess->fe->timeout.connect = tmout;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002585 s->req.rto = tmout;
2586 s->req.wto = tmout;
2587 s->res.rto = tmout;
2588 s->res.wto = tmout;
Cyril Bonté7bb63452018-08-17 23:51:02 +02002589 s->req.rex = tick_add_ifset(now_ms, tmout);
2590 s->req.wex = tick_add_ifset(now_ms, tmout);
2591 s->res.rex = tick_add_ifset(now_ms, tmout);
2592 s->res.wex = tick_add_ifset(now_ms, tmout);
2593
2594 s->task->expire = tick_add_ifset(now_ms, tmout);
2595 task_queue(s->task);
2596
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002597 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002598
Thierry Fourniere9636f12018-03-08 09:54:32 +01002599 lua_pushinteger(L, 1);
Tim Duesterhus119a5f12018-01-06 19:16:25 +01002600 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002601}
2602
2603__LJMP static int hlua_socket_new(lua_State *L)
2604{
2605 struct hlua_socket *socket;
2606 struct appctx *appctx;
Willy Tarreau15b5e142015-04-04 14:38:25 +02002607 struct session *sess;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002608 struct stream *strm;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002609
2610 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002611 if (!lua_checkstack(L, 3)) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002612 hlua_pusherror(L, "socket: full stack");
2613 goto out_fail_conf;
2614 }
2615
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002616 /* Create the object: obj[0] = userdata. */
2617 lua_newtable(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002618 socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket)));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002619 lua_rawseti(L, -2, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002620 memset(socket, 0, sizeof(*socket));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002621 socket->tid = tid;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002622
Thierry FOURNIER4a6170c2015-03-09 17:07:10 +01002623 /* Check if the various memory pools are intialized. */
Willy Tarreaubafbe012017-11-24 17:34:44 +01002624 if (!pool_head_stream || !pool_head_buffer) {
Thierry FOURNIER4a6170c2015-03-09 17:07:10 +01002625 hlua_pusherror(L, "socket: uninitialized pools.");
2626 goto out_fail_conf;
2627 }
2628
Willy Tarreau87b09662015-04-03 00:22:06 +02002629 /* Pop a class stream metatable and affect it to the userdata. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002630 lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref);
2631 lua_setmetatable(L, -2);
2632
Willy Tarreaud420a972015-04-06 00:39:18 +02002633 /* Create the applet context */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01002634 appctx = appctx_new(&update_applet, tid_bit);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002635 if (!appctx) {
2636 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaufeb76402015-04-03 14:10:06 +02002637 goto out_fail_conf;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002638 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002639
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002640 appctx->ctx.hlua_cosocket.connected = 0;
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02002641 appctx->ctx.hlua_cosocket.die = 0;
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002642 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write);
2643 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read);
Willy Tarreaub2bf8332015-04-04 15:58:58 +02002644
Willy Tarreaud420a972015-04-06 00:39:18 +02002645 /* Now create a session, task and stream for this applet */
2646 sess = session_new(&socket_proxy, NULL, &appctx->obj_type);
2647 if (!sess) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002648 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002649 goto out_fail_sess;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002650 }
2651
Willy Tarreau87787ac2017-08-28 16:22:54 +02002652 strm = stream_new(sess, &appctx->obj_type);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002653 if (!strm) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002654 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002655 goto out_fail_stream;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002656 }
2657
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002658 /* Initialise cross reference between stream and Lua socket object. */
2659 xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002660
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002661 /* Configure "right" stream interface. this "si" is used to connect
2662 * and retrieve data from the server. The connection is initialized
2663 * with the "struct server".
2664 */
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002665 si_set_state(&strm->si[1], SI_ST_ASS);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002666
2667 /* Force destination server. */
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002668 strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET | SF_BE_ASSIGNED;
2669 strm->target = &socket_tcp.obj_type;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002670
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002671 return 1;
2672
Willy Tarreaud420a972015-04-06 00:39:18 +02002673 out_fail_stream:
Willy Tarreau11c36242015-04-04 15:54:03 +02002674 session_free(sess);
Willy Tarreaud420a972015-04-06 00:39:18 +02002675 out_fail_sess:
2676 appctx_free(appctx);
2677 out_fail_conf:
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002678 WILL_LJMP(lua_error(L));
2679 return 0;
2680}
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01002681
2682/*
2683 *
2684 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002685 * Class Channel
2686 *
2687 *
2688 */
2689
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002690/* This function is called before the Lua execution. It stores
2691 * the differents parsers state before executing some Lua code.
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002692 */
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002693static inline void consistency_set(struct stream *stream, int opt, struct hlua_consistency *c)
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002694{
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002695 c->mode = stream->be->mode;
2696 switch (c->mode) {
2697 case PR_MODE_HTTP:
2698 c->data.http.dir = opt & SMP_OPT_DIR;
2699 if (c->data.http.dir == SMP_OPT_DIR_REQ)
2700 c->data.http.state = stream->txn->req.msg_state;
2701 else
2702 c->data.http.state = stream->txn->rsp.msg_state;
2703 break;
2704 default:
2705 break;
2706 }
2707}
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002708
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002709/* This function is called after the Lua execution. it
2710 * returns true if the parser state is consistent, otherwise,
2711 * it return false.
2712 *
2713 * In HTTP mode, the parser state must be in the same state
2714 * or greater when we exit the function. Even if we do a
2715 * control yield. This prevent to break the HTTP message
2716 * from the Lua code.
2717 */
2718static inline int consistency_check(struct stream *stream, int opt, struct hlua_consistency *c)
2719{
2720 if (c->mode != stream->be->mode)
2721 return 0;
2722
2723 switch (c->mode) {
2724 case PR_MODE_HTTP:
2725 if (c->data.http.dir != (opt & SMP_OPT_DIR))
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002726 return 0;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002727 if (c->data.http.dir == SMP_OPT_DIR_REQ)
2728 return stream->txn->req.msg_state >= c->data.http.state;
2729 else
2730 return stream->txn->rsp.msg_state >= c->data.http.state;
2731 default:
2732 return 1;
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002733 }
2734 return 1;
2735}
2736
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002737/* Returns the struct hlua_channel join to the class channel in the
2738 * stack entry "ud" or throws an argument error.
2739 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002740__LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002741{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02002742 return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002743}
2744
Willy Tarreau47860ed2015-03-10 14:07:50 +01002745/* Pushes the channel onto the top of the stack. If the stask does not have a
2746 * free slots, the function fails and returns 0;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002747 */
Willy Tarreau2a71af42015-03-10 13:51:50 +01002748static int hlua_channel_new(lua_State *L, struct channel *channel)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002749{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002750 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002751 if (!lua_checkstack(L, 3))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002752 return 0;
2753
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002754 lua_newtable(L);
Willy Tarreau47860ed2015-03-10 14:07:50 +01002755 lua_pushlightuserdata(L, channel);
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002756 lua_rawseti(L, -2, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002757
2758 /* Pop a class sesison metatable and affect it to the userdata. */
2759 lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref);
2760 lua_setmetatable(L, -2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002761 return 1;
2762}
2763
2764/* Duplicate all the data present in the input channel and put it
2765 * in a string LUA variables. Returns -1 and push a nil value in
2766 * the stack if the channel is closed and all the data are consumed,
2767 * returns 0 if no data are available, otherwise it returns the length
2768 * of the builded string.
2769 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002770static inline int _hlua_channel_dup(struct channel *chn, lua_State *L)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002771{
2772 char *blk1;
2773 char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02002774 size_t len1;
2775 size_t len2;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002776 int ret;
2777 luaL_Buffer b;
2778
Willy Tarreau06d80a92017-10-19 14:32:15 +02002779 ret = ci_getblk_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002780 if (unlikely(ret == 0))
2781 return 0;
2782
2783 if (unlikely(ret < 0)) {
2784 lua_pushnil(L);
2785 return -1;
2786 }
2787
2788 luaL_buffinit(L, &b);
2789 luaL_addlstring(&b, blk1, len1);
2790 if (unlikely(ret == 2))
2791 luaL_addlstring(&b, blk2, len2);
2792 luaL_pushresult(&b);
2793
2794 if (unlikely(ret == 2))
2795 return len1 + len2;
2796 return len1;
2797}
2798
2799/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2800 * a yield. This function keep the data in the buffer.
2801 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002802__LJMP static int hlua_channel_dup_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002803{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002804 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002805
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002806 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2807
Christopher Faulet3f829a42018-12-13 21:56:45 +01002808 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2809 WILL_LJMP(lua_error(L));
2810
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002811 if (_hlua_channel_dup(chn, L) == 0)
Willy Tarreau9635e032018-10-16 17:52:55 +02002812 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_dup_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002813 return 1;
2814}
2815
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002816/* Check arguments for the function "hlua_channel_dup_yield". */
2817__LJMP static int hlua_channel_dup(lua_State *L)
2818{
2819 MAY_LJMP(check_args(L, 1, "dup"));
2820 MAY_LJMP(hlua_checkchannel(L, 1));
2821 return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0));
2822}
2823
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002824/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2825 * a yield. This function consumes the data in the buffer. It returns
2826 * a string containing the data or a nil pointer if no data are available
2827 * and the channel is closed.
2828 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002829__LJMP static int hlua_channel_get_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002830{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002831 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002832 int ret;
2833
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002834 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002835
Christopher Faulet3f829a42018-12-13 21:56:45 +01002836 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2837 WILL_LJMP(lua_error(L));
2838
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002839 ret = _hlua_channel_dup(chn, L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002840 if (unlikely(ret == 0))
Willy Tarreau9635e032018-10-16 17:52:55 +02002841 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002842
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002843 if (unlikely(ret == -1))
2844 return 1;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002845
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002846 b_sub(&chn->buf, ret);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002847 return 1;
2848}
2849
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002850/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002851__LJMP static int hlua_channel_get(lua_State *L)
2852{
2853 MAY_LJMP(check_args(L, 1, "get"));
2854 MAY_LJMP(hlua_checkchannel(L, 1));
2855 return MAY_LJMP(hlua_channel_get_yield(L, 0, 0));
2856}
2857
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002858/* This functions consumes and returns one line. If the channel is closed,
2859 * and the last data does not contains a final '\n', the data are returned
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002860 * without the final '\n'. When no more data are available, it returns nil
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002861 * value.
2862 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002863__LJMP static int hlua_channel_getline_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002864{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002865 char *blk1;
2866 char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02002867 size_t len1;
2868 size_t len2;
2869 size_t len;
Willy Tarreau47860ed2015-03-10 14:07:50 +01002870 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002871 int ret;
2872 luaL_Buffer b;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002873
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002874 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2875
Christopher Faulet3f829a42018-12-13 21:56:45 +01002876 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2877 WILL_LJMP(lua_error(L));
2878
Willy Tarreau06d80a92017-10-19 14:32:15 +02002879 ret = ci_getline_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002880 if (ret == 0)
Willy Tarreau9635e032018-10-16 17:52:55 +02002881 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_getline_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002882
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002883 if (ret == -1) {
2884 lua_pushnil(L);
2885 return 1;
2886 }
2887
2888 luaL_buffinit(L, &b);
2889 luaL_addlstring(&b, blk1, len1);
2890 len = len1;
2891 if (unlikely(ret == 2)) {
2892 luaL_addlstring(&b, blk2, len2);
2893 len += len2;
2894 }
2895 luaL_pushresult(&b);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002896 b_rep_blk(&chn->buf, ci_head(chn), ci_head(chn) + len, NULL, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002897 return 1;
2898}
2899
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002900/* Check arguments for the function "hlua_channel_getline_yield". */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002901__LJMP static int hlua_channel_getline(lua_State *L)
2902{
2903 MAY_LJMP(check_args(L, 1, "getline"));
2904 MAY_LJMP(hlua_checkchannel(L, 1));
2905 return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0));
2906}
2907
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002908/* This function takes a string as input, and append it at the
2909 * input side of channel. If the data is too big, but a space
2910 * is probably available after sending some data, the function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002911 * yields. If the data is bigger than the buffer, or if the
2912 * channel is closed, it returns -1. Otherwise, it returns the
2913 * amount of data written.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002914 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002915__LJMP static int hlua_channel_append_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002916{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002917 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002918 size_t len;
2919 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
2920 int l = MAY_LJMP(luaL_checkinteger(L, 3));
2921 int ret;
2922 int max;
2923
Christopher Faulet3f829a42018-12-13 21:56:45 +01002924 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2925 WILL_LJMP(lua_error(L));
2926
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002927 /* Check if the buffer is available because HAProxy doesn't allocate
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002928 * the request buffer if its not required.
2929 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002930 if (chn->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +01002931 si_rx_buff_blk(chn_prod(chn));
Willy Tarreau9635e032018-10-16 17:52:55 +02002932 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002933 }
2934
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002935 max = channel_recv_limit(chn) - b_data(&chn->buf);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002936 if (max > len - l)
2937 max = len - l;
2938
Willy Tarreau06d80a92017-10-19 14:32:15 +02002939 ret = ci_putblk(chn, str + l, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002940 if (ret == -2 || ret == -3) {
2941 lua_pushinteger(L, -1);
2942 return 1;
2943 }
Willy Tarreaubc18da12015-03-13 14:00:47 +01002944 if (ret == -1) {
2945 chn->flags |= CF_WAKE_WRITE;
Willy Tarreau9635e032018-10-16 17:52:55 +02002946 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Willy Tarreaubc18da12015-03-13 14:00:47 +01002947 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002948 l += ret;
2949 lua_pop(L, 1);
2950 lua_pushinteger(L, l);
2951
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002952 max = channel_recv_limit(chn) - b_data(&chn->buf);
Willy Tarreaua79021a2018-06-15 18:07:57 +02002953 if (max == 0 && co_data(chn) == 0) {
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002954 /* There are no space available, and the output buffer is empty.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002955 * in this case, we cannot add more data, so we cannot yield,
2956 * we return the amount of copyied data.
2957 */
2958 return 1;
2959 }
2960 if (l < len)
Willy Tarreau9635e032018-10-16 17:52:55 +02002961 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002962 return 1;
2963}
2964
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002965/* Just a wrapper of "hlua_channel_append_yield". It returns the length
2966 * of the written string, or -1 if the channel is closed or if the
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002967 * buffer size is too little for the data.
2968 */
2969__LJMP static int hlua_channel_append(lua_State *L)
2970{
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002971 size_t len;
2972
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002973 MAY_LJMP(check_args(L, 2, "append"));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002974 MAY_LJMP(hlua_checkchannel(L, 1));
2975 MAY_LJMP(luaL_checklstring(L, 2, &len));
2976 MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002977 lua_pushinteger(L, 0);
2978
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002979 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002980}
2981
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002982/* Just a wrapper of "hlua_channel_append_yield". This wrapper starts
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002983 * his process by cleaning the buffer. The result is a replacement
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002984 * of the current data. It returns the length of the written string,
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002985 * or -1 if the channel is closed or if the buffer size is too
2986 * little for the data.
2987 */
2988__LJMP static int hlua_channel_set(lua_State *L)
2989{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002990 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002991
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002992 MAY_LJMP(check_args(L, 2, "set"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002993 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002994 lua_pushinteger(L, 0);
2995
Christopher Faulet3f829a42018-12-13 21:56:45 +01002996 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2997 WILL_LJMP(lua_error(L));
2998
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002999 b_set_data(&chn->buf, co_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003000
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003001 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003002}
3003
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003004/* Append data in the output side of the buffer. This data is immediately
3005 * sent. The function returns the amount of data written. If the buffer
3006 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003007 * if the channel is closed.
3008 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003009__LJMP static int hlua_channel_send_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003010{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003011 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003012 size_t len;
3013 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
3014 int l = MAY_LJMP(luaL_checkinteger(L, 3));
3015 int max;
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003016 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003017
Christopher Faulet3f829a42018-12-13 21:56:45 +01003018 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
3019 WILL_LJMP(lua_error(L));
3020
Willy Tarreau47860ed2015-03-10 14:07:50 +01003021 if (unlikely(channel_output_closed(chn))) {
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003022 lua_pushinteger(L, -1);
3023 return 1;
3024 }
3025
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003026 /* Check if the buffer is available because HAProxy doesn't allocate
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01003027 * the request buffer if its not required.
3028 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003029 if (chn->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +01003030 si_rx_buff_blk(chn_prod(chn));
Willy Tarreau9635e032018-10-16 17:52:55 +02003031 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01003032 }
3033
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003034 /* The written data will be immediately sent, so we can check
3035 * the available space without taking in account the reserve.
3036 * The reserve is guaranteed for the processing of incoming
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003037 * data, because the buffer will be flushed.
3038 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003039 max = b_room(&chn->buf);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003040
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003041 /* If there is no space available, and the output buffer is empty.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003042 * in this case, we cannot add more data, so we cannot yield,
3043 * we return the amount of copyied data.
3044 */
Willy Tarreaua79021a2018-06-15 18:07:57 +02003045 if (max == 0 && co_data(chn) == 0)
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003046 return 1;
3047
3048 /* Adjust the real required length. */
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003049 if (max > len - l)
3050 max = len - l;
3051
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003052 /* The buffer available size may be not contiguous. This test
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003053 * detects a non contiguous buffer and realign it.
3054 */
Willy Tarreau3f679992018-06-15 15:06:42 +02003055 if (ci_space_for_replace(chn) < max)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003056 channel_slow_realign(chn, trash.area);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003057
3058 /* Copy input data in the buffer. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003059 max = b_rep_blk(&chn->buf, ci_head(chn), ci_head(chn), str + l, max);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003060
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003061 /* buffer replace considers that the input part is filled.
3062 * so, I must forward these new data in the output part.
3063 */
Willy Tarreaubcbd3932018-06-06 07:13:22 +02003064 c_adv(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003065
3066 l += max;
3067 lua_pop(L, 1);
3068 lua_pushinteger(L, l);
3069
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003070 /* If there is no space available, and the output buffer is empty.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003071 * in this case, we cannot add more data, so we cannot yield,
3072 * we return the amount of copyied data.
3073 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003074 max = b_room(&chn->buf);
Willy Tarreaua79021a2018-06-15 18:07:57 +02003075 if (max == 0 && co_data(chn) == 0)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003076 return 1;
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003077
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003078 if (l < len) {
3079 /* If we are waiting for space in the response buffer, we
3080 * must set the flag WAKERESWR. This flag required the task
3081 * wake up if any activity is detected on the response buffer.
3082 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003083 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003084 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01003085 else
3086 HLUA_SET_WAKEREQWR(hlua);
Willy Tarreau9635e032018-10-16 17:52:55 +02003087 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003088 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003089
3090 return 1;
3091}
3092
3093/* Just a wraper of "_hlua_channel_send". This wrapper permits
3094 * yield the LUA process, and resume it without checking the
3095 * input arguments.
3096 */
3097__LJMP static int hlua_channel_send(lua_State *L)
3098{
3099 MAY_LJMP(check_args(L, 2, "send"));
3100 lua_pushinteger(L, 0);
3101
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003102 return MAY_LJMP(hlua_channel_send_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003103}
3104
3105/* This function forward and amount of butes. The data pass from
3106 * the input side of the buffer to the output side, and can be
3107 * forwarded. This function never fails.
3108 *
3109 * The Lua function takes an amount of bytes to be forwarded in
3110 * imput. It returns the number of bytes forwarded.
3111 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003112__LJMP static int hlua_channel_forward_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003113{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003114 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003115 int len;
3116 int l;
3117 int max;
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003118 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003119
3120 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Faulet3f829a42018-12-13 21:56:45 +01003121
3122 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
3123 WILL_LJMP(lua_error(L));
3124
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003125 len = MAY_LJMP(luaL_checkinteger(L, 2));
3126 l = MAY_LJMP(luaL_checkinteger(L, -1));
3127
3128 max = len - l;
Willy Tarreaua79021a2018-06-15 18:07:57 +02003129 if (max > ci_data(chn))
3130 max = ci_data(chn);
Willy Tarreau47860ed2015-03-10 14:07:50 +01003131 channel_forward(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003132 l += max;
3133
3134 lua_pop(L, 1);
3135 lua_pushinteger(L, l);
3136
3137 /* Check if it miss bytes to forward. */
3138 if (l < len) {
3139 /* The the input channel or the output channel are closed, we
3140 * must return the amount of data forwarded.
3141 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003142 if (channel_input_closed(chn) || channel_output_closed(chn))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003143 return 1;
3144
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003145 /* If we are waiting for space data in the response buffer, we
3146 * must set the flag WAKERESWR. This flag required the task
3147 * wake up if any activity is detected on the response buffer.
3148 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003149 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003150 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01003151 else
3152 HLUA_SET_WAKEREQWR(hlua);
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003153
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003154 /* Otherwise, we can yield waiting for new data in the inpout side. */
Willy Tarreau9635e032018-10-16 17:52:55 +02003155 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_forward_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003156 }
3157
3158 return 1;
3159}
3160
3161/* Just check the input and prepare the stack for the previous
3162 * function "hlua_channel_forward_yield"
3163 */
3164__LJMP static int hlua_channel_forward(lua_State *L)
3165{
3166 MAY_LJMP(check_args(L, 2, "forward"));
3167 MAY_LJMP(hlua_checkchannel(L, 1));
3168 MAY_LJMP(luaL_checkinteger(L, 2));
3169
3170 lua_pushinteger(L, 0);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003171 return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003172}
3173
3174/* Just returns the number of bytes available in the input
3175 * side of the buffer. This function never fails.
3176 */
3177__LJMP static int hlua_channel_get_in_len(lua_State *L)
3178{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003179 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003180
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003181 MAY_LJMP(check_args(L, 1, "get_in_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003182 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Fauleta3ceac12018-12-14 13:39:09 +01003183 if (IS_HTX_STRM(chn_strm(chn))) {
3184 struct htx *htx = htxbuf(&chn->buf);
3185 lua_pushinteger(L, htx->data - co_data(chn));
3186 }
3187 else
3188 lua_pushinteger(L, ci_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003189 return 1;
3190}
3191
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003192/* Returns true if the channel is full. */
3193__LJMP static int hlua_channel_is_full(lua_State *L)
3194{
3195 struct channel *chn;
3196 int rem;
3197
3198 MAY_LJMP(check_args(L, 1, "is_full"));
3199 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3200
Christopher Fauleta3ceac12018-12-14 13:39:09 +01003201 if (IS_HTX_STRM(chn_strm(chn))) {
3202 struct htx *htx = htxbuf(&chn->buf);
3203
3204 rem = htx_free_data_space(htx);
3205 }
3206 else
3207 rem = b_room(&chn->buf);
3208
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003209 rem -= global.tune.maxrewrite; /* Rewrite reserved size */
3210
3211 lua_pushboolean(L, rem <= 0);
3212 return 1;
3213}
3214
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003215/* Just returns the number of bytes available in the output
3216 * side of the buffer. This function never fails.
3217 */
3218__LJMP static int hlua_channel_get_out_len(lua_State *L)
3219{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003220 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003221
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003222 MAY_LJMP(check_args(L, 1, "get_out_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003223 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Willy Tarreaua79021a2018-06-15 18:07:57 +02003224 lua_pushinteger(L, co_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003225 return 1;
3226}
3227
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003228/*
3229 *
3230 *
3231 * Class Fetches
3232 *
3233 *
3234 */
3235
3236/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003237 * a class stream, otherwise it throws an error.
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003238 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003239__LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003240{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003241 return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003242}
3243
3244/* This function creates and push in the stack a fetch object according
3245 * with a current TXN.
3246 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003247static int hlua_fetches_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003248{
Willy Tarreau7073c472015-04-06 11:15:40 +02003249 struct hlua_smp *hsmp;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003250
3251 /* Check stack size. */
3252 if (!lua_checkstack(L, 3))
3253 return 0;
3254
3255 /* Create the object: obj[0] = userdata.
3256 * Note that the base of the Fetches object is the
3257 * transaction object.
3258 */
3259 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003260 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003261 lua_rawseti(L, -2, 0);
3262
Willy Tarreau7073c472015-04-06 11:15:40 +02003263 hsmp->s = txn->s;
3264 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003265 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003266 hsmp->flags = flags;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003267
3268 /* Pop a class sesison metatable and affect it to the userdata. */
3269 lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref);
3270 lua_setmetatable(L, -2);
3271
3272 return 1;
3273}
3274
3275/* This function is an LUA binding. It is called with each sample-fetch.
3276 * It uses closure argument to store the associated sample-fetch. It
3277 * returns only one argument or throws an error. An error is thrown
3278 * only if an error is encountered during the argument parsing. If
3279 * the "sample-fetch" function fails, nil is returned.
3280 */
3281__LJMP static int hlua_run_sample_fetch(lua_State *L)
3282{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003283 struct hlua_smp *hsmp;
Willy Tarreau2ec22742015-03-10 14:27:20 +01003284 struct sample_fetch *f;
Frédéric Lécaillef874a832018-06-15 13:56:04 +02003285 struct arg args[ARGM_NBARGS + 1] = {{0}};
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003286 int i;
3287 struct sample smp;
3288
3289 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003290 f = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003291
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003292 /* Get traditional arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003293 hsmp = MAY_LJMP(hlua_checkfetches(L, 1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003294
Thierry FOURNIERca988662015-12-20 18:43:03 +01003295 /* Check execution authorization. */
3296 if (f->use & SMP_USE_HTTP_ANY &&
3297 !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) {
3298 lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which "
3299 "is not available in Lua services", f->kw);
3300 WILL_LJMP(lua_error(L));
3301 }
3302
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003303 /* Get extra arguments. */
3304 for (i = 0; i < lua_gettop(L) - 1; i++) {
3305 if (i >= ARGM_NBARGS)
3306 break;
3307 hlua_lua2arg(L, i + 2, &args[i]);
3308 }
3309 args[i].type = ARGT_STOP;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003310 args[i].data.str.area = NULL;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003311
3312 /* Check arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003313 MAY_LJMP(hlua_lua2arg_check(L, 2, args, f->arg_mask, hsmp->p));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003314
3315 /* Run the special args checker. */
Willy Tarreau2ec22742015-03-10 14:27:20 +01003316 if (f->val_args && !f->val_args(args, NULL)) {
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003317 lua_pushfstring(L, "error in arguments");
3318 WILL_LJMP(lua_error(L));
3319 }
3320
3321 /* Initialise the sample. */
3322 memset(&smp, 0, sizeof(smp));
3323
3324 /* Run the sample fetch process. */
Willy Tarreau1777ea62016-03-10 16:15:46 +01003325 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003326 if (!f->process(args, &smp, f->kw, f->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003327 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003328 lua_pushstring(L, "");
3329 else
3330 lua_pushnil(L);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003331 return 1;
3332 }
3333
3334 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003335 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003336 hlua_smp2lua_str(L, &smp);
3337 else
3338 hlua_smp2lua(L, &smp);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003339 return 1;
3340}
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003341
3342/*
3343 *
3344 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003345 * Class Converters
3346 *
3347 *
3348 */
3349
3350/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003351 * a class stream, otherwise it throws an error.
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003352 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003353__LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003354{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003355 return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003356}
3357
3358/* This function creates and push in the stack a Converters object
3359 * according with a current TXN.
3360 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003361static int hlua_converters_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003362{
Willy Tarreau7073c472015-04-06 11:15:40 +02003363 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003364
3365 /* Check stack size. */
3366 if (!lua_checkstack(L, 3))
3367 return 0;
3368
3369 /* Create the object: obj[0] = userdata.
3370 * Note that the base of the Converters object is the
3371 * same than the TXN object.
3372 */
3373 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003374 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003375 lua_rawseti(L, -2, 0);
3376
Willy Tarreau7073c472015-04-06 11:15:40 +02003377 hsmp->s = txn->s;
3378 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003379 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003380 hsmp->flags = flags;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003381
Willy Tarreau87b09662015-04-03 00:22:06 +02003382 /* Pop a class stream metatable and affect it to the table. */
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003383 lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref);
3384 lua_setmetatable(L, -2);
3385
3386 return 1;
3387}
3388
3389/* This function is an LUA binding. It is called with each converter.
3390 * It uses closure argument to store the associated converter. It
3391 * returns only one argument or throws an error. An error is thrown
3392 * only if an error is encountered during the argument parsing. If
3393 * the converter function function fails, nil is returned.
3394 */
3395__LJMP static int hlua_run_sample_conv(lua_State *L)
3396{
Willy Tarreauda5f1082015-04-06 11:17:13 +02003397 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003398 struct sample_conv *conv;
Frédéric Lécaillef874a832018-06-15 13:56:04 +02003399 struct arg args[ARGM_NBARGS + 1] = {{0}};
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003400 int i;
3401 struct sample smp;
3402
3403 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003404 conv = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003405
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003406 /* Get traditional arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003407 hsmp = MAY_LJMP(hlua_checkconverters(L, 1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003408
3409 /* Get extra arguments. */
3410 for (i = 0; i < lua_gettop(L) - 2; i++) {
3411 if (i >= ARGM_NBARGS)
3412 break;
3413 hlua_lua2arg(L, i + 3, &args[i]);
3414 }
3415 args[i].type = ARGT_STOP;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003416 args[i].data.str.area = NULL;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003417
3418 /* Check arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003419 MAY_LJMP(hlua_lua2arg_check(L, 3, args, conv->arg_mask, hsmp->p));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003420
3421 /* Run the special args checker. */
3422 if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) {
3423 hlua_pusherror(L, "error in arguments");
3424 WILL_LJMP(lua_error(L));
3425 }
3426
3427 /* Initialise the sample. */
3428 if (!hlua_lua2smp(L, 2, &smp)) {
3429 hlua_pusherror(L, "error in the input argument");
3430 WILL_LJMP(lua_error(L));
3431 }
3432
Willy Tarreau1777ea62016-03-10 16:15:46 +01003433 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
3434
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003435 /* Apply expected cast. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003436 if (!sample_casts[smp.data.type][conv->in_type]) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003437 hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'",
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003438 smp_to_type[smp.data.type], smp_to_type[conv->in_type]);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003439 WILL_LJMP(lua_error(L));
3440 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003441 if (sample_casts[smp.data.type][conv->in_type] != c_none &&
3442 !sample_casts[smp.data.type][conv->in_type](&smp)) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003443 hlua_pusherror(L, "error during the input argument casting");
3444 WILL_LJMP(lua_error(L));
3445 }
3446
3447 /* Run the sample conversion process. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003448 if (!conv->process(args, &smp, conv->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003449 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003450 lua_pushstring(L, "");
3451 else
Willy Tarreaua678b432015-08-28 10:14:59 +02003452 lua_pushnil(L);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003453 return 1;
3454 }
3455
3456 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003457 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003458 hlua_smp2lua_str(L, &smp);
3459 else
3460 hlua_smp2lua(L, &smp);
Willy Tarreaua678b432015-08-28 10:14:59 +02003461 return 1;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003462}
3463
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003464/*
3465 *
3466 *
3467 * Class AppletTCP
3468 *
3469 *
3470 */
3471
3472/* Returns a struct hlua_txn if the stack entry "ud" is
3473 * a class stream, otherwise it throws an error.
3474 */
3475__LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud)
3476{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003477 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003478}
3479
3480/* This function creates and push in the stack an Applet object
3481 * according with a current TXN.
3482 */
3483static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx)
3484{
3485 struct hlua_appctx *appctx;
3486 struct stream_interface *si = ctx->owner;
3487 struct stream *s = si_strm(si);
3488 struct proxy *p = s->be;
3489
3490 /* Check stack size. */
3491 if (!lua_checkstack(L, 3))
3492 return 0;
3493
3494 /* Create the object: obj[0] = userdata.
3495 * Note that the base of the Converters object is the
3496 * same than the TXN object.
3497 */
3498 lua_newtable(L);
3499 appctx = lua_newuserdata(L, sizeof(*appctx));
3500 lua_rawseti(L, -2, 0);
3501 appctx->appctx = ctx;
3502 appctx->htxn.s = s;
3503 appctx->htxn.p = p;
3504
3505 /* Create the "f" field that contains a list of fetches. */
3506 lua_pushstring(L, "f");
3507 if (!hlua_fetches_new(L, &appctx->htxn, 0))
3508 return 0;
3509 lua_settable(L, -3);
3510
3511 /* Create the "sf" field that contains a list of stringsafe fetches. */
3512 lua_pushstring(L, "sf");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003513 if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003514 return 0;
3515 lua_settable(L, -3);
3516
3517 /* Create the "c" field that contains a list of converters. */
3518 lua_pushstring(L, "c");
3519 if (!hlua_converters_new(L, &appctx->htxn, 0))
3520 return 0;
3521 lua_settable(L, -3);
3522
3523 /* Create the "sc" field that contains a list of stringsafe converters. */
3524 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003525 if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003526 return 0;
3527 lua_settable(L, -3);
3528
3529 /* Pop a class stream metatable and affect it to the table. */
3530 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref);
3531 lua_setmetatable(L, -2);
3532
3533 return 1;
3534}
3535
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003536__LJMP static int hlua_applet_tcp_set_var(lua_State *L)
3537{
3538 struct hlua_appctx *appctx;
3539 struct stream *s;
3540 const char *name;
3541 size_t len;
3542 struct sample smp;
3543
3544 MAY_LJMP(check_args(L, 3, "set_var"));
3545
3546 /* It is useles to retrieve the stream, but this function
3547 * runs only in a stream context.
3548 */
3549 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3550 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3551 s = appctx->htxn.s;
3552
3553 /* Converts the third argument in a sample. */
3554 hlua_lua2smp(L, 3, &smp);
3555
3556 /* Store the sample in a variable. */
3557 smp_set_owner(&smp, s->be, s->sess, s, 0);
3558 vars_set_by_name(name, len, &smp);
3559 return 0;
3560}
3561
3562__LJMP static int hlua_applet_tcp_unset_var(lua_State *L)
3563{
3564 struct hlua_appctx *appctx;
3565 struct stream *s;
3566 const char *name;
3567 size_t len;
3568 struct sample smp;
3569
3570 MAY_LJMP(check_args(L, 2, "unset_var"));
3571
3572 /* It is useles to retrieve the stream, but this function
3573 * runs only in a stream context.
3574 */
3575 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3576 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3577 s = appctx->htxn.s;
3578
3579 /* Unset the variable. */
3580 smp_set_owner(&smp, s->be, s->sess, s, 0);
3581 vars_unset_by_name(name, len, &smp);
3582 return 0;
3583}
3584
3585__LJMP static int hlua_applet_tcp_get_var(lua_State *L)
3586{
3587 struct hlua_appctx *appctx;
3588 struct stream *s;
3589 const char *name;
3590 size_t len;
3591 struct sample smp;
3592
3593 MAY_LJMP(check_args(L, 2, "get_var"));
3594
3595 /* It is useles to retrieve the stream, but this function
3596 * runs only in a stream context.
3597 */
3598 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3599 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3600 s = appctx->htxn.s;
3601
3602 smp_set_owner(&smp, s->be, s->sess, s, 0);
3603 if (!vars_get_by_name(name, len, &smp)) {
3604 lua_pushnil(L);
3605 return 1;
3606 }
3607
3608 return hlua_smp2lua(L, &smp);
3609}
3610
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003611__LJMP static int hlua_applet_tcp_set_priv(lua_State *L)
3612{
3613 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3614 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003615 struct hlua *hlua;
3616
3617 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003618 if (!s->hlua)
3619 return 0;
3620 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003621
3622 MAY_LJMP(check_args(L, 2, "set_priv"));
3623
3624 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02003625 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003626
3627 /* Get and store new value. */
3628 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
3629 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
3630
3631 return 0;
3632}
3633
3634__LJMP static int hlua_applet_tcp_get_priv(lua_State *L)
3635{
3636 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3637 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003638 struct hlua *hlua;
3639
3640 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003641 if (!s->hlua) {
3642 lua_pushnil(L);
3643 return 1;
3644 }
3645 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003646
3647 /* Push configuration index in the stack. */
3648 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
3649
3650 return 1;
3651}
3652
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003653/* If expected data not yet available, it returns a yield. This function
3654 * consumes the data in the buffer. It returns a string containing the
3655 * data. This string can be empty.
3656 */
3657__LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx)
3658{
3659 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3660 struct stream_interface *si = appctx->appctx->owner;
3661 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02003662 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003663 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02003664 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003665 size_t len2;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003666
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003667 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003668 ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003669
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003670 /* Data not yet available. return yield. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003671 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003672 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003673 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_getline_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003674 }
3675
3676 /* End of data: commit the total strings and return. */
3677 if (ret < 0) {
3678 luaL_pushresult(&appctx->b);
3679 return 1;
3680 }
3681
3682 /* Ensure that the block 2 length is usable. */
3683 if (ret == 1)
3684 len2 = 0;
3685
3686 /* dont check the max length read and dont check. */
3687 luaL_addlstring(&appctx->b, blk1, len1);
3688 luaL_addlstring(&appctx->b, blk2, len2);
3689
3690 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003691 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003692 luaL_pushresult(&appctx->b);
3693 return 1;
3694}
3695
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003696/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003697__LJMP static int hlua_applet_tcp_getline(lua_State *L)
3698{
3699 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3700
3701 /* Initialise the string catenation. */
3702 luaL_buffinit(L, &appctx->b);
3703
3704 return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0));
3705}
3706
3707/* If expected data not yet available, it returns a yield. This function
3708 * consumes the data in the buffer. It returns a string containing the
3709 * data. This string can be empty.
3710 */
3711__LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx)
3712{
3713 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3714 struct stream_interface *si = appctx->appctx->owner;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003715 size_t len = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003716 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02003717 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003718 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02003719 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003720 size_t len2;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003721
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003722 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003723 ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003724
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003725 /* Data not yet available. return yield. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003726 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003727 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003728 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003729 }
3730
3731 /* End of data: commit the total strings and return. */
3732 if (ret < 0) {
3733 luaL_pushresult(&appctx->b);
3734 return 1;
3735 }
3736
3737 /* Ensure that the block 2 length is usable. */
3738 if (ret == 1)
3739 len2 = 0;
3740
3741 if (len == -1) {
3742
3743 /* If len == -1, catenate all the data avalaile and
3744 * yield because we want to get all the data until
3745 * the end of data stream.
3746 */
3747 luaL_addlstring(&appctx->b, blk1, len1);
3748 luaL_addlstring(&appctx->b, blk2, len2);
Willy Tarreau06d80a92017-10-19 14:32:15 +02003749 co_skip(si_oc(si), len1 + len2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003750 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003751 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003752
3753 } else {
3754
3755 /* Copy the fisrt block caping to the length required. */
3756 if (len1 > len)
3757 len1 = len;
3758 luaL_addlstring(&appctx->b, blk1, len1);
3759 len -= len1;
3760
3761 /* Copy the second block. */
3762 if (len2 > len)
3763 len2 = len;
3764 luaL_addlstring(&appctx->b, blk2, len2);
3765 len -= len2;
3766
3767 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003768 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003769
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003770 /* If there is no other data available, yield waiting for new data. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003771 if (len > 0) {
3772 lua_pushinteger(L, len);
3773 lua_replace(L, 2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003774 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003775 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003776 }
3777
3778 /* return the result. */
3779 luaL_pushresult(&appctx->b);
3780 return 1;
3781 }
3782
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003783 /* we never execute this */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003784 hlua_pusherror(L, "Lua: internal error");
3785 WILL_LJMP(lua_error(L));
3786 return 0;
3787}
3788
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003789/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003790__LJMP static int hlua_applet_tcp_recv(lua_State *L)
3791{
3792 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3793 int len = -1;
3794
3795 if (lua_gettop(L) > 2)
3796 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
3797 if (lua_gettop(L) >= 2) {
3798 len = MAY_LJMP(luaL_checkinteger(L, 2));
3799 lua_pop(L, 1);
3800 }
3801
3802 /* Confirm or set the required length */
3803 lua_pushinteger(L, len);
3804
3805 /* Initialise the string catenation. */
3806 luaL_buffinit(L, &appctx->b);
3807
3808 return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0));
3809}
3810
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003811/* Append data in the output side of the buffer. This data is immediately
3812 * sent. The function returns the amount of data written. If the buffer
3813 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003814 * if the channel is closed.
3815 */
3816__LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx)
3817{
3818 size_t len;
3819 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3820 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
3821 int l = MAY_LJMP(luaL_checkinteger(L, 3));
3822 struct stream_interface *si = appctx->appctx->owner;
3823 struct channel *chn = si_ic(si);
3824 int max;
3825
3826 /* Get the max amount of data which can write as input in the channel. */
3827 max = channel_recv_max(chn);
3828 if (max > (len - l))
3829 max = len - l;
3830
3831 /* Copy data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003832 ci_putblk(chn, str + l, max);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003833
3834 /* update counters. */
3835 l += max;
3836 lua_pop(L, 1);
3837 lua_pushinteger(L, l);
3838
3839 /* If some data is not send, declares the situation to the
3840 * applet, and returns a yield.
3841 */
3842 if (l < len) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003843 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003844 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003845 }
3846
3847 return 1;
3848}
3849
3850/* Just a wraper of "hlua_applet_tcp_send_yield". This wrapper permits
3851 * yield the LUA process, and resume it without checking the
3852 * input arguments.
3853 */
3854__LJMP static int hlua_applet_tcp_send(lua_State *L)
3855{
3856 MAY_LJMP(check_args(L, 2, "send"));
3857 lua_pushinteger(L, 0);
3858
3859 return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0));
3860}
3861
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003862/*
3863 *
3864 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003865 * Class AppletHTTP
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003866 *
3867 *
3868 */
3869
3870/* Returns a struct hlua_txn if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003871 * a class stream, otherwise it throws an error.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003872 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003873__LJMP static struct hlua_appctx *hlua_checkapplet_http(lua_State *L, int ud)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003874{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003875 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003876}
3877
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003878/* This function creates and push in the stack an Applet object
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003879 * according with a current TXN.
3880 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003881static int hlua_applet_http_new(lua_State *L, struct appctx *ctx)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003882{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003883 struct hlua_appctx *appctx;
Thierry FOURNIER841475e2015-12-11 17:10:09 +01003884 struct hlua_txn htxn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003885 struct stream_interface *si = ctx->owner;
3886 struct stream *s = si_strm(si);
3887 struct proxy *px = s->be;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003888
3889 /* Check stack size. */
3890 if (!lua_checkstack(L, 3))
3891 return 0;
3892
3893 /* Create the object: obj[0] = userdata.
3894 * Note that the base of the Converters object is the
3895 * same than the TXN object.
3896 */
3897 lua_newtable(L);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003898 appctx = lua_newuserdata(L, sizeof(*appctx));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003899 lua_rawseti(L, -2, 0);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003900 appctx->appctx = ctx;
3901 appctx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */
Robin H. Johnson52f5db22017-01-01 13:10:52 -08003902 appctx->appctx->ctx.hlua_apphttp.reason = NULL; /* Use default reason based on status */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003903 appctx->htxn.s = s;
3904 appctx->htxn.p = px;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003905
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003906 /* Create the "f" field that contains a list of fetches. */
3907 lua_pushstring(L, "f");
3908 if (!hlua_fetches_new(L, &appctx->htxn, 0))
3909 return 0;
3910 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003911
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003912 /* Create the "sf" field that contains a list of stringsafe fetches. */
3913 lua_pushstring(L, "sf");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003914 if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003915 return 0;
3916 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003917
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003918 /* Create the "c" field that contains a list of converters. */
3919 lua_pushstring(L, "c");
3920 if (!hlua_converters_new(L, &appctx->htxn, 0))
3921 return 0;
3922 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003923
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003924 /* Create the "sc" field that contains a list of stringsafe converters. */
3925 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003926 if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003927 return 0;
3928 lua_settable(L, -3);
Willy Tarreaueee5b512015-04-03 23:46:31 +02003929
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003930 if (IS_HTX_STRM(s)) {
3931 /* HTX version */
3932 struct htx *htx = htxbuf(&s->req.buf);
3933 struct htx_sl *sl = http_find_stline(htx);
3934 struct ist path;
3935 unsigned long long len = 0;
3936 int32_t pos;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003937
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003938 /* Stores the request method. */
3939 lua_pushstring(L, "method");
3940 lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl));
3941 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003942
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003943 /* Stores the http version. */
3944 lua_pushstring(L, "version");
3945 lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl));
3946 lua_settable(L, -3);
Thierry FOURNIER841475e2015-12-11 17:10:09 +01003947
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003948 /* creates an array of headers. hlua_http_get_headers() crates and push
3949 * the array on the top of the stack.
3950 */
3951 lua_pushstring(L, "headers");
3952 htxn.s = s;
3953 htxn.p = px;
3954 htxn.dir = SMP_OPT_DIR_REQ;
3955 if (!hlua_http_get_headers(L, &htxn, &htxn.s->txn->req))
3956 return 0;
3957 lua_settable(L, -3);
3958
3959 path = http_get_path(htx_sl_req_uri(sl));
3960 if (path.ptr) {
3961 char *p, *q, *end;
3962
3963 p = path.ptr;
3964 end = path.ptr + path.len;
3965 q = p;
3966 while (q < end && *q != '?')
3967 q++;
3968
3969 /* Stores the request path. */
3970 lua_pushstring(L, "path");
3971 lua_pushlstring(L, p, q - p);
3972 lua_settable(L, -3);
3973
3974 /* Stores the query string. */
3975 lua_pushstring(L, "qs");
3976 if (*q == '?')
3977 q++;
3978 lua_pushlstring(L, q, end - q);
3979 lua_settable(L, -3);
3980 }
3981
3982 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
3983 struct htx_blk *blk = htx_get_blk(htx, pos);
3984 enum htx_blk_type type = htx_get_blk_type(blk);
3985
3986 if (type == HTX_BLK_EOM || type == HTX_BLK_EOD)
3987 break;
3988 if (type == HTX_BLK_DATA)
3989 len += htx_get_blksz(blk);
3990 }
3991 if (htx->extra != ULLONG_MAX)
3992 len += htx->extra;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003993
Thierry FOURNIER7d388632017-02-22 02:06:16 +01003994 /* Stores the request path. */
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003995 lua_pushstring(L, "length");
3996 lua_pushinteger(L, len);
3997 lua_settable(L, -3);
3998 }
3999 else {
4000 /* Legacy HTTP version */
4001 struct http_txn *txn = s->txn;
4002 const char *path;
4003 const char *end;
4004 const char *p;
4005
4006 /* Stores the request method. */
4007 lua_pushstring(L, "method");
4008 lua_pushlstring(L, ci_head(txn->req.chn), txn->req.sl.rq.m_l);
Thierry FOURNIER7d388632017-02-22 02:06:16 +01004009 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004010
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004011 /* Stores the http version. */
4012 lua_pushstring(L, "version");
4013 lua_pushlstring(L, ci_head(txn->req.chn) + txn->req.sl.rq.v, txn->req.sl.rq.v_l);
Thierry FOURNIER7d388632017-02-22 02:06:16 +01004014 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01004015
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004016 /* creates an array of headers. hlua_http_get_headers() crates and push
4017 * the array on the top of the stack.
4018 */
4019 lua_pushstring(L, "headers");
4020 htxn.s = s;
4021 htxn.p = px;
4022 htxn.dir = SMP_OPT_DIR_REQ;
4023 if (!hlua_http_get_headers(L, &htxn, &htxn.s->txn->req))
4024 return 0;
4025 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01004026
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004027 /* Get path and qs */
4028 path = http_txn_get_path(txn);
4029 if (path) {
4030 end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
4031 p = path;
4032 while (p < end && *p != '?')
4033 p++;
4034
4035 /* Stores the request path. */
4036 lua_pushstring(L, "path");
4037 lua_pushlstring(L, path, p - path);
4038 lua_settable(L, -3);
4039
4040 /* Stores the query string. */
4041 lua_pushstring(L, "qs");
4042 if (*p == '?')
4043 p++;
4044 lua_pushlstring(L, p, end - p);
4045 lua_settable(L, -3);
4046 }
4047
4048 /* Stores the request path. */
4049 lua_pushstring(L, "length");
4050 lua_pushinteger(L, txn->req.body_len);
4051 lua_settable(L, -3);
4052 }
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01004053
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004054 /* Create an empty array of HTTP request headers. */
4055 lua_pushstring(L, "response");
4056 lua_newtable(L);
4057 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01004058
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004059 /* Pop a class stream metatable and affect it to the table. */
4060 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref);
4061 lua_setmetatable(L, -2);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004062
4063 return 1;
4064}
4065
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004066__LJMP static int hlua_applet_http_set_var(lua_State *L)
4067{
4068 struct hlua_appctx *appctx;
4069 struct stream *s;
4070 const char *name;
4071 size_t len;
4072 struct sample smp;
4073
4074 MAY_LJMP(check_args(L, 3, "set_var"));
4075
4076 /* It is useles to retrieve the stream, but this function
4077 * runs only in a stream context.
4078 */
4079 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4080 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4081 s = appctx->htxn.s;
4082
4083 /* Converts the third argument in a sample. */
4084 hlua_lua2smp(L, 3, &smp);
4085
4086 /* Store the sample in a variable. */
4087 smp_set_owner(&smp, s->be, s->sess, s, 0);
4088 vars_set_by_name(name, len, &smp);
4089 return 0;
4090}
4091
4092__LJMP static int hlua_applet_http_unset_var(lua_State *L)
4093{
4094 struct hlua_appctx *appctx;
4095 struct stream *s;
4096 const char *name;
4097 size_t len;
4098 struct sample smp;
4099
4100 MAY_LJMP(check_args(L, 2, "unset_var"));
4101
4102 /* It is useles to retrieve the stream, but this function
4103 * runs only in a stream context.
4104 */
4105 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4106 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4107 s = appctx->htxn.s;
4108
4109 /* Unset the variable. */
4110 smp_set_owner(&smp, s->be, s->sess, s, 0);
4111 vars_unset_by_name(name, len, &smp);
4112 return 0;
4113}
4114
4115__LJMP static int hlua_applet_http_get_var(lua_State *L)
4116{
4117 struct hlua_appctx *appctx;
4118 struct stream *s;
4119 const char *name;
4120 size_t len;
4121 struct sample smp;
4122
4123 MAY_LJMP(check_args(L, 2, "get_var"));
4124
4125 /* It is useles to retrieve the stream, but this function
4126 * runs only in a stream context.
4127 */
4128 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4129 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4130 s = appctx->htxn.s;
4131
4132 smp_set_owner(&smp, s->be, s->sess, s, 0);
4133 if (!vars_get_by_name(name, len, &smp)) {
4134 lua_pushnil(L);
4135 return 1;
4136 }
4137
4138 return hlua_smp2lua(L, &smp);
4139}
4140
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004141__LJMP static int hlua_applet_http_set_priv(lua_State *L)
4142{
4143 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4144 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004145 struct hlua *hlua;
4146
4147 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004148 if (!s->hlua)
4149 return 0;
4150 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004151
4152 MAY_LJMP(check_args(L, 2, "set_priv"));
4153
4154 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02004155 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004156
4157 /* Get and store new value. */
4158 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
4159 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
4160
4161 return 0;
4162}
4163
4164__LJMP static int hlua_applet_http_get_priv(lua_State *L)
4165{
4166 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4167 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004168 struct hlua *hlua;
4169
4170 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004171 if (!s->hlua) {
4172 lua_pushnil(L);
4173 return 1;
4174 }
4175 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004176
4177 /* Push configuration index in the stack. */
4178 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
4179
4180 return 1;
4181}
4182
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004183__LJMP static void hlua_applet_htx_reply_100_continue(lua_State *L)
4184{
4185 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4186 struct stream_interface *si = appctx->appctx->owner;
4187 struct channel *res = si_ic(si);
4188 struct htx *htx = htx_from_buf(&res->buf);
4189 struct htx_sl *sl;
4190 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|
4191 HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
4192 size_t data;
4193
4194 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4195 ist("HTTP/1.1"), ist("100"), ist("Continue"));
4196 if (!sl)
4197 goto fail;
4198 sl->info.res.status = 100;
4199 if (!htx_add_endof(htx, HTX_BLK_EOH) || !htx_add_endof(htx, HTX_BLK_EOM))
4200 goto fail;
4201
4202 data = htx->data - co_data(res);
4203 res->total += data;
4204 res->flags |= CF_READ_PARTIAL;
4205 appctx->appctx->ctx.hlua_apphttp.flags &= ~APPLET_100C;
4206 return;
4207
4208 fail:
4209 hlua_pusherror(L, "Lua applet http '%s': Failed to create 100-Continue response.\n",
4210 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4211 WILL_LJMP(lua_error(L));
4212}
4213
4214
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004215/* If expected data not yet available, it returns a yield. This function
4216 * consumes the data in the buffer. It returns a string containing the
4217 * data. This string can be empty.
4218 */
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004219__LJMP static int hlua_applet_htx_getline_yield(lua_State *L, int status, lua_KContext ctx)
4220{
4221 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4222 struct stream_interface *si = appctx->appctx->owner;
4223 struct channel *req = si_oc(si);
4224 struct htx *htx;
4225 struct htx_blk *blk;
4226 size_t count;
4227 int stop = 0;
4228
4229 /* Maybe we cant send a 100-continue ? */
4230 if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_100C)
4231 MAY_LJMP(hlua_applet_htx_reply_100_continue(L));
4232
4233 htx = htx_from_buf(&req->buf);
4234 count = co_data(req);
4235 blk = htx_get_head_blk(htx);
4236 while (count && !stop && blk) {
4237 enum htx_blk_type type = htx_get_blk_type(blk);
4238 uint32_t sz = htx_get_blksz(blk);
4239 struct ist v;
4240 uint32_t vlen;
4241 char *nl;
4242
4243 vlen = sz;
4244 if (vlen > count) {
4245 if (type != HTX_BLK_DATA)
4246 break;
4247 vlen = count;
4248 }
4249
4250 switch (type) {
4251 case HTX_BLK_UNUSED:
4252 break;
4253
4254 case HTX_BLK_DATA:
4255 v = htx_get_blk_value(htx, blk);
4256 v.len = vlen;
4257 nl = istchr(v, '\n');
4258 if (nl != NULL) {
4259 stop = 1;
4260 vlen = nl - v.ptr + 1;
4261 }
4262 luaL_addlstring(&appctx->b, v.ptr, vlen);
4263 break;
4264
4265 case HTX_BLK_EOD:
4266 case HTX_BLK_TLR:
4267 case HTX_BLK_EOM:
4268 stop = 1;
4269 break;
4270
4271 default:
4272 break;
4273 }
4274
4275 co_set_data(req, co_data(req) - vlen);
4276 count -= vlen;
4277 if (sz == vlen)
4278 blk = htx_remove_blk(htx, blk);
4279 else {
4280 htx_cut_data_blk(htx, blk, vlen);
4281 break;
4282 }
4283 }
4284
4285 if (!stop) {
4286 si_cant_get(si);
4287 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_htx_getline_yield, TICK_ETERNITY, 0));
4288 }
4289
4290 /* return the result. */
4291 luaL_pushresult(&appctx->b);
4292 return 1;
4293}
4294
4295
4296/* If expected data not yet available, it returns a yield. This function
4297 * consumes the data in the buffer. It returns a string containing the
4298 * data. This string can be empty.
4299 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004300__LJMP static int hlua_applet_http_getline_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004301{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004302 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4303 struct stream_interface *si = appctx->appctx->owner;
4304 struct channel *chn = si_ic(si);
4305 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02004306 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02004307 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02004308 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02004309 size_t len2;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004310
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004311 /* Maybe we cant send a 100-continue ? */
4312 if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_100C) {
Willy Tarreau04f1e2d2018-09-10 18:04:24 +02004313 ret = ci_putblk(chn, HTTP_100.ptr, HTTP_100.len);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004314 /* if ret == -2 or -3 the channel closed or the message si too
4315 * big for the buffers. We cant send anything. So, we ignoring
4316 * the error, considers that the 100-continue is sent, and try
4317 * to receive.
4318 * If ret is -1, we dont have room in the buffer, so we yield.
4319 */
4320 if (ret == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01004321 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004322 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_getline_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004323 }
4324 appctx->appctx->ctx.hlua_apphttp.flags &= ~APPLET_100C;
4325 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004326
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004327 /* Check for the end of the data. */
4328 if (appctx->appctx->ctx.hlua_apphttp.left_bytes <= 0) {
4329 luaL_pushresult(&appctx->b);
4330 return 1;
4331 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004332
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004333 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004334 ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004335
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004336 /* Data not yet available. return yield. */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004337 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01004338 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004339 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_getline_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004340 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004341
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004342 /* End of data: commit the total strings and return. */
4343 if (ret < 0) {
4344 luaL_pushresult(&appctx->b);
4345 return 1;
4346 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004347
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004348 /* Ensure that the block 2 length is usable. */
4349 if (ret == 1)
4350 len2 = 0;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004351
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004352 /* Copy the fisrt block caping to the length required. */
4353 if (len1 > appctx->appctx->ctx.hlua_apphttp.left_bytes)
4354 len1 = appctx->appctx->ctx.hlua_apphttp.left_bytes;
4355 luaL_addlstring(&appctx->b, blk1, len1);
4356 appctx->appctx->ctx.hlua_apphttp.left_bytes -= len1;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004357
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004358 /* Copy the second block. */
4359 if (len2 > appctx->appctx->ctx.hlua_apphttp.left_bytes)
4360 len2 = appctx->appctx->ctx.hlua_apphttp.left_bytes;
4361 luaL_addlstring(&appctx->b, blk2, len2);
4362 appctx->appctx->ctx.hlua_apphttp.left_bytes -= len2;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004363
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004364 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004365 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004366 luaL_pushresult(&appctx->b);
4367 return 1;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004368}
4369
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004370/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004371__LJMP static int hlua_applet_http_getline(lua_State *L)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004372{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004373 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004374
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004375 /* Initialise the string catenation. */
4376 luaL_buffinit(L, &appctx->b);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004377
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004378 if (IS_HTX_STRM(si_strm(appctx->appctx->owner)))
4379 return MAY_LJMP(hlua_applet_htx_getline_yield(L, 0, 0));
4380 else
4381 return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004382}
4383
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004384/* If expected data not yet available, it returns a yield. This function
4385 * consumes the data in the buffer. It returns a string containing the
4386 * data. This string can be empty.
4387 */
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004388__LJMP static int hlua_applet_htx_recv_yield(lua_State *L, int status, lua_KContext ctx)
4389{
4390 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4391 struct stream_interface *si = appctx->appctx->owner;
4392 struct channel *req = si_oc(si);
4393 struct htx *htx;
4394 struct htx_blk *blk;
4395 size_t count;
4396 int len;
4397
4398 /* Maybe we cant send a 100-continue ? */
4399 if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_100C)
4400 MAY_LJMP(hlua_applet_htx_reply_100_continue(L));
4401
4402 htx = htx_from_buf(&req->buf);
4403 len = MAY_LJMP(luaL_checkinteger(L, 2));
4404 count = co_data(req);
4405 blk = htx_get_head_blk(htx);
4406 while (count && len && blk) {
4407 enum htx_blk_type type = htx_get_blk_type(blk);
4408 uint32_t sz = htx_get_blksz(blk);
4409 struct ist v;
4410 uint32_t vlen;
4411
4412 vlen = sz;
4413 if (len > 0 && vlen > len)
4414 vlen = len;
4415 if (vlen > count) {
4416 if (type != HTX_BLK_DATA)
4417 break;
4418 vlen = count;
4419 }
4420
4421 switch (type) {
4422 case HTX_BLK_UNUSED:
4423 break;
4424
4425 case HTX_BLK_DATA:
4426 v = htx_get_blk_value(htx, blk);
4427 luaL_addlstring(&appctx->b, v.ptr, vlen);
4428 break;
4429
4430 case HTX_BLK_EOD:
4431 case HTX_BLK_TLR:
4432 case HTX_BLK_EOM:
4433 len = 0;
4434 break;
4435
4436 default:
4437 break;
4438 }
4439
4440 co_set_data(req, co_data(req) - vlen);
4441 count -= vlen;
4442 if (len > 0)
4443 len -= vlen;
4444 if (sz == vlen)
4445 blk = htx_remove_blk(htx, blk);
4446 else {
4447 htx_cut_data_blk(htx, blk, vlen);
4448 break;
4449 }
4450 }
4451
4452 /* If we are no other data available, yield waiting for new data. */
4453 if (len) {
4454 if (len > 0) {
4455 lua_pushinteger(L, len);
4456 lua_replace(L, 2);
4457 }
4458 si_cant_get(si);
4459 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_htx_recv_yield, TICK_ETERNITY, 0));
4460 }
4461
4462 /* return the result. */
4463 luaL_pushresult(&appctx->b);
4464 return 1;
4465}
4466
4467/* If expected data not yet available, it returns a yield. This function
4468 * consumes the data in the buffer. It returns a string containing the
4469 * data. This string can be empty.
4470 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004471__LJMP static int hlua_applet_http_recv_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004472{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004473 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4474 struct stream_interface *si = appctx->appctx->owner;
4475 int len = MAY_LJMP(luaL_checkinteger(L, 2));
4476 struct channel *chn = si_ic(si);
4477 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02004478 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02004479 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02004480 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02004481 size_t len2;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004482
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004483 /* Maybe we cant send a 100-continue ? */
4484 if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_100C) {
Willy Tarreau04f1e2d2018-09-10 18:04:24 +02004485 ret = ci_putblk(chn, HTTP_100.ptr, HTTP_100.len);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004486 /* if ret == -2 or -3 the channel closed or the message si too
4487 * big for the buffers. We cant send anything. So, we ignoring
4488 * the error, considers that the 100-continue is sent, and try
4489 * to receive.
4490 * If ret is -1, we dont have room in the buffer, so we yield.
4491 */
4492 if (ret == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01004493 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004494 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004495 }
4496 appctx->appctx->ctx.hlua_apphttp.flags &= ~APPLET_100C;
4497 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004498
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004499 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004500 ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004501
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004502 /* Data not yet available. return yield. */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004503 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01004504 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004505 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004506 }
4507
4508 /* End of data: commit the total strings and return. */
4509 if (ret < 0) {
4510 luaL_pushresult(&appctx->b);
4511 return 1;
4512 }
4513
4514 /* Ensure that the block 2 length is usable. */
4515 if (ret == 1)
4516 len2 = 0;
4517
4518 /* Copy the fisrt block caping to the length required. */
4519 if (len1 > len)
4520 len1 = len;
4521 luaL_addlstring(&appctx->b, blk1, len1);
4522 len -= len1;
4523
4524 /* Copy the second block. */
4525 if (len2 > len)
4526 len2 = len;
4527 luaL_addlstring(&appctx->b, blk2, len2);
4528 len -= len2;
4529
4530 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004531 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004532 if (appctx->appctx->ctx.hlua_apphttp.left_bytes != -1)
4533 appctx->appctx->ctx.hlua_apphttp.left_bytes -= len;
4534
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004535 /* If we are no other data available, yield waiting for new data. */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004536 if (len > 0) {
4537 lua_pushinteger(L, len);
4538 lua_replace(L, 2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01004539 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004540 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004541 }
4542
4543 /* return the result. */
4544 luaL_pushresult(&appctx->b);
4545 return 1;
4546}
4547
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004548/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004549__LJMP static int hlua_applet_http_recv(lua_State *L)
4550{
4551 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4552 int len = -1;
4553
4554 /* Check arguments. */
4555 if (lua_gettop(L) > 2)
4556 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
4557 if (lua_gettop(L) >= 2) {
4558 len = MAY_LJMP(luaL_checkinteger(L, 2));
4559 lua_pop(L, 1);
4560 }
4561
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004562 if (IS_HTX_STRM(si_strm(appctx->appctx->owner))) {
4563 /* HTX version */
4564 lua_pushinteger(L, len);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004565
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004566 /* Initialise the string catenation. */
4567 luaL_buffinit(L, &appctx->b);
4568
4569 return MAY_LJMP(hlua_applet_htx_recv_yield(L, 0, 0));
4570 }
4571 else {
4572 /* Legacy HTTP version */
4573 /* Check the required length */
4574 if (len == -1 || len > appctx->appctx->ctx.hlua_apphttp.left_bytes)
4575 len = appctx->appctx->ctx.hlua_apphttp.left_bytes;
4576 lua_pushinteger(L, len);
4577
4578 /* Initialise the string catenation. */
4579 luaL_buffinit(L, &appctx->b);
4580
4581 return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0));
4582 }
4583}
4584
4585/* Append data in the output side of the buffer. This data is immediately
4586 * sent. The function returns the amount of data written. If the buffer
4587 * cannot contain the data, the function yields. The function returns -1
4588 * if the channel is closed.
4589 */
4590__LJMP static int hlua_applet_htx_send_yield(lua_State *L, int status, lua_KContext ctx)
4591{
4592 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4593 struct stream_interface *si = appctx->appctx->owner;
4594 struct channel *res = si_ic(si);
4595 struct htx *htx = htx_from_buf(&res->buf);
4596 const char *data;
4597 size_t len;
4598 int l = MAY_LJMP(luaL_checkinteger(L, 3));
4599 int max;
4600
4601 max = htx_free_space(htx);
4602 if (channel_recv_limit(res) < b_size(&res->buf)) {
4603 if (max < global.tune.maxrewrite) {
4604 si_rx_room_blk(si);
4605 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_htx_send_yield, TICK_ETERNITY, 0));
4606 }
4607 max -= global.tune.maxrewrite;
4608 }
4609
4610 data = MAY_LJMP(luaL_checklstring(L, 2, &len));
4611
4612 /* Get the max amount of data which can write as input in the channel. */
4613 if (max > (len - l))
4614 max = len - l;
4615
4616 /* Copy data. */
4617 htx_add_data(htx, ist2(data + l, max));
4618 res->total += l;
4619 res->flags |= CF_READ_PARTIAL;
4620 htx_to_buf(htx, &res->buf);
4621
4622 /* update counters. */
4623 l += max;
4624 lua_pop(L, 1);
4625 lua_pushinteger(L, l);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004626
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004627 /* If some data is not send, declares the situation to the
4628 * applet, and returns a yield.
4629 */
4630 if (l < len) {
4631 si_rx_room_blk(si);
4632 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_htx_send_yield, TICK_ETERNITY, 0));
4633 }
4634
4635 return 1;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004636}
4637
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004638/* Append data in the output side of the buffer. This data is immediately
4639 * sent. The function returns the amount of data written. If the buffer
4640 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004641 * if the channel is closed.
4642 */
4643__LJMP static int hlua_applet_http_send_yield(lua_State *L, int status, lua_KContext ctx)
4644{
4645 size_t len;
4646 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4647 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
4648 int l = MAY_LJMP(luaL_checkinteger(L, 3));
4649 struct stream_interface *si = appctx->appctx->owner;
4650 struct channel *chn = si_ic(si);
4651 int max;
4652
4653 /* Get the max amount of data which can write as input in the channel. */
4654 max = channel_recv_max(chn);
4655 if (max > (len - l))
4656 max = len - l;
4657
4658 /* Copy data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004659 ci_putblk(chn, str + l, max);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004660
4661 /* update counters. */
4662 l += max;
4663 lua_pop(L, 1);
4664 lua_pushinteger(L, l);
4665
4666 /* If some data is not send, declares the situation to the
4667 * applet, and returns a yield.
4668 */
4669 if (l < len) {
Willy Tarreaudb398432018-11-15 11:08:52 +01004670 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004671 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004672 }
4673
4674 return 1;
4675}
4676
4677/* Just a wraper of "hlua_applet_send_yield". This wrapper permits
4678 * yield the LUA process, and resume it without checking the
4679 * input arguments.
4680 */
4681__LJMP static int hlua_applet_http_send(lua_State *L)
4682{
4683 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004684
4685 /* We want to send some data. Headers must be sent. */
4686 if (!(appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
4687 hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data.");
4688 WILL_LJMP(lua_error(L));
4689 }
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004690
4691 if (IS_HTX_STRM(si_strm(appctx->appctx->owner))) {
4692 /* HTX version */
4693 /* This interger is used for followinf the amount of data sent. */
4694 lua_pushinteger(L, 0);
4695
4696 return MAY_LJMP(hlua_applet_htx_send_yield(L, 0, 0));
4697 }
4698 else {
4699 /* Legacy HTTP version */
4700 size_t len;
4701 char hex[10];
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004702
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004703 MAY_LJMP(luaL_checklstring(L, 2, &len));
4704
4705 /* If transfer encoding chunked is selected, we surround the data
4706 * by chunk data.
4707 */
4708 if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_CHUNKED) {
4709 snprintf(hex, 9, "%x", (unsigned int)len);
4710 lua_pushfstring(L, "%s\r\n", hex);
4711 lua_insert(L, 2); /* swap the last 2 entries. */
4712 lua_pushstring(L, "\r\n");
4713 lua_concat(L, 3);
4714 }
4715
4716 /* This interger is used for followinf the amount of data sent. */
4717 lua_pushinteger(L, 0);
4718
4719 return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0));
4720 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004721}
4722
4723__LJMP static int hlua_applet_http_addheader(lua_State *L)
4724{
4725 const char *name;
4726 int ret;
4727
4728 MAY_LJMP(hlua_checkapplet_http(L, 1));
4729 name = MAY_LJMP(luaL_checkstring(L, 2));
4730 MAY_LJMP(luaL_checkstring(L, 3));
4731
4732 /* Push in the stack the "response" entry. */
4733 ret = lua_getfield(L, 1, "response");
4734 if (ret != LUA_TTABLE) {
4735 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] "
4736 "is expected as an array. %s found", lua_typename(L, ret));
4737 WILL_LJMP(lua_error(L));
4738 }
4739
4740 /* check if the header is already registered if it is not
4741 * the case, register it.
4742 */
4743 ret = lua_getfield(L, -1, name);
4744 if (ret == LUA_TNIL) {
4745
4746 /* Entry not found. */
4747 lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */
4748
4749 /* Insert the new header name in the array in the top of the stack.
4750 * It left the new array in the top of the stack.
4751 */
4752 lua_newtable(L);
4753 lua_pushvalue(L, 2);
4754 lua_pushvalue(L, -2);
4755 lua_settable(L, -4);
4756
4757 } else if (ret != LUA_TTABLE) {
4758
4759 /* corruption error. */
4760 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] "
4761 "is expected as an array. %s found", name, lua_typename(L, ret));
4762 WILL_LJMP(lua_error(L));
4763 }
4764
4765 /* Now the top od thestack is an array of values. We push
4766 * the header value as new entry.
4767 */
4768 lua_pushvalue(L, 3);
4769 ret = lua_rawlen(L, -2);
4770 lua_rawseti(L, -2, ret + 1);
4771 lua_pushboolean(L, 1);
4772 return 1;
4773}
4774
4775__LJMP static int hlua_applet_http_status(lua_State *L)
4776{
4777 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4778 int status = MAY_LJMP(luaL_checkinteger(L, 2));
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004779 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004780
4781 if (status < 100 || status > 599) {
4782 lua_pushboolean(L, 0);
4783 return 1;
4784 }
4785
4786 appctx->appctx->ctx.hlua_apphttp.status = status;
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004787 appctx->appctx->ctx.hlua_apphttp.reason = reason;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004788 lua_pushboolean(L, 1);
4789 return 1;
4790}
4791
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004792
4793__LJMP static int hlua_applet_htx_send_response(lua_State *L)
4794{
4795 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4796 struct stream_interface *si = appctx->appctx->owner;
4797 struct channel *res = si_ic(si);
4798 struct htx *htx;
4799 struct htx_sl *sl;
4800 struct h1m h1m;
4801 const char *status, *reason;
4802 const char *name, *value;
4803 size_t nlen, vlen;
4804 unsigned int flags;
4805
4806 /* Send the message at once. */
4807 htx = htx_from_buf(&res->buf);
4808 h1m_init_res(&h1m);
4809
4810 /* Use the same http version than the request. */
4811 status = ultoa_r(appctx->appctx->ctx.hlua_apphttp.status, trash.area, trash.size);
4812 reason = appctx->appctx->ctx.hlua_apphttp.reason;
4813 if (reason == NULL)
4814 reason = http_get_reason(appctx->appctx->ctx.hlua_apphttp.status);
4815 if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) {
4816 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11);
4817 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason));
4818 }
4819 else {
4820 flags = HTX_SL_F_IS_RESP;
4821 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason));
4822 }
4823 if (!sl) {
4824 hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n",
4825 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4826 WILL_LJMP(lua_error(L));
4827 }
4828 sl->info.res.status = appctx->appctx->ctx.hlua_apphttp.status;
4829
4830 /* Get the array associated to the field "response" in the object AppletHTTP. */
4831 lua_pushvalue(L, 0);
4832 if (lua_getfield(L, 1, "response") != LUA_TTABLE) {
4833 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n",
4834 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4835 WILL_LJMP(lua_error(L));
4836 }
4837
4838 /* Browse the list of headers. */
4839 lua_pushnil(L);
4840 while(lua_next(L, -2) != 0) {
4841 /* We expect a string as -2. */
4842 if (lua_type(L, -2) != LUA_TSTRING) {
4843 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n",
4844 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4845 lua_typename(L, lua_type(L, -2)));
4846 WILL_LJMP(lua_error(L));
4847 }
4848 name = lua_tolstring(L, -2, &nlen);
4849
4850 /* We expect an array as -1. */
4851 if (lua_type(L, -1) != LUA_TTABLE) {
4852 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n",
4853 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4854 name,
4855 lua_typename(L, lua_type(L, -1)));
4856 WILL_LJMP(lua_error(L));
4857 }
4858
4859 /* Browse the table who is on the top of the stack. */
4860 lua_pushnil(L);
4861 while(lua_next(L, -2) != 0) {
4862 int id;
4863
4864 /* We expect a number as -2. */
4865 if (lua_type(L, -2) != LUA_TNUMBER) {
4866 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n",
4867 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4868 name,
4869 lua_typename(L, lua_type(L, -2)));
4870 WILL_LJMP(lua_error(L));
4871 }
4872 id = lua_tointeger(L, -2);
4873
4874 /* We expect a string as -2. */
4875 if (lua_type(L, -1) != LUA_TSTRING) {
4876 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n",
4877 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4878 name, id,
4879 lua_typename(L, lua_type(L, -1)));
4880 WILL_LJMP(lua_error(L));
4881 }
4882 value = lua_tolstring(L, -1, &vlen);
4883
4884 /* Simple Protocol checks. */
4885 if (isteqi(ist2(name, nlen), ist("transfer-encoding")))
4886 h1_parse_xfer_enc_header(&h1m, ist2(name, nlen));
4887 else if (isteqi(ist2(name, nlen), ist("content-length"))) {
4888 struct ist v = ist2(value, vlen);
4889 int ret;
4890
4891 ret = h1_parse_cont_len_header(&h1m, &v);
4892 if (ret < 0) {
4893 hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n",
4894 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4895 name);
4896 WILL_LJMP(lua_error(L));
4897 }
4898 else if (ret == 0)
4899 goto next; /* Skip it */
4900 }
4901
4902 /* Add a new header */
4903 if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) {
4904 hlua_pusherror(L, "Lua applet http '%s': Failed to add header '%s' in the response.\n",
4905 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4906 name);
4907 WILL_LJMP(lua_error(L));
4908 }
4909 next:
4910 /* Remove the array from the stack, and get next element with a remaining string. */
4911 lua_pop(L, 1);
4912 }
4913
4914 /* Remove the array from the stack, and get next element with a remaining string. */
4915 lua_pop(L, 1);
4916 }
4917
4918 if (h1m.flags & H1_MF_CHNK)
4919 h1m.flags &= ~H1_MF_CLEN;
4920 if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK))
4921 h1m.flags |= H1_MF_XFER_LEN;
4922
4923 /* Uset HTX start-line flags */
4924 if (h1m.flags & H1_MF_XFER_ENC)
4925 flags |= HTX_SL_F_XFER_ENC;
4926 if (h1m.flags & H1_MF_XFER_LEN) {
4927 flags |= HTX_SL_F_XFER_LEN;
4928 if (h1m.flags & H1_MF_CHNK)
4929 flags |= HTX_SL_F_CHNK;
4930 else if (h1m.flags & H1_MF_CLEN)
4931 flags |= HTX_SL_F_CLEN;
4932 if (h1m.body_len == 0)
4933 flags |= HTX_SL_F_BODYLESS;
4934 }
4935 sl->flags |= flags;
4936
4937 /* If we dont have a content-length set, and the HTTP version is 1.1
4938 * and the status code implies the presence of a message body, we must
4939 * announce a transfer encoding chunked. This is required by haproxy
4940 * for the keepalive compliance. If the applet annouces a transfer-encoding
4941 * chunked itslef, don't do anything.
4942 */
4943 if ((flags & (HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN)) == HTX_SL_F_VER_11 &&
4944 appctx->appctx->ctx.hlua_apphttp.status >= 200 &&
4945 appctx->appctx->ctx.hlua_apphttp.status != 204 &&
4946 appctx->appctx->ctx.hlua_apphttp.status != 304) {
4947 /* Add a new header */
4948 sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN);
4949 if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) {
4950 hlua_pusherror(L, "Lua applet http '%s': Failed to add header 'transfer-encoding' in the response.\n",
4951 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4952 WILL_LJMP(lua_error(L));
4953 }
4954 }
4955
4956 /* Finalize headers. */
4957 if (!htx_add_endof(htx, HTX_BLK_EOH)) {
4958 hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n",
4959 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4960 WILL_LJMP(lua_error(L));
4961 }
4962
4963 if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) {
4964 b_reset(&res->buf);
4965 hlua_pusherror(L, "Lua: 'start_response': response header block too big");
4966 WILL_LJMP(lua_error(L));
4967 }
4968
4969 htx_to_buf(htx, &res->buf);
4970 res->total += htx->data;
4971 res->flags |= CF_READ_PARTIAL;
4972
4973 /* Headers sent, set the flag. */
4974 appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT;
4975 return 0;
4976
4977}
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004978/* We will build the status line and the headers of the HTTP response.
4979 * We will try send at once if its not possible, we give back the hand
4980 * waiting for more room.
4981 */
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004982__LJMP static int hlua_applet_htx_start_response_yield(lua_State *L, int status, lua_KContext ctx)
4983{
4984 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4985 struct stream_interface *si = appctx->appctx->owner;
4986 struct channel *res = si_ic(si);
4987
4988 if (co_data(res)) {
4989 si_rx_room_blk(si);
4990 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_htx_start_response_yield, TICK_ETERNITY, 0));
4991 }
4992 return MAY_LJMP(hlua_applet_htx_send_response(L));
4993}
4994
4995
4996__LJMP static int hlua_applet_htx_start_response(lua_State *L)
4997{
4998 return MAY_LJMP(hlua_applet_htx_start_response_yield(L, 0, 0));
4999}
5000
5001/* We will build the status line and the headers of the HTTP response.
5002 * We will try send at once if its not possible, we give back the hand
5003 * waiting for more room.
5004 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005005__LJMP static int hlua_applet_http_start_response_yield(lua_State *L, int status, lua_KContext ctx)
5006{
5007 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
5008 struct stream_interface *si = appctx->appctx->owner;
5009 struct channel *chn = si_ic(si);
5010 int ret;
5011 size_t len;
5012 const char *msg;
5013
5014 /* Get the message as the first argument on the stack. */
5015 msg = MAY_LJMP(luaL_checklstring(L, 2, &len));
5016
5017 /* Send the message at once. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02005018 ret = ci_putblk(chn, msg, len);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005019
5020 /* if ret == -2 or -3 the channel closed or the message si too
5021 * big for the buffers.
5022 */
5023 if (ret == -2 || ret == -3) {
5024 hlua_pusherror(L, "Lua: 'start_response': response header block too big");
5025 WILL_LJMP(lua_error(L));
5026 }
5027
5028 /* If ret is -1, we dont have room in the buffer, so we yield. */
5029 if (ret == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01005030 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02005031 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_start_response_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005032 }
5033
5034 /* Headers sent, set the flag. */
5035 appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT;
5036 return 0;
5037}
5038
5039__LJMP static int hlua_applet_http_start_response(lua_State *L)
5040{
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005041 struct buffer *tmp;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005042 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005043 const char *name;
Willy Tarreaua3294632017-08-23 11:24:47 +02005044 size_t name_len;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005045 const char *value;
Willy Tarreaua3294632017-08-23 11:24:47 +02005046 size_t value_len;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005047 int id;
Willy Tarreauc9f4ea02017-08-23 09:32:06 +02005048 long long hdr_contentlength = -1;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005049 int hdr_chunked = 0;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005050 const char *reason;
5051
5052 if (IS_HTX_STRM(si_strm(appctx->appctx->owner)))
5053 return MAY_LJMP(hlua_applet_htx_start_response(L));
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005054
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005055 reason = appctx->appctx->ctx.hlua_apphttp.reason;
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005056 if (reason == NULL)
Willy Tarreau04f1e2d2018-09-10 18:04:24 +02005057 reason = http_get_reason(appctx->appctx->ctx.hlua_apphttp.status);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005058
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005059 tmp = get_trash_chunk();
5060
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005061 /* Use the same http version than the request. */
5062 chunk_appendf(tmp, "HTTP/1.%c %d %s\r\n",
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +01005063 appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11 ? '1' : '0',
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005064 appctx->appctx->ctx.hlua_apphttp.status,
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005065 reason);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005066
5067 /* Get the array associated to the field "response" in the object AppletHTTP. */
5068 lua_pushvalue(L, 0);
5069 if (lua_getfield(L, 1, "response") != LUA_TTABLE) {
5070 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n",
5071 appctx->appctx->rule->arg.hlua_rule->fcn.name);
5072 WILL_LJMP(lua_error(L));
5073 }
5074
5075 /* Browse the list of headers. */
5076 lua_pushnil(L);
5077 while(lua_next(L, -2) != 0) {
5078
5079 /* We expect a string as -2. */
5080 if (lua_type(L, -2) != LUA_TSTRING) {
5081 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n",
5082 appctx->appctx->rule->arg.hlua_rule->fcn.name,
5083 lua_typename(L, lua_type(L, -2)));
5084 WILL_LJMP(lua_error(L));
5085 }
Willy Tarreaua3294632017-08-23 11:24:47 +02005086 name = lua_tolstring(L, -2, &name_len);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005087
5088 /* We expect an array as -1. */
5089 if (lua_type(L, -1) != LUA_TTABLE) {
5090 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n",
5091 appctx->appctx->rule->arg.hlua_rule->fcn.name,
5092 name,
5093 lua_typename(L, lua_type(L, -1)));
5094 WILL_LJMP(lua_error(L));
5095 }
5096
5097 /* Browse the table who is on the top of the stack. */
5098 lua_pushnil(L);
5099 while(lua_next(L, -2) != 0) {
5100
5101 /* We expect a number as -2. */
5102 if (lua_type(L, -2) != LUA_TNUMBER) {
5103 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n",
5104 appctx->appctx->rule->arg.hlua_rule->fcn.name,
5105 name,
5106 lua_typename(L, lua_type(L, -2)));
5107 WILL_LJMP(lua_error(L));
5108 }
5109 id = lua_tointeger(L, -2);
5110
5111 /* We expect a string as -2. */
5112 if (lua_type(L, -1) != LUA_TSTRING) {
5113 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n",
5114 appctx->appctx->rule->arg.hlua_rule->fcn.name,
5115 name, id,
5116 lua_typename(L, lua_type(L, -1)));
5117 WILL_LJMP(lua_error(L));
5118 }
Willy Tarreaua3294632017-08-23 11:24:47 +02005119 value = lua_tolstring(L, -1, &value_len);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005120
5121 /* Catenate a new header. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005122 if (tmp->data + name_len + 2 + value_len + 2 < tmp->size) {
5123 memcpy(tmp->area + tmp->data, name, name_len);
5124 tmp->data += name_len;
5125 tmp->area[tmp->data++] = ':';
5126 tmp->area[tmp->data++] = ' ';
Willy Tarreaua3294632017-08-23 11:24:47 +02005127
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005128 memcpy(tmp->area + tmp->data, value,
5129 value_len);
5130 tmp->data += value_len;
5131 tmp->area[tmp->data++] = '\r';
5132 tmp->area[tmp->data++] = '\n';
Willy Tarreaua3294632017-08-23 11:24:47 +02005133 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005134
5135 /* Protocol checks. */
5136
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005137 /* Copy the header content length. The length conversion
Willy Tarreauc9f4ea02017-08-23 09:32:06 +02005138 * is done without control. If it contains a bad value,
5139 * the content-length remains negative so that we can
5140 * switch to either chunked encoding or close.
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005141 */
Willy Tarreaua3294632017-08-23 11:24:47 +02005142 if (name_len == 14 && strcasecmp("content-length", name) == 0)
Willy Tarreauc9f4ea02017-08-23 09:32:06 +02005143 strl2llrc(value, strlen(value), &hdr_contentlength);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005144
5145 /* Check if the client annouces a transfer-encoding chunked it self. */
Willy Tarreaua3294632017-08-23 11:24:47 +02005146 if (name_len == 17 && value_len == 7 &&
5147 strcasecmp("transfer-encoding", name) == 0 &&
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005148 strcasecmp("chunked", value) == 0)
5149 hdr_chunked = 1;
5150
5151 /* Remove the array from the stack, and get next element with a remaining string. */
5152 lua_pop(L, 1);
5153 }
5154
5155 /* Remove the array from the stack, and get next element with a remaining string. */
5156 lua_pop(L, 1);
5157 }
5158
Willy Tarreau06c75fe2017-08-23 09:10:38 +02005159 /* If we dont have a content-length set, and the HTTP version is 1.1
5160 * and the status code implies the presence of a message body, we must
5161 * announce a transfer encoding chunked. This is required by haproxy
5162 * for the keepalive compliance. If the applet annouces a transfer-encoding
5163 * chunked itslef, don't do anything.
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005164 */
Willy Tarreauc9f4ea02017-08-23 09:32:06 +02005165 if (hdr_contentlength < 0 && hdr_chunked == 0 &&
Willy Tarreau06c75fe2017-08-23 09:10:38 +02005166 (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) &&
5167 appctx->appctx->ctx.hlua_apphttp.status >= 200 &&
5168 appctx->appctx->ctx.hlua_apphttp.status != 204 &&
5169 appctx->appctx->ctx.hlua_apphttp.status != 304) {
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005170 chunk_appendf(tmp, "Transfer-encoding: chunked\r\n");
5171 appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_CHUNKED;
5172 }
5173
5174 /* Finalize headers. */
5175 chunk_appendf(tmp, "\r\n");
5176
5177 /* Remove the last entry and the array of headers */
5178 lua_pop(L, 2);
5179
5180 /* Push the headers block. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005181 lua_pushlstring(L, tmp->area, tmp->data);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005182
5183 return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0));
5184}
5185
5186/*
5187 *
5188 *
5189 * Class HTTP
5190 *
5191 *
5192 */
5193
5194/* Returns a struct hlua_txn if the stack entry "ud" is
5195 * a class stream, otherwise it throws an error.
5196 */
5197__LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud)
5198{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02005199 return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005200}
5201
5202/* This function creates and push in the stack a HTTP object
5203 * according with a current TXN.
5204 */
5205static int hlua_http_new(lua_State *L, struct hlua_txn *txn)
5206{
5207 struct hlua_txn *htxn;
5208
5209 /* Check stack size. */
5210 if (!lua_checkstack(L, 3))
5211 return 0;
5212
5213 /* Create the object: obj[0] = userdata.
5214 * Note that the base of the Converters object is the
5215 * same than the TXN object.
5216 */
5217 lua_newtable(L);
5218 htxn = lua_newuserdata(L, sizeof(*htxn));
5219 lua_rawseti(L, -2, 0);
5220
5221 htxn->s = txn->s;
5222 htxn->p = txn->p;
5223
5224 /* Pop a class stream metatable and affect it to the table. */
5225 lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref);
5226 lua_setmetatable(L, -2);
5227
5228 return 1;
5229}
5230
5231/* This function creates ans returns an array of HTTP headers.
5232 * This function does not fails. It is used as wrapper with the
5233 * 2 following functions.
5234 */
5235__LJMP static int hlua_http_get_headers(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg)
5236{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005237 /* Create the table. */
5238 lua_newtable(L);
5239
5240 if (!htxn->s->txn)
5241 return 1;
5242
Christopher Faulet724a12c2018-12-13 22:12:15 +01005243 if (IS_HTX_STRM(htxn->s)) {
5244 /* HTX version */
5245 struct htx *htx = htxbuf(&msg->chn->buf);
5246 int32_t pos;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005247
Christopher Faulet724a12c2018-12-13 22:12:15 +01005248 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
5249 struct htx_blk *blk = htx_get_blk(htx, pos);
5250 enum htx_blk_type type = htx_get_blk_type(blk);
5251 struct ist n, v;
5252 int len;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005253
Christopher Faulet724a12c2018-12-13 22:12:15 +01005254 if (type == HTX_BLK_HDR) {
5255 n = htx_get_blk_name(htx,blk);
5256 v = htx_get_blk_value(htx, blk);
5257 }
5258 else if (type == HTX_BLK_EOH)
5259 break;
5260 else
5261 continue;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005262
Christopher Faulet724a12c2018-12-13 22:12:15 +01005263 /* Check for existing entry:
5264 * assume that the table is on the top of the stack, and
5265 * push the key in the stack, the function lua_gettable()
5266 * perform the lookup.
5267 */
5268 lua_pushlstring(L, n.ptr, n.len);
5269 lua_gettable(L, -2);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005270
Christopher Faulet724a12c2018-12-13 22:12:15 +01005271 switch (lua_type(L, -1)) {
5272 case LUA_TNIL:
5273 /* Table not found, create it. */
5274 lua_pop(L, 1); /* remove the nil value. */
5275 lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */
5276 lua_newtable(L); /* create and push empty table. */
5277 lua_pushlstring(L, v.ptr, v.len); /* push header value. */
5278 lua_rawseti(L, -2, 0); /* index header value (pop it). */
5279 lua_rawset(L, -3); /* index new table with header name (pop the values). */
5280 break;
5281
5282 case LUA_TTABLE:
5283 /* Entry found: push the value in the table. */
5284 len = lua_rawlen(L, -1);
5285 lua_pushlstring(L, v.ptr, v.len); /* push header value. */
5286 lua_rawseti(L, -2, len+1); /* index header value (pop it). */
5287 lua_pop(L, 1); /* remove the table (it is stored in the main table). */
5288 break;
5289
5290 default:
5291 /* Other cases are errors. */
5292 hlua_pusherror(L, "internal error during the parsing of headers.");
5293 WILL_LJMP(lua_error(L));
5294 }
5295 }
5296 }
5297 else {
5298 /* Legacy HTTP version */
5299 const char *cur_ptr, *cur_next, *p;
5300 int old_idx, cur_idx;
5301 struct hdr_idx_elem *cur_hdr;
5302 const char *hn, *hv;
5303 int hnl, hvl;
5304 const char *in;
5305 char *out;
5306 int len;
5307
5308 /* Build array of headers. */
5309 old_idx = 0;
5310 cur_next = ci_head(msg->chn) + hdr_idx_first_pos(&htxn->s->txn->hdr_idx);
5311
5312 while (1) {
5313 cur_idx = htxn->s->txn->hdr_idx.v[old_idx].next;
5314 if (!cur_idx)
5315 break;
5316 old_idx = cur_idx;
5317
5318 cur_hdr = &htxn->s->txn->hdr_idx.v[cur_idx];
5319 cur_ptr = cur_next;
5320 cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1;
5321
5322 /* Now we have one full header at cur_ptr of len cur_hdr->len,
5323 * and the next header starts at cur_next. We'll check
5324 * this header in the list as well as against the default
5325 * rule.
5326 */
5327
5328 /* look for ': *'. */
5329 hn = cur_ptr;
5330 for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++);
5331 if (p >= cur_ptr+cur_hdr->len)
5332 continue;
5333 hnl = p - hn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005334 p++;
Christopher Faulet724a12c2018-12-13 22:12:15 +01005335 while (p < cur_ptr+cur_hdr->len && ( *p == ' ' || *p == '\t' ))
5336 p++;
5337 if (p >= cur_ptr+cur_hdr->len)
5338 continue;
5339 hv = p;
5340 hvl = cur_ptr+cur_hdr->len-p;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005341
Christopher Faulet724a12c2018-12-13 22:12:15 +01005342 /* Lowercase the key. Don't check the size of trash, it have
5343 * the size of one buffer and the input data contains in one
5344 * buffer.
5345 */
5346 out = trash.area;
5347 for (in=hn; in<hn+hnl; in++, out++)
5348 *out = tolower(*in);
5349 *out = '\0';
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005350
Christopher Faulet724a12c2018-12-13 22:12:15 +01005351 /* Check for existing entry:
5352 * assume that the table is on the top of the stack, and
5353 * push the key in the stack, the function lua_gettable()
5354 * perform the lookup.
5355 */
5356 lua_pushlstring(L, trash.area, hnl);
5357 lua_gettable(L, -2);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005358
Christopher Faulet724a12c2018-12-13 22:12:15 +01005359 switch (lua_type(L, -1)) {
5360 case LUA_TNIL:
5361 /* Table not found, create it. */
5362 lua_pop(L, 1); /* remove the nil value. */
5363 lua_pushlstring(L, trash.area, hnl); /* push the header name as key. */
5364 lua_newtable(L); /* create and push empty table. */
5365 lua_pushlstring(L, hv, hvl); /* push header value. */
5366 lua_rawseti(L, -2, 0); /* index header value (pop it). */
5367 lua_rawset(L, -3); /* index new table with header name (pop the values). */
5368 break;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005369
Christopher Faulet724a12c2018-12-13 22:12:15 +01005370 case LUA_TTABLE:
5371 /* Entry found: push the value in the table. */
5372 len = lua_rawlen(L, -1);
5373 lua_pushlstring(L, hv, hvl); /* push header value. */
5374 lua_rawseti(L, -2, len+1); /* index header value (pop it). */
5375 lua_pop(L, 1); /* remove the table (it is stored in the main table). */
5376 break;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005377
Christopher Faulet724a12c2018-12-13 22:12:15 +01005378 default:
5379 /* Other cases are errors. */
5380 hlua_pusherror(L, "internal error during the parsing of headers.");
5381 WILL_LJMP(lua_error(L));
5382 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005383 }
5384 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005385 return 1;
5386}
5387
5388__LJMP static int hlua_http_req_get_headers(lua_State *L)
5389{
5390 struct hlua_txn *htxn;
5391
5392 MAY_LJMP(check_args(L, 1, "req_get_headers"));
5393 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5394
5395 return hlua_http_get_headers(L, htxn, &htxn->s->txn->req);
5396}
5397
5398__LJMP static int hlua_http_res_get_headers(lua_State *L)
5399{
5400 struct hlua_txn *htxn;
5401
5402 MAY_LJMP(check_args(L, 1, "res_get_headers"));
5403 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5404
5405 return hlua_http_get_headers(L, htxn, &htxn->s->txn->rsp);
5406}
5407
5408/* This function replace full header, or just a value in
5409 * the request or in the response. It is a wrapper fir the
5410 * 4 following functions.
5411 */
5412__LJMP static inline int hlua_http_rep_hdr(lua_State *L, struct hlua_txn *htxn,
5413 struct http_msg *msg, int action)
5414{
5415 size_t name_len;
5416 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
5417 const char *reg = MAY_LJMP(luaL_checkstring(L, 3));
5418 const char *value = MAY_LJMP(luaL_checkstring(L, 4));
5419 struct my_regex re;
5420
5421 if (!regex_comp(reg, &re, 1, 1, NULL))
5422 WILL_LJMP(luaL_argerror(L, 3, "invalid regex"));
5423
5424 http_transform_header_str(htxn->s, msg, name, name_len, value, &re, action);
5425 regex_free(&re);
5426 return 0;
5427}
5428
5429__LJMP static int hlua_http_req_rep_hdr(lua_State *L)
5430{
5431 struct hlua_txn *htxn;
5432
5433 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
5434 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5435
5436 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_HDR));
5437}
5438
5439__LJMP static int hlua_http_res_rep_hdr(lua_State *L)
5440{
5441 struct hlua_txn *htxn;
5442
5443 MAY_LJMP(check_args(L, 4, "res_rep_hdr"));
5444 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5445
5446 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->rsp, ACT_HTTP_REPLACE_HDR));
5447}
5448
5449__LJMP static int hlua_http_req_rep_val(lua_State *L)
5450{
5451 struct hlua_txn *htxn;
5452
5453 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
5454 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5455
5456 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_VAL));
5457}
5458
5459__LJMP static int hlua_http_res_rep_val(lua_State *L)
5460{
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005461 struct hlua_txn *htxn;
5462
5463 MAY_LJMP(check_args(L, 4, "res_rep_val"));
5464 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5465
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02005466 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->rsp, ACT_HTTP_REPLACE_VAL));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005467}
5468
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08005469/* This function deletes all the occurrences of an header.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005470 * It is a wrapper for the 2 following functions.
5471 */
5472__LJMP static inline int hlua_http_del_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg)
5473{
5474 size_t len;
5475 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005476
Christopher Faulet724a12c2018-12-13 22:12:15 +01005477 if (IS_HTX_STRM(htxn->s)) {
5478 /* HTX version */
5479 struct htx *htx = htxbuf(&msg->chn->buf);
5480 struct http_hdr_ctx ctx;
5481
5482 ctx.blk = NULL;
5483 while (http_find_header(htx, ist2(name, len), &ctx, 1))
5484 http_remove_header(htx, &ctx);
5485 }
5486 else {
5487 /* Legacy HTTP version */
5488 struct hdr_ctx ctx;
5489 struct http_txn *txn = htxn->s->txn;
5490
5491 ctx.idx = 0;
5492 while (http_find_header2(name, len, ci_head(msg->chn), &txn->hdr_idx, &ctx))
5493 http_remove_header2(msg, &txn->hdr_idx, &ctx);
5494 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005495 return 0;
5496}
5497
5498__LJMP static int hlua_http_req_del_hdr(lua_State *L)
5499{
5500 struct hlua_txn *htxn;
5501
5502 MAY_LJMP(check_args(L, 2, "req_del_hdr"));
5503 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5504
Willy Tarreaueee5b512015-04-03 23:46:31 +02005505 return hlua_http_del_hdr(L, htxn, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005506}
5507
5508__LJMP static int hlua_http_res_del_hdr(lua_State *L)
5509{
5510 struct hlua_txn *htxn;
5511
5512 MAY_LJMP(check_args(L, 2, "req_del_hdr"));
5513 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5514
Willy Tarreaueee5b512015-04-03 23:46:31 +02005515 return hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005516}
5517
5518/* This function adds an header. It is a wrapper used by
5519 * the 2 following functions.
5520 */
5521__LJMP static inline int hlua_http_add_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg)
5522{
5523 size_t name_len;
5524 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
5525 size_t value_len;
5526 const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len));
5527 char *p;
5528
Christopher Faulet724a12c2018-12-13 22:12:15 +01005529 if (IS_HTX_STRM(htxn->s)) {
5530 /* HTX version */
5531 struct htx *htx = htxbuf(&msg->chn->buf);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005532
Christopher Faulet724a12c2018-12-13 22:12:15 +01005533 lua_pushboolean(L, http_add_header(htx, ist2(name, name_len),
5534 ist2(value, value_len)));
5535 }
5536 else {
5537 /* Legacy HTTP version */
5538 /* Check length. */
5539 trash.data = value_len + name_len + 2;
5540 if (trash.data > trash.size)
5541 return 0;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005542
Christopher Faulet724a12c2018-12-13 22:12:15 +01005543 /* Creates the header string. */
5544 p = trash.area;
5545 memcpy(p, name, name_len);
5546 p += name_len;
5547 *p = ':';
5548 p++;
5549 *p = ' ';
5550 p++;
5551 memcpy(p, value, value_len);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005552
Christopher Faulet724a12c2018-12-13 22:12:15 +01005553 lua_pushboolean(L, http_header_add_tail2(msg, &htxn->s->txn->hdr_idx,
5554 trash.area, trash.data) != 0);
5555 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005556 return 0;
5557}
5558
5559__LJMP static int hlua_http_req_add_hdr(lua_State *L)
5560{
5561 struct hlua_txn *htxn;
5562
5563 MAY_LJMP(check_args(L, 3, "req_add_hdr"));
5564 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5565
Willy Tarreaueee5b512015-04-03 23:46:31 +02005566 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005567}
5568
5569__LJMP static int hlua_http_res_add_hdr(lua_State *L)
5570{
5571 struct hlua_txn *htxn;
5572
5573 MAY_LJMP(check_args(L, 3, "res_add_hdr"));
5574 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5575
Willy Tarreaueee5b512015-04-03 23:46:31 +02005576 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005577}
5578
5579static int hlua_http_req_set_hdr(lua_State *L)
5580{
5581 struct hlua_txn *htxn;
5582
5583 MAY_LJMP(check_args(L, 3, "req_set_hdr"));
5584 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5585
Willy Tarreaueee5b512015-04-03 23:46:31 +02005586 hlua_http_del_hdr(L, htxn, &htxn->s->txn->req);
5587 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005588}
5589
5590static int hlua_http_res_set_hdr(lua_State *L)
5591{
5592 struct hlua_txn *htxn;
5593
5594 MAY_LJMP(check_args(L, 3, "res_set_hdr"));
5595 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5596
Willy Tarreaueee5b512015-04-03 23:46:31 +02005597 hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp);
5598 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005599}
5600
5601/* This function set the method. */
5602static int hlua_http_req_set_meth(lua_State *L)
5603{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005604 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005605 size_t name_len;
5606 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005607
Willy Tarreau987e3fb2015-04-04 01:09:08 +02005608 lua_pushboolean(L, http_replace_req_line(0, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005609 return 1;
5610}
5611
5612/* This function set the method. */
5613static int hlua_http_req_set_path(lua_State *L)
5614{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005615 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005616 size_t name_len;
5617 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02005618
Willy Tarreau987e3fb2015-04-04 01:09:08 +02005619 lua_pushboolean(L, http_replace_req_line(1, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005620 return 1;
5621}
5622
5623/* This function set the query-string. */
5624static int hlua_http_req_set_query(lua_State *L)
5625{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005626 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005627 size_t name_len;
5628 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005629
5630 /* Check length. */
5631 if (name_len > trash.size - 1) {
5632 lua_pushboolean(L, 0);
5633 return 1;
5634 }
5635
5636 /* Add the mark question as prefix. */
5637 chunk_reset(&trash);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005638 trash.area[trash.data++] = '?';
5639 memcpy(trash.area + trash.data, name, name_len);
5640 trash.data += name_len;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005641
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005642 lua_pushboolean(L,
5643 http_replace_req_line(2, trash.area, trash.data, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005644 return 1;
5645}
5646
5647/* This function set the uri. */
5648static int hlua_http_req_set_uri(lua_State *L)
5649{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005650 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005651 size_t name_len;
5652 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005653
Willy Tarreau987e3fb2015-04-04 01:09:08 +02005654 lua_pushboolean(L, http_replace_req_line(3, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005655 return 1;
5656}
5657
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005658/* This function set the response code & optionally reason. */
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005659static int hlua_http_res_set_status(lua_State *L)
5660{
5661 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5662 unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2));
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005663 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005664
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005665 http_set_status(code, reason, htxn->s);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005666 return 0;
5667}
5668
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005669/*
5670 *
5671 *
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005672 * Class TXN
5673 *
5674 *
5675 */
5676
5677/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02005678 * a class stream, otherwise it throws an error.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005679 */
5680__LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud)
5681{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02005682 return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref));
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005683}
5684
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005685__LJMP static int hlua_set_var(lua_State *L)
5686{
5687 struct hlua_txn *htxn;
5688 const char *name;
5689 size_t len;
5690 struct sample smp;
5691
5692 MAY_LJMP(check_args(L, 3, "set_var"));
5693
5694 /* It is useles to retrieve the stream, but this function
5695 * runs only in a stream context.
5696 */
5697 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5698 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5699
5700 /* Converts the third argument in a sample. */
5701 hlua_lua2smp(L, 3, &smp);
5702
5703 /* Store the sample in a variable. */
Willy Tarreau7560dd42016-03-10 16:28:58 +01005704 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Willy Tarreau6204cd92016-03-10 16:33:04 +01005705 vars_set_by_name(name, len, &smp);
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005706 return 0;
5707}
5708
Christopher Faulet85d79c92016-11-09 16:54:56 +01005709__LJMP static int hlua_unset_var(lua_State *L)
5710{
5711 struct hlua_txn *htxn;
5712 const char *name;
5713 size_t len;
5714 struct sample smp;
5715
5716 MAY_LJMP(check_args(L, 2, "unset_var"));
5717
5718 /* It is useles to retrieve the stream, but this function
5719 * runs only in a stream context.
5720 */
5721 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5722 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5723
5724 /* Unset the variable. */
5725 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
5726 vars_unset_by_name(name, len, &smp);
5727 return 0;
5728}
5729
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005730__LJMP static int hlua_get_var(lua_State *L)
5731{
5732 struct hlua_txn *htxn;
5733 const char *name;
5734 size_t len;
5735 struct sample smp;
5736
5737 MAY_LJMP(check_args(L, 2, "get_var"));
5738
5739 /* It is useles to retrieve the stream, but this function
5740 * runs only in a stream context.
5741 */
5742 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5743 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5744
Willy Tarreau7560dd42016-03-10 16:28:58 +01005745 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Willy Tarreau6204cd92016-03-10 16:33:04 +01005746 if (!vars_get_by_name(name, len, &smp)) {
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005747 lua_pushnil(L);
5748 return 1;
5749 }
5750
5751 return hlua_smp2lua(L, &smp);
5752}
5753
Willy Tarreau59551662015-03-10 14:23:13 +01005754__LJMP static int hlua_set_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005755{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005756 struct hlua *hlua;
5757
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005758 MAY_LJMP(check_args(L, 2, "set_priv"));
5759
Willy Tarreau87b09662015-04-03 00:22:06 +02005760 /* It is useles to retrieve the stream, but this function
5761 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005762 */
5763 MAY_LJMP(hlua_checktxn(L, 1));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005764 hlua = hlua_gethlua(L);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005765
5766 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02005767 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005768
5769 /* Get and store new value. */
5770 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
5771 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
5772
5773 return 0;
5774}
5775
Willy Tarreau59551662015-03-10 14:23:13 +01005776__LJMP static int hlua_get_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005777{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005778 struct hlua *hlua;
5779
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005780 MAY_LJMP(check_args(L, 1, "get_priv"));
5781
Willy Tarreau87b09662015-04-03 00:22:06 +02005782 /* It is useles to retrieve the stream, but this function
5783 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005784 */
5785 MAY_LJMP(hlua_checktxn(L, 1));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005786 hlua = hlua_gethlua(L);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005787
5788 /* Push configuration index in the stack. */
5789 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
5790
5791 return 1;
5792}
5793
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005794/* Create stack entry containing a class TXN. This function
5795 * return 0 if the stack does not contains free slots,
5796 * otherwise it returns 1.
5797 */
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005798static int hlua_txn_new(lua_State *L, struct stream *s, struct proxy *p, int dir, int flags)
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005799{
Willy Tarreaude491382015-04-06 11:04:28 +02005800 struct hlua_txn *htxn;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005801
5802 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005803 if (!lua_checkstack(L, 3))
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005804 return 0;
5805
5806 /* NOTE: The allocation never fails. The failure
5807 * throw an error, and the function never returns.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08005808 * if the throw is not available, the process is aborted.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005809 */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005810 /* Create the object: obj[0] = userdata. */
5811 lua_newtable(L);
Willy Tarreaude491382015-04-06 11:04:28 +02005812 htxn = lua_newuserdata(L, sizeof(*htxn));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005813 lua_rawseti(L, -2, 0);
5814
Willy Tarreaude491382015-04-06 11:04:28 +02005815 htxn->s = s;
5816 htxn->p = p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01005817 htxn->dir = dir;
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005818 htxn->flags = flags;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005819
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005820 /* Create the "f" field that contains a list of fetches. */
5821 lua_pushstring(L, "f");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005822 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005823 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005824 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005825
5826 /* Create the "sf" field that contains a list of stringsafe fetches. */
5827 lua_pushstring(L, "sf");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005828 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP | HLUA_F_AS_STRING))
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005829 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005830 lua_rawset(L, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005831
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005832 /* Create the "c" field that contains a list of converters. */
5833 lua_pushstring(L, "c");
Willy Tarreaude491382015-04-06 11:04:28 +02005834 if (!hlua_converters_new(L, htxn, 0))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005835 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005836 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005837
5838 /* Create the "sc" field that contains a list of stringsafe converters. */
5839 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01005840 if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING))
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005841 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005842 lua_rawset(L, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005843
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005844 /* Create the "req" field that contains the request channel object. */
5845 lua_pushstring(L, "req");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005846 if (!hlua_channel_new(L, &s->req))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005847 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005848 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005849
5850 /* Create the "res" field that contains the response channel object. */
5851 lua_pushstring(L, "res");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005852 if (!hlua_channel_new(L, &s->res))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005853 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005854 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005855
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005856 /* Creates the HTTP object is the current proxy allows http. */
5857 lua_pushstring(L, "http");
5858 if (p->mode == PR_MODE_HTTP) {
Willy Tarreaude491382015-04-06 11:04:28 +02005859 if (!hlua_http_new(L, htxn))
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005860 return 0;
5861 }
5862 else
5863 lua_pushnil(L);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005864 lua_rawset(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005865
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005866 /* Pop a class sesison metatable and affect it to the userdata. */
5867 lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref);
5868 lua_setmetatable(L, -2);
5869
5870 return 1;
5871}
5872
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005873__LJMP static int hlua_txn_deflog(lua_State *L)
5874{
5875 const char *msg;
5876 struct hlua_txn *htxn;
5877
5878 MAY_LJMP(check_args(L, 2, "deflog"));
5879 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5880 msg = MAY_LJMP(luaL_checkstring(L, 2));
5881
5882 hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg);
5883 return 0;
5884}
5885
5886__LJMP static int hlua_txn_log(lua_State *L)
5887{
5888 int level;
5889 const char *msg;
5890 struct hlua_txn *htxn;
5891
5892 MAY_LJMP(check_args(L, 3, "log"));
5893 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5894 level = MAY_LJMP(luaL_checkinteger(L, 2));
5895 msg = MAY_LJMP(luaL_checkstring(L, 3));
5896
5897 if (level < 0 || level >= NB_LOG_LEVELS)
5898 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
5899
5900 hlua_sendlog(htxn->s->be, level, msg);
5901 return 0;
5902}
5903
5904__LJMP static int hlua_txn_log_debug(lua_State *L)
5905{
5906 const char *msg;
5907 struct hlua_txn *htxn;
5908
5909 MAY_LJMP(check_args(L, 2, "Debug"));
5910 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5911 msg = MAY_LJMP(luaL_checkstring(L, 2));
5912 hlua_sendlog(htxn->s->be, LOG_DEBUG, msg);
5913 return 0;
5914}
5915
5916__LJMP static int hlua_txn_log_info(lua_State *L)
5917{
5918 const char *msg;
5919 struct hlua_txn *htxn;
5920
5921 MAY_LJMP(check_args(L, 2, "Info"));
5922 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5923 msg = MAY_LJMP(luaL_checkstring(L, 2));
5924 hlua_sendlog(htxn->s->be, LOG_INFO, msg);
5925 return 0;
5926}
5927
5928__LJMP static int hlua_txn_log_warning(lua_State *L)
5929{
5930 const char *msg;
5931 struct hlua_txn *htxn;
5932
5933 MAY_LJMP(check_args(L, 2, "Warning"));
5934 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5935 msg = MAY_LJMP(luaL_checkstring(L, 2));
5936 hlua_sendlog(htxn->s->be, LOG_WARNING, msg);
5937 return 0;
5938}
5939
5940__LJMP static int hlua_txn_log_alert(lua_State *L)
5941{
5942 const char *msg;
5943 struct hlua_txn *htxn;
5944
5945 MAY_LJMP(check_args(L, 2, "Alert"));
5946 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5947 msg = MAY_LJMP(luaL_checkstring(L, 2));
5948 hlua_sendlog(htxn->s->be, LOG_ALERT, msg);
5949 return 0;
5950}
5951
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005952__LJMP static int hlua_txn_set_loglevel(lua_State *L)
5953{
5954 struct hlua_txn *htxn;
5955 int ll;
5956
5957 MAY_LJMP(check_args(L, 2, "set_loglevel"));
5958 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5959 ll = MAY_LJMP(luaL_checkinteger(L, 2));
5960
5961 if (ll < 0 || ll > 7)
5962 WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7"));
5963
5964 htxn->s->logs.level = ll;
5965 return 0;
5966}
5967
5968__LJMP static int hlua_txn_set_tos(lua_State *L)
5969{
5970 struct hlua_txn *htxn;
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005971 int tos;
5972
5973 MAY_LJMP(check_args(L, 2, "set_tos"));
5974 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5975 tos = MAY_LJMP(luaL_checkinteger(L, 2));
5976
Willy Tarreau1a18b542018-12-11 16:37:42 +01005977 conn_set_tos(objt_conn(htxn->s->sess->origin), tos);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005978 return 0;
5979}
5980
5981__LJMP static int hlua_txn_set_mark(lua_State *L)
5982{
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005983 struct hlua_txn *htxn;
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005984 int mark;
5985
5986 MAY_LJMP(check_args(L, 2, "set_mark"));
5987 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5988 mark = MAY_LJMP(luaL_checkinteger(L, 2));
5989
Willy Tarreau1a18b542018-12-11 16:37:42 +01005990 conn_set_tos(objt_conn(htxn->s->sess->origin), mark);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005991 return 0;
5992}
5993
Patrick Hemmer268a7072018-05-11 12:52:31 -04005994__LJMP static int hlua_txn_set_priority_class(lua_State *L)
5995{
5996 struct hlua_txn *htxn;
5997
5998 MAY_LJMP(check_args(L, 2, "set_priority_class"));
5999 htxn = MAY_LJMP(hlua_checktxn(L, 1));
6000 htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2)));
6001 return 0;
6002}
6003
6004__LJMP static int hlua_txn_set_priority_offset(lua_State *L)
6005{
6006 struct hlua_txn *htxn;
6007
6008 MAY_LJMP(check_args(L, 2, "set_priority_offset"));
6009 htxn = MAY_LJMP(hlua_checktxn(L, 1));
6010 htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2)));
6011 return 0;
6012}
6013
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01006014/* This function is an Lua binding that send pending data
6015 * to the client, and close the stream interface.
6016 */
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02006017__LJMP static int hlua_txn_done(lua_State *L)
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01006018{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02006019 struct hlua_txn *htxn;
Thierry FOURNIER9bd52d42016-07-14 11:45:33 +02006020 struct hlua *hlua;
Willy Tarreau81389672015-03-10 12:03:52 +01006021 struct channel *ic, *oc;
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01006022
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006023 MAY_LJMP(check_args(L, 1, "close"));
Willy Tarreaub2ccb562015-04-06 11:11:15 +02006024 htxn = MAY_LJMP(hlua_checktxn(L, 1));
Thierry FOURNIER9bd52d42016-07-14 11:45:33 +02006025 hlua = hlua_gethlua(L);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01006026
Thierry FOURNIERab00df62016-07-14 11:42:37 +02006027 /* If the flags NOTERM is set, we cannot terminate the http
6028 * session, so we just end the execution of the current
6029 * lua code.
6030 */
6031 if (htxn->flags & HLUA_TXN_NOTERM) {
6032 WILL_LJMP(hlua_done(L));
6033 return 0;
6034 }
6035
Willy Tarreaub2ccb562015-04-06 11:11:15 +02006036 ic = &htxn->s->req;
6037 oc = &htxn->s->res;
Willy Tarreau81389672015-03-10 12:03:52 +01006038
Willy Tarreau630ef452015-08-28 10:06:15 +02006039 if (htxn->s->txn) {
6040 /* HTTP mode, let's stay in sync with the stream */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006041 b_del(&ic->buf, htxn->s->txn->req.sov);
Willy Tarreau630ef452015-08-28 10:06:15 +02006042 htxn->s->txn->req.next -= htxn->s->txn->req.sov;
6043 htxn->s->txn->req.sov = 0;
6044 ic->analysers &= AN_REQ_HTTP_XFER_BODY;
6045 oc->analysers = AN_RES_HTTP_XFER_BODY;
6046 htxn->s->txn->req.msg_state = HTTP_MSG_CLOSED;
6047 htxn->s->txn->rsp.msg_state = HTTP_MSG_DONE;
6048
Willy Tarreau630ef452015-08-28 10:06:15 +02006049 /* Note that if we want to support keep-alive, we need
6050 * to bypass the close/shutr_now calls below, but that
6051 * may only be done if the HTTP request was already
6052 * processed and the connection header is known (ie
6053 * not during TCP rules).
6054 */
6055 }
6056
Thierry FOURNIER10ec2142015-08-24 17:23:45 +02006057 channel_auto_read(ic);
Willy Tarreau81389672015-03-10 12:03:52 +01006058 channel_abort(ic);
6059 channel_auto_close(ic);
6060 channel_erase(ic);
Thierry FOURNIER10ec2142015-08-24 17:23:45 +02006061
6062 oc->wex = tick_add_ifset(now_ms, oc->wto);
Willy Tarreau81389672015-03-10 12:03:52 +01006063 channel_auto_read(oc);
6064 channel_auto_close(oc);
6065 channel_shutr_now(oc);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01006066
Willy Tarreau0458b082015-08-28 09:40:04 +02006067 ic->analysers = 0;
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02006068
Thierry FOURNIER9bd52d42016-07-14 11:45:33 +02006069 hlua->flags |= HLUA_STOP;
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02006070 WILL_LJMP(hlua_done(L));
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01006071 return 0;
6072}
6073
6074__LJMP static int hlua_log(lua_State *L)
6075{
6076 int level;
6077 const char *msg;
6078
6079 MAY_LJMP(check_args(L, 2, "log"));
6080 level = MAY_LJMP(luaL_checkinteger(L, 1));
6081 msg = MAY_LJMP(luaL_checkstring(L, 2));
6082
6083 if (level < 0 || level >= NB_LOG_LEVELS)
6084 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
6085
6086 hlua_sendlog(NULL, level, msg);
6087 return 0;
6088}
6089
6090__LJMP static int hlua_log_debug(lua_State *L)
6091{
6092 const char *msg;
6093
6094 MAY_LJMP(check_args(L, 1, "debug"));
6095 msg = MAY_LJMP(luaL_checkstring(L, 1));
6096 hlua_sendlog(NULL, LOG_DEBUG, msg);
6097 return 0;
6098}
6099
6100__LJMP static int hlua_log_info(lua_State *L)
6101{
6102 const char *msg;
6103
6104 MAY_LJMP(check_args(L, 1, "info"));
6105 msg = MAY_LJMP(luaL_checkstring(L, 1));
6106 hlua_sendlog(NULL, LOG_INFO, msg);
6107 return 0;
6108}
6109
6110__LJMP static int hlua_log_warning(lua_State *L)
6111{
6112 const char *msg;
6113
6114 MAY_LJMP(check_args(L, 1, "warning"));
6115 msg = MAY_LJMP(luaL_checkstring(L, 1));
6116 hlua_sendlog(NULL, LOG_WARNING, msg);
6117 return 0;
6118}
6119
6120__LJMP static int hlua_log_alert(lua_State *L)
6121{
6122 const char *msg;
6123
6124 MAY_LJMP(check_args(L, 1, "alert"));
6125 msg = MAY_LJMP(luaL_checkstring(L, 1));
6126 hlua_sendlog(NULL, LOG_ALERT, msg);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01006127 return 0;
6128}
6129
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006130__LJMP static int hlua_sleep_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006131{
6132 int wakeup_ms = lua_tointeger(L, -1);
6133 if (now_ms < wakeup_ms)
Willy Tarreau9635e032018-10-16 17:52:55 +02006134 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006135 return 0;
6136}
6137
6138__LJMP static int hlua_sleep(lua_State *L)
6139{
6140 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006141 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006142
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006143 MAY_LJMP(check_args(L, 1, "sleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006144
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006145 delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006146 wakeup_ms = tick_add(now_ms, delay);
6147 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006148
Willy Tarreau9635e032018-10-16 17:52:55 +02006149 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006150 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006151}
6152
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006153__LJMP static int hlua_msleep(lua_State *L)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006154{
6155 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006156 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006157
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006158 MAY_LJMP(check_args(L, 1, "msleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006159
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006160 delay = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006161 wakeup_ms = tick_add(now_ms, delay);
6162 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006163
Willy Tarreau9635e032018-10-16 17:52:55 +02006164 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006165 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006166}
6167
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01006168/* This functionis an LUA binding. it permits to give back
6169 * the hand at the HAProxy scheduler. It is used when the
6170 * LUA processing consumes a lot of time.
6171 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006172__LJMP static int hlua_yield_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006173{
6174 return 0;
6175}
6176
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01006177__LJMP static int hlua_yield(lua_State *L)
6178{
Willy Tarreau9635e032018-10-16 17:52:55 +02006179 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_yield_yield, TICK_ETERNITY, HLUA_CTRLYIELD));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006180 return 0;
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01006181}
6182
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006183/* This function change the nice of the currently executed
6184 * task. It is used set low or high priority at the current
6185 * task.
6186 */
Willy Tarreau59551662015-03-10 14:23:13 +01006187__LJMP static int hlua_set_nice(lua_State *L)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006188{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006189 struct hlua *hlua;
6190 int nice;
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006191
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006192 MAY_LJMP(check_args(L, 1, "set_nice"));
6193 hlua = hlua_gethlua(L);
6194 nice = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006195
6196 /* If he task is not set, I'm in a start mode. */
6197 if (!hlua || !hlua->task)
6198 return 0;
6199
6200 if (nice < -1024)
6201 nice = -1024;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006202 else if (nice > 1024)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006203 nice = 1024;
6204
6205 hlua->task->nice = nice;
6206 return 0;
6207}
6208
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006209/* This function is used as a callback of a task. It is called by the
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006210 * HAProxy task subsystem when the task is awaked. The LUA runtime can
6211 * return an E_AGAIN signal, the emmiter of this signal must set a
6212 * signal to wake the task.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006213 *
6214 * Task wrapper are longjmp safe because the only one Lua code
6215 * executed is the safe hlua_ctx_resume();
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006216 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02006217static struct task *hlua_process_task(struct task *task, void *context, unsigned short state)
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006218{
Olivier Houchard9f6af332018-05-25 14:04:04 +02006219 struct hlua *hlua = context;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006220 enum hlua_exec status;
6221
Christopher Faulet5bc99722018-04-25 10:34:45 +02006222 if (task->thread_mask == MAX_THREADS_MASK)
6223 task_set_affinity(task, tid_bit);
6224
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006225 /* If it is the first call to the task, we must initialize the
6226 * execution timeouts.
6227 */
6228 if (!HLUA_IS_RUNNING(hlua))
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02006229 hlua->max_time = hlua_timeout_task;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006230
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006231 /* Execute the Lua code. */
6232 status = hlua_ctx_resume(hlua, 1);
6233
6234 switch (status) {
6235 /* finished or yield */
6236 case HLUA_E_OK:
6237 hlua_ctx_destroy(hlua);
6238 task_delete(task);
6239 task_free(task);
Tim Duesterhuscd235c62018-04-24 13:56:01 +02006240 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006241 break;
6242
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006243 case HLUA_E_AGAIN: /* co process or timeout wake me later. */
Thierry FOURNIERcb146882017-12-10 17:10:57 +01006244 notification_gc(&hlua->com);
PiBa-NLfe971b32018-05-02 22:27:14 +02006245 task->expire = hlua->wake_time;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006246 break;
6247
6248 /* finished with error. */
6249 case HLUA_E_ERRMSG:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006250 SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006251 hlua_ctx_destroy(hlua);
6252 task_delete(task);
6253 task_free(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02006254 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006255 break;
6256
6257 case HLUA_E_ERR:
6258 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006259 SEND_ERR(NULL, "Lua task: unknown error.\n");
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006260 hlua_ctx_destroy(hlua);
6261 task_delete(task);
6262 task_free(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02006263 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006264 break;
6265 }
Emeric Brun253e53e2017-10-17 18:58:40 +02006266 return task;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006267}
6268
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006269/* This function is an LUA binding that register LUA function to be
6270 * executed after the HAProxy configuration parsing and before the
6271 * HAProxy scheduler starts. This function expect only one LUA
6272 * argument that is a function. This function returns nothing, but
6273 * throws if an error is encountered.
6274 */
6275__LJMP static int hlua_register_init(lua_State *L)
6276{
6277 struct hlua_init_function *init;
6278 int ref;
6279
6280 MAY_LJMP(check_args(L, 1, "register_init"));
6281
6282 ref = MAY_LJMP(hlua_checkfunction(L, 1));
6283
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006284 init = calloc(1, sizeof(*init));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006285 if (!init)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006286 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006287
6288 init->function_ref = ref;
6289 LIST_ADDQ(&hlua_init_functions, &init->l);
6290 return 0;
6291}
6292
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006293/* This functio is an LUA binding. It permits to register a task
6294 * executed in parallel of the main HAroxy activity. The task is
6295 * created and it is set in the HAProxy scheduler. It can be called
6296 * from the "init" section, "post init" or during the runtime.
6297 *
6298 * Lua prototype:
6299 *
6300 * <none> core.register_task(<function>)
6301 */
6302static int hlua_register_task(lua_State *L)
6303{
6304 struct hlua *hlua;
6305 struct task *task;
6306 int ref;
6307
6308 MAY_LJMP(check_args(L, 1, "register_task"));
6309
6310 ref = MAY_LJMP(hlua_checkfunction(L, 1));
6311
Willy Tarreaubafbe012017-11-24 17:34:44 +01006312 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006313 if (!hlua)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006314 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006315
Emeric Brunc60def82017-09-27 14:59:38 +02006316 task = task_new(MAX_THREADS_MASK);
Willy Tarreaue09101e2018-10-16 17:37:12 +02006317 if (!task)
6318 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
6319
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006320 task->context = hlua;
6321 task->process = hlua_process_task;
6322
6323 if (!hlua_ctx_init(hlua, task))
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006324 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006325
6326 /* Restore the function in the stack. */
6327 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref);
6328 hlua->nargs = 0;
6329
6330 /* Schedule task. */
6331 task_schedule(task, now_ms);
6332
6333 return 0;
6334}
6335
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006336/* Wrapper called by HAProxy to execute an LUA converter. This wrapper
6337 * doesn't allow "yield" functions because the HAProxy engine cannot
6338 * resume converters.
6339 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006340static int hlua_sample_conv_wrapper(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006341{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006342 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006343 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006344 const char *error;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006345
Willy Tarreaube508f12016-03-10 11:47:01 +01006346 if (!stream)
6347 return 0;
6348
Willy Tarreau87b09662015-04-03 00:22:06 +02006349 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006350 * Lua context can be not initialized. This behavior
6351 * permits to save performances because a systematic
6352 * Lua initialization cause 5% performances loss.
6353 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006354 if (!stream->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006355 stream->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006356 if (!stream->hlua) {
6357 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
6358 return 0;
6359 }
6360 if (!hlua_ctx_init(stream->hlua, stream->task)) {
6361 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
6362 return 0;
6363 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006364 }
6365
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006366 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006367 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006368
6369 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006370 if (!SET_SAFE_LJMP(stream->hlua->T)) {
6371 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
6372 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006373 else
6374 error = "critical error";
6375 SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006376 return 0;
6377 }
6378
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006379 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006380 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006381 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006382 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006383 return 0;
6384 }
6385
6386 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006387 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006388
6389 /* convert input sample and pust-it in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006390 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006391 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006392 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006393 return 0;
6394 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006395 hlua_smp2lua(stream->hlua->T, smp);
6396 stream->hlua->nargs = 1;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006397
6398 /* push keywords in the stack. */
6399 if (arg_p) {
6400 for (; arg_p->type != ARGT_STOP; arg_p++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006401 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006402 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006403 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006404 return 0;
6405 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006406 hlua_arg2lua(stream->hlua->T, arg_p);
6407 stream->hlua->nargs++;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006408 }
6409 }
6410
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006411 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006412 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006413
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006414 /* At this point the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006415 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006416 }
6417
6418 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006419 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006420 /* finished. */
6421 case HLUA_E_OK:
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02006422 /* If the stack is empty, the function fails. */
6423 if (lua_gettop(stream->hlua->T) <= 0)
6424 return 0;
6425
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006426 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006427 hlua_lua2smp(stream->hlua->T, -1, smp);
6428 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006429 return 1;
6430
6431 /* yield. */
6432 case HLUA_E_AGAIN:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006433 SEND_ERR(stream->be, "Lua converter '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006434 return 0;
6435
6436 /* finished with error. */
6437 case HLUA_E_ERRMSG:
6438 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006439 SEND_ERR(stream->be, "Lua converter '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006440 fcn->name, lua_tostring(stream->hlua->T, -1));
6441 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006442 return 0;
6443
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006444 case HLUA_E_ETMOUT:
6445 SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name);
6446 return 0;
6447
6448 case HLUA_E_NOMEM:
6449 SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name);
6450 return 0;
6451
6452 case HLUA_E_YIELD:
6453 SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name);
6454 return 0;
6455
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006456 case HLUA_E_ERR:
6457 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006458 SEND_ERR(stream->be, "Lua converter '%s' returns an unknown error.\n", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006459
6460 default:
6461 return 0;
6462 }
6463}
6464
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006465/* Wrapper called by HAProxy to execute a sample-fetch. this wrapper
6466 * doesn't allow "yield" functions because the HAProxy engine cannot
Willy Tarreaube508f12016-03-10 11:47:01 +01006467 * resume sample-fetches. This function will be called by the sample
6468 * fetch engine to call lua-based fetch operations.
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006469 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +02006470static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp,
6471 const char *kw, void *private)
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006472{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006473 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006474 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006475 const char *error;
Willy Tarreau83061a82018-07-13 11:56:34 +02006476 const struct buffer msg = { };
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006477
Willy Tarreaube508f12016-03-10 11:47:01 +01006478 if (!stream)
6479 return 0;
Christopher Fauletafd8f102018-11-08 11:34:21 +01006480
Willy Tarreau87b09662015-04-03 00:22:06 +02006481 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006482 * Lua context can be not initialized. This behavior
6483 * permits to save performances because a systematic
6484 * Lua initialization cause 5% performances loss.
6485 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006486 if (!stream->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006487 stream->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006488 if (!stream->hlua) {
6489 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
6490 return 0;
6491 }
6492 if (!hlua_ctx_init(stream->hlua, stream->task)) {
6493 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
6494 return 0;
6495 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006496 }
6497
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006498 consistency_set(stream, smp->opt, &stream->hlua->cons);
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006499
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006500 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006501 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006502
6503 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006504 if (!SET_SAFE_LJMP(stream->hlua->T)) {
6505 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
6506 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006507 else
6508 error = "critical error";
6509 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006510 return 0;
6511 }
6512
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006513 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006514 if (!lua_checkstack(stream->hlua->T, 2)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006515 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006516 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006517 return 0;
6518 }
6519
6520 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006521 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006522
6523 /* push arguments in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006524 if (!hlua_txn_new(stream->hlua->T, stream, smp->px, smp->opt & SMP_OPT_DIR,
Thierry FOURNIERab00df62016-07-14 11:42:37 +02006525 HLUA_TXN_NOTERM)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006526 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006527 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006528 return 0;
6529 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006530 stream->hlua->nargs = 1;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006531
6532 /* push keywords in the stack. */
6533 for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) {
6534 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006535 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006536 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006537 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006538 return 0;
6539 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006540 hlua_arg2lua(stream->hlua->T, arg_p);
6541 stream->hlua->nargs++;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006542 }
6543
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006544 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006545 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006546
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006547 /* At this point the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006548 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006549 }
6550
6551 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006552 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006553 /* finished. */
6554 case HLUA_E_OK:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006555 if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) {
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006556 stream_int_retnclose(&stream->si[0], &msg);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02006557 return 0;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006558 }
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02006559 /* If the stack is empty, the function fails. */
6560 if (lua_gettop(stream->hlua->T) <= 0)
6561 return 0;
6562
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006563 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006564 hlua_lua2smp(stream->hlua->T, -1, smp);
6565 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006566
6567 /* Set the end of execution flag. */
6568 smp->flags &= ~SMP_F_MAY_CHANGE;
6569 return 1;
6570
6571 /* yield. */
6572 case HLUA_E_AGAIN:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006573 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006574 stream_int_retnclose(&stream->si[0], &msg);
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006575 SEND_ERR(smp->px, "Lua sample-fetch '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006576 return 0;
6577
6578 /* finished with error. */
6579 case HLUA_E_ERRMSG:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006580 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006581 stream_int_retnclose(&stream->si[0], &msg);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006582 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006583 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006584 fcn->name, lua_tostring(stream->hlua->T, -1));
6585 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006586 return 0;
6587
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006588 case HLUA_E_ETMOUT:
6589 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
6590 stream_int_retnclose(&stream->si[0], &msg);
6591 SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name);
6592 return 0;
6593
6594 case HLUA_E_NOMEM:
6595 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
6596 stream_int_retnclose(&stream->si[0], &msg);
6597 SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name);
6598 return 0;
6599
6600 case HLUA_E_YIELD:
6601 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
6602 stream_int_retnclose(&stream->si[0], &msg);
6603 SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name);
6604 return 0;
6605
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006606 case HLUA_E_ERR:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006607 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006608 stream_int_retnclose(&stream->si[0], &msg);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006609 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006610 SEND_ERR(smp->px, "Lua sample-fetch '%s' returns an unknown error.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006611
6612 default:
6613 return 0;
6614 }
6615}
6616
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006617/* This function is an LUA binding used for registering
6618 * "sample-conv" functions. It expects a converter name used
6619 * in the haproxy configuration file, and an LUA function.
6620 */
6621__LJMP static int hlua_register_converters(lua_State *L)
6622{
6623 struct sample_conv_kw_list *sck;
6624 const char *name;
6625 int ref;
6626 int len;
6627 struct hlua_function *fcn;
6628
6629 MAY_LJMP(check_args(L, 2, "register_converters"));
6630
6631 /* First argument : converter name. */
6632 name = MAY_LJMP(luaL_checkstring(L, 1));
6633
6634 /* Second argument : lua function. */
6635 ref = MAY_LJMP(hlua_checkfunction(L, 2));
6636
6637 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006638 sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006639 if (!sck)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006640 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006641 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006642 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006643 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006644
6645 /* Fill fcn. */
6646 fcn->name = strdup(name);
6647 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006648 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006649 fcn->function_ref = ref;
6650
6651 /* List head */
6652 sck->list.n = sck->list.p = NULL;
6653
6654 /* converter keyword. */
6655 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006656 sck->kw[0].kw = calloc(1, len);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006657 if (!sck->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006658 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006659
6660 snprintf((char *)sck->kw[0].kw, len, "lua.%s", name);
6661 sck->kw[0].process = hlua_sample_conv_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01006662 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 +01006663 sck->kw[0].val_args = NULL;
6664 sck->kw[0].in_type = SMP_T_STR;
6665 sck->kw[0].out_type = SMP_T_STR;
6666 sck->kw[0].private = fcn;
6667
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006668 /* Register this new converter */
6669 sample_register_convs(sck);
6670
6671 return 0;
6672}
6673
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006674/* This function is an LUA binding used for registering
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006675 * "sample-fetch" functions. It expects a converter name used
6676 * in the haproxy configuration file, and an LUA function.
6677 */
6678__LJMP static int hlua_register_fetches(lua_State *L)
6679{
6680 const char *name;
6681 int ref;
6682 int len;
6683 struct sample_fetch_kw_list *sfk;
6684 struct hlua_function *fcn;
6685
6686 MAY_LJMP(check_args(L, 2, "register_fetches"));
6687
6688 /* First argument : sample-fetch name. */
6689 name = MAY_LJMP(luaL_checkstring(L, 1));
6690
6691 /* Second argument : lua function. */
6692 ref = MAY_LJMP(hlua_checkfunction(L, 2));
6693
6694 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006695 sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006696 if (!sfk)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006697 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006698 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006699 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006700 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006701
6702 /* Fill fcn. */
6703 fcn->name = strdup(name);
6704 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006705 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006706 fcn->function_ref = ref;
6707
6708 /* List head */
6709 sfk->list.n = sfk->list.p = NULL;
6710
6711 /* sample-fetch keyword. */
6712 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006713 sfk->kw[0].kw = calloc(1, len);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006714 if (!sfk->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006715 return luaL_error(L, "Lua out of memory error.");
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006716
6717 snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name);
6718 sfk->kw[0].process = hlua_sample_fetch_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01006719 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 +01006720 sfk->kw[0].val_args = NULL;
6721 sfk->kw[0].out_type = SMP_T_STR;
6722 sfk->kw[0].use = SMP_USE_HTTP_ANY;
6723 sfk->kw[0].val = 0;
6724 sfk->kw[0].private = fcn;
6725
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006726 /* Register this new fetch. */
6727 sample_register_fetches(sfk);
6728
6729 return 0;
6730}
6731
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006732/* This function is a wrapper to execute each LUA function declared
6733 * as an action wrapper during the initialisation period. This function
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006734 * return ACT_RET_CONT if the processing is finished (with or without
6735 * error) and return ACT_RET_YIELD if the function must be called again
6736 * because the LUA returns a yield.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006737 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006738static enum act_return hlua_action(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +02006739 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006740{
6741 char **arg;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006742 unsigned int analyzer;
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02006743 int dir;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006744 const char *error;
Willy Tarreau83061a82018-07-13 11:56:34 +02006745 const struct buffer msg = { };
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006746
6747 switch (rule->from) {
Thierry FOURNIER6e01f382015-11-02 09:52:54 +01006748 case ACT_F_TCP_REQ_CNT: analyzer = AN_REQ_INSPECT_FE ; dir = SMP_OPT_DIR_REQ; break;
6749 case ACT_F_TCP_RES_CNT: analyzer = AN_RES_INSPECT ; dir = SMP_OPT_DIR_RES; break;
6750 case ACT_F_HTTP_REQ: analyzer = AN_REQ_HTTP_PROCESS_FE; dir = SMP_OPT_DIR_REQ; break;
6751 case ACT_F_HTTP_RES: analyzer = AN_RES_HTTP_PROCESS_BE; dir = SMP_OPT_DIR_RES; break;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006752 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006753 SEND_ERR(px, "Lua: internal error while execute action.\n");
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006754 return ACT_RET_CONT;
6755 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006756
Willy Tarreau87b09662015-04-03 00:22:06 +02006757 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006758 * Lua context can be not initialized. This behavior
6759 * permits to save performances because a systematic
6760 * Lua initialization cause 5% performances loss.
6761 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006762 if (!s->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006763 s->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006764 if (!s->hlua) {
6765 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
6766 rule->arg.hlua_rule->fcn.name);
6767 return ACT_RET_CONT;
6768 }
6769 if (!hlua_ctx_init(s->hlua, s->task)) {
6770 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
6771 rule->arg.hlua_rule->fcn.name);
6772 return ACT_RET_CONT;
6773 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006774 }
6775
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006776 consistency_set(s, dir, &s->hlua->cons);
6777
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006778 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006779 if (!HLUA_IS_RUNNING(s->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006780
6781 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006782 if (!SET_SAFE_LJMP(s->hlua->T)) {
6783 if (lua_type(s->hlua->T, -1) == LUA_TSTRING)
6784 error = lua_tostring(s->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006785 else
6786 error = "critical error";
6787 SEND_ERR(px, "Lua function '%s': %s.\n",
6788 rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006789 return ACT_RET_CONT;
6790 }
6791
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006792 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006793 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006794 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006795 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006796 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006797 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006798 }
6799
6800 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006801 lua_rawgeti(s->hlua->T, LUA_REGISTRYINDEX, rule->arg.hlua_rule->fcn.function_ref);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006802
Willy Tarreau87b09662015-04-03 00:22:06 +02006803 /* Create and and push object stream in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006804 if (!hlua_txn_new(s->hlua->T, s, px, dir, 0)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006805 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006806 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006807 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006808 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006809 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006810 s->hlua->nargs = 1;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006811
6812 /* push keywords in the stack. */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006813 for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006814 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006815 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006816 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006817 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006818 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006819 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006820 lua_pushstring(s->hlua->T, *arg);
6821 s->hlua->nargs++;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006822 }
6823
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006824 /* Now the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006825 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006826
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006827 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006828 s->hlua->max_time = hlua_timeout_session;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006829 }
6830
6831 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006832 switch (hlua_ctx_resume(s->hlua, !(flags & ACT_FLAG_FINAL))) {
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006833 /* finished. */
6834 case HLUA_E_OK:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006835 if (!consistency_check(s, dir, &s->hlua->cons)) {
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006836 stream_int_retnclose(&s->si[0], &msg);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02006837 return ACT_RET_ERR;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006838 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006839 if (s->hlua->flags & HLUA_STOP)
Thierry FOURNIER9bd52d42016-07-14 11:45:33 +02006840 return ACT_RET_STOP;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006841 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006842
6843 /* yield. */
6844 case HLUA_E_AGAIN:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006845 /* Set timeout in the required channel. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006846 if (s->hlua->wake_time != TICK_ETERNITY) {
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006847 if (analyzer & (AN_REQ_INSPECT_FE|AN_REQ_HTTP_PROCESS_FE))
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006848 s->req.analyse_exp = s->hlua->wake_time;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006849 else if (analyzer & (AN_RES_INSPECT|AN_RES_HTTP_PROCESS_BE))
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006850 s->res.analyse_exp = s->hlua->wake_time;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006851 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006852 /* Some actions can be wake up when a "write" event
6853 * is detected on a response channel. This is useful
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006854 * only for actions targeted on the requests.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006855 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006856 if (HLUA_IS_WAKERESWR(s->hlua)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006857 s->res.flags |= CF_WAKE_WRITE;
Willy Tarreau76bd97f2015-03-10 17:16:10 +01006858 if ((analyzer & (AN_REQ_INSPECT_FE|AN_REQ_HTTP_PROCESS_FE)))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006859 s->res.analysers |= analyzer;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006860 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006861 if (HLUA_IS_WAKEREQWR(s->hlua))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006862 s->req.flags |= CF_WAKE_WRITE;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006863 /* We can quit the function without consistency check
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006864 * because HAProxy is not able to manipulate data, it
6865 * is only allowed to call me again. */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006866 return ACT_RET_YIELD;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006867
6868 /* finished with error. */
6869 case HLUA_E_ERRMSG:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006870 if (!consistency_check(s, dir, &s->hlua->cons)) {
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006871 stream_int_retnclose(&s->si[0], &msg);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02006872 return ACT_RET_ERR;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006873 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006874 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006875 SEND_ERR(px, "Lua function '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006876 rule->arg.hlua_rule->fcn.name, lua_tostring(s->hlua->T, -1));
6877 lua_pop(s->hlua->T, 1);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006878 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006879
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006880 case HLUA_E_ETMOUT:
6881 if (!consistency_check(s, dir, &s->hlua->cons)) {
6882 stream_int_retnclose(&s->si[0], &msg);
6883 return ACT_RET_ERR;
6884 }
6885 SEND_ERR(px, "Lua function '%s': execution timeout.\n", rule->arg.hlua_rule->fcn.name);
6886 return 0;
6887
6888 case HLUA_E_NOMEM:
6889 if (!consistency_check(s, dir, &s->hlua->cons)) {
6890 stream_int_retnclose(&s->si[0], &msg);
6891 return ACT_RET_ERR;
6892 }
6893 SEND_ERR(px, "Lua function '%s': out of memory error.\n", rule->arg.hlua_rule->fcn.name);
6894 return 0;
6895
6896 case HLUA_E_YIELD:
6897 if (!consistency_check(s, dir, &s->hlua->cons)) {
6898 stream_int_retnclose(&s->si[0], &msg);
6899 return ACT_RET_ERR;
6900 }
6901 SEND_ERR(px, "Lua function '%s': aborting Lua processing on expired timeout.\n",
6902 rule->arg.hlua_rule->fcn.name);
6903 return 0;
6904
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006905 case HLUA_E_ERR:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006906 if (!consistency_check(s, dir, &s->hlua->cons)) {
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006907 stream_int_retnclose(&s->si[0], &msg);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02006908 return ACT_RET_ERR;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006909 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006910 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006911 SEND_ERR(px, "Lua function '%s' return an unknown error.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006912 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006913
6914 default:
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006915 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006916 }
6917}
6918
Olivier Houchard9f6af332018-05-25 14:04:04 +02006919struct task *hlua_applet_wakeup(struct task *t, void *context, unsigned short state)
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006920{
Olivier Houchard9f6af332018-05-25 14:04:04 +02006921 struct appctx *ctx = context;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006922
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006923 appctx_wakeup(ctx);
Willy Tarreaud9587412017-08-23 16:07:33 +02006924 t->expire = TICK_ETERNITY;
Willy Tarreaud1aa41f2017-07-21 16:41:56 +02006925 return t;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006926}
6927
6928static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
6929{
6930 struct stream_interface *si = ctx->owner;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006931 struct hlua *hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006932 struct task *task;
6933 char **arg;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006934 const char *error;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006935
Willy Tarreaubafbe012017-11-24 17:34:44 +01006936 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006937 if (!hlua) {
6938 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
6939 ctx->rule->arg.hlua_rule->fcn.name);
6940 return 0;
6941 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006942 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006943 ctx->ctx.hlua_apptcp.hlua = hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006944 ctx->ctx.hlua_apptcp.flags = 0;
6945
6946 /* Create task used by signal to wakeup applets. */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01006947 task = task_new(tid_bit);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006948 if (!task) {
6949 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
6950 ctx->rule->arg.hlua_rule->fcn.name);
6951 return 0;
6952 }
6953 task->nice = 0;
6954 task->context = ctx;
6955 task->process = hlua_applet_wakeup;
6956 ctx->ctx.hlua_apptcp.task = task;
6957
6958 /* In the execution wrappers linked with a stream, the
6959 * Lua context can be not initialized. This behavior
6960 * permits to save performances because a systematic
6961 * Lua initialization cause 5% performances loss.
6962 */
6963 if (!hlua_ctx_init(hlua, task)) {
6964 SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n",
6965 ctx->rule->arg.hlua_rule->fcn.name);
6966 return 0;
6967 }
6968
6969 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02006970 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006971
6972 /* The following Lua calls can fail. */
6973 if (!SET_SAFE_LJMP(hlua->T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01006974 if (lua_type(hlua->T, -1) == LUA_TSTRING)
6975 error = lua_tostring(hlua->T, -1);
6976 else
6977 error = "critical error";
6978 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
6979 ctx->rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006980 return 0;
6981 }
6982
6983 /* Check stack available size. */
6984 if (!lua_checkstack(hlua->T, 1)) {
6985 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
6986 ctx->rule->arg.hlua_rule->fcn.name);
6987 RESET_SAFE_LJMP(hlua->T);
6988 return 0;
6989 }
6990
6991 /* Restore the function in the stack. */
6992 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref);
6993
6994 /* Create and and push object stream in the stack. */
6995 if (!hlua_applet_tcp_new(hlua->T, ctx)) {
6996 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
6997 ctx->rule->arg.hlua_rule->fcn.name);
6998 RESET_SAFE_LJMP(hlua->T);
6999 return 0;
7000 }
7001 hlua->nargs = 1;
7002
7003 /* push keywords in the stack. */
7004 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
7005 if (!lua_checkstack(hlua->T, 1)) {
7006 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
7007 ctx->rule->arg.hlua_rule->fcn.name);
7008 RESET_SAFE_LJMP(hlua->T);
7009 return 0;
7010 }
7011 lua_pushstring(hlua->T, *arg);
7012 hlua->nargs++;
7013 }
7014
7015 RESET_SAFE_LJMP(hlua->T);
7016
7017 /* Wakeup the applet ASAP. */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01007018 si_cant_get(si);
Willy Tarreau3367d412018-11-15 10:57:41 +01007019 si_rx_endp_more(si);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007020
7021 return 1;
7022}
7023
7024static void hlua_applet_tcp_fct(struct appctx *ctx)
7025{
7026 struct stream_interface *si = ctx->owner;
7027 struct stream *strm = si_strm(si);
7028 struct channel *res = si_ic(si);
7029 struct act_rule *rule = ctx->rule;
7030 struct proxy *px = strm->be;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007031 struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007032
7033 /* The applet execution is already done. */
Olivier Houchard594c8c52018-08-28 14:41:31 +02007034 if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) {
7035 /* eat the whole request */
7036 co_skip(si_oc(si), co_data(si_oc(si)));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007037 return;
Olivier Houchard594c8c52018-08-28 14:41:31 +02007038 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007039
7040 /* If the stream is disconnect or closed, ldo nothing. */
7041 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
7042 return;
7043
7044 /* Execute the function. */
7045 switch (hlua_ctx_resume(hlua, 1)) {
7046 /* finished. */
7047 case HLUA_E_OK:
7048 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
7049
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007050 /* eat the whole request */
Willy Tarreaua79021a2018-06-15 18:07:57 +02007051 co_skip(si_oc(si), co_data(si_oc(si)));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007052 res->flags |= CF_READ_NULL;
7053 si_shutr(si);
7054 return;
7055
7056 /* yield. */
7057 case HLUA_E_AGAIN:
Thierry Fournier0164f202016-02-20 17:47:43 +01007058 if (hlua->wake_time != TICK_ETERNITY)
7059 task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007060 return;
7061
7062 /* finished with error. */
7063 case HLUA_E_ERRMSG:
7064 /* Display log. */
7065 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
7066 rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1));
7067 lua_pop(hlua->T, 1);
7068 goto error;
7069
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007070 case HLUA_E_ETMOUT:
7071 SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n",
7072 rule->arg.hlua_rule->fcn.name);
7073 goto error;
7074
7075 case HLUA_E_NOMEM:
7076 SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n",
7077 rule->arg.hlua_rule->fcn.name);
7078 goto error;
7079
7080 case HLUA_E_YIELD: /* unexpected */
7081 SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n",
7082 rule->arg.hlua_rule->fcn.name);
7083 goto error;
7084
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007085 case HLUA_E_ERR:
7086 /* Display log. */
7087 SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n",
7088 rule->arg.hlua_rule->fcn.name);
7089 goto error;
7090
7091 default:
7092 goto error;
7093 }
7094
7095error:
7096
7097 /* For all other cases, just close the stream. */
7098 si_shutw(si);
7099 si_shutr(si);
7100 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
7101}
7102
7103static void hlua_applet_tcp_release(struct appctx *ctx)
7104{
Willy Tarreaubd7fc952017-07-24 17:35:27 +02007105 task_delete(ctx->ctx.hlua_apptcp.task);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007106 task_free(ctx->ctx.hlua_apptcp.task);
7107 ctx->ctx.hlua_apptcp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007108 hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007109 ctx->ctx.hlua_apptcp.hlua = NULL;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007110}
7111
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007112/* The function returns 1 if the initialisation is complete, 0 if
7113 * an errors occurs and -1 if more data are required for initializing
7114 * the applet.
7115 */
7116static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
7117{
7118 struct stream_interface *si = ctx->owner;
7119 struct channel *req = si_oc(si);
7120 struct http_msg *msg;
7121 struct http_txn *txn;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007122 struct hlua *hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007123 char **arg;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007124 struct task *task;
Thierry Fournierfd107a22016-02-19 19:57:23 +01007125 const char *error;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007126
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007127 txn = strm->txn;
7128 msg = &txn->req;
7129
Willy Tarreau0078bfc2015-10-07 20:20:28 +02007130 /* We want two things in HTTP mode :
7131 * - enforce server-close mode if we were in keep-alive, so that the
7132 * applet is released after each response ;
7133 * - enable request body transfer to the applet in order to resync
7134 * with the response body.
7135 */
7136 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)
7137 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreau0078bfc2015-10-07 20:20:28 +02007138
Willy Tarreaubafbe012017-11-24 17:34:44 +01007139 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007140 if (!hlua) {
7141 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
7142 ctx->rule->arg.hlua_rule->fcn.name);
7143 return 0;
7144 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007145 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007146 ctx->ctx.hlua_apphttp.hlua = hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007147 ctx->ctx.hlua_apphttp.left_bytes = -1;
7148 ctx->ctx.hlua_apphttp.flags = 0;
7149
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +01007150 if (txn->req.flags & HTTP_MSGF_VER_11)
7151 ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11;
7152
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007153 /* Create task used by signal to wakeup applets. */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01007154 task = task_new(tid_bit);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007155 if (!task) {
7156 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
7157 ctx->rule->arg.hlua_rule->fcn.name);
7158 return 0;
7159 }
7160 task->nice = 0;
7161 task->context = ctx;
7162 task->process = hlua_applet_wakeup;
7163 ctx->ctx.hlua_apphttp.task = task;
7164
7165 /* In the execution wrappers linked with a stream, the
7166 * Lua context can be not initialized. This behavior
7167 * permits to save performances because a systematic
7168 * Lua initialization cause 5% performances loss.
7169 */
7170 if (!hlua_ctx_init(hlua, task)) {
7171 SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n",
7172 ctx->rule->arg.hlua_rule->fcn.name);
7173 return 0;
7174 }
7175
7176 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02007177 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007178
7179 /* The following Lua calls can fail. */
7180 if (!SET_SAFE_LJMP(hlua->T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01007181 if (lua_type(hlua->T, -1) == LUA_TSTRING)
7182 error = lua_tostring(hlua->T, -1);
7183 else
7184 error = "critical error";
7185 SEND_ERR(px, "Lua applet http '%s': %s.\n",
7186 ctx->rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007187 return 0;
7188 }
7189
7190 /* Check stack available size. */
7191 if (!lua_checkstack(hlua->T, 1)) {
7192 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
7193 ctx->rule->arg.hlua_rule->fcn.name);
7194 RESET_SAFE_LJMP(hlua->T);
7195 return 0;
7196 }
7197
7198 /* Restore the function in the stack. */
7199 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref);
7200
7201 /* Create and and push object stream in the stack. */
7202 if (!hlua_applet_http_new(hlua->T, ctx)) {
7203 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
7204 ctx->rule->arg.hlua_rule->fcn.name);
7205 RESET_SAFE_LJMP(hlua->T);
7206 return 0;
7207 }
7208 hlua->nargs = 1;
7209
7210 /* Look for a 100-continue expected. */
7211 if (msg->flags & HTTP_MSGF_VER_11) {
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007212 if (IS_HTX_STRM(si_strm(si))) {
7213 /* HTX version */
7214 struct htx *htx = htxbuf(&req->buf);
7215 struct ist hdr = { .ptr = "Expect", .len = 6 };
7216 struct http_hdr_ctx hdr_ctx;
7217
7218 hdr_ctx.blk = NULL;
7219 /* Expect is allowed in 1.1, look for it */
7220 if (http_find_header(htx, hdr, &hdr_ctx, 0) &&
7221 unlikely(isteqi(hdr_ctx.value, ist2("100-continue", 12))))
7222 ctx->ctx.hlua_apphttp.flags |= APPLET_100C;
7223 }
7224 else {
7225 /* Legacy HTTP version */
7226 struct hdr_ctx hdr;
7227
7228 hdr.idx = 0;
7229 if (http_find_header2("Expect", 6, ci_head(req), &txn->hdr_idx, &hdr) &&
7230 unlikely(hdr.vlen == 12 && strncasecmp(hdr.line+hdr.val, "100-continue", 12) == 0))
7231 ctx->ctx.hlua_apphttp.flags |= APPLET_100C;
7232 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007233 }
7234
7235 /* push keywords in the stack. */
7236 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
7237 if (!lua_checkstack(hlua->T, 1)) {
7238 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
7239 ctx->rule->arg.hlua_rule->fcn.name);
7240 RESET_SAFE_LJMP(hlua->T);
7241 return 0;
7242 }
7243 lua_pushstring(hlua->T, *arg);
7244 hlua->nargs++;
7245 }
7246
7247 RESET_SAFE_LJMP(hlua->T);
7248
7249 /* Wakeup the applet when data is ready for read. */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01007250 si_cant_get(si);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007251
7252 return 1;
7253}
7254
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007255static void hlua_applet_htx_fct(struct appctx *ctx)
7256{
7257 struct stream_interface *si = ctx->owner;
7258 struct stream *strm = si_strm(si);
7259 struct channel *req = si_oc(si);
7260 struct channel *res = si_ic(si);
7261 struct act_rule *rule = ctx->rule;
7262 struct proxy *px = strm->be;
7263 struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua;
7264 struct htx *req_htx, *res_htx;
7265
7266 res_htx = htx_from_buf(&res->buf);
7267
7268 /* If the stream is disconnect or closed, ldo nothing. */
7269 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
7270 goto out;
7271
7272 /* Check if the input buffer is avalaible. */
7273 if (!b_size(&res->buf)) {
7274 si_rx_room_blk(si);
7275 goto out;
7276 }
7277 /* check that the output is not closed */
7278 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW))
7279 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7280
7281 /* Set the currently running flag. */
7282 if (!HLUA_IS_RUNNING(hlua) &&
7283 !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
7284 struct htx_blk *blk;
7285 size_t count = co_data(req);
7286
7287 if (!count) {
7288 si_cant_get(si);
7289 goto out;
7290 }
7291
7292 /* We need to flush the request header. This left the body for
7293 * the Lua.
7294 */
7295 req_htx = htx_from_buf(&req->buf);
7296 blk = htx_get_head_blk(req_htx);
7297 while (count && blk) {
7298 enum htx_blk_type type = htx_get_blk_type(blk);
7299 uint32_t sz = htx_get_blksz(blk);
7300
7301 if (sz > count) {
7302 si_cant_get(si);
7303 goto out;
7304 }
7305
7306 count -= sz;
7307 co_set_data(req, co_data(req) - sz);
7308 blk = htx_remove_blk(req_htx, blk);
7309
7310 if (type == HTX_BLK_EOH)
7311 break;
7312 }
7313 }
7314
7315 /* Executes The applet if it is not done. */
7316 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
7317
7318 /* Execute the function. */
7319 switch (hlua_ctx_resume(hlua, 1)) {
7320 /* finished. */
7321 case HLUA_E_OK:
7322 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7323 break;
7324
7325 /* yield. */
7326 case HLUA_E_AGAIN:
7327 if (hlua->wake_time != TICK_ETERNITY)
7328 task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time);
7329 return;
7330
7331 /* finished with error. */
7332 case HLUA_E_ERRMSG:
7333 /* Display log. */
7334 SEND_ERR(px, "Lua applet http '%s': %s.\n",
7335 rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1));
7336 lua_pop(hlua->T, 1);
7337 goto error;
7338
7339 case HLUA_E_ETMOUT:
7340 SEND_ERR(px, "Lua applet http '%s': execution timeout.\n",
7341 rule->arg.hlua_rule->fcn.name);
7342 goto error;
7343
7344 case HLUA_E_NOMEM:
7345 SEND_ERR(px, "Lua applet http '%s': out of memory error.\n",
7346 rule->arg.hlua_rule->fcn.name);
7347 goto error;
7348
7349 case HLUA_E_YIELD: /* unexpected */
7350 SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n",
7351 rule->arg.hlua_rule->fcn.name);
7352 goto error;
7353
7354 case HLUA_E_ERR:
7355 /* Display log. */
7356 SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n",
7357 rule->arg.hlua_rule->fcn.name);
7358 goto error;
7359
7360 default:
7361 goto error;
7362 }
7363 }
7364
7365 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
7366 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT))
7367 goto error;
7368
7369 /* Don't add EOD and TLR because mux-h1 will take care of it */
7370 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
7371 si_rx_room_blk(si);
7372 goto out;
7373 }
7374 res->total++;
7375 res->flags |= CF_READ_PARTIAL;
7376 }
7377
7378 done:
7379 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
7380 /* eat the whole request */
7381 req_htx = htxbuf(&req->buf);
7382 htx_reset(req_htx);
7383 htx_to_buf(req_htx, &req->buf);
7384 co_set_data(req, 0);
7385 res->flags |= CF_READ_NULL;
7386 si_shutr(si);
7387 }
7388
7389 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
7390 si_shutw(si);
7391
7392 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
7393 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST)) {
7394 si_shutr(si);
7395 res->flags |= CF_READ_NULL;
7396 }
7397 }
7398
7399 out:
7400 /* we have left the request in the buffer for the case where we
7401 * process a POST, and this automatically re-enables activity on
7402 * read. It's better to indicate that we want to stop reading when
7403 * we're sending, so that we know there's at most one direction
7404 * deciding to wake the applet up. It saves it from looping when
7405 * emitting large blocks into small TCP windows.
7406 */
7407 htx_to_buf(res_htx, &res->buf);
7408 if (!channel_is_empty(res))
7409 si_stop_get(si);
7410 return;
7411
7412 error:
7413
7414 /* If we are in HTTP mode, and we are not send any
7415 * data, return a 500 server error in best effort:
7416 * if there is no room available in the buffer,
7417 * just close the connection.
7418 */
7419 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
7420 struct buffer *err = &htx_err_chunks[HTTP_ERR_500];
7421
7422 channel_erase(res);
7423 res->buf.data = b_data(err);
7424 memcpy(res->buf.area, b_head(err), b_data(err));
7425 res_htx = htx_from_buf(&res->buf);
7426
7427 res->total += res_htx->data;
7428 res->flags |= CF_READ_PARTIAL;
7429 }
7430 if (!(strm->flags & SF_ERR_MASK))
7431 strm->flags |= SF_ERR_RESOURCE;
7432 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7433 goto done;
7434}
7435
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007436static void hlua_applet_http_fct(struct appctx *ctx)
7437{
7438 struct stream_interface *si = ctx->owner;
7439 struct stream *strm = si_strm(si);
7440 struct channel *res = si_ic(si);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007441 struct act_rule *rule = ctx->rule;
7442 struct proxy *px = strm->be;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007443 struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua;
Willy Tarreau206ba832018-06-14 15:27:31 +02007444 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02007445 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02007446 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02007447 size_t len2;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007448 int ret;
7449
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007450 if (IS_HTX_STRM(strm))
7451 return hlua_applet_htx_fct(ctx);
7452
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007453 /* If the stream is disconnect or closed, ldo nothing. */
7454 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
7455 return;
7456
7457 /* Set the currently running flag. */
7458 if (!HLUA_IS_RUNNING(hlua) &&
7459 !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007460 /* Store the max amount of bytes that we can read. */
7461 ctx->ctx.hlua_apphttp.left_bytes = strm->txn->req.body_len;
7462
7463 /* We need to flush the request header. This left the body
7464 * for the Lua.
7465 */
7466
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007467 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02007468 ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007469 if (ret == -1)
7470 return;
7471
7472 /* No data available, ask for more data. */
7473 if (ret == 1)
7474 len2 = 0;
7475 if (ret == 0)
7476 len1 = 0;
Thierry FOURNIER70d318c2018-06-30 10:37:33 +02007477 if (len1 + len2 < strm->txn->req.eoh + strm->txn->req.eol) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01007478 si_cant_get(si);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007479 return;
7480 }
7481
7482 /* skip the requests bytes. */
Thierry FOURNIER70d318c2018-06-30 10:37:33 +02007483 co_skip(si_oc(si), strm->txn->req.eoh + strm->txn->req.eol);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007484 }
7485
7486 /* Executes The applet if it is not done. */
7487 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
7488
7489 /* Execute the function. */
7490 switch (hlua_ctx_resume(hlua, 1)) {
7491 /* finished. */
7492 case HLUA_E_OK:
7493 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7494 break;
7495
7496 /* yield. */
7497 case HLUA_E_AGAIN:
Thierry Fournier0164f202016-02-20 17:47:43 +01007498 if (hlua->wake_time != TICK_ETERNITY)
7499 task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007500 return;
7501
7502 /* finished with error. */
7503 case HLUA_E_ERRMSG:
7504 /* Display log. */
7505 SEND_ERR(px, "Lua applet http '%s': %s.\n",
7506 rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1));
7507 lua_pop(hlua->T, 1);
7508 goto error;
7509
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007510 case HLUA_E_ETMOUT:
7511 SEND_ERR(px, "Lua applet http '%s': execution timeout.\n",
7512 rule->arg.hlua_rule->fcn.name);
7513 goto error;
7514
7515 case HLUA_E_NOMEM:
7516 SEND_ERR(px, "Lua applet http '%s': out of memory error.\n",
7517 rule->arg.hlua_rule->fcn.name);
7518 goto error;
7519
7520 case HLUA_E_YIELD: /* unexpected */
7521 SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n",
7522 rule->arg.hlua_rule->fcn.name);
7523 goto error;
7524
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007525 case HLUA_E_ERR:
7526 /* Display log. */
7527 SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n",
7528 rule->arg.hlua_rule->fcn.name);
7529 goto error;
7530
7531 default:
7532 goto error;
7533 }
7534 }
7535
7536 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
7537
7538 /* We must send the final chunk. */
7539 if (ctx->ctx.hlua_apphttp.flags & APPLET_CHUNKED &&
7540 !(ctx->ctx.hlua_apphttp.flags & APPLET_LAST_CHK)) {
7541
7542 /* sent last chunk at once. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02007543 ret = ci_putblk(res, "0\r\n\r\n", 5);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007544
7545 /* critical error. */
7546 if (ret == -2 || ret == -3) {
7547 SEND_ERR(px, "Lua applet http '%s'cannont send last chunk.\n",
7548 rule->arg.hlua_rule->fcn.name);
7549 goto error;
7550 }
7551
7552 /* no enough space error. */
7553 if (ret == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01007554 si_rx_room_blk(si);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007555 return;
7556 }
7557
7558 /* set the last chunk sent. */
7559 ctx->ctx.hlua_apphttp.flags |= APPLET_LAST_CHK;
7560 }
7561
7562 /* close the connection. */
7563
Frédéric Lécaille83ed5d52018-07-18 14:25:26 +02007564 /* status */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007565 strm->txn->status = ctx->ctx.hlua_apphttp.status;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007566
7567 /* eat the whole request */
Willy Tarreaua79021a2018-06-15 18:07:57 +02007568 co_skip(si_oc(si), co_data(si_oc(si)));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007569 res->flags |= CF_READ_NULL;
7570 si_shutr(si);
7571
7572 return;
7573 }
7574
7575error:
7576
7577 /* If we are in HTTP mode, and we are not send any
7578 * data, return a 500 server error in best effort:
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007579 * if there is no room available in the buffer,
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007580 * just close the connection.
7581 */
Willy Tarreau06d80a92017-10-19 14:32:15 +02007582 ci_putblk(res, error_500, strlen(error_500));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007583 if (!(strm->flags & SF_ERR_MASK))
7584 strm->flags |= SF_ERR_RESOURCE;
7585 si_shutw(si);
7586 si_shutr(si);
7587 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7588}
7589
7590static void hlua_applet_http_release(struct appctx *ctx)
7591{
Willy Tarreaubd7fc952017-07-24 17:35:27 +02007592 task_delete(ctx->ctx.hlua_apphttp.task);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007593 task_free(ctx->ctx.hlua_apphttp.task);
7594 ctx->ctx.hlua_apphttp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007595 hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007596 ctx->ctx.hlua_apphttp.hlua = NULL;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007597}
7598
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007599/* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in
7600 * succes case, else return ACT_RET_PRS_ERR.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007601 *
7602 * This function can fail with an abort() due to an Lua critical error.
7603 * We are in the configuration parsing process of HAProxy, this abort() is
7604 * tolerated.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007605 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007606static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px,
7607 struct act_rule *rule, char **err)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007608{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007609 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007610 int i;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007611
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007612 /* Memory for the rule. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007613 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007614 if (!rule->arg.hlua_rule) {
7615 memprintf(err, "out of memory error");
Thierry FOURNIERafa80492015-08-19 09:04:15 +02007616 return ACT_RET_PRS_ERR;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007617 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007618
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007619 /* Memory for arguments. */
7620 rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, sizeof(char *));
7621 if (!rule->arg.hlua_rule->args) {
7622 memprintf(err, "out of memory error");
7623 return ACT_RET_PRS_ERR;
7624 }
7625
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007626 /* Reference the Lua function and store the reference. */
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007627 rule->arg.hlua_rule->fcn = *fcn;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007628
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007629 /* Expect some arguments */
7630 for (i = 0; i < fcn->nargs; i++) {
7631 if (*args[i+1] == '\0') {
7632 memprintf(err, "expect %d arguments", fcn->nargs);
7633 return ACT_RET_PRS_ERR;
7634 }
7635 rule->arg.hlua_rule->args[i] = strdup(args[i + 1]);
7636 if (!rule->arg.hlua_rule->args[i]) {
7637 memprintf(err, "out of memory error");
7638 return ACT_RET_PRS_ERR;
7639 }
7640 (*cur_arg)++;
7641 }
7642 rule->arg.hlua_rule->args[i] = NULL;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007643
Thierry FOURNIER42148732015-09-02 17:17:33 +02007644 rule->action = ACT_CUSTOM;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007645 rule->action_ptr = hlua_action;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02007646 return ACT_RET_PRS_OK;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007647}
7648
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007649static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px,
7650 struct act_rule *rule, char **err)
7651{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007652 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007653
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01007654 /* HTTP applets are forbidden in tcp-request rules.
7655 * HTTP applet request requires everything initilized by
7656 * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER).
7657 * The applet will be immediately initilized, but its before
7658 * the call of this analyzer.
7659 */
7660 if (rule->from != ACT_F_HTTP_REQ) {
7661 memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets");
7662 return ACT_RET_PRS_ERR;
7663 }
7664
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007665 /* Memory for the rule. */
7666 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
7667 if (!rule->arg.hlua_rule) {
7668 memprintf(err, "out of memory error");
7669 return ACT_RET_PRS_ERR;
7670 }
7671
7672 /* Reference the Lua function and store the reference. */
7673 rule->arg.hlua_rule->fcn = *fcn;
7674
7675 /* TODO: later accept arguments. */
7676 rule->arg.hlua_rule->args = NULL;
7677
7678 /* Add applet pointer in the rule. */
7679 rule->applet.obj_type = OBJ_TYPE_APPLET;
7680 rule->applet.name = fcn->name;
7681 rule->applet.init = hlua_applet_http_init;
7682 rule->applet.fct = hlua_applet_http_fct;
7683 rule->applet.release = hlua_applet_http_release;
7684 rule->applet.timeout = hlua_timeout_applet;
7685
7686 return ACT_RET_PRS_OK;
7687}
7688
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007689/* This function is an LUA binding used for registering
7690 * "sample-conv" functions. It expects a converter name used
7691 * in the haproxy configuration file, and an LUA function.
7692 */
7693__LJMP static int hlua_register_action(lua_State *L)
7694{
7695 struct action_kw_list *akl;
7696 const char *name;
7697 int ref;
7698 int len;
7699 struct hlua_function *fcn;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007700 int nargs;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007701
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007702 /* Initialise the number of expected arguments at 0. */
7703 nargs = 0;
7704
7705 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
7706 WILL_LJMP(luaL_error(L, "'register_action' needs between 3 and 4 arguments"));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007707
7708 /* First argument : converter name. */
7709 name = MAY_LJMP(luaL_checkstring(L, 1));
7710
7711 /* Second argument : environment. */
7712 if (lua_type(L, 2) != LUA_TTABLE)
7713 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
7714
7715 /* Third argument : lua function. */
7716 ref = MAY_LJMP(hlua_checkfunction(L, 3));
7717
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007718 /* Fourth argument : number of mandatory arguments expected on the configuration line. */
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007719 if (lua_gettop(L) >= 4)
7720 nargs = MAY_LJMP(luaL_checkinteger(L, 4));
7721
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007722 /* browse the second argument as an array. */
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007723 lua_pushnil(L);
7724 while (lua_next(L, 2) != 0) {
7725 if (lua_type(L, -1) != LUA_TSTRING)
7726 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
7727
7728 /* Check required environment. Only accepted "http" or "tcp". */
7729 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007730 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007731 if (!akl)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007732 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007733 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007734 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007735 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007736
7737 /* Fill fcn. */
7738 fcn->name = strdup(name);
7739 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007740 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007741 fcn->function_ref = ref;
7742
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007743 /* Set the expected number od arguments. */
7744 fcn->nargs = nargs;
7745
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007746 /* List head */
7747 akl->list.n = akl->list.p = NULL;
7748
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007749 /* action keyword. */
7750 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007751 akl->kw[0].kw = calloc(1, len);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007752 if (!akl->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007753 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007754
7755 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
7756
7757 akl->kw[0].match_pfx = 0;
7758 akl->kw[0].private = fcn;
7759 akl->kw[0].parse = action_register_lua;
7760
7761 /* select the action registering point. */
7762 if (strcmp(lua_tostring(L, -1), "tcp-req") == 0)
7763 tcp_req_cont_keywords_register(akl);
7764 else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0)
7765 tcp_res_cont_keywords_register(akl);
7766 else if (strcmp(lua_tostring(L, -1), "http-req") == 0)
7767 http_req_keywords_register(akl);
7768 else if (strcmp(lua_tostring(L, -1), "http-res") == 0)
7769 http_res_keywords_register(akl);
7770 else
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007771 WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. "
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007772 "'tcp-req', 'tcp-res', 'http-req' or 'http-res' "
7773 "are expected.", lua_tostring(L, -1)));
7774
7775 /* pop the environment string. */
7776 lua_pop(L, 1);
7777 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007778 return ACT_RET_PRS_OK;
7779}
7780
7781static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px,
7782 struct act_rule *rule, char **err)
7783{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007784 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007785
Christopher Fauletafd8f102018-11-08 11:34:21 +01007786 if (px->options2 & PR_O2_USE_HTX) {
7787 memprintf(err, "Lua services cannot be used when the HTX internal representation is enabled");
7788 return ACT_RET_PRS_ERR;
7789 }
7790
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007791 /* Memory for the rule. */
7792 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
7793 if (!rule->arg.hlua_rule) {
7794 memprintf(err, "out of memory error");
7795 return ACT_RET_PRS_ERR;
7796 }
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007797
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007798 /* Reference the Lua function and store the reference. */
7799 rule->arg.hlua_rule->fcn = *fcn;
7800
7801 /* TODO: later accept arguments. */
7802 rule->arg.hlua_rule->args = NULL;
7803
7804 /* Add applet pointer in the rule. */
7805 rule->applet.obj_type = OBJ_TYPE_APPLET;
7806 rule->applet.name = fcn->name;
7807 rule->applet.init = hlua_applet_tcp_init;
7808 rule->applet.fct = hlua_applet_tcp_fct;
7809 rule->applet.release = hlua_applet_tcp_release;
7810 rule->applet.timeout = hlua_timeout_applet;
7811
7812 return 0;
7813}
7814
7815/* This function is an LUA binding used for registering
7816 * "sample-conv" functions. It expects a converter name used
7817 * in the haproxy configuration file, and an LUA function.
7818 */
7819__LJMP static int hlua_register_service(lua_State *L)
7820{
7821 struct action_kw_list *akl;
7822 const char *name;
7823 const char *env;
7824 int ref;
7825 int len;
7826 struct hlua_function *fcn;
7827
7828 MAY_LJMP(check_args(L, 3, "register_service"));
7829
7830 /* First argument : converter name. */
7831 name = MAY_LJMP(luaL_checkstring(L, 1));
7832
7833 /* Second argument : environment. */
7834 env = MAY_LJMP(luaL_checkstring(L, 2));
7835
7836 /* Third argument : lua function. */
7837 ref = MAY_LJMP(hlua_checkfunction(L, 3));
7838
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007839 /* Allocate and fill the sample fetch keyword struct. */
7840 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
7841 if (!akl)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007842 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007843 fcn = calloc(1, sizeof(*fcn));
7844 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007845 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007846
7847 /* Fill fcn. */
7848 len = strlen("<lua.>") + strlen(name) + 1;
7849 fcn->name = calloc(1, len);
7850 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007851 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007852 snprintf((char *)fcn->name, len, "<lua.%s>", name);
7853 fcn->function_ref = ref;
7854
7855 /* List head */
7856 akl->list.n = akl->list.p = NULL;
7857
7858 /* converter keyword. */
7859 len = strlen("lua.") + strlen(name) + 1;
7860 akl->kw[0].kw = calloc(1, len);
7861 if (!akl->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007862 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007863
7864 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
7865
Thierry FOURNIER / OZON.IO02564fd2016-11-12 11:07:05 +01007866 /* Check required environment. Only accepted "http" or "tcp". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007867 if (strcmp(env, "tcp") == 0)
7868 akl->kw[0].parse = action_register_service_tcp;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007869 else if (strcmp(env, "http") == 0)
7870 akl->kw[0].parse = action_register_service_http;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007871 else
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007872 WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. "
Eric Salamafe7456f2017-12-21 14:30:07 +01007873 "'tcp' or 'http' are expected.", env));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007874
7875 akl->kw[0].match_pfx = 0;
7876 akl->kw[0].private = fcn;
7877
7878 /* End of array. */
7879 memset(&akl->kw[1], 0, sizeof(*akl->kw));
7880
7881 /* Register this new converter */
7882 service_keywords_register(akl);
7883
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007884 return 0;
7885}
7886
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007887/* This function initialises Lua cli handler. It copies the
7888 * arguments in the Lua stack and create channel IO objects.
7889 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02007890static int hlua_cli_parse_fct(char **args, char *payload, struct appctx *appctx, void *private)
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007891{
7892 struct hlua *hlua;
7893 struct hlua_function *fcn;
7894 int i;
7895 const char *error;
7896
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007897 fcn = private;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007898 appctx->ctx.hlua_cli.fcn = private;
7899
Willy Tarreaubafbe012017-11-24 17:34:44 +01007900 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007901 if (!hlua) {
7902 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007903 return 1;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007904 }
7905 HLUA_INIT(hlua);
7906 appctx->ctx.hlua_cli.hlua = hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007907
7908 /* Create task used by signal to wakeup applets.
7909 * We use the same wakeup fonction than the Lua applet_tcp and
7910 * applet_http. It is absolutely compatible.
7911 */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01007912 appctx->ctx.hlua_cli.task = task_new(tid_bit);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007913 if (!appctx->ctx.hlua_cli.task) {
Thierry FOURNIERffbf5692016-12-16 11:14:06 +01007914 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007915 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007916 }
7917 appctx->ctx.hlua_cli.task->nice = 0;
7918 appctx->ctx.hlua_cli.task->context = appctx;
7919 appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup;
7920
7921 /* Initialises the Lua context */
7922 if (!hlua_ctx_init(hlua, appctx->ctx.hlua_cli.task)) {
7923 SEND_ERR(NULL, "Lua cli '%s': can't initialize Lua context.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007924 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007925 }
7926
7927 /* The following Lua calls can fail. */
7928 if (!SET_SAFE_LJMP(hlua->T)) {
7929 if (lua_type(hlua->T, -1) == LUA_TSTRING)
7930 error = lua_tostring(hlua->T, -1);
7931 else
7932 error = "critical error";
7933 SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error);
7934 goto error;
7935 }
7936
7937 /* Check stack available size. */
7938 if (!lua_checkstack(hlua->T, 2)) {
7939 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7940 goto error;
7941 }
7942
7943 /* Restore the function in the stack. */
7944 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
7945
7946 /* Once the arguments parsed, the CLI is like an AppletTCP,
7947 * so push AppletTCP in the stack.
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007948 */
7949 if (!hlua_applet_tcp_new(hlua->T, appctx)) {
7950 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7951 goto error;
7952 }
7953 hlua->nargs = 1;
7954
7955 /* push keywords in the stack. */
7956 for (i = 0; *args[i]; i++) {
7957 /* Check stack available size. */
7958 if (!lua_checkstack(hlua->T, 1)) {
7959 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7960 goto error;
7961 }
7962 lua_pushstring(hlua->T, args[i]);
7963 hlua->nargs++;
7964 }
7965
7966 /* We must initialize the execution timeouts. */
7967 hlua->max_time = hlua_timeout_session;
7968
7969 /* At this point the execution is safe. */
7970 RESET_SAFE_LJMP(hlua->T);
7971
7972 /* It's ok */
7973 return 0;
7974
7975 /* It's not ok. */
7976error:
7977 RESET_SAFE_LJMP(hlua->T);
7978 hlua_ctx_destroy(hlua);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007979 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007980 return 1;
7981}
7982
7983static int hlua_cli_io_handler_fct(struct appctx *appctx)
7984{
7985 struct hlua *hlua;
7986 struct stream_interface *si;
7987 struct hlua_function *fcn;
7988
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007989 hlua = appctx->ctx.hlua_cli.hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007990 si = appctx->owner;
Willy Tarreau8ae4f752016-12-14 15:41:45 +01007991 fcn = appctx->ctx.hlua_cli.fcn;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007992
7993 /* If the stream is disconnect or closed, ldo nothing. */
7994 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
7995 return 1;
7996
7997 /* Execute the function. */
7998 switch (hlua_ctx_resume(hlua, 1)) {
7999
8000 /* finished. */
8001 case HLUA_E_OK:
8002 return 1;
8003
8004 /* yield. */
8005 case HLUA_E_AGAIN:
8006 /* We want write. */
8007 if (HLUA_IS_WAKERESWR(hlua))
Willy Tarreaudb398432018-11-15 11:08:52 +01008008 si_rx_room_blk(si);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008009 /* Set the timeout. */
8010 if (hlua->wake_time != TICK_ETERNITY)
8011 task_schedule(hlua->task, hlua->wake_time);
8012 return 0;
8013
8014 /* finished with error. */
8015 case HLUA_E_ERRMSG:
8016 /* Display log. */
8017 SEND_ERR(NULL, "Lua cli '%s': %s.\n",
8018 fcn->name, lua_tostring(hlua->T, -1));
8019 lua_pop(hlua->T, 1);
8020 return 1;
8021
Thierry Fournierd5b073c2018-05-21 19:42:47 +02008022 case HLUA_E_ETMOUT:
8023 SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n",
8024 fcn->name);
8025 return 1;
8026
8027 case HLUA_E_NOMEM:
8028 SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n",
8029 fcn->name);
8030 return 1;
8031
8032 case HLUA_E_YIELD: /* unexpected */
8033 SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n",
8034 fcn->name);
8035 return 1;
8036
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008037 case HLUA_E_ERR:
8038 /* Display log. */
8039 SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n",
8040 fcn->name);
8041 return 1;
8042
8043 default:
8044 return 1;
8045 }
8046
8047 return 1;
8048}
8049
8050static void hlua_cli_io_release_fct(struct appctx *appctx)
8051{
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01008052 hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01008053 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008054}
8055
8056/* This function is an LUA binding used for registering
8057 * new keywords in the cli. It expects a list of keywords
8058 * which are the "path". It is limited to 5 keywords. A
8059 * description of the command, a function to be executed
8060 * for the parsing and a function for io handlers.
8061 */
8062__LJMP static int hlua_register_cli(lua_State *L)
8063{
8064 struct cli_kw_list *cli_kws;
8065 const char *message;
8066 int ref_io;
8067 int len;
8068 struct hlua_function *fcn;
8069 int index;
8070 int i;
8071
8072 MAY_LJMP(check_args(L, 3, "register_cli"));
8073
8074 /* First argument : an array of maximum 5 keywords. */
8075 if (!lua_istable(L, 1))
8076 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table"));
8077
8078 /* Second argument : string with contextual message. */
8079 message = MAY_LJMP(luaL_checkstring(L, 2));
8080
8081 /* Third and fourth argument : lua function. */
8082 ref_io = MAY_LJMP(hlua_checkfunction(L, 3));
8083
8084 /* Allocate and fill the sample fetch keyword struct. */
8085 cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2);
8086 if (!cli_kws)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008087 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008088 fcn = calloc(1, sizeof(*fcn));
8089 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008090 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008091
8092 /* Fill path. */
8093 index = 0;
8094 lua_pushnil(L);
8095 while(lua_next(L, 1) != 0) {
8096 if (index >= 5)
8097 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries"));
8098 if (lua_type(L, -1) != LUA_TSTRING)
8099 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings"));
8100 cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1));
8101 if (!cli_kws->kw[0].str_kw[index])
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008102 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008103 index++;
8104 lua_pop(L, 1);
8105 }
8106
8107 /* Copy help message. */
8108 cli_kws->kw[0].usage = strdup(message);
8109 if (!cli_kws->kw[0].usage)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008110 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008111
8112 /* Fill fcn io handler. */
8113 len = strlen("<lua.cli>") + 1;
8114 for (i = 0; i < index; i++)
8115 len += strlen(cli_kws->kw[0].str_kw[i]) + 1;
8116 fcn->name = calloc(1, len);
8117 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008118 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008119 strncat((char *)fcn->name, "<lua.cli", len);
8120 for (i = 0; i < index; i++) {
8121 strncat((char *)fcn->name, ".", len);
8122 strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len);
8123 }
8124 strncat((char *)fcn->name, ">", len);
8125 fcn->function_ref = ref_io;
8126
8127 /* Fill last entries. */
8128 cli_kws->kw[0].private = fcn;
8129 cli_kws->kw[0].parse = hlua_cli_parse_fct;
8130 cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct;
8131 cli_kws->kw[0].io_release = hlua_cli_io_release_fct;
8132
8133 /* Register this new converter */
8134 cli_register_kw(cli_kws);
8135
8136 return 0;
8137}
8138
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008139static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx,
8140 struct proxy *defpx, const char *file, int line,
8141 char **err, unsigned int *timeout)
8142{
8143 const char *error;
8144
8145 error = parse_time_err(args[1], timeout, TIME_UNIT_MS);
8146 if (error && *error != '\0') {
8147 memprintf(err, "%s: invalid timeout", args[0]);
8148 return -1;
8149 }
8150 return 0;
8151}
8152
8153static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx,
8154 struct proxy *defpx, const char *file, int line,
8155 char **err)
8156{
8157 return hlua_read_timeout(args, section_type, curpx, defpx,
8158 file, line, err, &hlua_timeout_session);
8159}
8160
8161static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx,
8162 struct proxy *defpx, const char *file, int line,
8163 char **err)
8164{
8165 return hlua_read_timeout(args, section_type, curpx, defpx,
8166 file, line, err, &hlua_timeout_task);
8167}
8168
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008169static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx,
8170 struct proxy *defpx, const char *file, int line,
8171 char **err)
8172{
8173 return hlua_read_timeout(args, section_type, curpx, defpx,
8174 file, line, err, &hlua_timeout_applet);
8175}
8176
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01008177static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx,
8178 struct proxy *defpx, const char *file, int line,
8179 char **err)
8180{
8181 char *error;
8182
8183 hlua_nb_instruction = strtoll(args[1], &error, 10);
8184 if (*error != '\0') {
8185 memprintf(err, "%s: invalid number", args[0]);
8186 return -1;
8187 }
8188 return 0;
8189}
8190
Willy Tarreau32f61e22015-03-18 17:54:59 +01008191static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx,
8192 struct proxy *defpx, const char *file, int line,
8193 char **err)
8194{
8195 char *error;
8196
8197 if (*(args[1]) == 0) {
8198 memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]);
8199 return -1;
8200 }
8201 hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L;
8202 if (*error != '\0') {
8203 memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error);
8204 return -1;
8205 }
8206 return 0;
8207}
8208
8209
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008210/* This function is called by the main configuration key "lua-load". It loads and
8211 * execute an lua file during the parsing of the HAProxy configuration file. It is
8212 * the main lua entry point.
8213 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08008214 * This function runs with the HAProxy keywords API. It returns -1 if an error
8215 * occurs, otherwise it returns 0.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008216 *
8217 * In some error case, LUA set an error message in top of the stack. This function
8218 * returns this error message in the HAProxy logs and pop it from the stack.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008219 *
8220 * This function can fail with an abort() due to an Lua critical error.
8221 * We are in the configuration parsing process of HAProxy, this abort() is
8222 * tolerated.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008223 */
8224static int hlua_load(char **args, int section_type, struct proxy *curpx,
8225 struct proxy *defpx, const char *file, int line,
8226 char **err)
8227{
8228 int error;
8229
8230 /* Just load and compile the file. */
8231 error = luaL_loadfile(gL.T, args[1]);
8232 if (error) {
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008233 memprintf(err, "error in Lua file '%s': %s", args[1], lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008234 lua_pop(gL.T, 1);
8235 return -1;
8236 }
8237
8238 /* If no syntax error where detected, execute the code. */
8239 error = lua_pcall(gL.T, 0, LUA_MULTRET, 0);
8240 switch (error) {
8241 case LUA_OK:
8242 break;
8243 case LUA_ERRRUN:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008244 memprintf(err, "Lua runtime error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008245 lua_pop(gL.T, 1);
8246 return -1;
8247 case LUA_ERRMEM:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008248 memprintf(err, "Lua out of memory error.n");
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008249 return -1;
8250 case LUA_ERRERR:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008251 memprintf(err, "Lua message handler error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008252 lua_pop(gL.T, 1);
8253 return -1;
8254 case LUA_ERRGCMM:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008255 memprintf(err, "Lua garbage collector error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008256 lua_pop(gL.T, 1);
8257 return -1;
8258 default:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008259 memprintf(err, "Lua unknonwn error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008260 lua_pop(gL.T, 1);
8261 return -1;
8262 }
8263
8264 return 0;
8265}
8266
8267/* configuration keywords declaration */
8268static struct cfg_kw_list cfg_kws = {{ },{
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008269 { CFG_GLOBAL, "lua-load", hlua_load },
8270 { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout },
8271 { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout },
Thierry FOURNIER56da1012015-10-01 08:42:31 +02008272 { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout },
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01008273 { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield },
Willy Tarreau32f61e22015-03-18 17:54:59 +01008274 { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem },
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008275 { 0, NULL, NULL },
8276}};
8277
Willy Tarreau0108d902018-11-25 19:14:37 +01008278INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
8279
Christopher Fauletafd8f102018-11-08 11:34:21 +01008280
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008281/* This function can fail with an abort() due to an Lua critical error.
8282 * We are in the initialisation process of HAProxy, this abort() is
8283 * tolerated.
8284 */
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008285int hlua_post_init()
8286{
8287 struct hlua_init_function *init;
8288 const char *msg;
8289 enum hlua_exec ret;
Thierry Fournierfd107a22016-02-19 19:57:23 +01008290 const char *error;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008291
Thierry Fournier3d4a6752016-02-19 20:53:30 +01008292 /* Call post initialisation function in safe environement. */
8293 if (!SET_SAFE_LJMP(gL.T)) {
8294 if (lua_type(gL.T, -1) == LUA_TSTRING)
8295 error = lua_tostring(gL.T, -1);
8296 else
8297 error = "critical error";
8298 fprintf(stderr, "Lua post-init: %s.\n", error);
8299 exit(1);
8300 }
Frédéric Lécaille54f2bcf2018-08-29 13:46:24 +02008301
8302#if USE_OPENSSL
8303 /* Initialize SSL server. */
8304 if (socket_ssl.xprt->prepare_srv) {
8305 int saved_used_backed = global.ssl_used_backend;
8306 // don't affect maxconn automatic computation
8307 socket_ssl.xprt->prepare_srv(&socket_ssl);
8308 global.ssl_used_backend = saved_used_backed;
8309 }
8310#endif
8311
Thierry Fournier3d4a6752016-02-19 20:53:30 +01008312 hlua_fcn_post_init(gL.T);
8313 RESET_SAFE_LJMP(gL.T);
8314
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008315 list_for_each_entry(init, &hlua_init_functions, l) {
8316 lua_rawgeti(gL.T, LUA_REGISTRYINDEX, init->function_ref);
8317 ret = hlua_ctx_resume(&gL, 0);
8318 switch (ret) {
8319 case HLUA_E_OK:
8320 lua_pop(gL.T, -1);
8321 return 1;
8322 case HLUA_E_AGAIN:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008323 ha_alert("Lua init: yield not allowed.\n");
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008324 return 0;
8325 case HLUA_E_ERRMSG:
8326 msg = lua_tostring(gL.T, -1);
Christopher Faulet767a84b2017-11-24 16:50:31 +01008327 ha_alert("lua init: %s.\n", msg);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008328 return 0;
8329 case HLUA_E_ERR:
8330 default:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008331 ha_alert("Lua init: unknown runtime error.\n");
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008332 return 0;
8333 }
8334 }
8335 return 1;
8336}
8337
Willy Tarreau32f61e22015-03-18 17:54:59 +01008338/* The memory allocator used by the Lua stack. <ud> is a pointer to the
8339 * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize>
8340 * is the previously allocated size or the kind of object in case of a new
8341 * allocation. <nsize> is the requested new size.
8342 */
8343static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
8344{
8345 struct hlua_mem_allocator *zone = ud;
8346
8347 if (nsize == 0) {
8348 /* it's a free */
8349 if (ptr)
8350 zone->allocated -= osize;
8351 free(ptr);
8352 return NULL;
8353 }
8354
8355 if (!ptr) {
8356 /* it's a new allocation */
8357 if (zone->limit && zone->allocated + nsize > zone->limit)
8358 return NULL;
8359
8360 ptr = malloc(nsize);
8361 if (ptr)
8362 zone->allocated += nsize;
8363 return ptr;
8364 }
8365
8366 /* it's a realloc */
8367 if (zone->limit && zone->allocated + nsize - osize > zone->limit)
8368 return NULL;
8369
8370 ptr = realloc(ptr, nsize);
8371 if (ptr)
8372 zone->allocated += nsize - osize;
8373 return ptr;
8374}
8375
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008376/* Ithis function can fail with an abort() due to an Lua critical error.
8377 * We are in the initialisation process of HAProxy, this abort() is
8378 * tolerated.
8379 */
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01008380void hlua_init(void)
8381{
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008382 int i;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008383 int idx;
8384 struct sample_fetch *sf;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008385 struct sample_conv *sc;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008386 char *p;
Thierry Fournierfd107a22016-02-19 19:57:23 +01008387 const char *error_msg;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008388#ifdef USE_OPENSSL
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008389 struct srv_kw *kw;
8390 int tmp_error;
8391 char *error;
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008392 char *args[] = { /* SSL client configuration. */
8393 "ssl",
8394 "verify",
8395 "none",
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008396 NULL
8397 };
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008398#endif
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008399
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008400 /* Init main lua stack. */
8401 gL.Mref = LUA_REFNIL;
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01008402 gL.flags = 0;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01008403 LIST_INIT(&gL.com);
Willy Tarreau42ef75f2017-04-12 21:40:29 +02008404 gL.T = lua_newstate(hlua_alloc, &hlua_global_allocator);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008405 hlua_sethlua(&gL);
8406 gL.Tref = LUA_REFNIL;
8407 gL.task = NULL;
8408
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08008409 /* From this point, until the end of the initialisation function,
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008410 * the Lua function can fail with an abort. We are in the initialisation
8411 * process of HAProxy, this abort() is tolerated.
8412 */
8413
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008414 /* Initialise lua. */
8415 luaL_openlibs(gL.T);
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008416
Thierry Fournier75933d42016-01-21 09:30:18 +01008417 /* Set safe environment for the initialisation. */
8418 if (!SET_SAFE_LJMP(gL.T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01008419 if (lua_type(gL.T, -1) == LUA_TSTRING)
8420 error_msg = lua_tostring(gL.T, -1);
8421 else
8422 error_msg = "critical error";
8423 fprintf(stderr, "Lua init: %s.\n", error_msg);
Thierry Fournier75933d42016-01-21 09:30:18 +01008424 exit(1);
8425 }
8426
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008427 /*
8428 *
8429 * Create "core" object.
8430 *
8431 */
8432
Thierry FOURNIERa2d8c652015-03-11 17:29:39 +01008433 /* This table entry is the object "core" base. */
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008434 lua_newtable(gL.T);
8435
8436 /* Push the loglevel constants. */
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008437 for (i = 0; i < NB_LOG_LEVELS; i++)
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008438 hlua_class_const_int(gL.T, log_levels[i], i);
8439
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008440 /* Register special functions. */
8441 hlua_class_function(gL.T, "register_init", hlua_register_init);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01008442 hlua_class_function(gL.T, "register_task", hlua_register_task);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01008443 hlua_class_function(gL.T, "register_fetches", hlua_register_fetches);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01008444 hlua_class_function(gL.T, "register_converters", hlua_register_converters);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02008445 hlua_class_function(gL.T, "register_action", hlua_register_action);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008446 hlua_class_function(gL.T, "register_service", hlua_register_service);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008447 hlua_class_function(gL.T, "register_cli", hlua_register_cli);
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01008448 hlua_class_function(gL.T, "yield", hlua_yield);
Willy Tarreau59551662015-03-10 14:23:13 +01008449 hlua_class_function(gL.T, "set_nice", hlua_set_nice);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01008450 hlua_class_function(gL.T, "sleep", hlua_sleep);
8451 hlua_class_function(gL.T, "msleep", hlua_msleep);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01008452 hlua_class_function(gL.T, "add_acl", hlua_add_acl);
8453 hlua_class_function(gL.T, "del_acl", hlua_del_acl);
8454 hlua_class_function(gL.T, "set_map", hlua_set_map);
8455 hlua_class_function(gL.T, "del_map", hlua_del_map);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008456 hlua_class_function(gL.T, "tcp", hlua_socket_new);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01008457 hlua_class_function(gL.T, "log", hlua_log);
8458 hlua_class_function(gL.T, "Debug", hlua_log_debug);
8459 hlua_class_function(gL.T, "Info", hlua_log_info);
8460 hlua_class_function(gL.T, "Warning", hlua_log_warning);
8461 hlua_class_function(gL.T, "Alert", hlua_log_alert);
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02008462 hlua_class_function(gL.T, "done", hlua_done);
Thierry Fournierfb0b5462016-01-21 09:28:58 +01008463 hlua_fcn_reg_core_fcn(gL.T);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008464
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008465 lua_setglobal(gL.T, "core");
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008466
8467 /*
8468 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008469 * Register class Map
8470 *
8471 */
8472
8473 /* This table entry is the object "Map" base. */
8474 lua_newtable(gL.T);
8475
8476 /* register pattern types. */
8477 for (i=0; i<PAT_MATCH_NUM; i++)
8478 hlua_class_const_int(gL.T, pat_match_names[i], i);
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01008479 for (i=0; i<PAT_MATCH_NUM; i++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008480 snprintf(trash.area, trash.size, "_%s", pat_match_names[i]);
8481 hlua_class_const_int(gL.T, trash.area, i);
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01008482 }
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008483
8484 /* register constructor. */
8485 hlua_class_function(gL.T, "new", hlua_map_new);
8486
8487 /* Create and fill the metatable. */
8488 lua_newtable(gL.T);
8489
8490 /* Create and fille the __index entry. */
8491 lua_pushstring(gL.T, "__index");
8492 lua_newtable(gL.T);
8493
8494 /* Register . */
8495 hlua_class_function(gL.T, "lookup", hlua_map_lookup);
8496 hlua_class_function(gL.T, "slookup", hlua_map_slookup);
8497
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008498 lua_rawset(gL.T, -3);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008499
Thierry Fournier45e78d72016-02-19 18:34:46 +01008500 /* Register previous table in the registry with reference and named entry.
8501 * The function hlua_register_metatable() pops the stack, so we
8502 * previously create a copy of the table.
8503 */
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008504 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008505 class_map_ref = hlua_register_metatable(gL.T, CLASS_MAP);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008506
8507 /* Assign the metatable to the mai Map object. */
8508 lua_setmetatable(gL.T, -2);
8509
8510 /* Set a name to the table. */
8511 lua_setglobal(gL.T, "Map");
8512
8513 /*
8514 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008515 * Register class Channel
8516 *
8517 */
8518
8519 /* Create and fill the metatable. */
8520 lua_newtable(gL.T);
8521
8522 /* Create and fille the __index entry. */
8523 lua_pushstring(gL.T, "__index");
8524 lua_newtable(gL.T);
8525
8526 /* Register . */
8527 hlua_class_function(gL.T, "get", hlua_channel_get);
8528 hlua_class_function(gL.T, "dup", hlua_channel_dup);
8529 hlua_class_function(gL.T, "getline", hlua_channel_getline);
8530 hlua_class_function(gL.T, "set", hlua_channel_set);
8531 hlua_class_function(gL.T, "append", hlua_channel_append);
8532 hlua_class_function(gL.T, "send", hlua_channel_send);
8533 hlua_class_function(gL.T, "forward", hlua_channel_forward);
8534 hlua_class_function(gL.T, "get_in_len", hlua_channel_get_in_len);
8535 hlua_class_function(gL.T, "get_out_len", hlua_channel_get_out_len);
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01008536 hlua_class_function(gL.T, "is_full", hlua_channel_is_full);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008537
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008538 lua_rawset(gL.T, -3);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008539
8540 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008541 class_channel_ref = hlua_register_metatable(gL.T, CLASS_CHANNEL);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008542
8543 /*
8544 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008545 * Register class Fetches
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008546 *
8547 */
8548
8549 /* Create and fill the metatable. */
8550 lua_newtable(gL.T);
8551
8552 /* Create and fille the __index entry. */
8553 lua_pushstring(gL.T, "__index");
8554 lua_newtable(gL.T);
8555
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008556 /* Browse existing fetches and create the associated
8557 * object method.
8558 */
8559 sf = NULL;
8560 while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) {
8561
8562 /* Dont register the keywork if the arguments check function are
8563 * not safe during the runtime.
8564 */
8565 if ((sf->val_args != NULL) &&
8566 (sf->val_args != val_payload_lv) &&
8567 (sf->val_args != val_hdr))
8568 continue;
8569
8570 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
8571 * by an underscore.
8572 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008573 strncpy(trash.area, sf->kw, trash.size);
8574 trash.area[trash.size - 1] = '\0';
8575 for (p = trash.area; *p; p++)
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008576 if (*p == '.' || *p == '-' || *p == '+')
8577 *p = '_';
8578
8579 /* Register the function. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008580 lua_pushstring(gL.T, trash.area);
Willy Tarreau2ec22742015-03-10 14:27:20 +01008581 lua_pushlightuserdata(gL.T, sf);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008582 lua_pushcclosure(gL.T, hlua_run_sample_fetch, 1);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008583 lua_rawset(gL.T, -3);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008584 }
8585
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008586 lua_rawset(gL.T, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008587
8588 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008589 class_fetches_ref = hlua_register_metatable(gL.T, CLASS_FETCHES);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008590
8591 /*
8592 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008593 * Register class Converters
8594 *
8595 */
8596
8597 /* Create and fill the metatable. */
8598 lua_newtable(gL.T);
8599
8600 /* Create and fill the __index entry. */
8601 lua_pushstring(gL.T, "__index");
8602 lua_newtable(gL.T);
8603
8604 /* Browse existing converters and create the associated
8605 * object method.
8606 */
8607 sc = NULL;
8608 while ((sc = sample_conv_getnext(sc, &idx)) != NULL) {
8609 /* Dont register the keywork if the arguments check function are
8610 * not safe during the runtime.
8611 */
8612 if (sc->val_args != NULL)
8613 continue;
8614
8615 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
8616 * by an underscore.
8617 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008618 strncpy(trash.area, sc->kw, trash.size);
8619 trash.area[trash.size - 1] = '\0';
8620 for (p = trash.area; *p; p++)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008621 if (*p == '.' || *p == '-' || *p == '+')
8622 *p = '_';
8623
8624 /* Register the function. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008625 lua_pushstring(gL.T, trash.area);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008626 lua_pushlightuserdata(gL.T, sc);
8627 lua_pushcclosure(gL.T, hlua_run_sample_conv, 1);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008628 lua_rawset(gL.T, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008629 }
8630
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008631 lua_rawset(gL.T, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008632
8633 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008634 class_converters_ref = hlua_register_metatable(gL.T, CLASS_CONVERTERS);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008635
8636 /*
8637 *
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008638 * Register class HTTP
8639 *
8640 */
8641
8642 /* Create and fill the metatable. */
8643 lua_newtable(gL.T);
8644
8645 /* Create and fille the __index entry. */
8646 lua_pushstring(gL.T, "__index");
8647 lua_newtable(gL.T);
8648
8649 /* Register Lua functions. */
8650 hlua_class_function(gL.T, "req_get_headers",hlua_http_req_get_headers);
8651 hlua_class_function(gL.T, "req_del_header", hlua_http_req_del_hdr);
8652 hlua_class_function(gL.T, "req_rep_header", hlua_http_req_rep_hdr);
8653 hlua_class_function(gL.T, "req_rep_value", hlua_http_req_rep_val);
8654 hlua_class_function(gL.T, "req_add_header", hlua_http_req_add_hdr);
8655 hlua_class_function(gL.T, "req_set_header", hlua_http_req_set_hdr);
8656 hlua_class_function(gL.T, "req_set_method", hlua_http_req_set_meth);
8657 hlua_class_function(gL.T, "req_set_path", hlua_http_req_set_path);
8658 hlua_class_function(gL.T, "req_set_query", hlua_http_req_set_query);
8659 hlua_class_function(gL.T, "req_set_uri", hlua_http_req_set_uri);
8660
8661 hlua_class_function(gL.T, "res_get_headers",hlua_http_res_get_headers);
8662 hlua_class_function(gL.T, "res_del_header", hlua_http_res_del_hdr);
8663 hlua_class_function(gL.T, "res_rep_header", hlua_http_res_rep_hdr);
8664 hlua_class_function(gL.T, "res_rep_value", hlua_http_res_rep_val);
8665 hlua_class_function(gL.T, "res_add_header", hlua_http_res_add_hdr);
8666 hlua_class_function(gL.T, "res_set_header", hlua_http_res_set_hdr);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02008667 hlua_class_function(gL.T, "res_set_status", hlua_http_res_set_status);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008668
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008669 lua_rawset(gL.T, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008670
8671 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008672 class_http_ref = hlua_register_metatable(gL.T, CLASS_HTTP);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008673
8674 /*
8675 *
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008676 * Register class AppletTCP
8677 *
8678 */
8679
8680 /* Create and fill the metatable. */
8681 lua_newtable(gL.T);
8682
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008683 /* Create and fille the __index entry. */
8684 lua_pushstring(gL.T, "__index");
8685 lua_newtable(gL.T);
8686
8687 /* Register Lua functions. */
Thierry FOURNIER / OZON.IO3e1d7912016-12-12 12:29:34 +01008688 hlua_class_function(gL.T, "getline", hlua_applet_tcp_getline);
8689 hlua_class_function(gL.T, "receive", hlua_applet_tcp_recv);
8690 hlua_class_function(gL.T, "send", hlua_applet_tcp_send);
8691 hlua_class_function(gL.T, "set_priv", hlua_applet_tcp_set_priv);
8692 hlua_class_function(gL.T, "get_priv", hlua_applet_tcp_get_priv);
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01008693 hlua_class_function(gL.T, "set_var", hlua_applet_tcp_set_var);
8694 hlua_class_function(gL.T, "unset_var", hlua_applet_tcp_unset_var);
8695 hlua_class_function(gL.T, "get_var", hlua_applet_tcp_get_var);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008696
8697 lua_settable(gL.T, -3);
8698
8699 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008700 class_applet_tcp_ref = hlua_register_metatable(gL.T, CLASS_APPLET_TCP);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008701
8702 /*
8703 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02008704 * Register class AppletHTTP
8705 *
8706 */
8707
8708 /* Create and fill the metatable. */
8709 lua_newtable(gL.T);
8710
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02008711 /* Create and fille the __index entry. */
8712 lua_pushstring(gL.T, "__index");
8713 lua_newtable(gL.T);
8714
8715 /* Register Lua functions. */
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01008716 hlua_class_function(gL.T, "set_priv", hlua_applet_http_set_priv);
8717 hlua_class_function(gL.T, "get_priv", hlua_applet_http_get_priv);
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01008718 hlua_class_function(gL.T, "set_var", hlua_applet_http_set_var);
8719 hlua_class_function(gL.T, "unset_var", hlua_applet_http_unset_var);
8720 hlua_class_function(gL.T, "get_var", hlua_applet_http_get_var);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02008721 hlua_class_function(gL.T, "getline", hlua_applet_http_getline);
8722 hlua_class_function(gL.T, "receive", hlua_applet_http_recv);
8723 hlua_class_function(gL.T, "send", hlua_applet_http_send);
8724 hlua_class_function(gL.T, "add_header", hlua_applet_http_addheader);
8725 hlua_class_function(gL.T, "set_status", hlua_applet_http_status);
8726 hlua_class_function(gL.T, "start_response", hlua_applet_http_start_response);
8727
8728 lua_settable(gL.T, -3);
8729
8730 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008731 class_applet_http_ref = hlua_register_metatable(gL.T, CLASS_APPLET_HTTP);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02008732
8733 /*
8734 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008735 * Register class TXN
8736 *
8737 */
8738
8739 /* Create and fill the metatable. */
8740 lua_newtable(gL.T);
8741
8742 /* Create and fille the __index entry. */
8743 lua_pushstring(gL.T, "__index");
8744 lua_newtable(gL.T);
8745
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01008746 /* Register Lua functions. */
Patrick Hemmer268a7072018-05-11 12:52:31 -04008747 hlua_class_function(gL.T, "set_priv", hlua_set_priv);
8748 hlua_class_function(gL.T, "get_priv", hlua_get_priv);
8749 hlua_class_function(gL.T, "set_var", hlua_set_var);
8750 hlua_class_function(gL.T, "unset_var", hlua_unset_var);
8751 hlua_class_function(gL.T, "get_var", hlua_get_var);
8752 hlua_class_function(gL.T, "done", hlua_txn_done);
8753 hlua_class_function(gL.T, "set_loglevel", hlua_txn_set_loglevel);
8754 hlua_class_function(gL.T, "set_tos", hlua_txn_set_tos);
8755 hlua_class_function(gL.T, "set_mark", hlua_txn_set_mark);
8756 hlua_class_function(gL.T, "set_priority_class", hlua_txn_set_priority_class);
8757 hlua_class_function(gL.T, "set_priority_offset", hlua_txn_set_priority_offset);
8758 hlua_class_function(gL.T, "deflog", hlua_txn_deflog);
8759 hlua_class_function(gL.T, "log", hlua_txn_log);
8760 hlua_class_function(gL.T, "Debug", hlua_txn_log_debug);
8761 hlua_class_function(gL.T, "Info", hlua_txn_log_info);
8762 hlua_class_function(gL.T, "Warning", hlua_txn_log_warning);
8763 hlua_class_function(gL.T, "Alert", hlua_txn_log_alert);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01008764
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008765 lua_rawset(gL.T, -3);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008766
8767 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008768 class_txn_ref = hlua_register_metatable(gL.T, CLASS_TXN);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008769
8770 /*
8771 *
8772 * Register class Socket
8773 *
8774 */
8775
8776 /* Create and fill the metatable. */
8777 lua_newtable(gL.T);
8778
8779 /* Create and fille the __index entry. */
8780 lua_pushstring(gL.T, "__index");
8781 lua_newtable(gL.T);
8782
Baptiste Assmann84bb4932015-03-02 21:40:06 +01008783#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008784 hlua_class_function(gL.T, "connect_ssl", hlua_socket_connect_ssl);
Baptiste Assmann84bb4932015-03-02 21:40:06 +01008785#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008786 hlua_class_function(gL.T, "connect", hlua_socket_connect);
8787 hlua_class_function(gL.T, "send", hlua_socket_send);
8788 hlua_class_function(gL.T, "receive", hlua_socket_receive);
8789 hlua_class_function(gL.T, "close", hlua_socket_close);
8790 hlua_class_function(gL.T, "getpeername", hlua_socket_getpeername);
8791 hlua_class_function(gL.T, "getsockname", hlua_socket_getsockname);
8792 hlua_class_function(gL.T, "setoption", hlua_socket_setoption);
8793 hlua_class_function(gL.T, "settimeout", hlua_socket_settimeout);
8794
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008795 lua_rawset(gL.T, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008796
8797 /* Register the garbage collector entry. */
8798 lua_pushstring(gL.T, "__gc");
8799 lua_pushcclosure(gL.T, hlua_socket_gc, 0);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008800 lua_rawset(gL.T, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008801
8802 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008803 class_socket_ref = hlua_register_metatable(gL.T, CLASS_SOCKET);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008804
8805 /* Proxy and server configuration initialisation. */
8806 memset(&socket_proxy, 0, sizeof(socket_proxy));
8807 init_new_proxy(&socket_proxy);
8808 socket_proxy.parent = NULL;
8809 socket_proxy.last_change = now.tv_sec;
8810 socket_proxy.id = "LUA-SOCKET";
8811 socket_proxy.cap = PR_CAP_FE | PR_CAP_BE;
8812 socket_proxy.maxconn = 0;
8813 socket_proxy.accept = NULL;
8814 socket_proxy.options2 |= PR_O2_INDEPSTR;
8815 socket_proxy.srv = NULL;
8816 socket_proxy.conn_retries = 0;
8817 socket_proxy.timeout.connect = 5000; /* By default the timeout connection is 5s. */
8818
8819 /* Init TCP server: unchanged parameters */
8820 memset(&socket_tcp, 0, sizeof(socket_tcp));
8821 socket_tcp.next = NULL;
8822 socket_tcp.proxy = &socket_proxy;
8823 socket_tcp.obj_type = OBJ_TYPE_SERVER;
8824 LIST_INIT(&socket_tcp.actconns);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04008825 socket_tcp.pendconns = EB_ROOT;
Christopher Faulet40a007c2017-07-03 15:41:01 +02008826 socket_tcp.priv_conns = NULL;
8827 socket_tcp.idle_conns = NULL;
8828 socket_tcp.safe_conns = NULL;
Emeric Brun52a91d32017-08-31 14:41:55 +02008829 socket_tcp.next_state = SRV_ST_RUNNING; /* early server setup */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008830 socket_tcp.last_change = 0;
8831 socket_tcp.id = "LUA-TCP-CONN";
8832 socket_tcp.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8833 socket_tcp.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8834 socket_tcp.pp_opts = 0; /* Remove proxy protocol. */
8835
8836 /* XXX: Copy default parameter from default server,
8837 * but the default server is not initialized.
8838 */
8839 socket_tcp.maxqueue = socket_proxy.defsrv.maxqueue;
8840 socket_tcp.minconn = socket_proxy.defsrv.minconn;
8841 socket_tcp.maxconn = socket_proxy.defsrv.maxconn;
8842 socket_tcp.slowstart = socket_proxy.defsrv.slowstart;
8843 socket_tcp.onerror = socket_proxy.defsrv.onerror;
8844 socket_tcp.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
8845 socket_tcp.onmarkedup = socket_proxy.defsrv.onmarkedup;
8846 socket_tcp.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
8847 socket_tcp.uweight = socket_proxy.defsrv.iweight;
8848 socket_tcp.iweight = socket_proxy.defsrv.iweight;
8849
8850 socket_tcp.check.status = HCHK_STATUS_INI;
8851 socket_tcp.check.rise = socket_proxy.defsrv.check.rise;
8852 socket_tcp.check.fall = socket_proxy.defsrv.check.fall;
8853 socket_tcp.check.health = socket_tcp.check.rise; /* socket, but will fall down at first failure */
8854 socket_tcp.check.server = &socket_tcp;
8855
8856 socket_tcp.agent.status = HCHK_STATUS_INI;
8857 socket_tcp.agent.rise = socket_proxy.defsrv.agent.rise;
8858 socket_tcp.agent.fall = socket_proxy.defsrv.agent.fall;
8859 socket_tcp.agent.health = socket_tcp.agent.rise; /* socket, but will fall down at first failure */
8860 socket_tcp.agent.server = &socket_tcp;
8861
Willy Tarreaua261e9b2016-12-22 20:44:00 +01008862 socket_tcp.xprt = xprt_get(XPRT_RAW);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008863
8864#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008865 /* Init TCP server: unchanged parameters */
8866 memset(&socket_ssl, 0, sizeof(socket_ssl));
8867 socket_ssl.next = NULL;
8868 socket_ssl.proxy = &socket_proxy;
8869 socket_ssl.obj_type = OBJ_TYPE_SERVER;
8870 LIST_INIT(&socket_ssl.actconns);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04008871 socket_ssl.pendconns = EB_ROOT;
Christopher Faulet40a007c2017-07-03 15:41:01 +02008872 socket_tcp.priv_conns = NULL;
8873 socket_tcp.idle_conns = NULL;
8874 socket_tcp.safe_conns = NULL;
Emeric Brun52a91d32017-08-31 14:41:55 +02008875 socket_ssl.next_state = SRV_ST_RUNNING; /* early server setup */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008876 socket_ssl.last_change = 0;
8877 socket_ssl.id = "LUA-SSL-CONN";
8878 socket_ssl.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8879 socket_ssl.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8880 socket_ssl.pp_opts = 0; /* Remove proxy protocol. */
8881
8882 /* XXX: Copy default parameter from default server,
8883 * but the default server is not initialized.
8884 */
8885 socket_ssl.maxqueue = socket_proxy.defsrv.maxqueue;
8886 socket_ssl.minconn = socket_proxy.defsrv.minconn;
8887 socket_ssl.maxconn = socket_proxy.defsrv.maxconn;
8888 socket_ssl.slowstart = socket_proxy.defsrv.slowstart;
8889 socket_ssl.onerror = socket_proxy.defsrv.onerror;
8890 socket_ssl.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
8891 socket_ssl.onmarkedup = socket_proxy.defsrv.onmarkedup;
8892 socket_ssl.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
8893 socket_ssl.uweight = socket_proxy.defsrv.iweight;
8894 socket_ssl.iweight = socket_proxy.defsrv.iweight;
8895
8896 socket_ssl.check.status = HCHK_STATUS_INI;
8897 socket_ssl.check.rise = socket_proxy.defsrv.check.rise;
8898 socket_ssl.check.fall = socket_proxy.defsrv.check.fall;
8899 socket_ssl.check.health = socket_ssl.check.rise; /* socket, but will fall down at first failure */
8900 socket_ssl.check.server = &socket_ssl;
8901
8902 socket_ssl.agent.status = HCHK_STATUS_INI;
8903 socket_ssl.agent.rise = socket_proxy.defsrv.agent.rise;
8904 socket_ssl.agent.fall = socket_proxy.defsrv.agent.fall;
8905 socket_ssl.agent.health = socket_ssl.agent.rise; /* socket, but will fall down at first failure */
8906 socket_ssl.agent.server = &socket_ssl;
8907
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008908 socket_ssl.use_ssl = 1;
Willy Tarreaua261e9b2016-12-22 20:44:00 +01008909 socket_ssl.xprt = xprt_get(XPRT_SSL);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008910
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008911 for (idx = 0; args[idx] != NULL; idx++) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008912 if ((kw = srv_find_kw(args[idx])) != NULL) { /* Maybe it's registered server keyword */
8913 /*
8914 *
8915 * If the keyword is not known, we can search in the registered
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08008916 * server keywords. This is useful to configure special SSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008917 * features like client certificates and ssl_verify.
8918 *
8919 */
8920 tmp_error = kw->parse(args, &idx, &socket_proxy, &socket_ssl, &error);
8921 if (tmp_error != 0) {
8922 fprintf(stderr, "INTERNAL ERROR: %s\n", error);
8923 abort(); /* This must be never arrives because the command line
8924 not editable by the user. */
8925 }
8926 idx += kw->skip;
8927 }
8928 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008929#endif
Thierry Fournier75933d42016-01-21 09:30:18 +01008930
8931 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01008932}
Willy Tarreaubb57d942016-12-21 19:04:56 +01008933
Willy Tarreau80713382018-11-26 10:19:54 +01008934static void hlua_register_build_options(void)
8935{
Willy Tarreaubb57d942016-12-21 19:04:56 +01008936 char *ptr = NULL;
Willy Tarreau80713382018-11-26 10:19:54 +01008937
Willy Tarreaubb57d942016-12-21 19:04:56 +01008938 memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE);
8939 hap_register_build_opts(ptr, 1);
8940}
Willy Tarreau80713382018-11-26 10:19:54 +01008941
8942INITCALL0(STG_REGISTER, hlua_register_build_options);