blob: a3886cfaae274a2fc54cabbe9ca14361ad224dcb [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)))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01001641 si_update(si);
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001642
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);
Christopher Fauletcc26b132018-12-18 21:20:57 +01004236
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004237 while (count && !stop && blk) {
4238 enum htx_blk_type type = htx_get_blk_type(blk);
4239 uint32_t sz = htx_get_blksz(blk);
4240 struct ist v;
4241 uint32_t vlen;
4242 char *nl;
4243
Christopher Faulete461e342018-12-18 16:43:35 +01004244 if (type == HTX_BLK_EOM) {
4245 stop = 1;
4246 break;
4247 }
4248
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004249 vlen = sz;
4250 if (vlen > count) {
4251 if (type != HTX_BLK_DATA)
4252 break;
4253 vlen = count;
4254 }
4255
4256 switch (type) {
4257 case HTX_BLK_UNUSED:
4258 break;
4259
4260 case HTX_BLK_DATA:
4261 v = htx_get_blk_value(htx, blk);
4262 v.len = vlen;
4263 nl = istchr(v, '\n');
4264 if (nl != NULL) {
4265 stop = 1;
4266 vlen = nl - v.ptr + 1;
4267 }
4268 luaL_addlstring(&appctx->b, v.ptr, vlen);
4269 break;
4270
4271 case HTX_BLK_EOD:
4272 case HTX_BLK_TLR:
4273 case HTX_BLK_EOM:
4274 stop = 1;
4275 break;
4276
4277 default:
4278 break;
4279 }
4280
4281 co_set_data(req, co_data(req) - vlen);
4282 count -= vlen;
4283 if (sz == vlen)
4284 blk = htx_remove_blk(htx, blk);
4285 else {
4286 htx_cut_data_blk(htx, blk, vlen);
4287 break;
4288 }
4289 }
4290
4291 if (!stop) {
4292 si_cant_get(si);
4293 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_htx_getline_yield, TICK_ETERNITY, 0));
4294 }
4295
4296 /* return the result. */
4297 luaL_pushresult(&appctx->b);
4298 return 1;
4299}
4300
4301
4302/* If expected data not yet available, it returns a yield. This function
4303 * consumes the data in the buffer. It returns a string containing the
4304 * data. This string can be empty.
4305 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004306__LJMP static int hlua_applet_http_getline_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004307{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004308 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4309 struct stream_interface *si = appctx->appctx->owner;
4310 struct channel *chn = si_ic(si);
4311 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02004312 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02004313 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02004314 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02004315 size_t len2;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004316
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004317 /* Maybe we cant send a 100-continue ? */
4318 if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_100C) {
Willy Tarreau04f1e2d2018-09-10 18:04:24 +02004319 ret = ci_putblk(chn, HTTP_100.ptr, HTTP_100.len);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004320 /* if ret == -2 or -3 the channel closed or the message si too
4321 * big for the buffers. We cant send anything. So, we ignoring
4322 * the error, considers that the 100-continue is sent, and try
4323 * to receive.
4324 * If ret is -1, we dont have room in the buffer, so we yield.
4325 */
4326 if (ret == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01004327 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004328 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_getline_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004329 }
4330 appctx->appctx->ctx.hlua_apphttp.flags &= ~APPLET_100C;
4331 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004332
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004333 /* Check for the end of the data. */
4334 if (appctx->appctx->ctx.hlua_apphttp.left_bytes <= 0) {
4335 luaL_pushresult(&appctx->b);
4336 return 1;
4337 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004338
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004339 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004340 ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004341
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004342 /* Data not yet available. return yield. */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004343 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01004344 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004345 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_getline_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004346 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004347
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004348 /* End of data: commit the total strings and return. */
4349 if (ret < 0) {
4350 luaL_pushresult(&appctx->b);
4351 return 1;
4352 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004353
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004354 /* Ensure that the block 2 length is usable. */
4355 if (ret == 1)
4356 len2 = 0;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004357
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004358 /* Copy the fisrt block caping to the length required. */
4359 if (len1 > appctx->appctx->ctx.hlua_apphttp.left_bytes)
4360 len1 = appctx->appctx->ctx.hlua_apphttp.left_bytes;
4361 luaL_addlstring(&appctx->b, blk1, len1);
4362 appctx->appctx->ctx.hlua_apphttp.left_bytes -= len1;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004363
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004364 /* Copy the second block. */
4365 if (len2 > appctx->appctx->ctx.hlua_apphttp.left_bytes)
4366 len2 = appctx->appctx->ctx.hlua_apphttp.left_bytes;
4367 luaL_addlstring(&appctx->b, blk2, len2);
4368 appctx->appctx->ctx.hlua_apphttp.left_bytes -= len2;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004369
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004370 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004371 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004372 luaL_pushresult(&appctx->b);
4373 return 1;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004374}
4375
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004376/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004377__LJMP static int hlua_applet_http_getline(lua_State *L)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004378{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004379 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004380
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004381 /* Initialise the string catenation. */
4382 luaL_buffinit(L, &appctx->b);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004383
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004384 if (IS_HTX_STRM(si_strm(appctx->appctx->owner)))
4385 return MAY_LJMP(hlua_applet_htx_getline_yield(L, 0, 0));
4386 else
4387 return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004388}
4389
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004390/* If expected data not yet available, it returns a yield. This function
4391 * consumes the data in the buffer. It returns a string containing the
4392 * data. This string can be empty.
4393 */
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004394__LJMP static int hlua_applet_htx_recv_yield(lua_State *L, int status, lua_KContext ctx)
4395{
4396 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4397 struct stream_interface *si = appctx->appctx->owner;
4398 struct channel *req = si_oc(si);
4399 struct htx *htx;
4400 struct htx_blk *blk;
4401 size_t count;
4402 int len;
4403
4404 /* Maybe we cant send a 100-continue ? */
4405 if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_100C)
4406 MAY_LJMP(hlua_applet_htx_reply_100_continue(L));
4407
4408 htx = htx_from_buf(&req->buf);
4409 len = MAY_LJMP(luaL_checkinteger(L, 2));
4410 count = co_data(req);
4411 blk = htx_get_head_blk(htx);
4412 while (count && len && blk) {
4413 enum htx_blk_type type = htx_get_blk_type(blk);
4414 uint32_t sz = htx_get_blksz(blk);
4415 struct ist v;
4416 uint32_t vlen;
4417
Christopher Faulete461e342018-12-18 16:43:35 +01004418 if (type == HTX_BLK_EOM) {
4419 len = 0;
4420 break;
4421 }
4422
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004423 vlen = sz;
4424 if (len > 0 && vlen > len)
4425 vlen = len;
4426 if (vlen > count) {
4427 if (type != HTX_BLK_DATA)
4428 break;
4429 vlen = count;
4430 }
4431
4432 switch (type) {
4433 case HTX_BLK_UNUSED:
4434 break;
4435
4436 case HTX_BLK_DATA:
4437 v = htx_get_blk_value(htx, blk);
4438 luaL_addlstring(&appctx->b, v.ptr, vlen);
4439 break;
4440
4441 case HTX_BLK_EOD:
4442 case HTX_BLK_TLR:
4443 case HTX_BLK_EOM:
4444 len = 0;
4445 break;
4446
4447 default:
4448 break;
4449 }
4450
4451 co_set_data(req, co_data(req) - vlen);
4452 count -= vlen;
4453 if (len > 0)
4454 len -= vlen;
4455 if (sz == vlen)
4456 blk = htx_remove_blk(htx, blk);
4457 else {
4458 htx_cut_data_blk(htx, blk, vlen);
4459 break;
4460 }
4461 }
4462
4463 /* If we are no other data available, yield waiting for new data. */
4464 if (len) {
4465 if (len > 0) {
4466 lua_pushinteger(L, len);
4467 lua_replace(L, 2);
4468 }
4469 si_cant_get(si);
4470 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_htx_recv_yield, TICK_ETERNITY, 0));
4471 }
4472
4473 /* return the result. */
4474 luaL_pushresult(&appctx->b);
4475 return 1;
4476}
4477
4478/* If expected data not yet available, it returns a yield. This function
4479 * consumes the data in the buffer. It returns a string containing the
4480 * data. This string can be empty.
4481 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004482__LJMP static int hlua_applet_http_recv_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004483{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004484 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4485 struct stream_interface *si = appctx->appctx->owner;
4486 int len = MAY_LJMP(luaL_checkinteger(L, 2));
4487 struct channel *chn = si_ic(si);
4488 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02004489 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02004490 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02004491 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02004492 size_t len2;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004493
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004494 /* Maybe we cant send a 100-continue ? */
4495 if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_100C) {
Willy Tarreau04f1e2d2018-09-10 18:04:24 +02004496 ret = ci_putblk(chn, HTTP_100.ptr, HTTP_100.len);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004497 /* if ret == -2 or -3 the channel closed or the message si too
4498 * big for the buffers. We cant send anything. So, we ignoring
4499 * the error, considers that the 100-continue is sent, and try
4500 * to receive.
4501 * If ret is -1, we dont have room in the buffer, so we yield.
4502 */
4503 if (ret == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01004504 si_rx_room_blk(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 appctx->appctx->ctx.hlua_apphttp.flags &= ~APPLET_100C;
4508 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004509
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004510 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004511 ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004512
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004513 /* Data not yet available. return yield. */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004514 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01004515 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004516 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004517 }
4518
4519 /* End of data: commit the total strings and return. */
4520 if (ret < 0) {
4521 luaL_pushresult(&appctx->b);
4522 return 1;
4523 }
4524
4525 /* Ensure that the block 2 length is usable. */
4526 if (ret == 1)
4527 len2 = 0;
4528
4529 /* Copy the fisrt block caping to the length required. */
4530 if (len1 > len)
4531 len1 = len;
4532 luaL_addlstring(&appctx->b, blk1, len1);
4533 len -= len1;
4534
4535 /* Copy the second block. */
4536 if (len2 > len)
4537 len2 = len;
4538 luaL_addlstring(&appctx->b, blk2, len2);
4539 len -= len2;
4540
4541 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004542 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004543 if (appctx->appctx->ctx.hlua_apphttp.left_bytes != -1)
4544 appctx->appctx->ctx.hlua_apphttp.left_bytes -= len;
4545
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004546 /* If we are no other data available, yield waiting for new data. */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004547 if (len > 0) {
4548 lua_pushinteger(L, len);
4549 lua_replace(L, 2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01004550 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004551 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004552 }
4553
4554 /* return the result. */
4555 luaL_pushresult(&appctx->b);
4556 return 1;
4557}
4558
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004559/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004560__LJMP static int hlua_applet_http_recv(lua_State *L)
4561{
4562 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4563 int len = -1;
4564
4565 /* Check arguments. */
4566 if (lua_gettop(L) > 2)
4567 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
4568 if (lua_gettop(L) >= 2) {
4569 len = MAY_LJMP(luaL_checkinteger(L, 2));
4570 lua_pop(L, 1);
4571 }
4572
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004573 if (IS_HTX_STRM(si_strm(appctx->appctx->owner))) {
4574 /* HTX version */
4575 lua_pushinteger(L, len);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004576
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004577 /* Initialise the string catenation. */
4578 luaL_buffinit(L, &appctx->b);
4579
4580 return MAY_LJMP(hlua_applet_htx_recv_yield(L, 0, 0));
4581 }
4582 else {
4583 /* Legacy HTTP version */
4584 /* Check the required length */
4585 if (len == -1 || len > appctx->appctx->ctx.hlua_apphttp.left_bytes)
4586 len = appctx->appctx->ctx.hlua_apphttp.left_bytes;
4587 lua_pushinteger(L, len);
4588
4589 /* Initialise the string catenation. */
4590 luaL_buffinit(L, &appctx->b);
4591
4592 return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0));
4593 }
4594}
4595
4596/* Append data in the output side of the buffer. This data is immediately
4597 * sent. The function returns the amount of data written. If the buffer
4598 * cannot contain the data, the function yields. The function returns -1
4599 * if the channel is closed.
4600 */
4601__LJMP static int hlua_applet_htx_send_yield(lua_State *L, int status, lua_KContext ctx)
4602{
4603 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4604 struct stream_interface *si = appctx->appctx->owner;
4605 struct channel *res = si_ic(si);
4606 struct htx *htx = htx_from_buf(&res->buf);
4607 const char *data;
4608 size_t len;
4609 int l = MAY_LJMP(luaL_checkinteger(L, 3));
4610 int max;
4611
4612 max = htx_free_space(htx);
4613 if (channel_recv_limit(res) < b_size(&res->buf)) {
4614 if (max < global.tune.maxrewrite) {
4615 si_rx_room_blk(si);
4616 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_htx_send_yield, TICK_ETERNITY, 0));
4617 }
4618 max -= global.tune.maxrewrite;
4619 }
4620
4621 data = MAY_LJMP(luaL_checklstring(L, 2, &len));
4622
4623 /* Get the max amount of data which can write as input in the channel. */
4624 if (max > (len - l))
4625 max = len - l;
4626
4627 /* Copy data. */
4628 htx_add_data(htx, ist2(data + l, max));
4629 res->total += l;
4630 res->flags |= CF_READ_PARTIAL;
4631 htx_to_buf(htx, &res->buf);
4632
4633 /* update counters. */
4634 l += max;
4635 lua_pop(L, 1);
4636 lua_pushinteger(L, l);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004637
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004638 /* If some data is not send, declares the situation to the
4639 * applet, and returns a yield.
4640 */
4641 if (l < len) {
4642 si_rx_room_blk(si);
4643 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_htx_send_yield, TICK_ETERNITY, 0));
4644 }
4645
4646 return 1;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004647}
4648
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004649/* Append data in the output side of the buffer. This data is immediately
4650 * sent. The function returns the amount of data written. If the buffer
4651 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004652 * if the channel is closed.
4653 */
4654__LJMP static int hlua_applet_http_send_yield(lua_State *L, int status, lua_KContext ctx)
4655{
4656 size_t len;
4657 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4658 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
4659 int l = MAY_LJMP(luaL_checkinteger(L, 3));
4660 struct stream_interface *si = appctx->appctx->owner;
4661 struct channel *chn = si_ic(si);
4662 int max;
4663
4664 /* Get the max amount of data which can write as input in the channel. */
4665 max = channel_recv_max(chn);
4666 if (max > (len - l))
4667 max = len - l;
4668
4669 /* Copy data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004670 ci_putblk(chn, str + l, max);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004671
4672 /* update counters. */
4673 l += max;
4674 lua_pop(L, 1);
4675 lua_pushinteger(L, l);
4676
4677 /* If some data is not send, declares the situation to the
4678 * applet, and returns a yield.
4679 */
4680 if (l < len) {
Willy Tarreaudb398432018-11-15 11:08:52 +01004681 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004682 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004683 }
4684
4685 return 1;
4686}
4687
4688/* Just a wraper of "hlua_applet_send_yield". This wrapper permits
4689 * yield the LUA process, and resume it without checking the
4690 * input arguments.
4691 */
4692__LJMP static int hlua_applet_http_send(lua_State *L)
4693{
4694 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004695
4696 /* We want to send some data. Headers must be sent. */
4697 if (!(appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
4698 hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data.");
4699 WILL_LJMP(lua_error(L));
4700 }
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004701
4702 if (IS_HTX_STRM(si_strm(appctx->appctx->owner))) {
4703 /* HTX version */
4704 /* This interger is used for followinf the amount of data sent. */
4705 lua_pushinteger(L, 0);
4706
4707 return MAY_LJMP(hlua_applet_htx_send_yield(L, 0, 0));
4708 }
4709 else {
4710 /* Legacy HTTP version */
4711 size_t len;
4712 char hex[10];
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004713
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004714 MAY_LJMP(luaL_checklstring(L, 2, &len));
4715
4716 /* If transfer encoding chunked is selected, we surround the data
4717 * by chunk data.
4718 */
4719 if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_CHUNKED) {
4720 snprintf(hex, 9, "%x", (unsigned int)len);
4721 lua_pushfstring(L, "%s\r\n", hex);
4722 lua_insert(L, 2); /* swap the last 2 entries. */
4723 lua_pushstring(L, "\r\n");
4724 lua_concat(L, 3);
4725 }
4726
4727 /* This interger is used for followinf the amount of data sent. */
4728 lua_pushinteger(L, 0);
4729
4730 return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0));
4731 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004732}
4733
4734__LJMP static int hlua_applet_http_addheader(lua_State *L)
4735{
4736 const char *name;
4737 int ret;
4738
4739 MAY_LJMP(hlua_checkapplet_http(L, 1));
4740 name = MAY_LJMP(luaL_checkstring(L, 2));
4741 MAY_LJMP(luaL_checkstring(L, 3));
4742
4743 /* Push in the stack the "response" entry. */
4744 ret = lua_getfield(L, 1, "response");
4745 if (ret != LUA_TTABLE) {
4746 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] "
4747 "is expected as an array. %s found", lua_typename(L, ret));
4748 WILL_LJMP(lua_error(L));
4749 }
4750
4751 /* check if the header is already registered if it is not
4752 * the case, register it.
4753 */
4754 ret = lua_getfield(L, -1, name);
4755 if (ret == LUA_TNIL) {
4756
4757 /* Entry not found. */
4758 lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */
4759
4760 /* Insert the new header name in the array in the top of the stack.
4761 * It left the new array in the top of the stack.
4762 */
4763 lua_newtable(L);
4764 lua_pushvalue(L, 2);
4765 lua_pushvalue(L, -2);
4766 lua_settable(L, -4);
4767
4768 } else if (ret != LUA_TTABLE) {
4769
4770 /* corruption error. */
4771 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] "
4772 "is expected as an array. %s found", name, lua_typename(L, ret));
4773 WILL_LJMP(lua_error(L));
4774 }
4775
4776 /* Now the top od thestack is an array of values. We push
4777 * the header value as new entry.
4778 */
4779 lua_pushvalue(L, 3);
4780 ret = lua_rawlen(L, -2);
4781 lua_rawseti(L, -2, ret + 1);
4782 lua_pushboolean(L, 1);
4783 return 1;
4784}
4785
4786__LJMP static int hlua_applet_http_status(lua_State *L)
4787{
4788 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4789 int status = MAY_LJMP(luaL_checkinteger(L, 2));
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004790 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004791
4792 if (status < 100 || status > 599) {
4793 lua_pushboolean(L, 0);
4794 return 1;
4795 }
4796
4797 appctx->appctx->ctx.hlua_apphttp.status = status;
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004798 appctx->appctx->ctx.hlua_apphttp.reason = reason;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004799 lua_pushboolean(L, 1);
4800 return 1;
4801}
4802
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004803
4804__LJMP static int hlua_applet_htx_send_response(lua_State *L)
4805{
4806 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4807 struct stream_interface *si = appctx->appctx->owner;
4808 struct channel *res = si_ic(si);
4809 struct htx *htx;
4810 struct htx_sl *sl;
4811 struct h1m h1m;
4812 const char *status, *reason;
4813 const char *name, *value;
4814 size_t nlen, vlen;
4815 unsigned int flags;
4816
4817 /* Send the message at once. */
4818 htx = htx_from_buf(&res->buf);
4819 h1m_init_res(&h1m);
4820
4821 /* Use the same http version than the request. */
4822 status = ultoa_r(appctx->appctx->ctx.hlua_apphttp.status, trash.area, trash.size);
4823 reason = appctx->appctx->ctx.hlua_apphttp.reason;
4824 if (reason == NULL)
4825 reason = http_get_reason(appctx->appctx->ctx.hlua_apphttp.status);
4826 if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) {
4827 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11);
4828 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason));
4829 }
4830 else {
4831 flags = HTX_SL_F_IS_RESP;
4832 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason));
4833 }
4834 if (!sl) {
4835 hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n",
4836 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4837 WILL_LJMP(lua_error(L));
4838 }
4839 sl->info.res.status = appctx->appctx->ctx.hlua_apphttp.status;
4840
4841 /* Get the array associated to the field "response" in the object AppletHTTP. */
4842 lua_pushvalue(L, 0);
4843 if (lua_getfield(L, 1, "response") != LUA_TTABLE) {
4844 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n",
4845 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4846 WILL_LJMP(lua_error(L));
4847 }
4848
4849 /* Browse the list of headers. */
4850 lua_pushnil(L);
4851 while(lua_next(L, -2) != 0) {
4852 /* We expect a string as -2. */
4853 if (lua_type(L, -2) != LUA_TSTRING) {
4854 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n",
4855 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4856 lua_typename(L, lua_type(L, -2)));
4857 WILL_LJMP(lua_error(L));
4858 }
4859 name = lua_tolstring(L, -2, &nlen);
4860
4861 /* We expect an array as -1. */
4862 if (lua_type(L, -1) != LUA_TTABLE) {
4863 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n",
4864 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4865 name,
4866 lua_typename(L, lua_type(L, -1)));
4867 WILL_LJMP(lua_error(L));
4868 }
4869
4870 /* Browse the table who is on the top of the stack. */
4871 lua_pushnil(L);
4872 while(lua_next(L, -2) != 0) {
4873 int id;
4874
4875 /* We expect a number as -2. */
4876 if (lua_type(L, -2) != LUA_TNUMBER) {
4877 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n",
4878 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4879 name,
4880 lua_typename(L, lua_type(L, -2)));
4881 WILL_LJMP(lua_error(L));
4882 }
4883 id = lua_tointeger(L, -2);
4884
4885 /* We expect a string as -2. */
4886 if (lua_type(L, -1) != LUA_TSTRING) {
4887 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n",
4888 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4889 name, id,
4890 lua_typename(L, lua_type(L, -1)));
4891 WILL_LJMP(lua_error(L));
4892 }
4893 value = lua_tolstring(L, -1, &vlen);
4894
4895 /* Simple Protocol checks. */
4896 if (isteqi(ist2(name, nlen), ist("transfer-encoding")))
4897 h1_parse_xfer_enc_header(&h1m, ist2(name, nlen));
4898 else if (isteqi(ist2(name, nlen), ist("content-length"))) {
4899 struct ist v = ist2(value, vlen);
4900 int ret;
4901
4902 ret = h1_parse_cont_len_header(&h1m, &v);
4903 if (ret < 0) {
4904 hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n",
4905 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4906 name);
4907 WILL_LJMP(lua_error(L));
4908 }
4909 else if (ret == 0)
4910 goto next; /* Skip it */
4911 }
4912
4913 /* Add a new header */
4914 if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) {
4915 hlua_pusherror(L, "Lua applet http '%s': Failed to add header '%s' in the response.\n",
4916 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4917 name);
4918 WILL_LJMP(lua_error(L));
4919 }
4920 next:
4921 /* Remove the array from the stack, and get next element with a remaining string. */
4922 lua_pop(L, 1);
4923 }
4924
4925 /* Remove the array from the stack, and get next element with a remaining string. */
4926 lua_pop(L, 1);
4927 }
4928
4929 if (h1m.flags & H1_MF_CHNK)
4930 h1m.flags &= ~H1_MF_CLEN;
4931 if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK))
4932 h1m.flags |= H1_MF_XFER_LEN;
4933
4934 /* Uset HTX start-line flags */
4935 if (h1m.flags & H1_MF_XFER_ENC)
4936 flags |= HTX_SL_F_XFER_ENC;
4937 if (h1m.flags & H1_MF_XFER_LEN) {
4938 flags |= HTX_SL_F_XFER_LEN;
4939 if (h1m.flags & H1_MF_CHNK)
4940 flags |= HTX_SL_F_CHNK;
4941 else if (h1m.flags & H1_MF_CLEN)
4942 flags |= HTX_SL_F_CLEN;
4943 if (h1m.body_len == 0)
4944 flags |= HTX_SL_F_BODYLESS;
4945 }
4946 sl->flags |= flags;
4947
4948 /* If we dont have a content-length set, and the HTTP version is 1.1
4949 * and the status code implies the presence of a message body, we must
4950 * announce a transfer encoding chunked. This is required by haproxy
4951 * for the keepalive compliance. If the applet annouces a transfer-encoding
4952 * chunked itslef, don't do anything.
4953 */
4954 if ((flags & (HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN)) == HTX_SL_F_VER_11 &&
4955 appctx->appctx->ctx.hlua_apphttp.status >= 200 &&
4956 appctx->appctx->ctx.hlua_apphttp.status != 204 &&
4957 appctx->appctx->ctx.hlua_apphttp.status != 304) {
4958 /* Add a new header */
4959 sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN);
4960 if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) {
4961 hlua_pusherror(L, "Lua applet http '%s': Failed to add header 'transfer-encoding' in the response.\n",
4962 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4963 WILL_LJMP(lua_error(L));
4964 }
4965 }
4966
4967 /* Finalize headers. */
4968 if (!htx_add_endof(htx, HTX_BLK_EOH)) {
4969 hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n",
4970 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4971 WILL_LJMP(lua_error(L));
4972 }
4973
4974 if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) {
4975 b_reset(&res->buf);
4976 hlua_pusherror(L, "Lua: 'start_response': response header block too big");
4977 WILL_LJMP(lua_error(L));
4978 }
4979
4980 htx_to_buf(htx, &res->buf);
4981 res->total += htx->data;
4982 res->flags |= CF_READ_PARTIAL;
4983
4984 /* Headers sent, set the flag. */
4985 appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT;
4986 return 0;
4987
4988}
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004989/* We will build the status line and the headers of the HTTP response.
4990 * We will try send at once if its not possible, we give back the hand
4991 * waiting for more room.
4992 */
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004993__LJMP static int hlua_applet_htx_start_response_yield(lua_State *L, int status, lua_KContext ctx)
4994{
4995 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4996 struct stream_interface *si = appctx->appctx->owner;
4997 struct channel *res = si_ic(si);
4998
4999 if (co_data(res)) {
5000 si_rx_room_blk(si);
5001 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_htx_start_response_yield, TICK_ETERNITY, 0));
5002 }
5003 return MAY_LJMP(hlua_applet_htx_send_response(L));
5004}
5005
5006
5007__LJMP static int hlua_applet_htx_start_response(lua_State *L)
5008{
5009 return MAY_LJMP(hlua_applet_htx_start_response_yield(L, 0, 0));
5010}
5011
5012/* We will build the status line and the headers of the HTTP response.
5013 * We will try send at once if its not possible, we give back the hand
5014 * waiting for more room.
5015 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005016__LJMP static int hlua_applet_http_start_response_yield(lua_State *L, int status, lua_KContext ctx)
5017{
5018 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
5019 struct stream_interface *si = appctx->appctx->owner;
5020 struct channel *chn = si_ic(si);
5021 int ret;
5022 size_t len;
5023 const char *msg;
5024
5025 /* Get the message as the first argument on the stack. */
5026 msg = MAY_LJMP(luaL_checklstring(L, 2, &len));
5027
5028 /* Send the message at once. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02005029 ret = ci_putblk(chn, msg, len);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005030
5031 /* if ret == -2 or -3 the channel closed or the message si too
5032 * big for the buffers.
5033 */
5034 if (ret == -2 || ret == -3) {
5035 hlua_pusherror(L, "Lua: 'start_response': response header block too big");
5036 WILL_LJMP(lua_error(L));
5037 }
5038
5039 /* If ret is -1, we dont have room in the buffer, so we yield. */
5040 if (ret == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01005041 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02005042 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_start_response_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005043 }
5044
5045 /* Headers sent, set the flag. */
5046 appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT;
5047 return 0;
5048}
5049
5050__LJMP static int hlua_applet_http_start_response(lua_State *L)
5051{
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005052 struct buffer *tmp;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005053 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005054 const char *name;
Willy Tarreaua3294632017-08-23 11:24:47 +02005055 size_t name_len;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005056 const char *value;
Willy Tarreaua3294632017-08-23 11:24:47 +02005057 size_t value_len;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005058 int id;
Willy Tarreauc9f4ea02017-08-23 09:32:06 +02005059 long long hdr_contentlength = -1;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005060 int hdr_chunked = 0;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005061 const char *reason;
5062
5063 if (IS_HTX_STRM(si_strm(appctx->appctx->owner)))
5064 return MAY_LJMP(hlua_applet_htx_start_response(L));
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005065
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005066 reason = appctx->appctx->ctx.hlua_apphttp.reason;
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005067 if (reason == NULL)
Willy Tarreau04f1e2d2018-09-10 18:04:24 +02005068 reason = http_get_reason(appctx->appctx->ctx.hlua_apphttp.status);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005069
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005070 tmp = get_trash_chunk();
5071
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005072 /* Use the same http version than the request. */
5073 chunk_appendf(tmp, "HTTP/1.%c %d %s\r\n",
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +01005074 appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11 ? '1' : '0',
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005075 appctx->appctx->ctx.hlua_apphttp.status,
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005076 reason);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005077
5078 /* Get the array associated to the field "response" in the object AppletHTTP. */
5079 lua_pushvalue(L, 0);
5080 if (lua_getfield(L, 1, "response") != LUA_TTABLE) {
5081 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n",
5082 appctx->appctx->rule->arg.hlua_rule->fcn.name);
5083 WILL_LJMP(lua_error(L));
5084 }
5085
5086 /* Browse the list of headers. */
5087 lua_pushnil(L);
5088 while(lua_next(L, -2) != 0) {
5089
5090 /* We expect a string as -2. */
5091 if (lua_type(L, -2) != LUA_TSTRING) {
5092 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n",
5093 appctx->appctx->rule->arg.hlua_rule->fcn.name,
5094 lua_typename(L, lua_type(L, -2)));
5095 WILL_LJMP(lua_error(L));
5096 }
Willy Tarreaua3294632017-08-23 11:24:47 +02005097 name = lua_tolstring(L, -2, &name_len);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005098
5099 /* We expect an array as -1. */
5100 if (lua_type(L, -1) != LUA_TTABLE) {
5101 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n",
5102 appctx->appctx->rule->arg.hlua_rule->fcn.name,
5103 name,
5104 lua_typename(L, lua_type(L, -1)));
5105 WILL_LJMP(lua_error(L));
5106 }
5107
5108 /* Browse the table who is on the top of the stack. */
5109 lua_pushnil(L);
5110 while(lua_next(L, -2) != 0) {
5111
5112 /* We expect a number as -2. */
5113 if (lua_type(L, -2) != LUA_TNUMBER) {
5114 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n",
5115 appctx->appctx->rule->arg.hlua_rule->fcn.name,
5116 name,
5117 lua_typename(L, lua_type(L, -2)));
5118 WILL_LJMP(lua_error(L));
5119 }
5120 id = lua_tointeger(L, -2);
5121
5122 /* We expect a string as -2. */
5123 if (lua_type(L, -1) != LUA_TSTRING) {
5124 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n",
5125 appctx->appctx->rule->arg.hlua_rule->fcn.name,
5126 name, id,
5127 lua_typename(L, lua_type(L, -1)));
5128 WILL_LJMP(lua_error(L));
5129 }
Willy Tarreaua3294632017-08-23 11:24:47 +02005130 value = lua_tolstring(L, -1, &value_len);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005131
5132 /* Catenate a new header. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005133 if (tmp->data + name_len + 2 + value_len + 2 < tmp->size) {
5134 memcpy(tmp->area + tmp->data, name, name_len);
5135 tmp->data += name_len;
5136 tmp->area[tmp->data++] = ':';
5137 tmp->area[tmp->data++] = ' ';
Willy Tarreaua3294632017-08-23 11:24:47 +02005138
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005139 memcpy(tmp->area + tmp->data, value,
5140 value_len);
5141 tmp->data += value_len;
5142 tmp->area[tmp->data++] = '\r';
5143 tmp->area[tmp->data++] = '\n';
Willy Tarreaua3294632017-08-23 11:24:47 +02005144 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005145
5146 /* Protocol checks. */
5147
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005148 /* Copy the header content length. The length conversion
Willy Tarreauc9f4ea02017-08-23 09:32:06 +02005149 * is done without control. If it contains a bad value,
5150 * the content-length remains negative so that we can
5151 * switch to either chunked encoding or close.
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005152 */
Willy Tarreaua3294632017-08-23 11:24:47 +02005153 if (name_len == 14 && strcasecmp("content-length", name) == 0)
Willy Tarreauc9f4ea02017-08-23 09:32:06 +02005154 strl2llrc(value, strlen(value), &hdr_contentlength);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005155
5156 /* Check if the client annouces a transfer-encoding chunked it self. */
Willy Tarreaua3294632017-08-23 11:24:47 +02005157 if (name_len == 17 && value_len == 7 &&
5158 strcasecmp("transfer-encoding", name) == 0 &&
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005159 strcasecmp("chunked", value) == 0)
5160 hdr_chunked = 1;
5161
5162 /* Remove the array from the stack, and get next element with a remaining string. */
5163 lua_pop(L, 1);
5164 }
5165
5166 /* Remove the array from the stack, and get next element with a remaining string. */
5167 lua_pop(L, 1);
5168 }
5169
Willy Tarreau06c75fe2017-08-23 09:10:38 +02005170 /* If we dont have a content-length set, and the HTTP version is 1.1
5171 * and the status code implies the presence of a message body, we must
5172 * announce a transfer encoding chunked. This is required by haproxy
5173 * for the keepalive compliance. If the applet annouces a transfer-encoding
5174 * chunked itslef, don't do anything.
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005175 */
Willy Tarreauc9f4ea02017-08-23 09:32:06 +02005176 if (hdr_contentlength < 0 && hdr_chunked == 0 &&
Willy Tarreau06c75fe2017-08-23 09:10:38 +02005177 (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) &&
5178 appctx->appctx->ctx.hlua_apphttp.status >= 200 &&
5179 appctx->appctx->ctx.hlua_apphttp.status != 204 &&
5180 appctx->appctx->ctx.hlua_apphttp.status != 304) {
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005181 chunk_appendf(tmp, "Transfer-encoding: chunked\r\n");
5182 appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_CHUNKED;
5183 }
5184
5185 /* Finalize headers. */
5186 chunk_appendf(tmp, "\r\n");
5187
5188 /* Remove the last entry and the array of headers */
5189 lua_pop(L, 2);
5190
5191 /* Push the headers block. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005192 lua_pushlstring(L, tmp->area, tmp->data);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005193
5194 return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0));
5195}
5196
5197/*
5198 *
5199 *
5200 * Class HTTP
5201 *
5202 *
5203 */
5204
5205/* Returns a struct hlua_txn if the stack entry "ud" is
5206 * a class stream, otherwise it throws an error.
5207 */
5208__LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud)
5209{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02005210 return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005211}
5212
5213/* This function creates and push in the stack a HTTP object
5214 * according with a current TXN.
5215 */
5216static int hlua_http_new(lua_State *L, struct hlua_txn *txn)
5217{
5218 struct hlua_txn *htxn;
5219
5220 /* Check stack size. */
5221 if (!lua_checkstack(L, 3))
5222 return 0;
5223
5224 /* Create the object: obj[0] = userdata.
5225 * Note that the base of the Converters object is the
5226 * same than the TXN object.
5227 */
5228 lua_newtable(L);
5229 htxn = lua_newuserdata(L, sizeof(*htxn));
5230 lua_rawseti(L, -2, 0);
5231
5232 htxn->s = txn->s;
5233 htxn->p = txn->p;
5234
5235 /* Pop a class stream metatable and affect it to the table. */
5236 lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref);
5237 lua_setmetatable(L, -2);
5238
5239 return 1;
5240}
5241
5242/* This function creates ans returns an array of HTTP headers.
5243 * This function does not fails. It is used as wrapper with the
5244 * 2 following functions.
5245 */
5246__LJMP static int hlua_http_get_headers(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg)
5247{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005248 /* Create the table. */
5249 lua_newtable(L);
5250
5251 if (!htxn->s->txn)
5252 return 1;
5253
Christopher Faulet724a12c2018-12-13 22:12:15 +01005254 if (IS_HTX_STRM(htxn->s)) {
5255 /* HTX version */
5256 struct htx *htx = htxbuf(&msg->chn->buf);
5257 int32_t pos;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005258
Christopher Faulet724a12c2018-12-13 22:12:15 +01005259 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
5260 struct htx_blk *blk = htx_get_blk(htx, pos);
5261 enum htx_blk_type type = htx_get_blk_type(blk);
5262 struct ist n, v;
5263 int len;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005264
Christopher Faulet724a12c2018-12-13 22:12:15 +01005265 if (type == HTX_BLK_HDR) {
5266 n = htx_get_blk_name(htx,blk);
5267 v = htx_get_blk_value(htx, blk);
5268 }
5269 else if (type == HTX_BLK_EOH)
5270 break;
5271 else
5272 continue;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005273
Christopher Faulet724a12c2018-12-13 22:12:15 +01005274 /* Check for existing entry:
5275 * assume that the table is on the top of the stack, and
5276 * push the key in the stack, the function lua_gettable()
5277 * perform the lookup.
5278 */
5279 lua_pushlstring(L, n.ptr, n.len);
5280 lua_gettable(L, -2);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005281
Christopher Faulet724a12c2018-12-13 22:12:15 +01005282 switch (lua_type(L, -1)) {
5283 case LUA_TNIL:
5284 /* Table not found, create it. */
5285 lua_pop(L, 1); /* remove the nil value. */
5286 lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */
5287 lua_newtable(L); /* create and push empty table. */
5288 lua_pushlstring(L, v.ptr, v.len); /* push header value. */
5289 lua_rawseti(L, -2, 0); /* index header value (pop it). */
5290 lua_rawset(L, -3); /* index new table with header name (pop the values). */
5291 break;
5292
5293 case LUA_TTABLE:
5294 /* Entry found: push the value in the table. */
5295 len = lua_rawlen(L, -1);
5296 lua_pushlstring(L, v.ptr, v.len); /* push header value. */
5297 lua_rawseti(L, -2, len+1); /* index header value (pop it). */
5298 lua_pop(L, 1); /* remove the table (it is stored in the main table). */
5299 break;
5300
5301 default:
5302 /* Other cases are errors. */
5303 hlua_pusherror(L, "internal error during the parsing of headers.");
5304 WILL_LJMP(lua_error(L));
5305 }
5306 }
5307 }
5308 else {
5309 /* Legacy HTTP version */
5310 const char *cur_ptr, *cur_next, *p;
5311 int old_idx, cur_idx;
5312 struct hdr_idx_elem *cur_hdr;
5313 const char *hn, *hv;
5314 int hnl, hvl;
5315 const char *in;
5316 char *out;
5317 int len;
5318
5319 /* Build array of headers. */
5320 old_idx = 0;
5321 cur_next = ci_head(msg->chn) + hdr_idx_first_pos(&htxn->s->txn->hdr_idx);
5322
5323 while (1) {
5324 cur_idx = htxn->s->txn->hdr_idx.v[old_idx].next;
5325 if (!cur_idx)
5326 break;
5327 old_idx = cur_idx;
5328
5329 cur_hdr = &htxn->s->txn->hdr_idx.v[cur_idx];
5330 cur_ptr = cur_next;
5331 cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1;
5332
5333 /* Now we have one full header at cur_ptr of len cur_hdr->len,
5334 * and the next header starts at cur_next. We'll check
5335 * this header in the list as well as against the default
5336 * rule.
5337 */
5338
5339 /* look for ': *'. */
5340 hn = cur_ptr;
5341 for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++);
5342 if (p >= cur_ptr+cur_hdr->len)
5343 continue;
5344 hnl = p - hn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005345 p++;
Christopher Faulet724a12c2018-12-13 22:12:15 +01005346 while (p < cur_ptr+cur_hdr->len && ( *p == ' ' || *p == '\t' ))
5347 p++;
5348 if (p >= cur_ptr+cur_hdr->len)
5349 continue;
5350 hv = p;
5351 hvl = cur_ptr+cur_hdr->len-p;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005352
Christopher Faulet724a12c2018-12-13 22:12:15 +01005353 /* Lowercase the key. Don't check the size of trash, it have
5354 * the size of one buffer and the input data contains in one
5355 * buffer.
5356 */
5357 out = trash.area;
5358 for (in=hn; in<hn+hnl; in++, out++)
5359 *out = tolower(*in);
5360 *out = '\0';
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005361
Christopher Faulet724a12c2018-12-13 22:12:15 +01005362 /* Check for existing entry:
5363 * assume that the table is on the top of the stack, and
5364 * push the key in the stack, the function lua_gettable()
5365 * perform the lookup.
5366 */
5367 lua_pushlstring(L, trash.area, hnl);
5368 lua_gettable(L, -2);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005369
Christopher Faulet724a12c2018-12-13 22:12:15 +01005370 switch (lua_type(L, -1)) {
5371 case LUA_TNIL:
5372 /* Table not found, create it. */
5373 lua_pop(L, 1); /* remove the nil value. */
5374 lua_pushlstring(L, trash.area, hnl); /* push the header name as key. */
5375 lua_newtable(L); /* create and push empty table. */
5376 lua_pushlstring(L, hv, hvl); /* push header value. */
5377 lua_rawseti(L, -2, 0); /* index header value (pop it). */
5378 lua_rawset(L, -3); /* index new table with header name (pop the values). */
5379 break;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005380
Christopher Faulet724a12c2018-12-13 22:12:15 +01005381 case LUA_TTABLE:
5382 /* Entry found: push the value in the table. */
5383 len = lua_rawlen(L, -1);
5384 lua_pushlstring(L, hv, hvl); /* push header value. */
5385 lua_rawseti(L, -2, len+1); /* index header value (pop it). */
5386 lua_pop(L, 1); /* remove the table (it is stored in the main table). */
5387 break;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005388
Christopher Faulet724a12c2018-12-13 22:12:15 +01005389 default:
5390 /* Other cases are errors. */
5391 hlua_pusherror(L, "internal error during the parsing of headers.");
5392 WILL_LJMP(lua_error(L));
5393 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005394 }
5395 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005396 return 1;
5397}
5398
5399__LJMP static int hlua_http_req_get_headers(lua_State *L)
5400{
5401 struct hlua_txn *htxn;
5402
5403 MAY_LJMP(check_args(L, 1, "req_get_headers"));
5404 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5405
5406 return hlua_http_get_headers(L, htxn, &htxn->s->txn->req);
5407}
5408
5409__LJMP static int hlua_http_res_get_headers(lua_State *L)
5410{
5411 struct hlua_txn *htxn;
5412
5413 MAY_LJMP(check_args(L, 1, "res_get_headers"));
5414 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5415
5416 return hlua_http_get_headers(L, htxn, &htxn->s->txn->rsp);
5417}
5418
5419/* This function replace full header, or just a value in
5420 * the request or in the response. It is a wrapper fir the
5421 * 4 following functions.
5422 */
5423__LJMP static inline int hlua_http_rep_hdr(lua_State *L, struct hlua_txn *htxn,
5424 struct http_msg *msg, int action)
5425{
5426 size_t name_len;
5427 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
5428 const char *reg = MAY_LJMP(luaL_checkstring(L, 3));
5429 const char *value = MAY_LJMP(luaL_checkstring(L, 4));
5430 struct my_regex re;
5431
5432 if (!regex_comp(reg, &re, 1, 1, NULL))
5433 WILL_LJMP(luaL_argerror(L, 3, "invalid regex"));
5434
5435 http_transform_header_str(htxn->s, msg, name, name_len, value, &re, action);
5436 regex_free(&re);
5437 return 0;
5438}
5439
5440__LJMP static int hlua_http_req_rep_hdr(lua_State *L)
5441{
5442 struct hlua_txn *htxn;
5443
5444 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
5445 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5446
5447 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_HDR));
5448}
5449
5450__LJMP static int hlua_http_res_rep_hdr(lua_State *L)
5451{
5452 struct hlua_txn *htxn;
5453
5454 MAY_LJMP(check_args(L, 4, "res_rep_hdr"));
5455 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5456
5457 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->rsp, ACT_HTTP_REPLACE_HDR));
5458}
5459
5460__LJMP static int hlua_http_req_rep_val(lua_State *L)
5461{
5462 struct hlua_txn *htxn;
5463
5464 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
5465 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5466
5467 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_VAL));
5468}
5469
5470__LJMP static int hlua_http_res_rep_val(lua_State *L)
5471{
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005472 struct hlua_txn *htxn;
5473
5474 MAY_LJMP(check_args(L, 4, "res_rep_val"));
5475 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5476
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02005477 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->rsp, ACT_HTTP_REPLACE_VAL));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005478}
5479
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08005480/* This function deletes all the occurrences of an header.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005481 * It is a wrapper for the 2 following functions.
5482 */
5483__LJMP static inline int hlua_http_del_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg)
5484{
5485 size_t len;
5486 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005487
Christopher Faulet724a12c2018-12-13 22:12:15 +01005488 if (IS_HTX_STRM(htxn->s)) {
5489 /* HTX version */
5490 struct htx *htx = htxbuf(&msg->chn->buf);
5491 struct http_hdr_ctx ctx;
5492
5493 ctx.blk = NULL;
5494 while (http_find_header(htx, ist2(name, len), &ctx, 1))
5495 http_remove_header(htx, &ctx);
5496 }
5497 else {
5498 /* Legacy HTTP version */
5499 struct hdr_ctx ctx;
5500 struct http_txn *txn = htxn->s->txn;
5501
5502 ctx.idx = 0;
5503 while (http_find_header2(name, len, ci_head(msg->chn), &txn->hdr_idx, &ctx))
5504 http_remove_header2(msg, &txn->hdr_idx, &ctx);
5505 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005506 return 0;
5507}
5508
5509__LJMP static int hlua_http_req_del_hdr(lua_State *L)
5510{
5511 struct hlua_txn *htxn;
5512
5513 MAY_LJMP(check_args(L, 2, "req_del_hdr"));
5514 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5515
Willy Tarreaueee5b512015-04-03 23:46:31 +02005516 return hlua_http_del_hdr(L, htxn, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005517}
5518
5519__LJMP static int hlua_http_res_del_hdr(lua_State *L)
5520{
5521 struct hlua_txn *htxn;
5522
5523 MAY_LJMP(check_args(L, 2, "req_del_hdr"));
5524 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5525
Willy Tarreaueee5b512015-04-03 23:46:31 +02005526 return hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005527}
5528
5529/* This function adds an header. It is a wrapper used by
5530 * the 2 following functions.
5531 */
5532__LJMP static inline int hlua_http_add_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg)
5533{
5534 size_t name_len;
5535 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
5536 size_t value_len;
5537 const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len));
5538 char *p;
5539
Christopher Faulet724a12c2018-12-13 22:12:15 +01005540 if (IS_HTX_STRM(htxn->s)) {
5541 /* HTX version */
5542 struct htx *htx = htxbuf(&msg->chn->buf);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005543
Christopher Faulet724a12c2018-12-13 22:12:15 +01005544 lua_pushboolean(L, http_add_header(htx, ist2(name, name_len),
5545 ist2(value, value_len)));
5546 }
5547 else {
5548 /* Legacy HTTP version */
5549 /* Check length. */
5550 trash.data = value_len + name_len + 2;
5551 if (trash.data > trash.size)
5552 return 0;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005553
Christopher Faulet724a12c2018-12-13 22:12:15 +01005554 /* Creates the header string. */
5555 p = trash.area;
5556 memcpy(p, name, name_len);
5557 p += name_len;
5558 *p = ':';
5559 p++;
5560 *p = ' ';
5561 p++;
5562 memcpy(p, value, value_len);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005563
Christopher Faulet724a12c2018-12-13 22:12:15 +01005564 lua_pushboolean(L, http_header_add_tail2(msg, &htxn->s->txn->hdr_idx,
5565 trash.area, trash.data) != 0);
5566 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005567 return 0;
5568}
5569
5570__LJMP static int hlua_http_req_add_hdr(lua_State *L)
5571{
5572 struct hlua_txn *htxn;
5573
5574 MAY_LJMP(check_args(L, 3, "req_add_hdr"));
5575 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5576
Willy Tarreaueee5b512015-04-03 23:46:31 +02005577 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005578}
5579
5580__LJMP static int hlua_http_res_add_hdr(lua_State *L)
5581{
5582 struct hlua_txn *htxn;
5583
5584 MAY_LJMP(check_args(L, 3, "res_add_hdr"));
5585 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5586
Willy Tarreaueee5b512015-04-03 23:46:31 +02005587 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005588}
5589
5590static int hlua_http_req_set_hdr(lua_State *L)
5591{
5592 struct hlua_txn *htxn;
5593
5594 MAY_LJMP(check_args(L, 3, "req_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->req);
5598 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005599}
5600
5601static int hlua_http_res_set_hdr(lua_State *L)
5602{
5603 struct hlua_txn *htxn;
5604
5605 MAY_LJMP(check_args(L, 3, "res_set_hdr"));
5606 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5607
Willy Tarreaueee5b512015-04-03 23:46:31 +02005608 hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp);
5609 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005610}
5611
5612/* This function set the method. */
5613static int hlua_http_req_set_meth(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 FOURNIER08504f42015-03-16 14:17:08 +01005618
Willy Tarreau987e3fb2015-04-04 01:09:08 +02005619 lua_pushboolean(L, http_replace_req_line(0, 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 method. */
5624static int hlua_http_req_set_path(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 FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02005629
Willy Tarreau987e3fb2015-04-04 01:09:08 +02005630 lua_pushboolean(L, http_replace_req_line(1, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005631 return 1;
5632}
5633
5634/* This function set the query-string. */
5635static int hlua_http_req_set_query(lua_State *L)
5636{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005637 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005638 size_t name_len;
5639 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005640
5641 /* Check length. */
5642 if (name_len > trash.size - 1) {
5643 lua_pushboolean(L, 0);
5644 return 1;
5645 }
5646
5647 /* Add the mark question as prefix. */
5648 chunk_reset(&trash);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005649 trash.area[trash.data++] = '?';
5650 memcpy(trash.area + trash.data, name, name_len);
5651 trash.data += name_len;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005652
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005653 lua_pushboolean(L,
5654 http_replace_req_line(2, trash.area, trash.data, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005655 return 1;
5656}
5657
5658/* This function set the uri. */
5659static int hlua_http_req_set_uri(lua_State *L)
5660{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005661 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005662 size_t name_len;
5663 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005664
Willy Tarreau987e3fb2015-04-04 01:09:08 +02005665 lua_pushboolean(L, http_replace_req_line(3, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005666 return 1;
5667}
5668
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005669/* This function set the response code & optionally reason. */
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005670static int hlua_http_res_set_status(lua_State *L)
5671{
5672 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5673 unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2));
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005674 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005675
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005676 http_set_status(code, reason, htxn->s);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005677 return 0;
5678}
5679
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005680/*
5681 *
5682 *
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005683 * Class TXN
5684 *
5685 *
5686 */
5687
5688/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02005689 * a class stream, otherwise it throws an error.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005690 */
5691__LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud)
5692{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02005693 return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref));
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005694}
5695
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005696__LJMP static int hlua_set_var(lua_State *L)
5697{
5698 struct hlua_txn *htxn;
5699 const char *name;
5700 size_t len;
5701 struct sample smp;
5702
5703 MAY_LJMP(check_args(L, 3, "set_var"));
5704
5705 /* It is useles to retrieve the stream, but this function
5706 * runs only in a stream context.
5707 */
5708 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5709 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5710
5711 /* Converts the third argument in a sample. */
5712 hlua_lua2smp(L, 3, &smp);
5713
5714 /* Store the sample in a variable. */
Willy Tarreau7560dd42016-03-10 16:28:58 +01005715 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Willy Tarreau6204cd92016-03-10 16:33:04 +01005716 vars_set_by_name(name, len, &smp);
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005717 return 0;
5718}
5719
Christopher Faulet85d79c92016-11-09 16:54:56 +01005720__LJMP static int hlua_unset_var(lua_State *L)
5721{
5722 struct hlua_txn *htxn;
5723 const char *name;
5724 size_t len;
5725 struct sample smp;
5726
5727 MAY_LJMP(check_args(L, 2, "unset_var"));
5728
5729 /* It is useles to retrieve the stream, but this function
5730 * runs only in a stream context.
5731 */
5732 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5733 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5734
5735 /* Unset the variable. */
5736 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
5737 vars_unset_by_name(name, len, &smp);
5738 return 0;
5739}
5740
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005741__LJMP static int hlua_get_var(lua_State *L)
5742{
5743 struct hlua_txn *htxn;
5744 const char *name;
5745 size_t len;
5746 struct sample smp;
5747
5748 MAY_LJMP(check_args(L, 2, "get_var"));
5749
5750 /* It is useles to retrieve the stream, but this function
5751 * runs only in a stream context.
5752 */
5753 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5754 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5755
Willy Tarreau7560dd42016-03-10 16:28:58 +01005756 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Willy Tarreau6204cd92016-03-10 16:33:04 +01005757 if (!vars_get_by_name(name, len, &smp)) {
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005758 lua_pushnil(L);
5759 return 1;
5760 }
5761
5762 return hlua_smp2lua(L, &smp);
5763}
5764
Willy Tarreau59551662015-03-10 14:23:13 +01005765__LJMP static int hlua_set_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005766{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005767 struct hlua *hlua;
5768
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005769 MAY_LJMP(check_args(L, 2, "set_priv"));
5770
Willy Tarreau87b09662015-04-03 00:22:06 +02005771 /* It is useles to retrieve the stream, but this function
5772 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005773 */
5774 MAY_LJMP(hlua_checktxn(L, 1));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005775 hlua = hlua_gethlua(L);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005776
5777 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02005778 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005779
5780 /* Get and store new value. */
5781 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
5782 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
5783
5784 return 0;
5785}
5786
Willy Tarreau59551662015-03-10 14:23:13 +01005787__LJMP static int hlua_get_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005788{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005789 struct hlua *hlua;
5790
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005791 MAY_LJMP(check_args(L, 1, "get_priv"));
5792
Willy Tarreau87b09662015-04-03 00:22:06 +02005793 /* It is useles to retrieve the stream, but this function
5794 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005795 */
5796 MAY_LJMP(hlua_checktxn(L, 1));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005797 hlua = hlua_gethlua(L);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005798
5799 /* Push configuration index in the stack. */
5800 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
5801
5802 return 1;
5803}
5804
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005805/* Create stack entry containing a class TXN. This function
5806 * return 0 if the stack does not contains free slots,
5807 * otherwise it returns 1.
5808 */
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005809static int hlua_txn_new(lua_State *L, struct stream *s, struct proxy *p, int dir, int flags)
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005810{
Willy Tarreaude491382015-04-06 11:04:28 +02005811 struct hlua_txn *htxn;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005812
5813 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005814 if (!lua_checkstack(L, 3))
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005815 return 0;
5816
5817 /* NOTE: The allocation never fails. The failure
5818 * throw an error, and the function never returns.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08005819 * if the throw is not available, the process is aborted.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005820 */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005821 /* Create the object: obj[0] = userdata. */
5822 lua_newtable(L);
Willy Tarreaude491382015-04-06 11:04:28 +02005823 htxn = lua_newuserdata(L, sizeof(*htxn));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005824 lua_rawseti(L, -2, 0);
5825
Willy Tarreaude491382015-04-06 11:04:28 +02005826 htxn->s = s;
5827 htxn->p = p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01005828 htxn->dir = dir;
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005829 htxn->flags = flags;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005830
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005831 /* Create the "f" field that contains a list of fetches. */
5832 lua_pushstring(L, "f");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005833 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005834 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005835 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005836
5837 /* Create the "sf" field that contains a list of stringsafe fetches. */
5838 lua_pushstring(L, "sf");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005839 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP | HLUA_F_AS_STRING))
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005840 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005841 lua_rawset(L, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005842
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005843 /* Create the "c" field that contains a list of converters. */
5844 lua_pushstring(L, "c");
Willy Tarreaude491382015-04-06 11:04:28 +02005845 if (!hlua_converters_new(L, htxn, 0))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005846 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005847 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005848
5849 /* Create the "sc" field that contains a list of stringsafe converters. */
5850 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01005851 if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING))
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005852 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005853 lua_rawset(L, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005854
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005855 /* Create the "req" field that contains the request channel object. */
5856 lua_pushstring(L, "req");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005857 if (!hlua_channel_new(L, &s->req))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005858 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005859 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005860
5861 /* Create the "res" field that contains the response channel object. */
5862 lua_pushstring(L, "res");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005863 if (!hlua_channel_new(L, &s->res))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005864 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005865 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005866
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005867 /* Creates the HTTP object is the current proxy allows http. */
5868 lua_pushstring(L, "http");
5869 if (p->mode == PR_MODE_HTTP) {
Willy Tarreaude491382015-04-06 11:04:28 +02005870 if (!hlua_http_new(L, htxn))
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005871 return 0;
5872 }
5873 else
5874 lua_pushnil(L);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005875 lua_rawset(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005876
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005877 /* Pop a class sesison metatable and affect it to the userdata. */
5878 lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref);
5879 lua_setmetatable(L, -2);
5880
5881 return 1;
5882}
5883
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005884__LJMP static int hlua_txn_deflog(lua_State *L)
5885{
5886 const char *msg;
5887 struct hlua_txn *htxn;
5888
5889 MAY_LJMP(check_args(L, 2, "deflog"));
5890 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5891 msg = MAY_LJMP(luaL_checkstring(L, 2));
5892
5893 hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg);
5894 return 0;
5895}
5896
5897__LJMP static int hlua_txn_log(lua_State *L)
5898{
5899 int level;
5900 const char *msg;
5901 struct hlua_txn *htxn;
5902
5903 MAY_LJMP(check_args(L, 3, "log"));
5904 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5905 level = MAY_LJMP(luaL_checkinteger(L, 2));
5906 msg = MAY_LJMP(luaL_checkstring(L, 3));
5907
5908 if (level < 0 || level >= NB_LOG_LEVELS)
5909 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
5910
5911 hlua_sendlog(htxn->s->be, level, msg);
5912 return 0;
5913}
5914
5915__LJMP static int hlua_txn_log_debug(lua_State *L)
5916{
5917 const char *msg;
5918 struct hlua_txn *htxn;
5919
5920 MAY_LJMP(check_args(L, 2, "Debug"));
5921 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5922 msg = MAY_LJMP(luaL_checkstring(L, 2));
5923 hlua_sendlog(htxn->s->be, LOG_DEBUG, msg);
5924 return 0;
5925}
5926
5927__LJMP static int hlua_txn_log_info(lua_State *L)
5928{
5929 const char *msg;
5930 struct hlua_txn *htxn;
5931
5932 MAY_LJMP(check_args(L, 2, "Info"));
5933 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5934 msg = MAY_LJMP(luaL_checkstring(L, 2));
5935 hlua_sendlog(htxn->s->be, LOG_INFO, msg);
5936 return 0;
5937}
5938
5939__LJMP static int hlua_txn_log_warning(lua_State *L)
5940{
5941 const char *msg;
5942 struct hlua_txn *htxn;
5943
5944 MAY_LJMP(check_args(L, 2, "Warning"));
5945 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5946 msg = MAY_LJMP(luaL_checkstring(L, 2));
5947 hlua_sendlog(htxn->s->be, LOG_WARNING, msg);
5948 return 0;
5949}
5950
5951__LJMP static int hlua_txn_log_alert(lua_State *L)
5952{
5953 const char *msg;
5954 struct hlua_txn *htxn;
5955
5956 MAY_LJMP(check_args(L, 2, "Alert"));
5957 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5958 msg = MAY_LJMP(luaL_checkstring(L, 2));
5959 hlua_sendlog(htxn->s->be, LOG_ALERT, msg);
5960 return 0;
5961}
5962
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005963__LJMP static int hlua_txn_set_loglevel(lua_State *L)
5964{
5965 struct hlua_txn *htxn;
5966 int ll;
5967
5968 MAY_LJMP(check_args(L, 2, "set_loglevel"));
5969 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5970 ll = MAY_LJMP(luaL_checkinteger(L, 2));
5971
5972 if (ll < 0 || ll > 7)
5973 WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7"));
5974
5975 htxn->s->logs.level = ll;
5976 return 0;
5977}
5978
5979__LJMP static int hlua_txn_set_tos(lua_State *L)
5980{
5981 struct hlua_txn *htxn;
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005982 int tos;
5983
5984 MAY_LJMP(check_args(L, 2, "set_tos"));
5985 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5986 tos = MAY_LJMP(luaL_checkinteger(L, 2));
5987
Willy Tarreau1a18b542018-12-11 16:37:42 +01005988 conn_set_tos(objt_conn(htxn->s->sess->origin), tos);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005989 return 0;
5990}
5991
5992__LJMP static int hlua_txn_set_mark(lua_State *L)
5993{
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005994 struct hlua_txn *htxn;
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005995 int mark;
5996
5997 MAY_LJMP(check_args(L, 2, "set_mark"));
5998 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5999 mark = MAY_LJMP(luaL_checkinteger(L, 2));
6000
Willy Tarreau1a18b542018-12-11 16:37:42 +01006001 conn_set_tos(objt_conn(htxn->s->sess->origin), mark);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01006002 return 0;
6003}
6004
Patrick Hemmer268a7072018-05-11 12:52:31 -04006005__LJMP static int hlua_txn_set_priority_class(lua_State *L)
6006{
6007 struct hlua_txn *htxn;
6008
6009 MAY_LJMP(check_args(L, 2, "set_priority_class"));
6010 htxn = MAY_LJMP(hlua_checktxn(L, 1));
6011 htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2)));
6012 return 0;
6013}
6014
6015__LJMP static int hlua_txn_set_priority_offset(lua_State *L)
6016{
6017 struct hlua_txn *htxn;
6018
6019 MAY_LJMP(check_args(L, 2, "set_priority_offset"));
6020 htxn = MAY_LJMP(hlua_checktxn(L, 1));
6021 htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2)));
6022 return 0;
6023}
6024
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01006025/* This function is an Lua binding that send pending data
6026 * to the client, and close the stream interface.
6027 */
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02006028__LJMP static int hlua_txn_done(lua_State *L)
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01006029{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02006030 struct hlua_txn *htxn;
Thierry FOURNIER9bd52d42016-07-14 11:45:33 +02006031 struct hlua *hlua;
Willy Tarreau81389672015-03-10 12:03:52 +01006032 struct channel *ic, *oc;
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01006033
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006034 MAY_LJMP(check_args(L, 1, "close"));
Willy Tarreaub2ccb562015-04-06 11:11:15 +02006035 htxn = MAY_LJMP(hlua_checktxn(L, 1));
Thierry FOURNIER9bd52d42016-07-14 11:45:33 +02006036 hlua = hlua_gethlua(L);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01006037
Thierry FOURNIERab00df62016-07-14 11:42:37 +02006038 /* If the flags NOTERM is set, we cannot terminate the http
6039 * session, so we just end the execution of the current
6040 * lua code.
6041 */
6042 if (htxn->flags & HLUA_TXN_NOTERM) {
6043 WILL_LJMP(hlua_done(L));
6044 return 0;
6045 }
6046
Willy Tarreaub2ccb562015-04-06 11:11:15 +02006047 ic = &htxn->s->req;
6048 oc = &htxn->s->res;
Willy Tarreau81389672015-03-10 12:03:52 +01006049
Willy Tarreau630ef452015-08-28 10:06:15 +02006050 if (htxn->s->txn) {
6051 /* HTTP mode, let's stay in sync with the stream */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006052 b_del(&ic->buf, htxn->s->txn->req.sov);
Willy Tarreau630ef452015-08-28 10:06:15 +02006053 htxn->s->txn->req.next -= htxn->s->txn->req.sov;
6054 htxn->s->txn->req.sov = 0;
6055 ic->analysers &= AN_REQ_HTTP_XFER_BODY;
6056 oc->analysers = AN_RES_HTTP_XFER_BODY;
6057 htxn->s->txn->req.msg_state = HTTP_MSG_CLOSED;
6058 htxn->s->txn->rsp.msg_state = HTTP_MSG_DONE;
6059
Willy Tarreau630ef452015-08-28 10:06:15 +02006060 /* Note that if we want to support keep-alive, we need
6061 * to bypass the close/shutr_now calls below, but that
6062 * may only be done if the HTTP request was already
6063 * processed and the connection header is known (ie
6064 * not during TCP rules).
6065 */
6066 }
6067
Thierry FOURNIER10ec2142015-08-24 17:23:45 +02006068 channel_auto_read(ic);
Willy Tarreau81389672015-03-10 12:03:52 +01006069 channel_abort(ic);
6070 channel_auto_close(ic);
6071 channel_erase(ic);
Thierry FOURNIER10ec2142015-08-24 17:23:45 +02006072
6073 oc->wex = tick_add_ifset(now_ms, oc->wto);
Willy Tarreau81389672015-03-10 12:03:52 +01006074 channel_auto_read(oc);
6075 channel_auto_close(oc);
6076 channel_shutr_now(oc);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01006077
Willy Tarreau0458b082015-08-28 09:40:04 +02006078 ic->analysers = 0;
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02006079
Thierry FOURNIER9bd52d42016-07-14 11:45:33 +02006080 hlua->flags |= HLUA_STOP;
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02006081 WILL_LJMP(hlua_done(L));
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01006082 return 0;
6083}
6084
6085__LJMP static int hlua_log(lua_State *L)
6086{
6087 int level;
6088 const char *msg;
6089
6090 MAY_LJMP(check_args(L, 2, "log"));
6091 level = MAY_LJMP(luaL_checkinteger(L, 1));
6092 msg = MAY_LJMP(luaL_checkstring(L, 2));
6093
6094 if (level < 0 || level >= NB_LOG_LEVELS)
6095 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
6096
6097 hlua_sendlog(NULL, level, msg);
6098 return 0;
6099}
6100
6101__LJMP static int hlua_log_debug(lua_State *L)
6102{
6103 const char *msg;
6104
6105 MAY_LJMP(check_args(L, 1, "debug"));
6106 msg = MAY_LJMP(luaL_checkstring(L, 1));
6107 hlua_sendlog(NULL, LOG_DEBUG, msg);
6108 return 0;
6109}
6110
6111__LJMP static int hlua_log_info(lua_State *L)
6112{
6113 const char *msg;
6114
6115 MAY_LJMP(check_args(L, 1, "info"));
6116 msg = MAY_LJMP(luaL_checkstring(L, 1));
6117 hlua_sendlog(NULL, LOG_INFO, msg);
6118 return 0;
6119}
6120
6121__LJMP static int hlua_log_warning(lua_State *L)
6122{
6123 const char *msg;
6124
6125 MAY_LJMP(check_args(L, 1, "warning"));
6126 msg = MAY_LJMP(luaL_checkstring(L, 1));
6127 hlua_sendlog(NULL, LOG_WARNING, msg);
6128 return 0;
6129}
6130
6131__LJMP static int hlua_log_alert(lua_State *L)
6132{
6133 const char *msg;
6134
6135 MAY_LJMP(check_args(L, 1, "alert"));
6136 msg = MAY_LJMP(luaL_checkstring(L, 1));
6137 hlua_sendlog(NULL, LOG_ALERT, msg);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01006138 return 0;
6139}
6140
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006141__LJMP static int hlua_sleep_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006142{
6143 int wakeup_ms = lua_tointeger(L, -1);
6144 if (now_ms < wakeup_ms)
Willy Tarreau9635e032018-10-16 17:52:55 +02006145 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006146 return 0;
6147}
6148
6149__LJMP static int hlua_sleep(lua_State *L)
6150{
6151 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006152 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006153
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006154 MAY_LJMP(check_args(L, 1, "sleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006155
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006156 delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006157 wakeup_ms = tick_add(now_ms, delay);
6158 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006159
Willy Tarreau9635e032018-10-16 17:52:55 +02006160 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006161 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006162}
6163
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006164__LJMP static int hlua_msleep(lua_State *L)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006165{
6166 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006167 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006168
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006169 MAY_LJMP(check_args(L, 1, "msleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006170
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006171 delay = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006172 wakeup_ms = tick_add(now_ms, delay);
6173 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006174
Willy Tarreau9635e032018-10-16 17:52:55 +02006175 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006176 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006177}
6178
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01006179/* This functionis an LUA binding. it permits to give back
6180 * the hand at the HAProxy scheduler. It is used when the
6181 * LUA processing consumes a lot of time.
6182 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006183__LJMP static int hlua_yield_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006184{
6185 return 0;
6186}
6187
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01006188__LJMP static int hlua_yield(lua_State *L)
6189{
Willy Tarreau9635e032018-10-16 17:52:55 +02006190 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_yield_yield, TICK_ETERNITY, HLUA_CTRLYIELD));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006191 return 0;
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01006192}
6193
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006194/* This function change the nice of the currently executed
6195 * task. It is used set low or high priority at the current
6196 * task.
6197 */
Willy Tarreau59551662015-03-10 14:23:13 +01006198__LJMP static int hlua_set_nice(lua_State *L)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006199{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006200 struct hlua *hlua;
6201 int nice;
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006202
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006203 MAY_LJMP(check_args(L, 1, "set_nice"));
6204 hlua = hlua_gethlua(L);
6205 nice = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006206
6207 /* If he task is not set, I'm in a start mode. */
6208 if (!hlua || !hlua->task)
6209 return 0;
6210
6211 if (nice < -1024)
6212 nice = -1024;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006213 else if (nice > 1024)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006214 nice = 1024;
6215
6216 hlua->task->nice = nice;
6217 return 0;
6218}
6219
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006220/* This function is used as a callback of a task. It is called by the
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006221 * HAProxy task subsystem when the task is awaked. The LUA runtime can
6222 * return an E_AGAIN signal, the emmiter of this signal must set a
6223 * signal to wake the task.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006224 *
6225 * Task wrapper are longjmp safe because the only one Lua code
6226 * executed is the safe hlua_ctx_resume();
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006227 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02006228static struct task *hlua_process_task(struct task *task, void *context, unsigned short state)
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006229{
Olivier Houchard9f6af332018-05-25 14:04:04 +02006230 struct hlua *hlua = context;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006231 enum hlua_exec status;
6232
Christopher Faulet5bc99722018-04-25 10:34:45 +02006233 if (task->thread_mask == MAX_THREADS_MASK)
6234 task_set_affinity(task, tid_bit);
6235
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006236 /* If it is the first call to the task, we must initialize the
6237 * execution timeouts.
6238 */
6239 if (!HLUA_IS_RUNNING(hlua))
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02006240 hlua->max_time = hlua_timeout_task;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006241
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006242 /* Execute the Lua code. */
6243 status = hlua_ctx_resume(hlua, 1);
6244
6245 switch (status) {
6246 /* finished or yield */
6247 case HLUA_E_OK:
6248 hlua_ctx_destroy(hlua);
6249 task_delete(task);
6250 task_free(task);
Tim Duesterhuscd235c62018-04-24 13:56:01 +02006251 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006252 break;
6253
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006254 case HLUA_E_AGAIN: /* co process or timeout wake me later. */
Thierry FOURNIERcb146882017-12-10 17:10:57 +01006255 notification_gc(&hlua->com);
PiBa-NLfe971b32018-05-02 22:27:14 +02006256 task->expire = hlua->wake_time;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006257 break;
6258
6259 /* finished with error. */
6260 case HLUA_E_ERRMSG:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006261 SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006262 hlua_ctx_destroy(hlua);
6263 task_delete(task);
6264 task_free(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02006265 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006266 break;
6267
6268 case HLUA_E_ERR:
6269 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006270 SEND_ERR(NULL, "Lua task: unknown error.\n");
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006271 hlua_ctx_destroy(hlua);
6272 task_delete(task);
6273 task_free(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02006274 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006275 break;
6276 }
Emeric Brun253e53e2017-10-17 18:58:40 +02006277 return task;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006278}
6279
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006280/* This function is an LUA binding that register LUA function to be
6281 * executed after the HAProxy configuration parsing and before the
6282 * HAProxy scheduler starts. This function expect only one LUA
6283 * argument that is a function. This function returns nothing, but
6284 * throws if an error is encountered.
6285 */
6286__LJMP static int hlua_register_init(lua_State *L)
6287{
6288 struct hlua_init_function *init;
6289 int ref;
6290
6291 MAY_LJMP(check_args(L, 1, "register_init"));
6292
6293 ref = MAY_LJMP(hlua_checkfunction(L, 1));
6294
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006295 init = calloc(1, sizeof(*init));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006296 if (!init)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006297 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006298
6299 init->function_ref = ref;
6300 LIST_ADDQ(&hlua_init_functions, &init->l);
6301 return 0;
6302}
6303
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006304/* This functio is an LUA binding. It permits to register a task
6305 * executed in parallel of the main HAroxy activity. The task is
6306 * created and it is set in the HAProxy scheduler. It can be called
6307 * from the "init" section, "post init" or during the runtime.
6308 *
6309 * Lua prototype:
6310 *
6311 * <none> core.register_task(<function>)
6312 */
6313static int hlua_register_task(lua_State *L)
6314{
6315 struct hlua *hlua;
6316 struct task *task;
6317 int ref;
6318
6319 MAY_LJMP(check_args(L, 1, "register_task"));
6320
6321 ref = MAY_LJMP(hlua_checkfunction(L, 1));
6322
Willy Tarreaubafbe012017-11-24 17:34:44 +01006323 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006324 if (!hlua)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006325 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006326
Emeric Brunc60def82017-09-27 14:59:38 +02006327 task = task_new(MAX_THREADS_MASK);
Willy Tarreaue09101e2018-10-16 17:37:12 +02006328 if (!task)
6329 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
6330
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006331 task->context = hlua;
6332 task->process = hlua_process_task;
6333
6334 if (!hlua_ctx_init(hlua, task))
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006335 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006336
6337 /* Restore the function in the stack. */
6338 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref);
6339 hlua->nargs = 0;
6340
6341 /* Schedule task. */
6342 task_schedule(task, now_ms);
6343
6344 return 0;
6345}
6346
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006347/* Wrapper called by HAProxy to execute an LUA converter. This wrapper
6348 * doesn't allow "yield" functions because the HAProxy engine cannot
6349 * resume converters.
6350 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006351static int hlua_sample_conv_wrapper(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006352{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006353 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006354 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006355 const char *error;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006356
Willy Tarreaube508f12016-03-10 11:47:01 +01006357 if (!stream)
6358 return 0;
6359
Willy Tarreau87b09662015-04-03 00:22:06 +02006360 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006361 * Lua context can be not initialized. This behavior
6362 * permits to save performances because a systematic
6363 * Lua initialization cause 5% performances loss.
6364 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006365 if (!stream->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006366 stream->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006367 if (!stream->hlua) {
6368 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
6369 return 0;
6370 }
6371 if (!hlua_ctx_init(stream->hlua, stream->task)) {
6372 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
6373 return 0;
6374 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006375 }
6376
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006377 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006378 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006379
6380 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006381 if (!SET_SAFE_LJMP(stream->hlua->T)) {
6382 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
6383 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006384 else
6385 error = "critical error";
6386 SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006387 return 0;
6388 }
6389
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006390 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006391 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006392 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006393 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006394 return 0;
6395 }
6396
6397 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006398 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006399
6400 /* convert input sample and pust-it in the stack. */
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_smp2lua(stream->hlua->T, smp);
6407 stream->hlua->nargs = 1;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006408
6409 /* push keywords in the stack. */
6410 if (arg_p) {
6411 for (; arg_p->type != ARGT_STOP; arg_p++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006412 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006413 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006414 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006415 return 0;
6416 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006417 hlua_arg2lua(stream->hlua->T, arg_p);
6418 stream->hlua->nargs++;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006419 }
6420 }
6421
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006422 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006423 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006424
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006425 /* At this point the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006426 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006427 }
6428
6429 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006430 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006431 /* finished. */
6432 case HLUA_E_OK:
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02006433 /* If the stack is empty, the function fails. */
6434 if (lua_gettop(stream->hlua->T) <= 0)
6435 return 0;
6436
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006437 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006438 hlua_lua2smp(stream->hlua->T, -1, smp);
6439 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006440 return 1;
6441
6442 /* yield. */
6443 case HLUA_E_AGAIN:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006444 SEND_ERR(stream->be, "Lua converter '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006445 return 0;
6446
6447 /* finished with error. */
6448 case HLUA_E_ERRMSG:
6449 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006450 SEND_ERR(stream->be, "Lua converter '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006451 fcn->name, lua_tostring(stream->hlua->T, -1));
6452 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006453 return 0;
6454
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006455 case HLUA_E_ETMOUT:
6456 SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name);
6457 return 0;
6458
6459 case HLUA_E_NOMEM:
6460 SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name);
6461 return 0;
6462
6463 case HLUA_E_YIELD:
6464 SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name);
6465 return 0;
6466
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006467 case HLUA_E_ERR:
6468 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006469 SEND_ERR(stream->be, "Lua converter '%s' returns an unknown error.\n", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006470
6471 default:
6472 return 0;
6473 }
6474}
6475
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006476/* Wrapper called by HAProxy to execute a sample-fetch. this wrapper
6477 * doesn't allow "yield" functions because the HAProxy engine cannot
Willy Tarreaube508f12016-03-10 11:47:01 +01006478 * resume sample-fetches. This function will be called by the sample
6479 * fetch engine to call lua-based fetch operations.
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006480 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +02006481static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp,
6482 const char *kw, void *private)
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006483{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006484 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006485 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006486 const char *error;
Willy Tarreau83061a82018-07-13 11:56:34 +02006487 const struct buffer msg = { };
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006488
Willy Tarreaube508f12016-03-10 11:47:01 +01006489 if (!stream)
6490 return 0;
Christopher Fauletafd8f102018-11-08 11:34:21 +01006491
Willy Tarreau87b09662015-04-03 00:22:06 +02006492 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006493 * Lua context can be not initialized. This behavior
6494 * permits to save performances because a systematic
6495 * Lua initialization cause 5% performances loss.
6496 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006497 if (!stream->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006498 stream->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006499 if (!stream->hlua) {
6500 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
6501 return 0;
6502 }
6503 if (!hlua_ctx_init(stream->hlua, stream->task)) {
6504 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
6505 return 0;
6506 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006507 }
6508
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006509 consistency_set(stream, smp->opt, &stream->hlua->cons);
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006510
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006511 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006512 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006513
6514 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006515 if (!SET_SAFE_LJMP(stream->hlua->T)) {
6516 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
6517 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006518 else
6519 error = "critical error";
6520 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006521 return 0;
6522 }
6523
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006524 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006525 if (!lua_checkstack(stream->hlua->T, 2)) {
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 }
6530
6531 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006532 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006533
6534 /* push arguments in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006535 if (!hlua_txn_new(stream->hlua->T, stream, smp->px, smp->opt & SMP_OPT_DIR,
Thierry FOURNIERab00df62016-07-14 11:42:37 +02006536 HLUA_TXN_NOTERM)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006537 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006538 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006539 return 0;
6540 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006541 stream->hlua->nargs = 1;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006542
6543 /* push keywords in the stack. */
6544 for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) {
6545 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006546 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006547 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006548 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006549 return 0;
6550 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006551 hlua_arg2lua(stream->hlua->T, arg_p);
6552 stream->hlua->nargs++;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006553 }
6554
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006555 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006556 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006557
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006558 /* At this point the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006559 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006560 }
6561
6562 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006563 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006564 /* finished. */
6565 case HLUA_E_OK:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006566 if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) {
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006567 si_retnclose(&stream->si[0], &msg);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02006568 return 0;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006569 }
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02006570 /* If the stack is empty, the function fails. */
6571 if (lua_gettop(stream->hlua->T) <= 0)
6572 return 0;
6573
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006574 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006575 hlua_lua2smp(stream->hlua->T, -1, smp);
6576 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006577
6578 /* Set the end of execution flag. */
6579 smp->flags &= ~SMP_F_MAY_CHANGE;
6580 return 1;
6581
6582 /* yield. */
6583 case HLUA_E_AGAIN:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006584 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006585 si_retnclose(&stream->si[0], &msg);
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006586 SEND_ERR(smp->px, "Lua sample-fetch '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006587 return 0;
6588
6589 /* finished with error. */
6590 case HLUA_E_ERRMSG:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006591 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006592 si_retnclose(&stream->si[0], &msg);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006593 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006594 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006595 fcn->name, lua_tostring(stream->hlua->T, -1));
6596 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006597 return 0;
6598
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006599 case HLUA_E_ETMOUT:
6600 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006601 si_retnclose(&stream->si[0], &msg);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006602 SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name);
6603 return 0;
6604
6605 case HLUA_E_NOMEM:
6606 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006607 si_retnclose(&stream->si[0], &msg);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006608 SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name);
6609 return 0;
6610
6611 case HLUA_E_YIELD:
6612 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006613 si_retnclose(&stream->si[0], &msg);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006614 SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name);
6615 return 0;
6616
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006617 case HLUA_E_ERR:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006618 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006619 si_retnclose(&stream->si[0], &msg);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006620 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006621 SEND_ERR(smp->px, "Lua sample-fetch '%s' returns an unknown error.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006622
6623 default:
6624 return 0;
6625 }
6626}
6627
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006628/* This function is an LUA binding used for registering
6629 * "sample-conv" functions. It expects a converter name used
6630 * in the haproxy configuration file, and an LUA function.
6631 */
6632__LJMP static int hlua_register_converters(lua_State *L)
6633{
6634 struct sample_conv_kw_list *sck;
6635 const char *name;
6636 int ref;
6637 int len;
6638 struct hlua_function *fcn;
6639
6640 MAY_LJMP(check_args(L, 2, "register_converters"));
6641
6642 /* First argument : converter name. */
6643 name = MAY_LJMP(luaL_checkstring(L, 1));
6644
6645 /* Second argument : lua function. */
6646 ref = MAY_LJMP(hlua_checkfunction(L, 2));
6647
6648 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006649 sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006650 if (!sck)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006651 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006652 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006653 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006654 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006655
6656 /* Fill fcn. */
6657 fcn->name = strdup(name);
6658 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006659 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006660 fcn->function_ref = ref;
6661
6662 /* List head */
6663 sck->list.n = sck->list.p = NULL;
6664
6665 /* converter keyword. */
6666 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006667 sck->kw[0].kw = calloc(1, len);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006668 if (!sck->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006669 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006670
6671 snprintf((char *)sck->kw[0].kw, len, "lua.%s", name);
6672 sck->kw[0].process = hlua_sample_conv_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01006673 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 +01006674 sck->kw[0].val_args = NULL;
6675 sck->kw[0].in_type = SMP_T_STR;
6676 sck->kw[0].out_type = SMP_T_STR;
6677 sck->kw[0].private = fcn;
6678
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006679 /* Register this new converter */
6680 sample_register_convs(sck);
6681
6682 return 0;
6683}
6684
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006685/* This function is an LUA binding used for registering
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006686 * "sample-fetch" functions. It expects a converter name used
6687 * in the haproxy configuration file, and an LUA function.
6688 */
6689__LJMP static int hlua_register_fetches(lua_State *L)
6690{
6691 const char *name;
6692 int ref;
6693 int len;
6694 struct sample_fetch_kw_list *sfk;
6695 struct hlua_function *fcn;
6696
6697 MAY_LJMP(check_args(L, 2, "register_fetches"));
6698
6699 /* First argument : sample-fetch name. */
6700 name = MAY_LJMP(luaL_checkstring(L, 1));
6701
6702 /* Second argument : lua function. */
6703 ref = MAY_LJMP(hlua_checkfunction(L, 2));
6704
6705 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006706 sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006707 if (!sfk)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006708 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006709 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006710 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006711 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006712
6713 /* Fill fcn. */
6714 fcn->name = strdup(name);
6715 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006716 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006717 fcn->function_ref = ref;
6718
6719 /* List head */
6720 sfk->list.n = sfk->list.p = NULL;
6721
6722 /* sample-fetch keyword. */
6723 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006724 sfk->kw[0].kw = calloc(1, len);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006725 if (!sfk->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006726 return luaL_error(L, "Lua out of memory error.");
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006727
6728 snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name);
6729 sfk->kw[0].process = hlua_sample_fetch_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01006730 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 +01006731 sfk->kw[0].val_args = NULL;
6732 sfk->kw[0].out_type = SMP_T_STR;
6733 sfk->kw[0].use = SMP_USE_HTTP_ANY;
6734 sfk->kw[0].val = 0;
6735 sfk->kw[0].private = fcn;
6736
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006737 /* Register this new fetch. */
6738 sample_register_fetches(sfk);
6739
6740 return 0;
6741}
6742
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006743/* This function is a wrapper to execute each LUA function declared
6744 * as an action wrapper during the initialisation period. This function
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006745 * return ACT_RET_CONT if the processing is finished (with or without
6746 * error) and return ACT_RET_YIELD if the function must be called again
6747 * because the LUA returns a yield.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006748 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006749static enum act_return hlua_action(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +02006750 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006751{
6752 char **arg;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006753 unsigned int analyzer;
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02006754 int dir;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006755 const char *error;
Willy Tarreau83061a82018-07-13 11:56:34 +02006756 const struct buffer msg = { };
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006757
6758 switch (rule->from) {
Thierry FOURNIER6e01f382015-11-02 09:52:54 +01006759 case ACT_F_TCP_REQ_CNT: analyzer = AN_REQ_INSPECT_FE ; dir = SMP_OPT_DIR_REQ; break;
6760 case ACT_F_TCP_RES_CNT: analyzer = AN_RES_INSPECT ; dir = SMP_OPT_DIR_RES; break;
6761 case ACT_F_HTTP_REQ: analyzer = AN_REQ_HTTP_PROCESS_FE; dir = SMP_OPT_DIR_REQ; break;
6762 case ACT_F_HTTP_RES: analyzer = AN_RES_HTTP_PROCESS_BE; dir = SMP_OPT_DIR_RES; break;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006763 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006764 SEND_ERR(px, "Lua: internal error while execute action.\n");
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006765 return ACT_RET_CONT;
6766 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006767
Willy Tarreau87b09662015-04-03 00:22:06 +02006768 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006769 * Lua context can be not initialized. This behavior
6770 * permits to save performances because a systematic
6771 * Lua initialization cause 5% performances loss.
6772 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006773 if (!s->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006774 s->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006775 if (!s->hlua) {
6776 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
6777 rule->arg.hlua_rule->fcn.name);
6778 return ACT_RET_CONT;
6779 }
6780 if (!hlua_ctx_init(s->hlua, s->task)) {
6781 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
6782 rule->arg.hlua_rule->fcn.name);
6783 return ACT_RET_CONT;
6784 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006785 }
6786
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006787 consistency_set(s, dir, &s->hlua->cons);
6788
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006789 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006790 if (!HLUA_IS_RUNNING(s->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006791
6792 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006793 if (!SET_SAFE_LJMP(s->hlua->T)) {
6794 if (lua_type(s->hlua->T, -1) == LUA_TSTRING)
6795 error = lua_tostring(s->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006796 else
6797 error = "critical error";
6798 SEND_ERR(px, "Lua function '%s': %s.\n",
6799 rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006800 return ACT_RET_CONT;
6801 }
6802
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006803 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006804 if (!lua_checkstack(s->hlua->T, 1)) {
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 }
6810
6811 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006812 lua_rawgeti(s->hlua->T, LUA_REGISTRYINDEX, rule->arg.hlua_rule->fcn.function_ref);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006813
Willy Tarreau87b09662015-04-03 00:22:06 +02006814 /* Create and and push object stream in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006815 if (!hlua_txn_new(s->hlua->T, s, px, dir, 0)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006816 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006817 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006818 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006819 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006820 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006821 s->hlua->nargs = 1;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006822
6823 /* push keywords in the stack. */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006824 for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006825 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006826 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006827 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006828 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006829 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006830 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006831 lua_pushstring(s->hlua->T, *arg);
6832 s->hlua->nargs++;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006833 }
6834
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006835 /* Now the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006836 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006837
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006838 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006839 s->hlua->max_time = hlua_timeout_session;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006840 }
6841
6842 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006843 switch (hlua_ctx_resume(s->hlua, !(flags & ACT_FLAG_FINAL))) {
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006844 /* finished. */
6845 case HLUA_E_OK:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006846 if (!consistency_check(s, dir, &s->hlua->cons)) {
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006847 si_retnclose(&s->si[0], &msg);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02006848 return ACT_RET_ERR;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006849 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006850 if (s->hlua->flags & HLUA_STOP)
Thierry FOURNIER9bd52d42016-07-14 11:45:33 +02006851 return ACT_RET_STOP;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006852 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006853
6854 /* yield. */
6855 case HLUA_E_AGAIN:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006856 /* Set timeout in the required channel. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006857 if (s->hlua->wake_time != TICK_ETERNITY) {
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006858 if (analyzer & (AN_REQ_INSPECT_FE|AN_REQ_HTTP_PROCESS_FE))
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006859 s->req.analyse_exp = s->hlua->wake_time;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006860 else if (analyzer & (AN_RES_INSPECT|AN_RES_HTTP_PROCESS_BE))
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006861 s->res.analyse_exp = s->hlua->wake_time;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006862 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006863 /* Some actions can be wake up when a "write" event
6864 * is detected on a response channel. This is useful
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006865 * only for actions targeted on the requests.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006866 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006867 if (HLUA_IS_WAKERESWR(s->hlua)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006868 s->res.flags |= CF_WAKE_WRITE;
Willy Tarreau76bd97f2015-03-10 17:16:10 +01006869 if ((analyzer & (AN_REQ_INSPECT_FE|AN_REQ_HTTP_PROCESS_FE)))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006870 s->res.analysers |= analyzer;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006871 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006872 if (HLUA_IS_WAKEREQWR(s->hlua))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006873 s->req.flags |= CF_WAKE_WRITE;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006874 /* We can quit the function without consistency check
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006875 * because HAProxy is not able to manipulate data, it
6876 * is only allowed to call me again. */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006877 return ACT_RET_YIELD;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006878
6879 /* finished with error. */
6880 case HLUA_E_ERRMSG:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006881 if (!consistency_check(s, dir, &s->hlua->cons)) {
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006882 si_retnclose(&s->si[0], &msg);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02006883 return ACT_RET_ERR;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006884 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006885 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006886 SEND_ERR(px, "Lua function '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006887 rule->arg.hlua_rule->fcn.name, lua_tostring(s->hlua->T, -1));
6888 lua_pop(s->hlua->T, 1);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006889 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006890
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006891 case HLUA_E_ETMOUT:
6892 if (!consistency_check(s, dir, &s->hlua->cons)) {
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006893 si_retnclose(&s->si[0], &msg);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006894 return ACT_RET_ERR;
6895 }
6896 SEND_ERR(px, "Lua function '%s': execution timeout.\n", rule->arg.hlua_rule->fcn.name);
6897 return 0;
6898
6899 case HLUA_E_NOMEM:
6900 if (!consistency_check(s, dir, &s->hlua->cons)) {
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006901 si_retnclose(&s->si[0], &msg);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006902 return ACT_RET_ERR;
6903 }
6904 SEND_ERR(px, "Lua function '%s': out of memory error.\n", rule->arg.hlua_rule->fcn.name);
6905 return 0;
6906
6907 case HLUA_E_YIELD:
6908 if (!consistency_check(s, dir, &s->hlua->cons)) {
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006909 si_retnclose(&s->si[0], &msg);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006910 return ACT_RET_ERR;
6911 }
6912 SEND_ERR(px, "Lua function '%s': aborting Lua processing on expired timeout.\n",
6913 rule->arg.hlua_rule->fcn.name);
6914 return 0;
6915
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006916 case HLUA_E_ERR:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006917 if (!consistency_check(s, dir, &s->hlua->cons)) {
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006918 si_retnclose(&s->si[0], &msg);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02006919 return ACT_RET_ERR;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006920 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006921 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006922 SEND_ERR(px, "Lua function '%s' return an unknown error.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006923 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006924
6925 default:
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006926 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006927 }
6928}
6929
Olivier Houchard9f6af332018-05-25 14:04:04 +02006930struct task *hlua_applet_wakeup(struct task *t, void *context, unsigned short state)
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006931{
Olivier Houchard9f6af332018-05-25 14:04:04 +02006932 struct appctx *ctx = context;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006933
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006934 appctx_wakeup(ctx);
Willy Tarreaud9587412017-08-23 16:07:33 +02006935 t->expire = TICK_ETERNITY;
Willy Tarreaud1aa41f2017-07-21 16:41:56 +02006936 return t;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006937}
6938
6939static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
6940{
6941 struct stream_interface *si = ctx->owner;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006942 struct hlua *hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006943 struct task *task;
6944 char **arg;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006945 const char *error;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006946
Willy Tarreaubafbe012017-11-24 17:34:44 +01006947 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006948 if (!hlua) {
6949 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
6950 ctx->rule->arg.hlua_rule->fcn.name);
6951 return 0;
6952 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006953 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006954 ctx->ctx.hlua_apptcp.hlua = hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006955 ctx->ctx.hlua_apptcp.flags = 0;
6956
6957 /* Create task used by signal to wakeup applets. */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01006958 task = task_new(tid_bit);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006959 if (!task) {
6960 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
6961 ctx->rule->arg.hlua_rule->fcn.name);
6962 return 0;
6963 }
6964 task->nice = 0;
6965 task->context = ctx;
6966 task->process = hlua_applet_wakeup;
6967 ctx->ctx.hlua_apptcp.task = task;
6968
6969 /* In the execution wrappers linked with a stream, the
6970 * Lua context can be not initialized. This behavior
6971 * permits to save performances because a systematic
6972 * Lua initialization cause 5% performances loss.
6973 */
6974 if (!hlua_ctx_init(hlua, task)) {
6975 SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n",
6976 ctx->rule->arg.hlua_rule->fcn.name);
6977 return 0;
6978 }
6979
6980 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02006981 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006982
6983 /* The following Lua calls can fail. */
6984 if (!SET_SAFE_LJMP(hlua->T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01006985 if (lua_type(hlua->T, -1) == LUA_TSTRING)
6986 error = lua_tostring(hlua->T, -1);
6987 else
6988 error = "critical error";
6989 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
6990 ctx->rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006991 return 0;
6992 }
6993
6994 /* Check stack available size. */
6995 if (!lua_checkstack(hlua->T, 1)) {
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
7002 /* Restore the function in the stack. */
7003 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref);
7004
7005 /* Create and and push object stream in the stack. */
7006 if (!hlua_applet_tcp_new(hlua->T, ctx)) {
7007 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
7008 ctx->rule->arg.hlua_rule->fcn.name);
7009 RESET_SAFE_LJMP(hlua->T);
7010 return 0;
7011 }
7012 hlua->nargs = 1;
7013
7014 /* push keywords in the stack. */
7015 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
7016 if (!lua_checkstack(hlua->T, 1)) {
7017 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
7018 ctx->rule->arg.hlua_rule->fcn.name);
7019 RESET_SAFE_LJMP(hlua->T);
7020 return 0;
7021 }
7022 lua_pushstring(hlua->T, *arg);
7023 hlua->nargs++;
7024 }
7025
7026 RESET_SAFE_LJMP(hlua->T);
7027
7028 /* Wakeup the applet ASAP. */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01007029 si_cant_get(si);
Willy Tarreau3367d412018-11-15 10:57:41 +01007030 si_rx_endp_more(si);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007031
7032 return 1;
7033}
7034
7035static void hlua_applet_tcp_fct(struct appctx *ctx)
7036{
7037 struct stream_interface *si = ctx->owner;
7038 struct stream *strm = si_strm(si);
7039 struct channel *res = si_ic(si);
7040 struct act_rule *rule = ctx->rule;
7041 struct proxy *px = strm->be;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007042 struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007043
7044 /* The applet execution is already done. */
Olivier Houchard594c8c52018-08-28 14:41:31 +02007045 if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) {
7046 /* eat the whole request */
7047 co_skip(si_oc(si), co_data(si_oc(si)));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007048 return;
Olivier Houchard594c8c52018-08-28 14:41:31 +02007049 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007050
7051 /* If the stream is disconnect or closed, ldo nothing. */
7052 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
7053 return;
7054
7055 /* Execute the function. */
7056 switch (hlua_ctx_resume(hlua, 1)) {
7057 /* finished. */
7058 case HLUA_E_OK:
7059 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
7060
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007061 /* eat the whole request */
Willy Tarreaua79021a2018-06-15 18:07:57 +02007062 co_skip(si_oc(si), co_data(si_oc(si)));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007063 res->flags |= CF_READ_NULL;
7064 si_shutr(si);
7065 return;
7066
7067 /* yield. */
7068 case HLUA_E_AGAIN:
Thierry Fournier0164f202016-02-20 17:47:43 +01007069 if (hlua->wake_time != TICK_ETERNITY)
7070 task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007071 return;
7072
7073 /* finished with error. */
7074 case HLUA_E_ERRMSG:
7075 /* Display log. */
7076 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
7077 rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1));
7078 lua_pop(hlua->T, 1);
7079 goto error;
7080
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007081 case HLUA_E_ETMOUT:
7082 SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n",
7083 rule->arg.hlua_rule->fcn.name);
7084 goto error;
7085
7086 case HLUA_E_NOMEM:
7087 SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n",
7088 rule->arg.hlua_rule->fcn.name);
7089 goto error;
7090
7091 case HLUA_E_YIELD: /* unexpected */
7092 SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n",
7093 rule->arg.hlua_rule->fcn.name);
7094 goto error;
7095
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007096 case HLUA_E_ERR:
7097 /* Display log. */
7098 SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n",
7099 rule->arg.hlua_rule->fcn.name);
7100 goto error;
7101
7102 default:
7103 goto error;
7104 }
7105
7106error:
7107
7108 /* For all other cases, just close the stream. */
7109 si_shutw(si);
7110 si_shutr(si);
7111 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
7112}
7113
7114static void hlua_applet_tcp_release(struct appctx *ctx)
7115{
Willy Tarreaubd7fc952017-07-24 17:35:27 +02007116 task_delete(ctx->ctx.hlua_apptcp.task);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007117 task_free(ctx->ctx.hlua_apptcp.task);
7118 ctx->ctx.hlua_apptcp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007119 hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007120 ctx->ctx.hlua_apptcp.hlua = NULL;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007121}
7122
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007123/* The function returns 1 if the initialisation is complete, 0 if
7124 * an errors occurs and -1 if more data are required for initializing
7125 * the applet.
7126 */
7127static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
7128{
7129 struct stream_interface *si = ctx->owner;
7130 struct channel *req = si_oc(si);
7131 struct http_msg *msg;
7132 struct http_txn *txn;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007133 struct hlua *hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007134 char **arg;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007135 struct task *task;
Thierry Fournierfd107a22016-02-19 19:57:23 +01007136 const char *error;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007137
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007138 txn = strm->txn;
7139 msg = &txn->req;
7140
Willy Tarreau0078bfc2015-10-07 20:20:28 +02007141 /* We want two things in HTTP mode :
7142 * - enforce server-close mode if we were in keep-alive, so that the
7143 * applet is released after each response ;
7144 * - enable request body transfer to the applet in order to resync
7145 * with the response body.
7146 */
7147 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)
7148 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreau0078bfc2015-10-07 20:20:28 +02007149
Willy Tarreaubafbe012017-11-24 17:34:44 +01007150 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007151 if (!hlua) {
7152 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
7153 ctx->rule->arg.hlua_rule->fcn.name);
7154 return 0;
7155 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007156 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007157 ctx->ctx.hlua_apphttp.hlua = hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007158 ctx->ctx.hlua_apphttp.left_bytes = -1;
7159 ctx->ctx.hlua_apphttp.flags = 0;
7160
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +01007161 if (txn->req.flags & HTTP_MSGF_VER_11)
7162 ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11;
7163
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007164 /* Create task used by signal to wakeup applets. */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01007165 task = task_new(tid_bit);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007166 if (!task) {
7167 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
7168 ctx->rule->arg.hlua_rule->fcn.name);
7169 return 0;
7170 }
7171 task->nice = 0;
7172 task->context = ctx;
7173 task->process = hlua_applet_wakeup;
7174 ctx->ctx.hlua_apphttp.task = task;
7175
7176 /* In the execution wrappers linked with a stream, the
7177 * Lua context can be not initialized. This behavior
7178 * permits to save performances because a systematic
7179 * Lua initialization cause 5% performances loss.
7180 */
7181 if (!hlua_ctx_init(hlua, task)) {
7182 SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n",
7183 ctx->rule->arg.hlua_rule->fcn.name);
7184 return 0;
7185 }
7186
7187 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02007188 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007189
7190 /* The following Lua calls can fail. */
7191 if (!SET_SAFE_LJMP(hlua->T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01007192 if (lua_type(hlua->T, -1) == LUA_TSTRING)
7193 error = lua_tostring(hlua->T, -1);
7194 else
7195 error = "critical error";
7196 SEND_ERR(px, "Lua applet http '%s': %s.\n",
7197 ctx->rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007198 return 0;
7199 }
7200
7201 /* Check stack available size. */
7202 if (!lua_checkstack(hlua->T, 1)) {
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
7209 /* Restore the function in the stack. */
7210 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref);
7211
7212 /* Create and and push object stream in the stack. */
7213 if (!hlua_applet_http_new(hlua->T, ctx)) {
7214 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
7215 ctx->rule->arg.hlua_rule->fcn.name);
7216 RESET_SAFE_LJMP(hlua->T);
7217 return 0;
7218 }
7219 hlua->nargs = 1;
7220
7221 /* Look for a 100-continue expected. */
7222 if (msg->flags & HTTP_MSGF_VER_11) {
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007223 if (IS_HTX_STRM(si_strm(si))) {
7224 /* HTX version */
7225 struct htx *htx = htxbuf(&req->buf);
7226 struct ist hdr = { .ptr = "Expect", .len = 6 };
7227 struct http_hdr_ctx hdr_ctx;
7228
7229 hdr_ctx.blk = NULL;
7230 /* Expect is allowed in 1.1, look for it */
7231 if (http_find_header(htx, hdr, &hdr_ctx, 0) &&
7232 unlikely(isteqi(hdr_ctx.value, ist2("100-continue", 12))))
7233 ctx->ctx.hlua_apphttp.flags |= APPLET_100C;
7234 }
7235 else {
7236 /* Legacy HTTP version */
7237 struct hdr_ctx hdr;
7238
7239 hdr.idx = 0;
7240 if (http_find_header2("Expect", 6, ci_head(req), &txn->hdr_idx, &hdr) &&
7241 unlikely(hdr.vlen == 12 && strncasecmp(hdr.line+hdr.val, "100-continue", 12) == 0))
7242 ctx->ctx.hlua_apphttp.flags |= APPLET_100C;
7243 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007244 }
7245
7246 /* push keywords in the stack. */
7247 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
7248 if (!lua_checkstack(hlua->T, 1)) {
7249 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
7250 ctx->rule->arg.hlua_rule->fcn.name);
7251 RESET_SAFE_LJMP(hlua->T);
7252 return 0;
7253 }
7254 lua_pushstring(hlua->T, *arg);
7255 hlua->nargs++;
7256 }
7257
7258 RESET_SAFE_LJMP(hlua->T);
7259
7260 /* Wakeup the applet when data is ready for read. */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01007261 si_cant_get(si);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007262
7263 return 1;
7264}
7265
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007266static void hlua_applet_htx_fct(struct appctx *ctx)
7267{
7268 struct stream_interface *si = ctx->owner;
7269 struct stream *strm = si_strm(si);
7270 struct channel *req = si_oc(si);
7271 struct channel *res = si_ic(si);
7272 struct act_rule *rule = ctx->rule;
7273 struct proxy *px = strm->be;
7274 struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua;
7275 struct htx *req_htx, *res_htx;
7276
7277 res_htx = htx_from_buf(&res->buf);
7278
7279 /* If the stream is disconnect or closed, ldo nothing. */
7280 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
7281 goto out;
7282
7283 /* Check if the input buffer is avalaible. */
7284 if (!b_size(&res->buf)) {
7285 si_rx_room_blk(si);
7286 goto out;
7287 }
7288 /* check that the output is not closed */
7289 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW))
7290 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7291
7292 /* Set the currently running flag. */
7293 if (!HLUA_IS_RUNNING(hlua) &&
7294 !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
7295 struct htx_blk *blk;
7296 size_t count = co_data(req);
7297
7298 if (!count) {
7299 si_cant_get(si);
7300 goto out;
7301 }
7302
7303 /* We need to flush the request header. This left the body for
7304 * the Lua.
7305 */
7306 req_htx = htx_from_buf(&req->buf);
7307 blk = htx_get_head_blk(req_htx);
7308 while (count && blk) {
7309 enum htx_blk_type type = htx_get_blk_type(blk);
7310 uint32_t sz = htx_get_blksz(blk);
7311
7312 if (sz > count) {
7313 si_cant_get(si);
7314 goto out;
7315 }
7316
7317 count -= sz;
7318 co_set_data(req, co_data(req) - sz);
7319 blk = htx_remove_blk(req_htx, blk);
7320
7321 if (type == HTX_BLK_EOH)
7322 break;
7323 }
7324 }
7325
7326 /* Executes The applet if it is not done. */
7327 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
7328
7329 /* Execute the function. */
7330 switch (hlua_ctx_resume(hlua, 1)) {
7331 /* finished. */
7332 case HLUA_E_OK:
7333 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7334 break;
7335
7336 /* yield. */
7337 case HLUA_E_AGAIN:
7338 if (hlua->wake_time != TICK_ETERNITY)
7339 task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time);
7340 return;
7341
7342 /* finished with error. */
7343 case HLUA_E_ERRMSG:
7344 /* Display log. */
7345 SEND_ERR(px, "Lua applet http '%s': %s.\n",
7346 rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1));
7347 lua_pop(hlua->T, 1);
7348 goto error;
7349
7350 case HLUA_E_ETMOUT:
7351 SEND_ERR(px, "Lua applet http '%s': execution timeout.\n",
7352 rule->arg.hlua_rule->fcn.name);
7353 goto error;
7354
7355 case HLUA_E_NOMEM:
7356 SEND_ERR(px, "Lua applet http '%s': out of memory error.\n",
7357 rule->arg.hlua_rule->fcn.name);
7358 goto error;
7359
7360 case HLUA_E_YIELD: /* unexpected */
7361 SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n",
7362 rule->arg.hlua_rule->fcn.name);
7363 goto error;
7364
7365 case HLUA_E_ERR:
7366 /* Display log. */
7367 SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n",
7368 rule->arg.hlua_rule->fcn.name);
7369 goto error;
7370
7371 default:
7372 goto error;
7373 }
7374 }
7375
7376 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
7377 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT))
7378 goto error;
7379
7380 /* Don't add EOD and TLR because mux-h1 will take care of it */
7381 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
7382 si_rx_room_blk(si);
7383 goto out;
7384 }
7385 res->total++;
7386 res->flags |= CF_READ_PARTIAL;
7387 }
7388
7389 done:
7390 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
7391 /* eat the whole request */
7392 req_htx = htxbuf(&req->buf);
7393 htx_reset(req_htx);
7394 htx_to_buf(req_htx, &req->buf);
7395 co_set_data(req, 0);
7396 res->flags |= CF_READ_NULL;
7397 si_shutr(si);
7398 }
7399
7400 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
7401 si_shutw(si);
7402
7403 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
7404 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST)) {
7405 si_shutr(si);
7406 res->flags |= CF_READ_NULL;
7407 }
7408 }
7409
7410 out:
7411 /* we have left the request in the buffer for the case where we
7412 * process a POST, and this automatically re-enables activity on
7413 * read. It's better to indicate that we want to stop reading when
7414 * we're sending, so that we know there's at most one direction
7415 * deciding to wake the applet up. It saves it from looping when
7416 * emitting large blocks into small TCP windows.
7417 */
7418 htx_to_buf(res_htx, &res->buf);
7419 if (!channel_is_empty(res))
7420 si_stop_get(si);
7421 return;
7422
7423 error:
7424
7425 /* If we are in HTTP mode, and we are not send any
7426 * data, return a 500 server error in best effort:
7427 * if there is no room available in the buffer,
7428 * just close the connection.
7429 */
7430 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
7431 struct buffer *err = &htx_err_chunks[HTTP_ERR_500];
7432
7433 channel_erase(res);
7434 res->buf.data = b_data(err);
7435 memcpy(res->buf.area, b_head(err), b_data(err));
7436 res_htx = htx_from_buf(&res->buf);
7437
7438 res->total += res_htx->data;
7439 res->flags |= CF_READ_PARTIAL;
7440 }
7441 if (!(strm->flags & SF_ERR_MASK))
7442 strm->flags |= SF_ERR_RESOURCE;
7443 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7444 goto done;
7445}
7446
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007447static void hlua_applet_http_fct(struct appctx *ctx)
7448{
7449 struct stream_interface *si = ctx->owner;
7450 struct stream *strm = si_strm(si);
7451 struct channel *res = si_ic(si);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007452 struct act_rule *rule = ctx->rule;
7453 struct proxy *px = strm->be;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007454 struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua;
Willy Tarreau206ba832018-06-14 15:27:31 +02007455 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02007456 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02007457 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02007458 size_t len2;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007459 int ret;
7460
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007461 if (IS_HTX_STRM(strm))
7462 return hlua_applet_htx_fct(ctx);
7463
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007464 /* If the stream is disconnect or closed, ldo nothing. */
7465 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
7466 return;
7467
7468 /* Set the currently running flag. */
7469 if (!HLUA_IS_RUNNING(hlua) &&
7470 !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007471 /* Store the max amount of bytes that we can read. */
7472 ctx->ctx.hlua_apphttp.left_bytes = strm->txn->req.body_len;
7473
7474 /* We need to flush the request header. This left the body
7475 * for the Lua.
7476 */
7477
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007478 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02007479 ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007480 if (ret == -1)
7481 return;
7482
7483 /* No data available, ask for more data. */
7484 if (ret == 1)
7485 len2 = 0;
7486 if (ret == 0)
7487 len1 = 0;
Thierry FOURNIER70d318c2018-06-30 10:37:33 +02007488 if (len1 + len2 < strm->txn->req.eoh + strm->txn->req.eol) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01007489 si_cant_get(si);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007490 return;
7491 }
7492
7493 /* skip the requests bytes. */
Thierry FOURNIER70d318c2018-06-30 10:37:33 +02007494 co_skip(si_oc(si), strm->txn->req.eoh + strm->txn->req.eol);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007495 }
7496
7497 /* Executes The applet if it is not done. */
7498 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
7499
7500 /* Execute the function. */
7501 switch (hlua_ctx_resume(hlua, 1)) {
7502 /* finished. */
7503 case HLUA_E_OK:
7504 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7505 break;
7506
7507 /* yield. */
7508 case HLUA_E_AGAIN:
Thierry Fournier0164f202016-02-20 17:47:43 +01007509 if (hlua->wake_time != TICK_ETERNITY)
7510 task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007511 return;
7512
7513 /* finished with error. */
7514 case HLUA_E_ERRMSG:
7515 /* Display log. */
7516 SEND_ERR(px, "Lua applet http '%s': %s.\n",
7517 rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1));
7518 lua_pop(hlua->T, 1);
7519 goto error;
7520
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007521 case HLUA_E_ETMOUT:
7522 SEND_ERR(px, "Lua applet http '%s': execution timeout.\n",
7523 rule->arg.hlua_rule->fcn.name);
7524 goto error;
7525
7526 case HLUA_E_NOMEM:
7527 SEND_ERR(px, "Lua applet http '%s': out of memory error.\n",
7528 rule->arg.hlua_rule->fcn.name);
7529 goto error;
7530
7531 case HLUA_E_YIELD: /* unexpected */
7532 SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n",
7533 rule->arg.hlua_rule->fcn.name);
7534 goto error;
7535
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007536 case HLUA_E_ERR:
7537 /* Display log. */
7538 SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n",
7539 rule->arg.hlua_rule->fcn.name);
7540 goto error;
7541
7542 default:
7543 goto error;
7544 }
7545 }
7546
7547 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
Christopher Fauletcc26b132018-12-18 21:20:57 +01007548 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT))
7549 goto error;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007550
7551 /* We must send the final chunk. */
7552 if (ctx->ctx.hlua_apphttp.flags & APPLET_CHUNKED &&
7553 !(ctx->ctx.hlua_apphttp.flags & APPLET_LAST_CHK)) {
7554
7555 /* sent last chunk at once. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02007556 ret = ci_putblk(res, "0\r\n\r\n", 5);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007557
7558 /* critical error. */
7559 if (ret == -2 || ret == -3) {
7560 SEND_ERR(px, "Lua applet http '%s'cannont send last chunk.\n",
7561 rule->arg.hlua_rule->fcn.name);
7562 goto error;
7563 }
7564
7565 /* no enough space error. */
7566 if (ret == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01007567 si_rx_room_blk(si);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007568 return;
7569 }
7570
7571 /* set the last chunk sent. */
7572 ctx->ctx.hlua_apphttp.flags |= APPLET_LAST_CHK;
7573 }
7574
7575 /* close the connection. */
7576
Frédéric Lécaille83ed5d52018-07-18 14:25:26 +02007577 /* status */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007578 strm->txn->status = ctx->ctx.hlua_apphttp.status;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007579
7580 /* eat the whole request */
Willy Tarreaua79021a2018-06-15 18:07:57 +02007581 co_skip(si_oc(si), co_data(si_oc(si)));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007582 res->flags |= CF_READ_NULL;
7583 si_shutr(si);
7584
7585 return;
7586 }
7587
7588error:
7589
7590 /* If we are in HTTP mode, and we are not send any
7591 * data, return a 500 server error in best effort:
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007592 * if there is no room available in the buffer,
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007593 * just close the connection.
7594 */
Willy Tarreau06d80a92017-10-19 14:32:15 +02007595 ci_putblk(res, error_500, strlen(error_500));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007596 if (!(strm->flags & SF_ERR_MASK))
7597 strm->flags |= SF_ERR_RESOURCE;
7598 si_shutw(si);
7599 si_shutr(si);
7600 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7601}
7602
7603static void hlua_applet_http_release(struct appctx *ctx)
7604{
Willy Tarreaubd7fc952017-07-24 17:35:27 +02007605 task_delete(ctx->ctx.hlua_apphttp.task);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007606 task_free(ctx->ctx.hlua_apphttp.task);
7607 ctx->ctx.hlua_apphttp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007608 hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007609 ctx->ctx.hlua_apphttp.hlua = NULL;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007610}
7611
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007612/* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in
7613 * succes case, else return ACT_RET_PRS_ERR.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007614 *
7615 * This function can fail with an abort() due to an Lua critical error.
7616 * We are in the configuration parsing process of HAProxy, this abort() is
7617 * tolerated.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007618 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007619static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px,
7620 struct act_rule *rule, char **err)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007621{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007622 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007623 int i;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007624
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007625 /* Memory for the rule. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007626 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007627 if (!rule->arg.hlua_rule) {
7628 memprintf(err, "out of memory error");
Thierry FOURNIERafa80492015-08-19 09:04:15 +02007629 return ACT_RET_PRS_ERR;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007630 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007631
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007632 /* Memory for arguments. */
7633 rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, sizeof(char *));
7634 if (!rule->arg.hlua_rule->args) {
7635 memprintf(err, "out of memory error");
7636 return ACT_RET_PRS_ERR;
7637 }
7638
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007639 /* Reference the Lua function and store the reference. */
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007640 rule->arg.hlua_rule->fcn = *fcn;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007641
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007642 /* Expect some arguments */
7643 for (i = 0; i < fcn->nargs; i++) {
7644 if (*args[i+1] == '\0') {
7645 memprintf(err, "expect %d arguments", fcn->nargs);
7646 return ACT_RET_PRS_ERR;
7647 }
7648 rule->arg.hlua_rule->args[i] = strdup(args[i + 1]);
7649 if (!rule->arg.hlua_rule->args[i]) {
7650 memprintf(err, "out of memory error");
7651 return ACT_RET_PRS_ERR;
7652 }
7653 (*cur_arg)++;
7654 }
7655 rule->arg.hlua_rule->args[i] = NULL;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007656
Thierry FOURNIER42148732015-09-02 17:17:33 +02007657 rule->action = ACT_CUSTOM;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007658 rule->action_ptr = hlua_action;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02007659 return ACT_RET_PRS_OK;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007660}
7661
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007662static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px,
7663 struct act_rule *rule, char **err)
7664{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007665 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007666
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01007667 /* HTTP applets are forbidden in tcp-request rules.
7668 * HTTP applet request requires everything initilized by
7669 * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER).
7670 * The applet will be immediately initilized, but its before
7671 * the call of this analyzer.
7672 */
7673 if (rule->from != ACT_F_HTTP_REQ) {
7674 memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets");
7675 return ACT_RET_PRS_ERR;
7676 }
7677
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007678 /* Memory for the rule. */
7679 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
7680 if (!rule->arg.hlua_rule) {
7681 memprintf(err, "out of memory error");
7682 return ACT_RET_PRS_ERR;
7683 }
7684
7685 /* Reference the Lua function and store the reference. */
7686 rule->arg.hlua_rule->fcn = *fcn;
7687
7688 /* TODO: later accept arguments. */
7689 rule->arg.hlua_rule->args = NULL;
7690
7691 /* Add applet pointer in the rule. */
7692 rule->applet.obj_type = OBJ_TYPE_APPLET;
7693 rule->applet.name = fcn->name;
7694 rule->applet.init = hlua_applet_http_init;
7695 rule->applet.fct = hlua_applet_http_fct;
7696 rule->applet.release = hlua_applet_http_release;
7697 rule->applet.timeout = hlua_timeout_applet;
7698
7699 return ACT_RET_PRS_OK;
7700}
7701
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007702/* This function is an LUA binding used for registering
7703 * "sample-conv" functions. It expects a converter name used
7704 * in the haproxy configuration file, and an LUA function.
7705 */
7706__LJMP static int hlua_register_action(lua_State *L)
7707{
7708 struct action_kw_list *akl;
7709 const char *name;
7710 int ref;
7711 int len;
7712 struct hlua_function *fcn;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007713 int nargs;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007714
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007715 /* Initialise the number of expected arguments at 0. */
7716 nargs = 0;
7717
7718 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
7719 WILL_LJMP(luaL_error(L, "'register_action' needs between 3 and 4 arguments"));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007720
7721 /* First argument : converter name. */
7722 name = MAY_LJMP(luaL_checkstring(L, 1));
7723
7724 /* Second argument : environment. */
7725 if (lua_type(L, 2) != LUA_TTABLE)
7726 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
7727
7728 /* Third argument : lua function. */
7729 ref = MAY_LJMP(hlua_checkfunction(L, 3));
7730
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007731 /* Fourth argument : number of mandatory arguments expected on the configuration line. */
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007732 if (lua_gettop(L) >= 4)
7733 nargs = MAY_LJMP(luaL_checkinteger(L, 4));
7734
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007735 /* browse the second argument as an array. */
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007736 lua_pushnil(L);
7737 while (lua_next(L, 2) != 0) {
7738 if (lua_type(L, -1) != LUA_TSTRING)
7739 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
7740
7741 /* Check required environment. Only accepted "http" or "tcp". */
7742 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007743 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007744 if (!akl)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007745 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007746 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007747 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007748 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007749
7750 /* Fill fcn. */
7751 fcn->name = strdup(name);
7752 if (!fcn->name)
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 fcn->function_ref = ref;
7755
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007756 /* Set the expected number od arguments. */
7757 fcn->nargs = nargs;
7758
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007759 /* List head */
7760 akl->list.n = akl->list.p = NULL;
7761
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007762 /* action keyword. */
7763 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007764 akl->kw[0].kw = calloc(1, len);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007765 if (!akl->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007766 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007767
7768 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
7769
7770 akl->kw[0].match_pfx = 0;
7771 akl->kw[0].private = fcn;
7772 akl->kw[0].parse = action_register_lua;
7773
7774 /* select the action registering point. */
7775 if (strcmp(lua_tostring(L, -1), "tcp-req") == 0)
7776 tcp_req_cont_keywords_register(akl);
7777 else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0)
7778 tcp_res_cont_keywords_register(akl);
7779 else if (strcmp(lua_tostring(L, -1), "http-req") == 0)
7780 http_req_keywords_register(akl);
7781 else if (strcmp(lua_tostring(L, -1), "http-res") == 0)
7782 http_res_keywords_register(akl);
7783 else
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007784 WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. "
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007785 "'tcp-req', 'tcp-res', 'http-req' or 'http-res' "
7786 "are expected.", lua_tostring(L, -1)));
7787
7788 /* pop the environment string. */
7789 lua_pop(L, 1);
7790 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007791 return ACT_RET_PRS_OK;
7792}
7793
7794static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px,
7795 struct act_rule *rule, char **err)
7796{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007797 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007798
Christopher Fauletafd8f102018-11-08 11:34:21 +01007799 if (px->options2 & PR_O2_USE_HTX) {
7800 memprintf(err, "Lua services cannot be used when the HTX internal representation is enabled");
7801 return ACT_RET_PRS_ERR;
7802 }
7803
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007804 /* Memory for the rule. */
7805 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
7806 if (!rule->arg.hlua_rule) {
7807 memprintf(err, "out of memory error");
7808 return ACT_RET_PRS_ERR;
7809 }
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007810
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007811 /* Reference the Lua function and store the reference. */
7812 rule->arg.hlua_rule->fcn = *fcn;
7813
7814 /* TODO: later accept arguments. */
7815 rule->arg.hlua_rule->args = NULL;
7816
7817 /* Add applet pointer in the rule. */
7818 rule->applet.obj_type = OBJ_TYPE_APPLET;
7819 rule->applet.name = fcn->name;
7820 rule->applet.init = hlua_applet_tcp_init;
7821 rule->applet.fct = hlua_applet_tcp_fct;
7822 rule->applet.release = hlua_applet_tcp_release;
7823 rule->applet.timeout = hlua_timeout_applet;
7824
7825 return 0;
7826}
7827
7828/* This function is an LUA binding used for registering
7829 * "sample-conv" functions. It expects a converter name used
7830 * in the haproxy configuration file, and an LUA function.
7831 */
7832__LJMP static int hlua_register_service(lua_State *L)
7833{
7834 struct action_kw_list *akl;
7835 const char *name;
7836 const char *env;
7837 int ref;
7838 int len;
7839 struct hlua_function *fcn;
7840
7841 MAY_LJMP(check_args(L, 3, "register_service"));
7842
7843 /* First argument : converter name. */
7844 name = MAY_LJMP(luaL_checkstring(L, 1));
7845
7846 /* Second argument : environment. */
7847 env = MAY_LJMP(luaL_checkstring(L, 2));
7848
7849 /* Third argument : lua function. */
7850 ref = MAY_LJMP(hlua_checkfunction(L, 3));
7851
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007852 /* Allocate and fill the sample fetch keyword struct. */
7853 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
7854 if (!akl)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007855 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007856 fcn = calloc(1, sizeof(*fcn));
7857 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007858 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007859
7860 /* Fill fcn. */
7861 len = strlen("<lua.>") + strlen(name) + 1;
7862 fcn->name = calloc(1, len);
7863 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007864 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007865 snprintf((char *)fcn->name, len, "<lua.%s>", name);
7866 fcn->function_ref = ref;
7867
7868 /* List head */
7869 akl->list.n = akl->list.p = NULL;
7870
7871 /* converter keyword. */
7872 len = strlen("lua.") + strlen(name) + 1;
7873 akl->kw[0].kw = calloc(1, len);
7874 if (!akl->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007875 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007876
7877 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
7878
Thierry FOURNIER / OZON.IO02564fd2016-11-12 11:07:05 +01007879 /* Check required environment. Only accepted "http" or "tcp". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007880 if (strcmp(env, "tcp") == 0)
7881 akl->kw[0].parse = action_register_service_tcp;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007882 else if (strcmp(env, "http") == 0)
7883 akl->kw[0].parse = action_register_service_http;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007884 else
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007885 WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. "
Eric Salamafe7456f2017-12-21 14:30:07 +01007886 "'tcp' or 'http' are expected.", env));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007887
7888 akl->kw[0].match_pfx = 0;
7889 akl->kw[0].private = fcn;
7890
7891 /* End of array. */
7892 memset(&akl->kw[1], 0, sizeof(*akl->kw));
7893
7894 /* Register this new converter */
7895 service_keywords_register(akl);
7896
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007897 return 0;
7898}
7899
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007900/* This function initialises Lua cli handler. It copies the
7901 * arguments in the Lua stack and create channel IO objects.
7902 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02007903static int hlua_cli_parse_fct(char **args, char *payload, struct appctx *appctx, void *private)
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007904{
7905 struct hlua *hlua;
7906 struct hlua_function *fcn;
7907 int i;
7908 const char *error;
7909
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007910 fcn = private;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007911 appctx->ctx.hlua_cli.fcn = private;
7912
Willy Tarreaubafbe012017-11-24 17:34:44 +01007913 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007914 if (!hlua) {
7915 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007916 return 1;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007917 }
7918 HLUA_INIT(hlua);
7919 appctx->ctx.hlua_cli.hlua = hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007920
7921 /* Create task used by signal to wakeup applets.
7922 * We use the same wakeup fonction than the Lua applet_tcp and
7923 * applet_http. It is absolutely compatible.
7924 */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01007925 appctx->ctx.hlua_cli.task = task_new(tid_bit);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007926 if (!appctx->ctx.hlua_cli.task) {
Thierry FOURNIERffbf5692016-12-16 11:14:06 +01007927 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007928 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007929 }
7930 appctx->ctx.hlua_cli.task->nice = 0;
7931 appctx->ctx.hlua_cli.task->context = appctx;
7932 appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup;
7933
7934 /* Initialises the Lua context */
7935 if (!hlua_ctx_init(hlua, appctx->ctx.hlua_cli.task)) {
7936 SEND_ERR(NULL, "Lua cli '%s': can't initialize Lua context.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007937 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007938 }
7939
7940 /* The following Lua calls can fail. */
7941 if (!SET_SAFE_LJMP(hlua->T)) {
7942 if (lua_type(hlua->T, -1) == LUA_TSTRING)
7943 error = lua_tostring(hlua->T, -1);
7944 else
7945 error = "critical error";
7946 SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error);
7947 goto error;
7948 }
7949
7950 /* Check stack available size. */
7951 if (!lua_checkstack(hlua->T, 2)) {
7952 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7953 goto error;
7954 }
7955
7956 /* Restore the function in the stack. */
7957 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
7958
7959 /* Once the arguments parsed, the CLI is like an AppletTCP,
7960 * so push AppletTCP in the stack.
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007961 */
7962 if (!hlua_applet_tcp_new(hlua->T, appctx)) {
7963 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7964 goto error;
7965 }
7966 hlua->nargs = 1;
7967
7968 /* push keywords in the stack. */
7969 for (i = 0; *args[i]; i++) {
7970 /* Check stack available size. */
7971 if (!lua_checkstack(hlua->T, 1)) {
7972 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7973 goto error;
7974 }
7975 lua_pushstring(hlua->T, args[i]);
7976 hlua->nargs++;
7977 }
7978
7979 /* We must initialize the execution timeouts. */
7980 hlua->max_time = hlua_timeout_session;
7981
7982 /* At this point the execution is safe. */
7983 RESET_SAFE_LJMP(hlua->T);
7984
7985 /* It's ok */
7986 return 0;
7987
7988 /* It's not ok. */
7989error:
7990 RESET_SAFE_LJMP(hlua->T);
7991 hlua_ctx_destroy(hlua);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007992 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007993 return 1;
7994}
7995
7996static int hlua_cli_io_handler_fct(struct appctx *appctx)
7997{
7998 struct hlua *hlua;
7999 struct stream_interface *si;
8000 struct hlua_function *fcn;
8001
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01008002 hlua = appctx->ctx.hlua_cli.hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008003 si = appctx->owner;
Willy Tarreau8ae4f752016-12-14 15:41:45 +01008004 fcn = appctx->ctx.hlua_cli.fcn;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008005
8006 /* If the stream is disconnect or closed, ldo nothing. */
8007 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
8008 return 1;
8009
8010 /* Execute the function. */
8011 switch (hlua_ctx_resume(hlua, 1)) {
8012
8013 /* finished. */
8014 case HLUA_E_OK:
8015 return 1;
8016
8017 /* yield. */
8018 case HLUA_E_AGAIN:
8019 /* We want write. */
8020 if (HLUA_IS_WAKERESWR(hlua))
Willy Tarreaudb398432018-11-15 11:08:52 +01008021 si_rx_room_blk(si);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008022 /* Set the timeout. */
8023 if (hlua->wake_time != TICK_ETERNITY)
8024 task_schedule(hlua->task, hlua->wake_time);
8025 return 0;
8026
8027 /* finished with error. */
8028 case HLUA_E_ERRMSG:
8029 /* Display log. */
8030 SEND_ERR(NULL, "Lua cli '%s': %s.\n",
8031 fcn->name, lua_tostring(hlua->T, -1));
8032 lua_pop(hlua->T, 1);
8033 return 1;
8034
Thierry Fournierd5b073c2018-05-21 19:42:47 +02008035 case HLUA_E_ETMOUT:
8036 SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n",
8037 fcn->name);
8038 return 1;
8039
8040 case HLUA_E_NOMEM:
8041 SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n",
8042 fcn->name);
8043 return 1;
8044
8045 case HLUA_E_YIELD: /* unexpected */
8046 SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n",
8047 fcn->name);
8048 return 1;
8049
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008050 case HLUA_E_ERR:
8051 /* Display log. */
8052 SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n",
8053 fcn->name);
8054 return 1;
8055
8056 default:
8057 return 1;
8058 }
8059
8060 return 1;
8061}
8062
8063static void hlua_cli_io_release_fct(struct appctx *appctx)
8064{
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01008065 hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01008066 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008067}
8068
8069/* This function is an LUA binding used for registering
8070 * new keywords in the cli. It expects a list of keywords
8071 * which are the "path". It is limited to 5 keywords. A
8072 * description of the command, a function to be executed
8073 * for the parsing and a function for io handlers.
8074 */
8075__LJMP static int hlua_register_cli(lua_State *L)
8076{
8077 struct cli_kw_list *cli_kws;
8078 const char *message;
8079 int ref_io;
8080 int len;
8081 struct hlua_function *fcn;
8082 int index;
8083 int i;
8084
8085 MAY_LJMP(check_args(L, 3, "register_cli"));
8086
8087 /* First argument : an array of maximum 5 keywords. */
8088 if (!lua_istable(L, 1))
8089 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table"));
8090
8091 /* Second argument : string with contextual message. */
8092 message = MAY_LJMP(luaL_checkstring(L, 2));
8093
8094 /* Third and fourth argument : lua function. */
8095 ref_io = MAY_LJMP(hlua_checkfunction(L, 3));
8096
8097 /* Allocate and fill the sample fetch keyword struct. */
8098 cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2);
8099 if (!cli_kws)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008100 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008101 fcn = calloc(1, sizeof(*fcn));
8102 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008103 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008104
8105 /* Fill path. */
8106 index = 0;
8107 lua_pushnil(L);
8108 while(lua_next(L, 1) != 0) {
8109 if (index >= 5)
8110 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries"));
8111 if (lua_type(L, -1) != LUA_TSTRING)
8112 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings"));
8113 cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1));
8114 if (!cli_kws->kw[0].str_kw[index])
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008115 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008116 index++;
8117 lua_pop(L, 1);
8118 }
8119
8120 /* Copy help message. */
8121 cli_kws->kw[0].usage = strdup(message);
8122 if (!cli_kws->kw[0].usage)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008123 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008124
8125 /* Fill fcn io handler. */
8126 len = strlen("<lua.cli>") + 1;
8127 for (i = 0; i < index; i++)
8128 len += strlen(cli_kws->kw[0].str_kw[i]) + 1;
8129 fcn->name = calloc(1, len);
8130 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008131 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008132 strncat((char *)fcn->name, "<lua.cli", len);
8133 for (i = 0; i < index; i++) {
8134 strncat((char *)fcn->name, ".", len);
8135 strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len);
8136 }
8137 strncat((char *)fcn->name, ">", len);
8138 fcn->function_ref = ref_io;
8139
8140 /* Fill last entries. */
8141 cli_kws->kw[0].private = fcn;
8142 cli_kws->kw[0].parse = hlua_cli_parse_fct;
8143 cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct;
8144 cli_kws->kw[0].io_release = hlua_cli_io_release_fct;
8145
8146 /* Register this new converter */
8147 cli_register_kw(cli_kws);
8148
8149 return 0;
8150}
8151
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008152static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx,
8153 struct proxy *defpx, const char *file, int line,
8154 char **err, unsigned int *timeout)
8155{
8156 const char *error;
8157
8158 error = parse_time_err(args[1], timeout, TIME_UNIT_MS);
8159 if (error && *error != '\0') {
8160 memprintf(err, "%s: invalid timeout", args[0]);
8161 return -1;
8162 }
8163 return 0;
8164}
8165
8166static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx,
8167 struct proxy *defpx, const char *file, int line,
8168 char **err)
8169{
8170 return hlua_read_timeout(args, section_type, curpx, defpx,
8171 file, line, err, &hlua_timeout_session);
8172}
8173
8174static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx,
8175 struct proxy *defpx, const char *file, int line,
8176 char **err)
8177{
8178 return hlua_read_timeout(args, section_type, curpx, defpx,
8179 file, line, err, &hlua_timeout_task);
8180}
8181
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008182static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx,
8183 struct proxy *defpx, const char *file, int line,
8184 char **err)
8185{
8186 return hlua_read_timeout(args, section_type, curpx, defpx,
8187 file, line, err, &hlua_timeout_applet);
8188}
8189
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01008190static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx,
8191 struct proxy *defpx, const char *file, int line,
8192 char **err)
8193{
8194 char *error;
8195
8196 hlua_nb_instruction = strtoll(args[1], &error, 10);
8197 if (*error != '\0') {
8198 memprintf(err, "%s: invalid number", args[0]);
8199 return -1;
8200 }
8201 return 0;
8202}
8203
Willy Tarreau32f61e22015-03-18 17:54:59 +01008204static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx,
8205 struct proxy *defpx, const char *file, int line,
8206 char **err)
8207{
8208 char *error;
8209
8210 if (*(args[1]) == 0) {
8211 memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]);
8212 return -1;
8213 }
8214 hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L;
8215 if (*error != '\0') {
8216 memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error);
8217 return -1;
8218 }
8219 return 0;
8220}
8221
8222
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008223/* This function is called by the main configuration key "lua-load". It loads and
8224 * execute an lua file during the parsing of the HAProxy configuration file. It is
8225 * the main lua entry point.
8226 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08008227 * This function runs with the HAProxy keywords API. It returns -1 if an error
8228 * occurs, otherwise it returns 0.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008229 *
8230 * In some error case, LUA set an error message in top of the stack. This function
8231 * returns this error message in the HAProxy logs and pop it from the stack.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008232 *
8233 * This function can fail with an abort() due to an Lua critical error.
8234 * We are in the configuration parsing process of HAProxy, this abort() is
8235 * tolerated.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008236 */
8237static int hlua_load(char **args, int section_type, struct proxy *curpx,
8238 struct proxy *defpx, const char *file, int line,
8239 char **err)
8240{
8241 int error;
8242
8243 /* Just load and compile the file. */
8244 error = luaL_loadfile(gL.T, args[1]);
8245 if (error) {
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008246 memprintf(err, "error in Lua file '%s': %s", args[1], lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008247 lua_pop(gL.T, 1);
8248 return -1;
8249 }
8250
8251 /* If no syntax error where detected, execute the code. */
8252 error = lua_pcall(gL.T, 0, LUA_MULTRET, 0);
8253 switch (error) {
8254 case LUA_OK:
8255 break;
8256 case LUA_ERRRUN:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008257 memprintf(err, "Lua runtime error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008258 lua_pop(gL.T, 1);
8259 return -1;
8260 case LUA_ERRMEM:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008261 memprintf(err, "Lua out of memory error.n");
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008262 return -1;
8263 case LUA_ERRERR:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008264 memprintf(err, "Lua message handler error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008265 lua_pop(gL.T, 1);
8266 return -1;
8267 case LUA_ERRGCMM:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008268 memprintf(err, "Lua garbage collector error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008269 lua_pop(gL.T, 1);
8270 return -1;
8271 default:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008272 memprintf(err, "Lua unknonwn error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008273 lua_pop(gL.T, 1);
8274 return -1;
8275 }
8276
8277 return 0;
8278}
8279
8280/* configuration keywords declaration */
8281static struct cfg_kw_list cfg_kws = {{ },{
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008282 { CFG_GLOBAL, "lua-load", hlua_load },
8283 { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout },
8284 { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout },
Thierry FOURNIER56da1012015-10-01 08:42:31 +02008285 { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout },
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01008286 { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield },
Willy Tarreau32f61e22015-03-18 17:54:59 +01008287 { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem },
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008288 { 0, NULL, NULL },
8289}};
8290
Willy Tarreau0108d902018-11-25 19:14:37 +01008291INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
8292
Christopher Fauletafd8f102018-11-08 11:34:21 +01008293
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008294/* This function can fail with an abort() due to an Lua critical error.
8295 * We are in the initialisation process of HAProxy, this abort() is
8296 * tolerated.
8297 */
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008298int hlua_post_init()
8299{
8300 struct hlua_init_function *init;
8301 const char *msg;
8302 enum hlua_exec ret;
Thierry Fournierfd107a22016-02-19 19:57:23 +01008303 const char *error;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008304
Thierry Fournier3d4a6752016-02-19 20:53:30 +01008305 /* Call post initialisation function in safe environement. */
8306 if (!SET_SAFE_LJMP(gL.T)) {
8307 if (lua_type(gL.T, -1) == LUA_TSTRING)
8308 error = lua_tostring(gL.T, -1);
8309 else
8310 error = "critical error";
8311 fprintf(stderr, "Lua post-init: %s.\n", error);
8312 exit(1);
8313 }
Frédéric Lécaille54f2bcf2018-08-29 13:46:24 +02008314
8315#if USE_OPENSSL
8316 /* Initialize SSL server. */
8317 if (socket_ssl.xprt->prepare_srv) {
8318 int saved_used_backed = global.ssl_used_backend;
8319 // don't affect maxconn automatic computation
8320 socket_ssl.xprt->prepare_srv(&socket_ssl);
8321 global.ssl_used_backend = saved_used_backed;
8322 }
8323#endif
8324
Thierry Fournier3d4a6752016-02-19 20:53:30 +01008325 hlua_fcn_post_init(gL.T);
8326 RESET_SAFE_LJMP(gL.T);
8327
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008328 list_for_each_entry(init, &hlua_init_functions, l) {
8329 lua_rawgeti(gL.T, LUA_REGISTRYINDEX, init->function_ref);
8330 ret = hlua_ctx_resume(&gL, 0);
8331 switch (ret) {
8332 case HLUA_E_OK:
8333 lua_pop(gL.T, -1);
8334 return 1;
8335 case HLUA_E_AGAIN:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008336 ha_alert("Lua init: yield not allowed.\n");
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008337 return 0;
8338 case HLUA_E_ERRMSG:
8339 msg = lua_tostring(gL.T, -1);
Christopher Faulet767a84b2017-11-24 16:50:31 +01008340 ha_alert("lua init: %s.\n", msg);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008341 return 0;
8342 case HLUA_E_ERR:
8343 default:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008344 ha_alert("Lua init: unknown runtime error.\n");
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008345 return 0;
8346 }
8347 }
8348 return 1;
8349}
8350
Willy Tarreau32f61e22015-03-18 17:54:59 +01008351/* The memory allocator used by the Lua stack. <ud> is a pointer to the
8352 * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize>
8353 * is the previously allocated size or the kind of object in case of a new
8354 * allocation. <nsize> is the requested new size.
8355 */
8356static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
8357{
8358 struct hlua_mem_allocator *zone = ud;
8359
8360 if (nsize == 0) {
8361 /* it's a free */
8362 if (ptr)
8363 zone->allocated -= osize;
8364 free(ptr);
8365 return NULL;
8366 }
8367
8368 if (!ptr) {
8369 /* it's a new allocation */
8370 if (zone->limit && zone->allocated + nsize > zone->limit)
8371 return NULL;
8372
8373 ptr = malloc(nsize);
8374 if (ptr)
8375 zone->allocated += nsize;
8376 return ptr;
8377 }
8378
8379 /* it's a realloc */
8380 if (zone->limit && zone->allocated + nsize - osize > zone->limit)
8381 return NULL;
8382
8383 ptr = realloc(ptr, nsize);
8384 if (ptr)
8385 zone->allocated += nsize - osize;
8386 return ptr;
8387}
8388
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008389/* Ithis function can fail with an abort() due to an Lua critical error.
8390 * We are in the initialisation process of HAProxy, this abort() is
8391 * tolerated.
8392 */
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01008393void hlua_init(void)
8394{
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008395 int i;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008396 int idx;
8397 struct sample_fetch *sf;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008398 struct sample_conv *sc;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008399 char *p;
Thierry Fournierfd107a22016-02-19 19:57:23 +01008400 const char *error_msg;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008401#ifdef USE_OPENSSL
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008402 struct srv_kw *kw;
8403 int tmp_error;
8404 char *error;
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008405 char *args[] = { /* SSL client configuration. */
8406 "ssl",
8407 "verify",
8408 "none",
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008409 NULL
8410 };
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008411#endif
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008412
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008413 /* Init main lua stack. */
8414 gL.Mref = LUA_REFNIL;
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01008415 gL.flags = 0;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01008416 LIST_INIT(&gL.com);
Willy Tarreau42ef75f2017-04-12 21:40:29 +02008417 gL.T = lua_newstate(hlua_alloc, &hlua_global_allocator);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008418 hlua_sethlua(&gL);
8419 gL.Tref = LUA_REFNIL;
8420 gL.task = NULL;
8421
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08008422 /* From this point, until the end of the initialisation function,
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008423 * the Lua function can fail with an abort. We are in the initialisation
8424 * process of HAProxy, this abort() is tolerated.
8425 */
8426
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008427 /* Initialise lua. */
8428 luaL_openlibs(gL.T);
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008429
Thierry Fournier75933d42016-01-21 09:30:18 +01008430 /* Set safe environment for the initialisation. */
8431 if (!SET_SAFE_LJMP(gL.T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01008432 if (lua_type(gL.T, -1) == LUA_TSTRING)
8433 error_msg = lua_tostring(gL.T, -1);
8434 else
8435 error_msg = "critical error";
8436 fprintf(stderr, "Lua init: %s.\n", error_msg);
Thierry Fournier75933d42016-01-21 09:30:18 +01008437 exit(1);
8438 }
8439
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008440 /*
8441 *
8442 * Create "core" object.
8443 *
8444 */
8445
Thierry FOURNIERa2d8c652015-03-11 17:29:39 +01008446 /* This table entry is the object "core" base. */
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008447 lua_newtable(gL.T);
8448
8449 /* Push the loglevel constants. */
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008450 for (i = 0; i < NB_LOG_LEVELS; i++)
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008451 hlua_class_const_int(gL.T, log_levels[i], i);
8452
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008453 /* Register special functions. */
8454 hlua_class_function(gL.T, "register_init", hlua_register_init);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01008455 hlua_class_function(gL.T, "register_task", hlua_register_task);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01008456 hlua_class_function(gL.T, "register_fetches", hlua_register_fetches);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01008457 hlua_class_function(gL.T, "register_converters", hlua_register_converters);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02008458 hlua_class_function(gL.T, "register_action", hlua_register_action);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008459 hlua_class_function(gL.T, "register_service", hlua_register_service);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008460 hlua_class_function(gL.T, "register_cli", hlua_register_cli);
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01008461 hlua_class_function(gL.T, "yield", hlua_yield);
Willy Tarreau59551662015-03-10 14:23:13 +01008462 hlua_class_function(gL.T, "set_nice", hlua_set_nice);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01008463 hlua_class_function(gL.T, "sleep", hlua_sleep);
8464 hlua_class_function(gL.T, "msleep", hlua_msleep);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01008465 hlua_class_function(gL.T, "add_acl", hlua_add_acl);
8466 hlua_class_function(gL.T, "del_acl", hlua_del_acl);
8467 hlua_class_function(gL.T, "set_map", hlua_set_map);
8468 hlua_class_function(gL.T, "del_map", hlua_del_map);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008469 hlua_class_function(gL.T, "tcp", hlua_socket_new);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01008470 hlua_class_function(gL.T, "log", hlua_log);
8471 hlua_class_function(gL.T, "Debug", hlua_log_debug);
8472 hlua_class_function(gL.T, "Info", hlua_log_info);
8473 hlua_class_function(gL.T, "Warning", hlua_log_warning);
8474 hlua_class_function(gL.T, "Alert", hlua_log_alert);
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02008475 hlua_class_function(gL.T, "done", hlua_done);
Thierry Fournierfb0b5462016-01-21 09:28:58 +01008476 hlua_fcn_reg_core_fcn(gL.T);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008477
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008478 lua_setglobal(gL.T, "core");
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008479
8480 /*
8481 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008482 * Register class Map
8483 *
8484 */
8485
8486 /* This table entry is the object "Map" base. */
8487 lua_newtable(gL.T);
8488
8489 /* register pattern types. */
8490 for (i=0; i<PAT_MATCH_NUM; i++)
8491 hlua_class_const_int(gL.T, pat_match_names[i], i);
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01008492 for (i=0; i<PAT_MATCH_NUM; i++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008493 snprintf(trash.area, trash.size, "_%s", pat_match_names[i]);
8494 hlua_class_const_int(gL.T, trash.area, i);
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01008495 }
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008496
8497 /* register constructor. */
8498 hlua_class_function(gL.T, "new", hlua_map_new);
8499
8500 /* Create and fill the metatable. */
8501 lua_newtable(gL.T);
8502
8503 /* Create and fille the __index entry. */
8504 lua_pushstring(gL.T, "__index");
8505 lua_newtable(gL.T);
8506
8507 /* Register . */
8508 hlua_class_function(gL.T, "lookup", hlua_map_lookup);
8509 hlua_class_function(gL.T, "slookup", hlua_map_slookup);
8510
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008511 lua_rawset(gL.T, -3);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008512
Thierry Fournier45e78d72016-02-19 18:34:46 +01008513 /* Register previous table in the registry with reference and named entry.
8514 * The function hlua_register_metatable() pops the stack, so we
8515 * previously create a copy of the table.
8516 */
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008517 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008518 class_map_ref = hlua_register_metatable(gL.T, CLASS_MAP);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008519
8520 /* Assign the metatable to the mai Map object. */
8521 lua_setmetatable(gL.T, -2);
8522
8523 /* Set a name to the table. */
8524 lua_setglobal(gL.T, "Map");
8525
8526 /*
8527 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008528 * Register class Channel
8529 *
8530 */
8531
8532 /* Create and fill the metatable. */
8533 lua_newtable(gL.T);
8534
8535 /* Create and fille the __index entry. */
8536 lua_pushstring(gL.T, "__index");
8537 lua_newtable(gL.T);
8538
8539 /* Register . */
8540 hlua_class_function(gL.T, "get", hlua_channel_get);
8541 hlua_class_function(gL.T, "dup", hlua_channel_dup);
8542 hlua_class_function(gL.T, "getline", hlua_channel_getline);
8543 hlua_class_function(gL.T, "set", hlua_channel_set);
8544 hlua_class_function(gL.T, "append", hlua_channel_append);
8545 hlua_class_function(gL.T, "send", hlua_channel_send);
8546 hlua_class_function(gL.T, "forward", hlua_channel_forward);
8547 hlua_class_function(gL.T, "get_in_len", hlua_channel_get_in_len);
8548 hlua_class_function(gL.T, "get_out_len", hlua_channel_get_out_len);
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01008549 hlua_class_function(gL.T, "is_full", hlua_channel_is_full);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008550
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008551 lua_rawset(gL.T, -3);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008552
8553 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008554 class_channel_ref = hlua_register_metatable(gL.T, CLASS_CHANNEL);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008555
8556 /*
8557 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008558 * Register class Fetches
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008559 *
8560 */
8561
8562 /* Create and fill the metatable. */
8563 lua_newtable(gL.T);
8564
8565 /* Create and fille the __index entry. */
8566 lua_pushstring(gL.T, "__index");
8567 lua_newtable(gL.T);
8568
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008569 /* Browse existing fetches and create the associated
8570 * object method.
8571 */
8572 sf = NULL;
8573 while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) {
8574
8575 /* Dont register the keywork if the arguments check function are
8576 * not safe during the runtime.
8577 */
8578 if ((sf->val_args != NULL) &&
8579 (sf->val_args != val_payload_lv) &&
8580 (sf->val_args != val_hdr))
8581 continue;
8582
8583 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
8584 * by an underscore.
8585 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008586 strncpy(trash.area, sf->kw, trash.size);
8587 trash.area[trash.size - 1] = '\0';
8588 for (p = trash.area; *p; p++)
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008589 if (*p == '.' || *p == '-' || *p == '+')
8590 *p = '_';
8591
8592 /* Register the function. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008593 lua_pushstring(gL.T, trash.area);
Willy Tarreau2ec22742015-03-10 14:27:20 +01008594 lua_pushlightuserdata(gL.T, sf);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008595 lua_pushcclosure(gL.T, hlua_run_sample_fetch, 1);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008596 lua_rawset(gL.T, -3);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008597 }
8598
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008599 lua_rawset(gL.T, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008600
8601 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008602 class_fetches_ref = hlua_register_metatable(gL.T, CLASS_FETCHES);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008603
8604 /*
8605 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008606 * Register class Converters
8607 *
8608 */
8609
8610 /* Create and fill the metatable. */
8611 lua_newtable(gL.T);
8612
8613 /* Create and fill the __index entry. */
8614 lua_pushstring(gL.T, "__index");
8615 lua_newtable(gL.T);
8616
8617 /* Browse existing converters and create the associated
8618 * object method.
8619 */
8620 sc = NULL;
8621 while ((sc = sample_conv_getnext(sc, &idx)) != NULL) {
8622 /* Dont register the keywork if the arguments check function are
8623 * not safe during the runtime.
8624 */
8625 if (sc->val_args != NULL)
8626 continue;
8627
8628 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
8629 * by an underscore.
8630 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008631 strncpy(trash.area, sc->kw, trash.size);
8632 trash.area[trash.size - 1] = '\0';
8633 for (p = trash.area; *p; p++)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008634 if (*p == '.' || *p == '-' || *p == '+')
8635 *p = '_';
8636
8637 /* Register the function. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008638 lua_pushstring(gL.T, trash.area);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008639 lua_pushlightuserdata(gL.T, sc);
8640 lua_pushcclosure(gL.T, hlua_run_sample_conv, 1);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008641 lua_rawset(gL.T, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008642 }
8643
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008644 lua_rawset(gL.T, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008645
8646 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008647 class_converters_ref = hlua_register_metatable(gL.T, CLASS_CONVERTERS);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008648
8649 /*
8650 *
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008651 * Register class HTTP
8652 *
8653 */
8654
8655 /* Create and fill the metatable. */
8656 lua_newtable(gL.T);
8657
8658 /* Create and fille the __index entry. */
8659 lua_pushstring(gL.T, "__index");
8660 lua_newtable(gL.T);
8661
8662 /* Register Lua functions. */
8663 hlua_class_function(gL.T, "req_get_headers",hlua_http_req_get_headers);
8664 hlua_class_function(gL.T, "req_del_header", hlua_http_req_del_hdr);
8665 hlua_class_function(gL.T, "req_rep_header", hlua_http_req_rep_hdr);
8666 hlua_class_function(gL.T, "req_rep_value", hlua_http_req_rep_val);
8667 hlua_class_function(gL.T, "req_add_header", hlua_http_req_add_hdr);
8668 hlua_class_function(gL.T, "req_set_header", hlua_http_req_set_hdr);
8669 hlua_class_function(gL.T, "req_set_method", hlua_http_req_set_meth);
8670 hlua_class_function(gL.T, "req_set_path", hlua_http_req_set_path);
8671 hlua_class_function(gL.T, "req_set_query", hlua_http_req_set_query);
8672 hlua_class_function(gL.T, "req_set_uri", hlua_http_req_set_uri);
8673
8674 hlua_class_function(gL.T, "res_get_headers",hlua_http_res_get_headers);
8675 hlua_class_function(gL.T, "res_del_header", hlua_http_res_del_hdr);
8676 hlua_class_function(gL.T, "res_rep_header", hlua_http_res_rep_hdr);
8677 hlua_class_function(gL.T, "res_rep_value", hlua_http_res_rep_val);
8678 hlua_class_function(gL.T, "res_add_header", hlua_http_res_add_hdr);
8679 hlua_class_function(gL.T, "res_set_header", hlua_http_res_set_hdr);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02008680 hlua_class_function(gL.T, "res_set_status", hlua_http_res_set_status);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008681
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008682 lua_rawset(gL.T, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008683
8684 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008685 class_http_ref = hlua_register_metatable(gL.T, CLASS_HTTP);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008686
8687 /*
8688 *
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008689 * Register class AppletTCP
8690 *
8691 */
8692
8693 /* Create and fill the metatable. */
8694 lua_newtable(gL.T);
8695
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008696 /* Create and fille the __index entry. */
8697 lua_pushstring(gL.T, "__index");
8698 lua_newtable(gL.T);
8699
8700 /* Register Lua functions. */
Thierry FOURNIER / OZON.IO3e1d7912016-12-12 12:29:34 +01008701 hlua_class_function(gL.T, "getline", hlua_applet_tcp_getline);
8702 hlua_class_function(gL.T, "receive", hlua_applet_tcp_recv);
8703 hlua_class_function(gL.T, "send", hlua_applet_tcp_send);
8704 hlua_class_function(gL.T, "set_priv", hlua_applet_tcp_set_priv);
8705 hlua_class_function(gL.T, "get_priv", hlua_applet_tcp_get_priv);
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01008706 hlua_class_function(gL.T, "set_var", hlua_applet_tcp_set_var);
8707 hlua_class_function(gL.T, "unset_var", hlua_applet_tcp_unset_var);
8708 hlua_class_function(gL.T, "get_var", hlua_applet_tcp_get_var);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008709
8710 lua_settable(gL.T, -3);
8711
8712 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008713 class_applet_tcp_ref = hlua_register_metatable(gL.T, CLASS_APPLET_TCP);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008714
8715 /*
8716 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02008717 * Register class AppletHTTP
8718 *
8719 */
8720
8721 /* Create and fill the metatable. */
8722 lua_newtable(gL.T);
8723
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02008724 /* Create and fille the __index entry. */
8725 lua_pushstring(gL.T, "__index");
8726 lua_newtable(gL.T);
8727
8728 /* Register Lua functions. */
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01008729 hlua_class_function(gL.T, "set_priv", hlua_applet_http_set_priv);
8730 hlua_class_function(gL.T, "get_priv", hlua_applet_http_get_priv);
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01008731 hlua_class_function(gL.T, "set_var", hlua_applet_http_set_var);
8732 hlua_class_function(gL.T, "unset_var", hlua_applet_http_unset_var);
8733 hlua_class_function(gL.T, "get_var", hlua_applet_http_get_var);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02008734 hlua_class_function(gL.T, "getline", hlua_applet_http_getline);
8735 hlua_class_function(gL.T, "receive", hlua_applet_http_recv);
8736 hlua_class_function(gL.T, "send", hlua_applet_http_send);
8737 hlua_class_function(gL.T, "add_header", hlua_applet_http_addheader);
8738 hlua_class_function(gL.T, "set_status", hlua_applet_http_status);
8739 hlua_class_function(gL.T, "start_response", hlua_applet_http_start_response);
8740
8741 lua_settable(gL.T, -3);
8742
8743 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008744 class_applet_http_ref = hlua_register_metatable(gL.T, CLASS_APPLET_HTTP);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02008745
8746 /*
8747 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008748 * Register class TXN
8749 *
8750 */
8751
8752 /* Create and fill the metatable. */
8753 lua_newtable(gL.T);
8754
8755 /* Create and fille the __index entry. */
8756 lua_pushstring(gL.T, "__index");
8757 lua_newtable(gL.T);
8758
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01008759 /* Register Lua functions. */
Patrick Hemmer268a7072018-05-11 12:52:31 -04008760 hlua_class_function(gL.T, "set_priv", hlua_set_priv);
8761 hlua_class_function(gL.T, "get_priv", hlua_get_priv);
8762 hlua_class_function(gL.T, "set_var", hlua_set_var);
8763 hlua_class_function(gL.T, "unset_var", hlua_unset_var);
8764 hlua_class_function(gL.T, "get_var", hlua_get_var);
8765 hlua_class_function(gL.T, "done", hlua_txn_done);
8766 hlua_class_function(gL.T, "set_loglevel", hlua_txn_set_loglevel);
8767 hlua_class_function(gL.T, "set_tos", hlua_txn_set_tos);
8768 hlua_class_function(gL.T, "set_mark", hlua_txn_set_mark);
8769 hlua_class_function(gL.T, "set_priority_class", hlua_txn_set_priority_class);
8770 hlua_class_function(gL.T, "set_priority_offset", hlua_txn_set_priority_offset);
8771 hlua_class_function(gL.T, "deflog", hlua_txn_deflog);
8772 hlua_class_function(gL.T, "log", hlua_txn_log);
8773 hlua_class_function(gL.T, "Debug", hlua_txn_log_debug);
8774 hlua_class_function(gL.T, "Info", hlua_txn_log_info);
8775 hlua_class_function(gL.T, "Warning", hlua_txn_log_warning);
8776 hlua_class_function(gL.T, "Alert", hlua_txn_log_alert);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01008777
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008778 lua_rawset(gL.T, -3);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008779
8780 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008781 class_txn_ref = hlua_register_metatable(gL.T, CLASS_TXN);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008782
8783 /*
8784 *
8785 * Register class Socket
8786 *
8787 */
8788
8789 /* Create and fill the metatable. */
8790 lua_newtable(gL.T);
8791
8792 /* Create and fille the __index entry. */
8793 lua_pushstring(gL.T, "__index");
8794 lua_newtable(gL.T);
8795
Baptiste Assmann84bb4932015-03-02 21:40:06 +01008796#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008797 hlua_class_function(gL.T, "connect_ssl", hlua_socket_connect_ssl);
Baptiste Assmann84bb4932015-03-02 21:40:06 +01008798#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008799 hlua_class_function(gL.T, "connect", hlua_socket_connect);
8800 hlua_class_function(gL.T, "send", hlua_socket_send);
8801 hlua_class_function(gL.T, "receive", hlua_socket_receive);
8802 hlua_class_function(gL.T, "close", hlua_socket_close);
8803 hlua_class_function(gL.T, "getpeername", hlua_socket_getpeername);
8804 hlua_class_function(gL.T, "getsockname", hlua_socket_getsockname);
8805 hlua_class_function(gL.T, "setoption", hlua_socket_setoption);
8806 hlua_class_function(gL.T, "settimeout", hlua_socket_settimeout);
8807
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008808 lua_rawset(gL.T, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008809
8810 /* Register the garbage collector entry. */
8811 lua_pushstring(gL.T, "__gc");
8812 lua_pushcclosure(gL.T, hlua_socket_gc, 0);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008813 lua_rawset(gL.T, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008814
8815 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008816 class_socket_ref = hlua_register_metatable(gL.T, CLASS_SOCKET);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008817
8818 /* Proxy and server configuration initialisation. */
8819 memset(&socket_proxy, 0, sizeof(socket_proxy));
8820 init_new_proxy(&socket_proxy);
8821 socket_proxy.parent = NULL;
8822 socket_proxy.last_change = now.tv_sec;
8823 socket_proxy.id = "LUA-SOCKET";
8824 socket_proxy.cap = PR_CAP_FE | PR_CAP_BE;
8825 socket_proxy.maxconn = 0;
8826 socket_proxy.accept = NULL;
8827 socket_proxy.options2 |= PR_O2_INDEPSTR;
8828 socket_proxy.srv = NULL;
8829 socket_proxy.conn_retries = 0;
8830 socket_proxy.timeout.connect = 5000; /* By default the timeout connection is 5s. */
8831
8832 /* Init TCP server: unchanged parameters */
8833 memset(&socket_tcp, 0, sizeof(socket_tcp));
8834 socket_tcp.next = NULL;
8835 socket_tcp.proxy = &socket_proxy;
8836 socket_tcp.obj_type = OBJ_TYPE_SERVER;
8837 LIST_INIT(&socket_tcp.actconns);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04008838 socket_tcp.pendconns = EB_ROOT;
Christopher Faulet40a007c2017-07-03 15:41:01 +02008839 socket_tcp.priv_conns = NULL;
8840 socket_tcp.idle_conns = NULL;
8841 socket_tcp.safe_conns = NULL;
Emeric Brun52a91d32017-08-31 14:41:55 +02008842 socket_tcp.next_state = SRV_ST_RUNNING; /* early server setup */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008843 socket_tcp.last_change = 0;
8844 socket_tcp.id = "LUA-TCP-CONN";
8845 socket_tcp.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8846 socket_tcp.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8847 socket_tcp.pp_opts = 0; /* Remove proxy protocol. */
8848
8849 /* XXX: Copy default parameter from default server,
8850 * but the default server is not initialized.
8851 */
8852 socket_tcp.maxqueue = socket_proxy.defsrv.maxqueue;
8853 socket_tcp.minconn = socket_proxy.defsrv.minconn;
8854 socket_tcp.maxconn = socket_proxy.defsrv.maxconn;
8855 socket_tcp.slowstart = socket_proxy.defsrv.slowstart;
8856 socket_tcp.onerror = socket_proxy.defsrv.onerror;
8857 socket_tcp.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
8858 socket_tcp.onmarkedup = socket_proxy.defsrv.onmarkedup;
8859 socket_tcp.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
8860 socket_tcp.uweight = socket_proxy.defsrv.iweight;
8861 socket_tcp.iweight = socket_proxy.defsrv.iweight;
8862
8863 socket_tcp.check.status = HCHK_STATUS_INI;
8864 socket_tcp.check.rise = socket_proxy.defsrv.check.rise;
8865 socket_tcp.check.fall = socket_proxy.defsrv.check.fall;
8866 socket_tcp.check.health = socket_tcp.check.rise; /* socket, but will fall down at first failure */
8867 socket_tcp.check.server = &socket_tcp;
8868
8869 socket_tcp.agent.status = HCHK_STATUS_INI;
8870 socket_tcp.agent.rise = socket_proxy.defsrv.agent.rise;
8871 socket_tcp.agent.fall = socket_proxy.defsrv.agent.fall;
8872 socket_tcp.agent.health = socket_tcp.agent.rise; /* socket, but will fall down at first failure */
8873 socket_tcp.agent.server = &socket_tcp;
8874
Willy Tarreaua261e9b2016-12-22 20:44:00 +01008875 socket_tcp.xprt = xprt_get(XPRT_RAW);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008876
8877#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008878 /* Init TCP server: unchanged parameters */
8879 memset(&socket_ssl, 0, sizeof(socket_ssl));
8880 socket_ssl.next = NULL;
8881 socket_ssl.proxy = &socket_proxy;
8882 socket_ssl.obj_type = OBJ_TYPE_SERVER;
8883 LIST_INIT(&socket_ssl.actconns);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04008884 socket_ssl.pendconns = EB_ROOT;
Christopher Faulet40a007c2017-07-03 15:41:01 +02008885 socket_tcp.priv_conns = NULL;
8886 socket_tcp.idle_conns = NULL;
8887 socket_tcp.safe_conns = NULL;
Emeric Brun52a91d32017-08-31 14:41:55 +02008888 socket_ssl.next_state = SRV_ST_RUNNING; /* early server setup */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008889 socket_ssl.last_change = 0;
8890 socket_ssl.id = "LUA-SSL-CONN";
8891 socket_ssl.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8892 socket_ssl.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8893 socket_ssl.pp_opts = 0; /* Remove proxy protocol. */
8894
8895 /* XXX: Copy default parameter from default server,
8896 * but the default server is not initialized.
8897 */
8898 socket_ssl.maxqueue = socket_proxy.defsrv.maxqueue;
8899 socket_ssl.minconn = socket_proxy.defsrv.minconn;
8900 socket_ssl.maxconn = socket_proxy.defsrv.maxconn;
8901 socket_ssl.slowstart = socket_proxy.defsrv.slowstart;
8902 socket_ssl.onerror = socket_proxy.defsrv.onerror;
8903 socket_ssl.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
8904 socket_ssl.onmarkedup = socket_proxy.defsrv.onmarkedup;
8905 socket_ssl.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
8906 socket_ssl.uweight = socket_proxy.defsrv.iweight;
8907 socket_ssl.iweight = socket_proxy.defsrv.iweight;
8908
8909 socket_ssl.check.status = HCHK_STATUS_INI;
8910 socket_ssl.check.rise = socket_proxy.defsrv.check.rise;
8911 socket_ssl.check.fall = socket_proxy.defsrv.check.fall;
8912 socket_ssl.check.health = socket_ssl.check.rise; /* socket, but will fall down at first failure */
8913 socket_ssl.check.server = &socket_ssl;
8914
8915 socket_ssl.agent.status = HCHK_STATUS_INI;
8916 socket_ssl.agent.rise = socket_proxy.defsrv.agent.rise;
8917 socket_ssl.agent.fall = socket_proxy.defsrv.agent.fall;
8918 socket_ssl.agent.health = socket_ssl.agent.rise; /* socket, but will fall down at first failure */
8919 socket_ssl.agent.server = &socket_ssl;
8920
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008921 socket_ssl.use_ssl = 1;
Willy Tarreaua261e9b2016-12-22 20:44:00 +01008922 socket_ssl.xprt = xprt_get(XPRT_SSL);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008923
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008924 for (idx = 0; args[idx] != NULL; idx++) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008925 if ((kw = srv_find_kw(args[idx])) != NULL) { /* Maybe it's registered server keyword */
8926 /*
8927 *
8928 * If the keyword is not known, we can search in the registered
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08008929 * server keywords. This is useful to configure special SSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008930 * features like client certificates and ssl_verify.
8931 *
8932 */
8933 tmp_error = kw->parse(args, &idx, &socket_proxy, &socket_ssl, &error);
8934 if (tmp_error != 0) {
8935 fprintf(stderr, "INTERNAL ERROR: %s\n", error);
8936 abort(); /* This must be never arrives because the command line
8937 not editable by the user. */
8938 }
8939 idx += kw->skip;
8940 }
8941 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008942#endif
Thierry Fournier75933d42016-01-21 09:30:18 +01008943
8944 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01008945}
Willy Tarreaubb57d942016-12-21 19:04:56 +01008946
Willy Tarreau80713382018-11-26 10:19:54 +01008947static void hlua_register_build_options(void)
8948{
Willy Tarreaubb57d942016-12-21 19:04:56 +01008949 char *ptr = NULL;
Willy Tarreau80713382018-11-26 10:19:54 +01008950
Willy Tarreaubb57d942016-12-21 19:04:56 +01008951 memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE);
8952 hap_register_build_opts(ptr, 1);
8953}
Willy Tarreau80713382018-11-26 10:19:54 +01008954
8955INITCALL0(STG_REGISTER, hlua_register_build_options);