blob: 588576c276c0bc1a1f50d631f59ac051bb87b26d [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>
Thierry FOURNIER2da788e2017-09-11 18:37:23 +020028#include <common/xref.h>
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +020029#include <common/hathreads.h>
Thierry FOURNIER380d0932015-01-23 14:27:52 +010030
William Lallemand9ed62032016-11-21 17:49:11 +010031#include <types/cli.h>
Thierry FOURNIER380d0932015-01-23 14:27:52 +010032#include <types/hlua.h>
33#include <types/proxy.h>
William Lallemand9ed62032016-11-21 17:49:11 +010034#include <types/stats.h>
Thierry FOURNIER380d0932015-01-23 14:27:52 +010035
Thierry FOURNIER55da1652015-01-23 11:36:30 +010036#include <proto/arg.h>
Willy Tarreau8a8d83b2015-04-13 13:24:54 +020037#include <proto/applet.h>
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010038#include <proto/channel.h>
William Lallemand9ed62032016-11-21 17:49:11 +010039#include <proto/cli.h>
Willy Tarreaua71f6422016-11-16 17:00:14 +010040#include <proto/connection.h>
William Lallemand9ed62032016-11-21 17:49:11 +010041#include <proto/stats.h>
Thierry FOURNIER9a819e72015-02-16 20:22:55 +010042#include <proto/hdr_idx.h>
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +010043#include <proto/hlua.h>
Thierry Fournierfb0b5462016-01-21 09:28:58 +010044#include <proto/hlua_fcn.h>
Thierry FOURNIER3def3932015-04-07 11:27:54 +020045#include <proto/map.h>
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010046#include <proto/obj_type.h>
Thierry FOURNIER83758bb2015-02-04 13:21:04 +010047#include <proto/pattern.h>
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +010048#include <proto/payload.h>
49#include <proto/proto_http.h>
50#include <proto/sample.h>
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010051#include <proto/server.h>
Willy Tarreaufeb76402015-04-03 14:10:06 +020052#include <proto/session.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020053#include <proto/stream.h>
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010054#include <proto/stream_interface.h>
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +010055#include <proto/task.h>
Willy Tarreau39713102016-11-25 15:49:32 +010056#include <proto/tcp_rules.h>
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +020057#include <proto/vars.h>
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +010058
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +010059/* Lua uses longjmp to perform yield or throwing errors. This
60 * macro is used only for identifying the function that can
61 * not return because a longjmp is executed.
62 * __LJMP marks a prototype of hlua file that can use longjmp.
63 * WILL_LJMP() marks an lua function that will use longjmp.
64 * MAY_LJMP() marks an lua function that may use longjmp.
65 */
66#define __LJMP
67#define WILL_LJMP(func) func
68#define MAY_LJMP(func) func
69
Thierry FOURNIERbabae282015-09-17 11:36:37 +020070/* This couple of function executes securely some Lua calls outside of
71 * the lua runtime environment. Each Lua call can return a longjmp
72 * if it encounter a memory error.
73 *
74 * Lua documentation extract:
75 *
76 * If an error happens outside any protected environment, Lua calls
77 * a panic function (see lua_atpanic) and then calls abort, thus
78 * exiting the host application. Your panic function can avoid this
79 * exit by never returning (e.g., doing a long jump to your own
80 * recovery point outside Lua).
81 *
82 * The panic function runs as if it were a message handler (see
83 * §2.3); in particular, the error message is at the top of the
84 * stack. However, there is no guarantee about stack space. To push
85 * anything on the stack, the panic function must first check the
86 * available space (see §4.2).
87 *
88 * We must check all the Lua entry point. This includes:
89 * - The include/proto/hlua.h exported functions
90 * - the task wrapper function
91 * - The action wrapper function
92 * - The converters wrapper function
93 * - The sample-fetch wrapper functions
94 *
95 * It is tolerated that the initilisation function returns an abort.
96 * Before each Lua abort, an error message is writed on stderr.
97 *
98 * The macro SET_SAFE_LJMP initialise the longjmp. The Macro
99 * RESET_SAFE_LJMP reset the longjmp. These function must be macro
100 * because they must be exists in the program stack when the longjmp
101 * is called.
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +0200102 *
103 * Note that the Lua processing is not really thread safe. It provides
104 * heavy system which consists to add our own lock function in the Lua
105 * code and recompile the library. This system will probably not accepted
106 * by maintainers of various distribs.
107 *
108 * Our main excution point of the Lua is the function lua_resume(). A
109 * quick looking on the Lua sources displays a lua_lock() a the start
110 * of function and a lua_unlock() at the end of the function. So I
111 * conclude that the Lua thread safe mode just perform a mutex around
112 * all execution. So I prefer to do this in the HAProxy code, it will be
113 * easier for distro maintainers.
114 *
115 * Note that the HAProxy lua functions rounded by the macro SET_SAFE_LJMP
116 * and RESET_SAFE_LJMP manipulates the Lua stack, so it will be careful
117 * to set mutex around these functions.
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200118 */
Christopher Faulet9dcf9b62017-11-13 10:34:01 +0100119__decl_hathreads(HA_SPINLOCK_T hlua_global_lock);
Thierry FOURNIERffbad792017-07-12 11:39:04 +0200120THREAD_LOCAL jmp_buf safe_ljmp_env;
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200121static int hlua_panic_safe(lua_State *L) { return 0; }
122static int hlua_panic_ljmp(lua_State *L) { longjmp(safe_ljmp_env, 1); }
123
124#define SET_SAFE_LJMP(__L) \
125 ({ \
126 int ret; \
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100127 HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200128 if (setjmp(safe_ljmp_env) != 0) { \
129 lua_atpanic(__L, hlua_panic_safe); \
130 ret = 0; \
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100131 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200132 } else { \
133 lua_atpanic(__L, hlua_panic_ljmp); \
134 ret = 1; \
135 } \
136 ret; \
137 })
138
139/* If we are the last function catching Lua errors, we
140 * must reset the panic function.
141 */
142#define RESET_SAFE_LJMP(__L) \
143 do { \
144 lua_atpanic(__L, hlua_panic_safe); \
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100145 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200146 } while(0)
147
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200148/* Applet status flags */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200149#define APPLET_DONE 0x01 /* applet processing is done. */
150#define APPLET_100C 0x02 /* 100 continue expected. */
151#define APPLET_HDR_SENT 0x04 /* Response header sent. */
152#define APPLET_CHUNKED 0x08 /* Use transfer encoding chunked. */
153#define APPLET_LAST_CHK 0x10 /* Last chunk sent. */
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +0100154#define APPLET_HTTP11 0x20 /* Last chunk sent. */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200155
156#define HTTP_100C "HTTP/1.1 100 Continue\r\n\r\n"
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200157
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100158/* The main Lua execution context. */
159struct hlua gL;
160
Thierry FOURNIERebed6e92016-12-16 11:54:07 +0100161/* This is the memory pool containing struct lua for applets
162 * (including cli).
163 */
Willy Tarreaubafbe012017-11-24 17:34:44 +0100164struct pool_head *pool_head_hlua;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +0100165
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100166/* Used for Socket connection. */
167static struct proxy socket_proxy;
168static struct server socket_tcp;
169#ifdef USE_OPENSSL
170static struct server socket_ssl;
171#endif
172
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +0100173/* List head of the function called at the initialisation time. */
174struct list hlua_init_functions = LIST_HEAD_INIT(hlua_init_functions);
175
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +0100176/* The following variables contains the reference of the different
177 * Lua classes. These references are useful for identify metadata
178 * associated with an object.
179 */
Thierry FOURNIER65f34c62015-02-16 20:11:43 +0100180static int class_txn_ref;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100181static int class_socket_ref;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +0100182static int class_channel_ref;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +0100183static int class_fetches_ref;
Thierry FOURNIER594afe72015-03-10 23:58:30 +0100184static int class_converters_ref;
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100185static int class_http_ref;
Thierry FOURNIER3def3932015-04-07 11:27:54 +0200186static int class_map_ref;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200187static int class_applet_tcp_ref;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200188static int class_applet_http_ref;
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +0100189
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100190/* Global Lua execution timeout. By default Lua, execution linked
Willy Tarreau87b09662015-04-03 00:22:06 +0200191 * with stream (actions, sample-fetches and converters) have a
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100192 * short timeout. Lua linked with tasks doesn't have a timeout
193 * because a task may remain alive during all the haproxy execution.
194 */
195static unsigned int hlua_timeout_session = 4000; /* session timeout. */
196static unsigned int hlua_timeout_task = TICK_ETERNITY; /* task timeout. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200197static unsigned int hlua_timeout_applet = 4000; /* applet timeout. */
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100198
Thierry FOURNIERee9f8022015-03-03 17:37:37 +0100199/* Interrupts the Lua processing each "hlua_nb_instruction" instructions.
200 * it is used for preventing infinite loops.
201 *
202 * I test the scheer with an infinite loop containing one incrementation
203 * and one test. I run this loop between 10 seconds, I raise a ceil of
204 * 710M loops from one interrupt each 9000 instructions, so I fix the value
205 * to one interrupt each 10 000 instructions.
206 *
207 * configured | Number of
208 * instructions | loops executed
209 * between two | in milions
210 * forced yields |
211 * ---------------+---------------
212 * 10 | 160
213 * 500 | 670
214 * 1000 | 680
215 * 5000 | 700
216 * 7000 | 700
217 * 8000 | 700
218 * 9000 | 710 <- ceil
219 * 10000 | 710
220 * 100000 | 710
221 * 1000000 | 710
222 *
223 */
224static unsigned int hlua_nb_instruction = 10000;
225
Willy Tarreau32f61e22015-03-18 17:54:59 +0100226/* Descriptor for the memory allocation state. If limit is not null, it will
227 * be enforced on any memory allocation.
228 */
229struct hlua_mem_allocator {
230 size_t allocated;
231 size_t limit;
232};
233
234static struct hlua_mem_allocator hlua_global_allocator;
235
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200236static const char error_500[] =
Jarno Huuskonen16ad94a2017-01-09 14:17:10 +0200237 "HTTP/1.0 500 Internal Server Error\r\n"
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200238 "Cache-Control: no-cache\r\n"
239 "Connection: close\r\n"
240 "Content-Type: text/html\r\n"
241 "\r\n"
Jarno Huuskonen16ad94a2017-01-09 14:17:10 +0200242 "<html><body><h1>500 Internal Server Error</h1>\nAn internal server error occured.\n</body></html>\n";
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200243
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100244/* These functions converts types between HAProxy internal args or
245 * sample and LUA types. Another function permits to check if the
246 * LUA stack contains arguments according with an required ARG_T
247 * format.
248 */
249static int hlua_arg2lua(lua_State *L, const struct arg *arg);
250static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100251__LJMP static int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp,
David Carlier0c437f42016-04-27 16:21:56 +0100252 uint64_t mask, struct proxy *p);
Willy Tarreau5eadada2015-03-10 17:28:54 +0100253static int hlua_smp2lua(lua_State *L, struct sample *smp);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100254static int hlua_smp2lua_str(lua_State *L, struct sample *smp);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100255static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp);
256
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200257__LJMP static int hlua_http_get_headers(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg);
258
Thierry FOURNIER23bc3752015-09-11 19:15:43 +0200259#define SEND_ERR(__be, __fmt, __args...) \
260 do { \
261 send_log(__be, LOG_ERR, __fmt, ## __args); \
262 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) \
Christopher Faulet767a84b2017-11-24 16:50:31 +0100263 ha_alert(__fmt, ## __args); \
Thierry FOURNIER23bc3752015-09-11 19:15:43 +0200264 } while (0)
265
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100266/* Used to check an Lua function type in the stack. It creates and
267 * returns a reference of the function. This function throws an
268 * error if the rgument is not a "function".
269 */
270__LJMP unsigned int hlua_checkfunction(lua_State *L, int argno)
271{
272 if (!lua_isfunction(L, argno)) {
Thierry FOURNIERfd1e9552018-02-23 18:41:18 +0100273 const char *msg = lua_pushfstring(L, "function expected, got %s", luaL_typename(L, argno));
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100274 WILL_LJMP(luaL_argerror(L, argno, msg));
275 }
276 lua_pushvalue(L, argno);
277 return luaL_ref(L, LUA_REGISTRYINDEX);
278}
279
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200280/* Return the string that is of the top of the stack. */
281const char *hlua_get_top_error_string(lua_State *L)
282{
283 if (lua_gettop(L) < 1)
284 return "unknown error";
285 if (lua_type(L, -1) != LUA_TSTRING)
286 return "unknown error";
287 return lua_tostring(L, -1);
288}
289
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200290__LJMP static const char *hlua_traceback(lua_State *L)
291{
292 lua_Debug ar;
293 int level = 0;
294 struct chunk *msg = get_trash_chunk();
295 int filled = 0;
296
297 while (lua_getstack(L, level++, &ar)) {
298
299 /* Add separator */
300 if (filled)
301 chunk_appendf(msg, ", ");
302 filled = 1;
303
304 /* Fill fields:
305 * 'S': fills in the fields source, short_src, linedefined, lastlinedefined, and what;
306 * 'l': fills in the field currentline;
307 * 'n': fills in the field name and namewhat;
308 * 't': fills in the field istailcall;
309 */
310 lua_getinfo(L, "Slnt", &ar);
311
312 /* Append code localisation */
313 if (ar.currentline > 0)
314 chunk_appendf(msg, "%s:%d ", ar.short_src, ar.currentline);
315 else
316 chunk_appendf(msg, "%s ", ar.short_src);
317
318 /*
319 * Get function name
320 *
321 * if namewhat is no empty, name is defined.
322 * what contains "Lua" for Lua function, "C" for C function,
323 * or "main" for main code.
324 */
325 if (*ar.namewhat != '\0' && ar.name != NULL) /* is there a name from code? */
326 chunk_appendf(msg, "%s '%s'", ar.namewhat, ar.name); /* use it */
327
328 else if (*ar.what == 'm') /* "main", the code is not executed in a function */
329 chunk_appendf(msg, "main chunk");
330
331 else if (*ar.what != 'C') /* for Lua functions, use <file:line> */
332 chunk_appendf(msg, "C function line %d", ar.linedefined);
333
334 else /* nothing left... */
335 chunk_appendf(msg, "?");
336
337
338 /* Display tailed call */
339 if (ar.istailcall)
340 chunk_appendf(msg, " ...");
341 }
342
343 return msg->str;
344}
345
346
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100347/* This function check the number of arguments available in the
348 * stack. If the number of arguments available is not the same
349 * then <nb> an error is throwed.
350 */
351__LJMP static inline void check_args(lua_State *L, int nb, char *fcn)
352{
353 if (lua_gettop(L) == nb)
354 return;
355 WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb));
356}
357
Mark Lakes22154b42018-01-29 14:38:40 -0800358/* This function pushes an error string prefixed by the file name
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100359 * and the line number where the error is encountered.
360 */
361static int hlua_pusherror(lua_State *L, const char *fmt, ...)
362{
363 va_list argp;
364 va_start(argp, fmt);
365 luaL_where(L, 1);
366 lua_pushvfstring(L, fmt, argp);
367 va_end(argp);
368 lua_concat(L, 2);
369 return 1;
370}
371
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100372/* This functions is used with sample fetch and converters. It
373 * converts the HAProxy configuration argument in a lua stack
374 * values.
375 *
376 * It takes an array of "arg", and each entry of the array is
377 * converted and pushed in the LUA stack.
378 */
379static int hlua_arg2lua(lua_State *L, const struct arg *arg)
380{
381 switch (arg->type) {
382 case ARGT_SINT:
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100383 case ARGT_TIME:
384 case ARGT_SIZE:
Thierry FOURNIERf90838b2015-03-06 13:48:32 +0100385 lua_pushinteger(L, arg->data.sint);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100386 break;
387
388 case ARGT_STR:
389 lua_pushlstring(L, arg->data.str.str, arg->data.str.len);
390 break;
391
392 case ARGT_IPV4:
393 case ARGT_IPV6:
394 case ARGT_MSK4:
395 case ARGT_MSK6:
396 case ARGT_FE:
397 case ARGT_BE:
398 case ARGT_TAB:
399 case ARGT_SRV:
400 case ARGT_USR:
401 case ARGT_MAP:
402 default:
403 lua_pushnil(L);
404 break;
405 }
406 return 1;
407}
408
409/* This function take one entrie in an LUA stack at the index "ud",
410 * and try to convert it in an HAProxy argument entry. This is useful
411 * with sample fetch wrappers. The input arguments are gived to the
412 * lua wrapper and converted as arg list by thi function.
413 */
414static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg)
415{
416 switch (lua_type(L, ud)) {
417
418 case LUA_TNUMBER:
419 case LUA_TBOOLEAN:
420 arg->type = ARGT_SINT;
421 arg->data.sint = lua_tointeger(L, ud);
422 break;
423
424 case LUA_TSTRING:
425 arg->type = ARGT_STR;
426 arg->data.str.str = (char *)lua_tolstring(L, ud, (size_t *)&arg->data.str.len);
427 break;
428
429 case LUA_TUSERDATA:
430 case LUA_TNIL:
431 case LUA_TTABLE:
432 case LUA_TFUNCTION:
433 case LUA_TTHREAD:
434 case LUA_TLIGHTUSERDATA:
435 arg->type = ARGT_SINT;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +0200436 arg->data.sint = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100437 break;
438 }
439 return 1;
440}
441
442/* the following functions are used to convert a struct sample
443 * in Lua type. This useful to convert the return of the
444 * fetchs or converters.
445 */
Willy Tarreau5eadada2015-03-10 17:28:54 +0100446static int hlua_smp2lua(lua_State *L, struct sample *smp)
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100447{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200448 switch (smp->data.type) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100449 case SMP_T_SINT:
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100450 case SMP_T_BOOL:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200451 lua_pushinteger(L, smp->data.u.sint);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100452 break;
453
454 case SMP_T_BIN:
455 case SMP_T_STR:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200456 lua_pushlstring(L, smp->data.u.str.str, smp->data.u.str.len);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100457 break;
458
459 case SMP_T_METH:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200460 switch (smp->data.u.meth.meth) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100461 case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break;
462 case HTTP_METH_GET: lua_pushstring(L, "GET"); break;
463 case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break;
464 case HTTP_METH_POST: lua_pushstring(L, "POST"); break;
465 case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break;
466 case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break;
467 case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break;
468 case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break;
469 case HTTP_METH_OTHER:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200470 lua_pushlstring(L, smp->data.u.meth.str.str, smp->data.u.meth.str.len);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100471 break;
472 default:
473 lua_pushnil(L);
474 break;
475 }
476 break;
477
478 case SMP_T_IPV4:
479 case SMP_T_IPV6:
480 case SMP_T_ADDR: /* This type is never used to qualify a sample. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200481 if (sample_casts[smp->data.type][SMP_T_STR] &&
482 sample_casts[smp->data.type][SMP_T_STR](smp))
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200483 lua_pushlstring(L, smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreau5eadada2015-03-10 17:28:54 +0100484 else
485 lua_pushnil(L);
486 break;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100487 default:
488 lua_pushnil(L);
489 break;
490 }
491 return 1;
492}
493
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100494/* the following functions are used to convert a struct sample
495 * in Lua strings. This is useful to convert the return of the
496 * fetchs or converters.
497 */
498static int hlua_smp2lua_str(lua_State *L, struct sample *smp)
499{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200500 switch (smp->data.type) {
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100501
502 case SMP_T_BIN:
503 case SMP_T_STR:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200504 lua_pushlstring(L, smp->data.u.str.str, smp->data.u.str.len);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100505 break;
506
507 case SMP_T_METH:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200508 switch (smp->data.u.meth.meth) {
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100509 case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break;
510 case HTTP_METH_GET: lua_pushstring(L, "GET"); break;
511 case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break;
512 case HTTP_METH_POST: lua_pushstring(L, "POST"); break;
513 case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break;
514 case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break;
515 case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break;
516 case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break;
517 case HTTP_METH_OTHER:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200518 lua_pushlstring(L, smp->data.u.meth.str.str, smp->data.u.meth.str.len);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100519 break;
520 default:
521 lua_pushstring(L, "");
522 break;
523 }
524 break;
525
526 case SMP_T_SINT:
527 case SMP_T_BOOL:
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100528 case SMP_T_IPV4:
529 case SMP_T_IPV6:
530 case SMP_T_ADDR: /* This type is never used to qualify a sample. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200531 if (sample_casts[smp->data.type][SMP_T_STR] &&
532 sample_casts[smp->data.type][SMP_T_STR](smp))
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200533 lua_pushlstring(L, smp->data.u.str.str, smp->data.u.str.len);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100534 else
535 lua_pushstring(L, "");
536 break;
537 default:
538 lua_pushstring(L, "");
539 break;
540 }
541 return 1;
542}
543
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100544/* the following functions are used to convert an Lua type in a
545 * struct sample. This is useful to provide data from a converter
546 * to the LUA code.
547 */
548static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp)
549{
550 switch (lua_type(L, ud)) {
551
552 case LUA_TNUMBER:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200553 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200554 smp->data.u.sint = lua_tointeger(L, ud);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100555 break;
556
557
558 case LUA_TBOOLEAN:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200559 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200560 smp->data.u.sint = lua_toboolean(L, ud);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100561 break;
562
563 case LUA_TSTRING:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200564 smp->data.type = SMP_T_STR;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100565 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200566 smp->data.u.str.str = (char *)lua_tolstring(L, ud, (size_t *)&smp->data.u.str.len);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100567 break;
568
569 case LUA_TUSERDATA:
570 case LUA_TNIL:
571 case LUA_TTABLE:
572 case LUA_TFUNCTION:
573 case LUA_TTHREAD:
574 case LUA_TLIGHTUSERDATA:
Thierry FOURNIER93405e12015-08-26 14:19:03 +0200575 case LUA_TNONE:
576 default:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200577 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200578 smp->data.u.sint = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100579 break;
580 }
581 return 1;
582}
583
584/* This function check the "argp" builded by another conversion function
585 * is in accord with the expected argp defined by the "mask". The fucntion
586 * returns true or false. It can be adjust the types if there compatibles.
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100587 *
588 * This function assumes thant the argp argument contains ARGM_NBARGS + 1
589 * entries.
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100590 */
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100591__LJMP int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp,
David Carlier0c437f42016-04-27 16:21:56 +0100592 uint64_t mask, struct proxy *p)
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100593{
594 int min_arg;
595 int idx;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100596 struct proxy *px;
597 char *sname, *pname;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100598
599 idx = 0;
600 min_arg = ARGM(mask);
601 mask >>= ARGM_BITS;
602
603 while (1) {
604
605 /* Check oversize. */
606 if (idx >= ARGM_NBARGS && argp[idx].type != ARGT_STOP) {
Cyril Bonté577a36a2015-03-02 00:08:38 +0100607 WILL_LJMP(luaL_argerror(L, first + idx, "Malformed argument mask"));
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100608 }
609
610 /* Check for mandatory arguments. */
611 if (argp[idx].type == ARGT_STOP) {
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100612 if (idx < min_arg) {
613
614 /* If miss other argument than the first one, we return an error. */
615 if (idx > 0)
616 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
617
618 /* If first argument have a certain type, some default values
619 * may be used. See the function smp_resolve_args().
620 */
621 switch (mask & ARGT_MASK) {
622
623 case ARGT_FE:
624 if (!(p->cap & PR_CAP_FE))
625 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
626 argp[idx].data.prx = p;
627 argp[idx].type = ARGT_FE;
628 argp[idx+1].type = ARGT_STOP;
629 break;
630
631 case ARGT_BE:
632 if (!(p->cap & PR_CAP_BE))
633 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
634 argp[idx].data.prx = p;
635 argp[idx].type = ARGT_BE;
636 argp[idx+1].type = ARGT_STOP;
637 break;
638
639 case ARGT_TAB:
640 argp[idx].data.prx = p;
641 argp[idx].type = ARGT_TAB;
642 argp[idx+1].type = ARGT_STOP;
643 break;
644
645 default:
646 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
647 break;
648 }
649 }
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100650 return 0;
651 }
652
653 /* Check for exceed the number of requiered argument. */
654 if ((mask & ARGT_MASK) == ARGT_STOP &&
655 argp[idx].type != ARGT_STOP) {
656 WILL_LJMP(luaL_argerror(L, first + idx, "Last argument expected"));
657 }
658
659 if ((mask & ARGT_MASK) == ARGT_STOP &&
660 argp[idx].type == ARGT_STOP) {
661 return 0;
662 }
663
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100664 /* Convert some argument types. */
665 switch (mask & ARGT_MASK) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100666 case ARGT_SINT:
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100667 if (argp[idx].type != ARGT_SINT)
668 WILL_LJMP(luaL_argerror(L, first + idx, "integer expected"));
669 argp[idx].type = ARGT_SINT;
670 break;
671
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100672 case ARGT_TIME:
673 if (argp[idx].type != ARGT_SINT)
674 WILL_LJMP(luaL_argerror(L, first + idx, "integer expected"));
Thierry FOURNIER29176f32015-07-07 00:41:29 +0200675 argp[idx].type = ARGT_TIME;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100676 break;
677
678 case ARGT_SIZE:
679 if (argp[idx].type != ARGT_SINT)
680 WILL_LJMP(luaL_argerror(L, first + idx, "integer expected"));
Thierry FOURNIER29176f32015-07-07 00:41:29 +0200681 argp[idx].type = ARGT_SIZE;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100682 break;
683
684 case ARGT_FE:
685 if (argp[idx].type != ARGT_STR)
686 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
687 memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len);
688 trash.str[argp[idx].data.str.len] = 0;
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200689 argp[idx].data.prx = proxy_fe_by_name(trash.str);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100690 if (!argp[idx].data.prx)
691 WILL_LJMP(luaL_argerror(L, first + idx, "frontend doesn't exist"));
692 argp[idx].type = ARGT_FE;
693 break;
694
695 case ARGT_BE:
696 if (argp[idx].type != ARGT_STR)
697 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
698 memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len);
699 trash.str[argp[idx].data.str.len] = 0;
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200700 argp[idx].data.prx = proxy_be_by_name(trash.str);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100701 if (!argp[idx].data.prx)
702 WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist"));
703 argp[idx].type = ARGT_BE;
704 break;
705
706 case ARGT_TAB:
707 if (argp[idx].type != ARGT_STR)
708 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
709 memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len);
710 trash.str[argp[idx].data.str.len] = 0;
Willy Tarreaue2dc1fa2015-05-26 12:08:07 +0200711 argp[idx].data.prx = proxy_tbl_by_name(trash.str);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100712 if (!argp[idx].data.prx)
713 WILL_LJMP(luaL_argerror(L, first + idx, "table doesn't exist"));
714 argp[idx].type = ARGT_TAB;
715 break;
716
717 case ARGT_SRV:
718 if (argp[idx].type != ARGT_STR)
719 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
720 memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len);
721 trash.str[argp[idx].data.str.len] = 0;
722 sname = strrchr(trash.str, '/');
723 if (sname) {
724 *sname++ = '\0';
725 pname = trash.str;
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200726 px = proxy_be_by_name(pname);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100727 if (!px)
728 WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist"));
729 }
730 else {
731 sname = trash.str;
732 px = p;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100733 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100734 argp[idx].data.srv = findserver(px, sname);
735 if (!argp[idx].data.srv)
736 WILL_LJMP(luaL_argerror(L, first + idx, "server doesn't exist"));
737 argp[idx].type = ARGT_SRV;
738 break;
739
740 case ARGT_IPV4:
741 memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len);
742 trash.str[argp[idx].data.str.len] = 0;
743 if (inet_pton(AF_INET, trash.str, &argp[idx].data.ipv4))
744 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 address"));
745 argp[idx].type = ARGT_IPV4;
746 break;
747
748 case ARGT_MSK4:
749 memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len);
750 trash.str[argp[idx].data.str.len] = 0;
751 if (!str2mask(trash.str, &argp[idx].data.ipv4))
752 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 mask"));
753 argp[idx].type = ARGT_MSK4;
754 break;
755
756 case ARGT_IPV6:
757 memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len);
758 trash.str[argp[idx].data.str.len] = 0;
759 if (inet_pton(AF_INET6, trash.str, &argp[idx].data.ipv6))
760 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 address"));
761 argp[idx].type = ARGT_IPV6;
762 break;
763
764 case ARGT_MSK6:
Tim Duesterhusb814da62018-01-25 16:24:50 +0100765 memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len);
766 trash.str[argp[idx].data.str.len] = 0;
767 if (!str2mask6(trash.str, &argp[idx].data.ipv6))
768 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 mask"));
769 argp[idx].type = ARGT_MSK6;
770 break;
771
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100772 case ARGT_MAP:
773 case ARGT_REG:
774 case ARGT_USR:
775 WILL_LJMP(luaL_argerror(L, first + idx, "type not yet supported"));
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100776 break;
777 }
778
779 /* Check for type of argument. */
780 if ((mask & ARGT_MASK) != argp[idx].type) {
781 const char *msg = lua_pushfstring(L, "'%s' expected, got '%s'",
782 arg_type_names[(mask & ARGT_MASK)],
783 arg_type_names[argp[idx].type & ARGT_MASK]);
784 WILL_LJMP(luaL_argerror(L, first + idx, msg));
785 }
786
787 /* Next argument. */
788 mask >>= ARGT_BITS;
789 idx++;
790 }
791}
792
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100793/*
794 * The following functions are used to make correspondance between the the
795 * executed lua pointer and the "struct hlua *" that contain the context.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100796 *
797 * - hlua_gethlua : return the hlua context associated with an lua_State.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100798 * - hlua_sethlua : create the association between hlua context and lua_state.
799 */
800static inline struct hlua *hlua_gethlua(lua_State *L)
801{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +0100802 struct hlua **hlua = lua_getextraspace(L);
803 return *hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100804}
805static inline void hlua_sethlua(struct hlua *hlua)
806{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +0100807 struct hlua **hlua_store = lua_getextraspace(hlua->T);
808 *hlua_store = hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100809}
810
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100811/* This function is used to send logs. It try to send on screen (stderr)
812 * and on the default syslog server.
813 */
814static inline void hlua_sendlog(struct proxy *px, int level, const char *msg)
815{
816 struct tm tm;
817 char *p;
818
819 /* Cleanup the log message. */
820 p = trash.str;
821 for (; *msg != '\0'; msg++, p++) {
Thierry FOURNIERccf00632015-09-16 12:47:03 +0200822 if (p >= trash.str + trash.size - 1) {
823 /* Break the message if exceed the buffer size. */
824 *(p-4) = ' ';
825 *(p-3) = '.';
826 *(p-2) = '.';
827 *(p-1) = '.';
828 break;
829 }
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100830 if (isprint(*msg))
831 *p = *msg;
832 else
833 *p = '.';
834 }
835 *p = '\0';
836
Thierry FOURNIER5554e292015-09-09 11:21:37 +0200837 send_log(px, level, "%s\n", trash.str);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100838 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
Willy Tarreaua678b432015-08-28 10:14:59 +0200839 get_localtime(date.tv_sec, &tm);
840 fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n",
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100841 log_levels[level], tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec,
842 (int)getpid(), trash.str);
843 fflush(stderr);
844 }
845}
846
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100847/* This function just ensure that the yield will be always
848 * returned with a timeout and permit to set some flags
849 */
850__LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx,
Thierry FOURNIERf90838b2015-03-06 13:48:32 +0100851 lua_KFunction k, int timeout, unsigned int flags)
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100852{
853 struct hlua *hlua = hlua_gethlua(L);
854
855 /* Set the wake timeout. If timeout is required, we set
856 * the expiration time.
857 */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +0200858 hlua->wake_time = timeout;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100859
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +0100860 hlua->flags |= flags;
861
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100862 /* Process the yield. */
863 WILL_LJMP(lua_yieldk(L, nresults, ctx, k));
864}
865
Willy Tarreau87b09662015-04-03 00:22:06 +0200866/* This function initialises the Lua environment stored in the stream.
867 * It must be called at the start of the stream. This function creates
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100868 * an LUA coroutine. It can not be use to crete the main LUA context.
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200869 *
870 * This function is particular. it initialises a new Lua thread. If the
871 * initialisation fails (example: out of memory error), the lua function
872 * throws an error (longjmp).
873 *
874 * This function manipulates two Lua stack: the main and the thread. Only
875 * the main stack can fail. The thread is not manipulated. This function
876 * MUST NOT manipulate the created thread stack state, because is not
877 * proctected agains error throwed by the thread stack.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100878 */
879int hlua_ctx_init(struct hlua *lua, struct task *task)
880{
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200881 if (!SET_SAFE_LJMP(gL.T)) {
882 lua->Tref = LUA_REFNIL;
883 return 0;
884 }
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100885 lua->Mref = LUA_REFNIL;
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +0100886 lua->flags = 0;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100887 LIST_INIT(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100888 lua->T = lua_newthread(gL.T);
889 if (!lua->T) {
890 lua->Tref = LUA_REFNIL;
Thierry FOURNIER0a976202017-07-12 11:18:00 +0200891 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100892 return 0;
893 }
894 hlua_sethlua(lua);
895 lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
896 lua->task = task;
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200897 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100898 return 1;
899}
900
Willy Tarreau87b09662015-04-03 00:22:06 +0200901/* Used to destroy the Lua coroutine when the attached stream or task
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100902 * is destroyed. The destroy also the memory context. The struct "lua"
903 * is not freed.
904 */
905void hlua_ctx_destroy(struct hlua *lua)
906{
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100907 if (!lua)
Thierry FOURNIERa718b292015-03-04 16:48:34 +0100908 return;
909
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100910 if (!lua->T)
911 goto end;
912
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100913 /* Purge all the pending signals. */
Thierry FOURNIERd6975962017-07-12 14:31:10 +0200914 notification_purge(&lua->com);
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100915
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200916 if (!SET_SAFE_LJMP(lua->T))
917 return;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100918 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200919 RESET_SAFE_LJMP(lua->T);
Thierry FOURNIER5a50a852015-09-23 16:59:28 +0200920
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200921 if (!SET_SAFE_LJMP(gL.T))
922 return;
923 luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref);
924 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER5a50a852015-09-23 16:59:28 +0200925 /* Forces a garbage collecting process. If the Lua program is finished
926 * without error, we run the GC on the thread pointer. Its freed all
927 * the unused memory.
928 * If the thread is finnish with an error or is currently yielded,
929 * it seems that the GC applied on the thread doesn't clean anything,
930 * so e run the GC on the main thread.
931 * NOTE: maybe this action locks all the Lua threads untiml the en of
932 * the garbage collection.
933 */
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +0200934 if (lua->flags & HLUA_MUST_GC) {
Thierry FOURNIER7bd10d52017-07-17 00:44:40 +0200935 if (!SET_SAFE_LJMP(gL.T))
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200936 return;
Thierry FOURNIER7bd10d52017-07-17 00:44:40 +0200937 lua_gc(gL.T, LUA_GCCOLLECT, 0);
938 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +0200939 }
Thierry FOURNIER5a50a852015-09-23 16:59:28 +0200940
Thierry FOURNIERa7b536b2015-09-21 22:50:24 +0200941 lua->T = NULL;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100942
943end:
Willy Tarreaubafbe012017-11-24 17:34:44 +0100944 pool_free(pool_head_hlua, lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100945}
946
947/* This function is used to restore the Lua context when a coroutine
948 * fails. This function copy the common memory between old coroutine
949 * and the new coroutine. The old coroutine is destroyed, and its
950 * replaced by the new coroutine.
951 * If the flag "keep_msg" is set, the last entry of the old is assumed
952 * as string error message and it is copied in the new stack.
953 */
954static int hlua_ctx_renew(struct hlua *lua, int keep_msg)
955{
956 lua_State *T;
957 int new_ref;
958
959 /* Renew the main LUA stack doesn't have sense. */
960 if (lua == &gL)
961 return 0;
962
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100963 /* New Lua coroutine. */
964 T = lua_newthread(gL.T);
965 if (!T)
966 return 0;
967
968 /* Copy last error message. */
969 if (keep_msg)
970 lua_xmove(lua->T, T, 1);
971
972 /* Copy data between the coroutines. */
973 lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref);
974 lua_xmove(lua->T, T, 1);
975 new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Valur poped. */
976
977 /* Destroy old data. */
978 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
979
980 /* The thread is garbage collected by Lua. */
981 luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref);
982
983 /* Fill the struct with the new coroutine values. */
984 lua->Mref = new_ref;
985 lua->T = T;
986 lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
987
988 /* Set context. */
989 hlua_sethlua(lua);
990
991 return 1;
992}
993
Thierry FOURNIERee9f8022015-03-03 17:37:37 +0100994void hlua_hook(lua_State *L, lua_Debug *ar)
995{
Thierry FOURNIERcae49c92015-03-06 14:05:24 +0100996 struct hlua *hlua = hlua_gethlua(L);
997
998 /* Lua cannot yield when its returning from a function,
999 * so, we can fix the interrupt hook to 1 instruction,
1000 * expecting that the function is finnished.
1001 */
1002 if (lua_gethookmask(L) & LUA_MASKRET) {
1003 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1);
1004 return;
1005 }
1006
1007 /* restore the interrupt condition. */
1008 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
1009
1010 /* If we interrupt the Lua processing in yieldable state, we yield.
1011 * If the state is not yieldable, trying yield causes an error.
1012 */
1013 if (lua_isyieldable(L))
1014 WILL_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD));
1015
Thierry FOURNIERa85cfb12015-03-13 14:50:06 +01001016 /* If we cannot yield, update the clock and check the timeout. */
1017 tv_update_date(0, 1);
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001018 hlua->run_time += now_ms - hlua->start_time;
1019 if (hlua->max_time && hlua->run_time >= hlua->max_time) {
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001020 lua_pushfstring(L, "execution timeout");
1021 WILL_LJMP(lua_error(L));
1022 }
1023
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001024 /* Update the start time. */
1025 hlua->start_time = now_ms;
1026
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001027 /* Try to interrupt the process at the end of the current
1028 * unyieldable function.
1029 */
1030 lua_sethook(hlua->T, hlua_hook, LUA_MASKRET|LUA_MASKCOUNT, hlua_nb_instruction);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001031}
1032
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001033/* This function start or resumes the Lua stack execution. If the flag
1034 * "yield_allowed" if no set and the LUA stack execution returns a yield
1035 * The function return an error.
1036 *
1037 * The function can returns 4 values:
1038 * - HLUA_E_OK : The execution is terminated without any errors.
1039 * - HLUA_E_AGAIN : The execution must continue at the next associated
1040 * task wakeup.
1041 * - HLUA_E_ERRMSG : An error has occured, an error message is set in
1042 * the top of the stack.
1043 * - HLUA_E_ERR : An error has occured without error message.
1044 *
1045 * If an error occured, the stack is renewed and it is ready to run new
1046 * LUA code.
1047 */
1048static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed)
1049{
1050 int ret;
1051 const char *msg;
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001052 const char *trace;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001053
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001054 /* Initialise run time counter. */
1055 if (!HLUA_IS_RUNNING(lua))
1056 lua->run_time = 0;
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001057
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001058 /* Lock the whole Lua execution. This lock must be before the
1059 * label "resume_execution".
1060 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001061 HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock);
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001062
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001063resume_execution:
1064
1065 /* This hook interrupts the Lua processing each 'hlua_nb_instruction'
1066 * instructions. it is used for preventing infinite loops.
1067 */
1068 lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
1069
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +01001070 /* Remove all flags except the running flags. */
Thierry FOURNIER2f3867f2015-09-28 01:02:01 +02001071 HLUA_SET_RUN(lua);
1072 HLUA_CLR_CTRLYIELD(lua);
1073 HLUA_CLR_WAKERESWR(lua);
1074 HLUA_CLR_WAKEREQWR(lua);
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +01001075
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001076 /* Update the start time. */
1077 lua->start_time = now_ms;
1078
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001079 /* Call the function. */
1080 ret = lua_resume(lua->T, gL.T, lua->nargs);
1081 switch (ret) {
1082
1083 case LUA_OK:
1084 ret = HLUA_E_OK;
1085 break;
1086
1087 case LUA_YIELD:
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001088 /* Check if the execution timeout is expired. It it is the case, we
1089 * break the Lua execution.
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001090 */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001091 tv_update_date(0, 1);
1092 lua->run_time += now_ms - lua->start_time;
1093 if (lua->max_time && lua->run_time > lua->max_time) {
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001094 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001095 ret = HLUA_E_ETMOUT;
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001096 break;
1097 }
1098 /* Process the forced yield. if the general yield is not allowed or
1099 * if no task were associated this the current Lua execution
1100 * coroutine, we resume the execution. Else we want to return in the
1101 * scheduler and we want to be waked up again, to continue the
1102 * current Lua execution. So we schedule our own task.
1103 */
1104 if (HLUA_IS_CTRLYIELDING(lua)) {
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001105 if (!yield_allowed || !lua->task)
1106 goto resume_execution;
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001107 task_wakeup(lua->task, TASK_WOKEN_MSG);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001108 }
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001109 if (!yield_allowed) {
1110 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001111 ret = HLUA_E_YIELD;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001112 break;
1113 }
1114 ret = HLUA_E_AGAIN;
1115 break;
1116
1117 case LUA_ERRRUN:
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001118
1119 /* Special exit case. The traditionnal exit is returned as an error
1120 * because the errors ares the only one mean to return immediately
1121 * from and lua execution.
1122 */
1123 if (lua->flags & HLUA_EXIT) {
1124 ret = HLUA_E_OK;
Thierry FOURNIERe1587b32015-08-28 09:54:13 +02001125 hlua_ctx_renew(lua, 0);
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001126 break;
1127 }
1128
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001129 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001130 if (!lua_checkstack(lua->T, 1)) {
1131 ret = HLUA_E_ERR;
1132 break;
1133 }
1134 msg = lua_tostring(lua->T, -1);
1135 lua_settop(lua->T, 0); /* Empty the stack. */
1136 lua_pop(lua->T, 1);
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001137 trace = hlua_traceback(lua->T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001138 if (msg)
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001139 lua_pushfstring(lua->T, "runtime error: %s from %s", msg, trace);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001140 else
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001141 lua_pushfstring(lua->T, "unknown runtime error from %s", trace);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001142 ret = HLUA_E_ERRMSG;
1143 break;
1144
1145 case LUA_ERRMEM:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001146 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001147 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001148 ret = HLUA_E_NOMEM;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001149 break;
1150
1151 case LUA_ERRERR:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001152 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001153 if (!lua_checkstack(lua->T, 1)) {
1154 ret = HLUA_E_ERR;
1155 break;
1156 }
1157 msg = lua_tostring(lua->T, -1);
1158 lua_settop(lua->T, 0); /* Empty the stack. */
1159 lua_pop(lua->T, 1);
1160 if (msg)
1161 lua_pushfstring(lua->T, "message handler error: %s", msg);
1162 else
1163 lua_pushfstring(lua->T, "message handler error");
1164 ret = HLUA_E_ERRMSG;
1165 break;
1166
1167 default:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001168 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001169 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001170 ret = HLUA_E_ERR;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001171 break;
1172 }
1173
Thierry FOURNIER6ab4d8e2015-09-27 22:17:19 +02001174 /* This GC permits to destroy some object when a Lua timeout strikes. */
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +02001175 if (lua->flags & HLUA_MUST_GC &&
1176 ret != HLUA_E_AGAIN)
Thierry FOURNIER6ab4d8e2015-09-27 22:17:19 +02001177 lua_gc(lua->T, LUA_GCCOLLECT, 0);
1178
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001179 switch (ret) {
1180 case HLUA_E_AGAIN:
1181 break;
1182
1183 case HLUA_E_ERRMSG:
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001184 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001185 hlua_ctx_renew(lua, 1);
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001186 HLUA_CLR_RUN(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001187 break;
1188
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001189 case HLUA_E_ETMOUT:
1190 case HLUA_E_NOMEM:
1191 case HLUA_E_YIELD:
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001192 case HLUA_E_ERR:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001193 HLUA_CLR_RUN(lua);
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001194 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001195 hlua_ctx_renew(lua, 0);
1196 break;
1197
1198 case HLUA_E_OK:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001199 HLUA_CLR_RUN(lua);
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001200 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001201 break;
1202 }
1203
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001204 /* This is the main exit point, remove the Lua lock. */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001205 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock);
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001206
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001207 return ret;
1208}
1209
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001210/* This function exit the current code. */
1211__LJMP static int hlua_done(lua_State *L)
1212{
1213 struct hlua *hlua = hlua_gethlua(L);
1214
1215 hlua->flags |= HLUA_EXIT;
1216 WILL_LJMP(lua_error(L));
1217
1218 return 0;
1219}
1220
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001221/* This function is an LUA binding. It provides a function
1222 * for deleting ACL from a referenced ACL file.
1223 */
1224__LJMP static int hlua_del_acl(lua_State *L)
1225{
1226 const char *name;
1227 const char *key;
1228 struct pat_ref *ref;
1229
1230 MAY_LJMP(check_args(L, 2, "del_acl"));
1231
1232 name = MAY_LJMP(luaL_checkstring(L, 1));
1233 key = MAY_LJMP(luaL_checkstring(L, 2));
1234
1235 ref = pat_ref_lookup(name);
1236 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001237 WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001238
1239 pat_ref_delete(ref, key);
1240 return 0;
1241}
1242
1243/* This function is an LUA binding. It provides a function
1244 * for deleting map entry from a referenced map file.
1245 */
1246static int hlua_del_map(lua_State *L)
1247{
1248 const char *name;
1249 const char *key;
1250 struct pat_ref *ref;
1251
1252 MAY_LJMP(check_args(L, 2, "del_map"));
1253
1254 name = MAY_LJMP(luaL_checkstring(L, 1));
1255 key = MAY_LJMP(luaL_checkstring(L, 2));
1256
1257 ref = pat_ref_lookup(name);
1258 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001259 WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001260
1261 pat_ref_delete(ref, key);
1262 return 0;
1263}
1264
1265/* This function is an LUA binding. It provides a function
1266 * for adding ACL pattern from a referenced ACL file.
1267 */
1268static int hlua_add_acl(lua_State *L)
1269{
1270 const char *name;
1271 const char *key;
1272 struct pat_ref *ref;
1273
1274 MAY_LJMP(check_args(L, 2, "add_acl"));
1275
1276 name = MAY_LJMP(luaL_checkstring(L, 1));
1277 key = MAY_LJMP(luaL_checkstring(L, 2));
1278
1279 ref = pat_ref_lookup(name);
1280 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001281 WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001282
1283 if (pat_ref_find_elt(ref, key) == NULL)
1284 pat_ref_add(ref, key, NULL, NULL);
1285 return 0;
1286}
1287
1288/* This function is an LUA binding. It provides a function
1289 * for setting map pattern and sample from a referenced map
1290 * file.
1291 */
1292static int hlua_set_map(lua_State *L)
1293{
1294 const char *name;
1295 const char *key;
1296 const char *value;
1297 struct pat_ref *ref;
1298
1299 MAY_LJMP(check_args(L, 3, "set_map"));
1300
1301 name = MAY_LJMP(luaL_checkstring(L, 1));
1302 key = MAY_LJMP(luaL_checkstring(L, 2));
1303 value = MAY_LJMP(luaL_checkstring(L, 3));
1304
1305 ref = pat_ref_lookup(name);
1306 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001307 WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001308
1309 if (pat_ref_find_elt(ref, key) != NULL)
1310 pat_ref_set(ref, key, value, NULL);
1311 else
1312 pat_ref_add(ref, key, value, NULL);
1313 return 0;
1314}
1315
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001316/* A class is a lot of memory that contain data. This data can be a table,
1317 * an integer or user data. This data is associated with a metatable. This
1318 * metatable have an original version registred in the global context with
1319 * the name of the object (_G[<name>] = <metable> ).
1320 *
1321 * A metable is a table that modify the standard behavior of a standard
1322 * access to the associated data. The entries of this new metatable are
1323 * defined as is:
1324 *
1325 * http://lua-users.org/wiki/MetatableEvents
1326 *
1327 * __index
1328 *
1329 * we access an absent field in a table, the result is nil. This is
1330 * true, but it is not the whole truth. Actually, such access triggers
1331 * the interpreter to look for an __index metamethod: If there is no
1332 * such method, as usually happens, then the access results in nil;
1333 * otherwise, the metamethod will provide the result.
1334 *
1335 * Control 'prototype' inheritance. When accessing "myTable[key]" and
1336 * the key does not appear in the table, but the metatable has an __index
1337 * property:
1338 *
1339 * - if the value is a function, the function is called, passing in the
1340 * table and the key; the return value of that function is returned as
1341 * the result.
1342 *
1343 * - if the value is another table, the value of the key in that table is
1344 * asked for and returned (and if it doesn't exist in that table, but that
1345 * table's metatable has an __index property, then it continues on up)
1346 *
1347 * - Use "rawget(myTable,key)" to skip this metamethod.
1348 *
1349 * http://www.lua.org/pil/13.4.1.html
1350 *
1351 * __newindex
1352 *
1353 * Like __index, but control property assignment.
1354 *
1355 * __mode - Control weak references. A string value with one or both
1356 * of the characters 'k' and 'v' which specifies that the the
1357 * keys and/or values in the table are weak references.
1358 *
1359 * __call - Treat a table like a function. When a table is followed by
1360 * parenthesis such as "myTable( 'foo' )" and the metatable has
1361 * a __call key pointing to a function, that function is invoked
1362 * (passing any specified arguments) and the return value is
1363 * returned.
1364 *
1365 * __metatable - Hide the metatable. When "getmetatable( myTable )" is
1366 * called, if the metatable for myTable has a __metatable
1367 * key, the value of that key is returned instead of the
1368 * actual metatable.
1369 *
1370 * __tostring - Control string representation. When the builtin
1371 * "tostring( myTable )" function is called, if the metatable
1372 * for myTable has a __tostring property set to a function,
1373 * that function is invoked (passing myTable to it) and the
1374 * return value is used as the string representation.
1375 *
1376 * __len - Control table length. When the table length is requested using
1377 * the length operator ( '#' ), if the metatable for myTable has
1378 * a __len key pointing to a function, that function is invoked
1379 * (passing myTable to it) and the return value used as the value
1380 * of "#myTable".
1381 *
1382 * __gc - Userdata finalizer code. When userdata is set to be garbage
1383 * collected, if the metatable has a __gc field pointing to a
1384 * function, that function is first invoked, passing the userdata
1385 * to it. The __gc metamethod is not called for tables.
1386 * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html)
1387 *
1388 * Special metamethods for redefining standard operators:
1389 * http://www.lua.org/pil/13.1.html
1390 *
1391 * __add "+"
1392 * __sub "-"
1393 * __mul "*"
1394 * __div "/"
1395 * __unm "!"
1396 * __pow "^"
1397 * __concat ".."
1398 *
1399 * Special methods for redfining standar relations
1400 * http://www.lua.org/pil/13.2.html
1401 *
1402 * __eq "=="
1403 * __lt "<"
1404 * __le "<="
1405 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001406
1407/*
1408 *
1409 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001410 * Class Map
1411 *
1412 *
1413 */
1414
1415/* Returns a struct hlua_map if the stack entry "ud" is
1416 * a class session, otherwise it throws an error.
1417 */
1418__LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud)
1419{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001420 return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001421}
1422
1423/* This function is the map constructor. It don't need
1424 * the class Map object. It creates and return a new Map
1425 * object. It must be called only during "body" or "init"
1426 * context because it process some filesystem accesses.
1427 */
1428__LJMP static int hlua_map_new(struct lua_State *L)
1429{
1430 const char *fn;
1431 int match = PAT_MATCH_STR;
1432 struct sample_conv conv;
1433 const char *file = "";
1434 int line = 0;
1435 lua_Debug ar;
1436 char *err = NULL;
1437 struct arg args[2];
1438
1439 if (lua_gettop(L) < 1 || lua_gettop(L) > 2)
1440 WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument."));
1441
1442 fn = MAY_LJMP(luaL_checkstring(L, 1));
1443
1444 if (lua_gettop(L) >= 2) {
1445 match = MAY_LJMP(luaL_checkinteger(L, 2));
1446 if (match < 0 || match >= PAT_MATCH_NUM)
1447 WILL_LJMP(luaL_error(L, "'new' needs a valid match method."));
1448 }
1449
1450 /* Get Lua filename and line number. */
1451 if (lua_getstack(L, 1, &ar)) { /* check function at level */
1452 lua_getinfo(L, "Sl", &ar); /* get info about it */
1453 if (ar.currentline > 0) { /* is there info? */
1454 file = ar.short_src;
1455 line = ar.currentline;
1456 }
1457 }
1458
1459 /* fill fake sample_conv struct. */
1460 conv.kw = ""; /* unused. */
1461 conv.process = NULL; /* unused. */
1462 conv.arg_mask = 0; /* unused. */
1463 conv.val_args = NULL; /* unused. */
1464 conv.out_type = SMP_T_STR;
1465 conv.private = (void *)(long)match;
1466 switch (match) {
1467 case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break;
1468 case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break;
1469 case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break;
1470 case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break;
1471 case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break;
1472 case PAT_MATCH_END: conv.in_type = SMP_T_STR; break;
1473 case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001474 case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001475 case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break;
1476 default:
1477 WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode."));
1478 }
1479
1480 /* fill fake args. */
1481 args[0].type = ARGT_STR;
1482 args[0].data.str.str = (char *)fn;
1483 args[1].type = ARGT_STOP;
1484
1485 /* load the map. */
1486 if (!sample_load_map(args, &conv, file, line, &err)) {
1487 /* error case: we cant use luaL_error because we must
1488 * free the err variable.
1489 */
1490 luaL_where(L, 1);
1491 lua_pushfstring(L, "'new': %s.", err);
1492 lua_concat(L, 2);
1493 free(err);
1494 WILL_LJMP(lua_error(L));
1495 }
1496
1497 /* create the lua object. */
1498 lua_newtable(L);
1499 lua_pushlightuserdata(L, args[0].data.map);
1500 lua_rawseti(L, -2, 0);
1501
1502 /* Pop a class Map metatable and affect it to the userdata. */
1503 lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref);
1504 lua_setmetatable(L, -2);
1505
1506
1507 return 1;
1508}
1509
1510__LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str)
1511{
1512 struct map_descriptor *desc;
1513 struct pattern *pat;
1514 struct sample smp;
1515
1516 MAY_LJMP(check_args(L, 2, "lookup"));
1517 desc = MAY_LJMP(hlua_checkmap(L, 1));
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001518 if (desc->pat.expect_type == SMP_T_SINT) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001519 smp.data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001520 smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001521 }
1522 else {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001523 smp.data.type = SMP_T_STR;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001524 smp.flags = SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001525 smp.data.u.str.str = (char *)MAY_LJMP(luaL_checklstring(L, 2, (size_t *)&smp.data.u.str.len));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001526 }
1527
1528 pat = pattern_exec_match(&desc->pat, &smp, 1);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001529 if (!pat || !pat->data) {
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001530 if (str)
1531 lua_pushstring(L, "");
1532 else
1533 lua_pushnil(L);
1534 return 1;
1535 }
1536
1537 /* The Lua pattern must return a string, so we can't check the returned type */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001538 lua_pushlstring(L, pat->data->u.str.str, pat->data->u.str.len);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001539 return 1;
1540}
1541
1542__LJMP static int hlua_map_lookup(struct lua_State *L)
1543{
1544 return _hlua_map_lookup(L, 0);
1545}
1546
1547__LJMP static int hlua_map_slookup(struct lua_State *L)
1548{
1549 return _hlua_map_lookup(L, 1);
1550}
1551
1552/*
1553 *
1554 *
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001555 * Class Socket
1556 *
1557 *
1558 */
1559
1560__LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud)
1561{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001562 return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001563}
1564
1565/* This function is the handler called for each I/O on the established
1566 * connection. It is used for notify space avalaible to send or data
1567 * received.
1568 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001569static void hlua_socket_handler(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001570{
Willy Tarreau00a37f02015-04-13 12:05:19 +02001571 struct stream_interface *si = appctx->owner;
Olivier Houchard9aaf7782017-09-13 18:30:23 +02001572 struct connection *c = cs_conn(objt_cs(si_opposite(si)->end));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001573
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001574 if (appctx->ctx.hlua_cosocket.die) {
1575 si_shutw(si);
1576 si_shutr(si);
1577 si_ic(si)->flags |= CF_READ_NULL;
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001578 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1579 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001580 stream_shutdown(si_strm(si), SF_ERR_KILLED);
1581 }
1582
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001583 /* If the connection object is not avalaible, close all the
1584 * streams and wakeup everithing waiting for.
1585 */
1586 if (!c) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001587 si_shutw(si);
1588 si_shutr(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01001589 si_ic(si)->flags |= CF_READ_NULL;
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001590 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1591 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Willy Tarreaud4da1962015-04-20 01:31:23 +02001592 return;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001593 }
1594
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001595 /* If we cant write, wakeup the pending write signals. */
1596 if (channel_output_closed(si_ic(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001597 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001598
1599 /* If we cant read, wakeup the pending read signals. */
1600 if (channel_input_closed(si_oc(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001601 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001602
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001603 /* if the connection is not estabkished, inform the stream that we want
1604 * to be notified whenever the connection completes.
1605 */
1606 if (!(c->flags & CO_FL_CONNECTED)) {
1607 si_applet_cant_get(si);
1608 si_applet_cant_put(si);
Willy Tarreaud4da1962015-04-20 01:31:23 +02001609 return;
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001610 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001611
1612 /* This function is called after the connect. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01001613 appctx->ctx.hlua_cosocket.connected = 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001614
1615 /* Wake the tasks which wants to write if the buffer have avalaible space. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001616 if (channel_may_recv(si_ic(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001617 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001618
1619 /* Wake the tasks which wants to read if the buffer contains data. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001620 if (!channel_is_empty(si_oc(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001621 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001622
1623 /* Some data were injected in the buffer, notify the stream
1624 * interface.
1625 */
1626 if (!channel_is_empty(si_ic(si)))
1627 stream_int_update(si);
1628
1629 /* If write notifications are registered, we considers we want
1630 * to write, so we set the flag cant put
1631 */
1632 if (notification_registered(&appctx->ctx.hlua_cosocket.wake_on_write))
1633 si_applet_cant_put(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001634}
1635
Willy Tarreau87b09662015-04-03 00:22:06 +02001636/* This function is called when the "struct stream" is destroyed.
1637 * Remove the link from the object to this stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001638 * Wake all the pending signals.
1639 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001640static void hlua_socket_release(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001641{
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001642 struct xref *peer;
1643
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001644 /* Remove my link in the original object. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001645 peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref);
1646 if (peer)
1647 xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001648
1649 /* Wake all the task waiting for me. */
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001650 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1651 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001652}
1653
1654/* If the garbage collectio of the object is launch, nobody
Willy Tarreau87b09662015-04-03 00:22:06 +02001655 * uses this object. If the stream does not exists, just quit.
1656 * Send the shutdown signal to the stream. In some cases,
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001657 * pending signal can rest in the read and write lists. destroy
1658 * it.
1659 */
1660__LJMP static int hlua_socket_gc(lua_State *L)
1661{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001662 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001663 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001664 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001665
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001666 MAY_LJMP(check_args(L, 1, "__gc"));
1667
1668 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001669 peer = xref_get_peer_and_lock(&socket->xref);
1670 if (!peer)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001671 return 0;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001672 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001673
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001674 /* Set the flag which destroy the session. */
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001675 appctx->ctx.hlua_cosocket.die = 1;
1676 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001677
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001678 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001679 xref_disconnect(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001680 return 0;
1681}
1682
1683/* The close function send shutdown signal and break the
Willy Tarreau87b09662015-04-03 00:22:06 +02001684 * links between the stream and the object.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001685 */
sada05ed3302018-05-11 11:48:18 -07001686__LJMP static int hlua_socket_close_helper(lua_State *L)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001687{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001688 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001689 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001690 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001691
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001692 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001693
1694 /* Check if we run on the same thread than the xreator thread.
1695 * We cannot access to the socket if the thread is different.
1696 */
1697 if (socket->tid != tid)
1698 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1699
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001700 peer = xref_get_peer_and_lock(&socket->xref);
1701 if (!peer)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001702 return 0;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001703 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001704
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001705 /* Set the flag which destroy the session. */
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001706 appctx->ctx.hlua_cosocket.die = 1;
1707 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001708
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001709 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001710 xref_disconnect(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001711 return 0;
1712}
1713
sada05ed3302018-05-11 11:48:18 -07001714/* The close function calls close_helper.
1715 */
1716__LJMP static int hlua_socket_close(lua_State *L)
1717{
1718 MAY_LJMP(check_args(L, 1, "close"));
1719 return hlua_socket_close_helper(L);
1720}
1721
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001722/* This Lua function assumes that the stack contain three parameters.
1723 * 1 - USERDATA containing a struct socket
1724 * 2 - INTEGER with values of the macro defined below
1725 * If the integer is -1, we must read at most one line.
1726 * If the integer is -2, we ust read all the data until the
1727 * end of the stream.
1728 * If the integer is positive value, we must read a number of
1729 * bytes corresponding to this value.
1730 */
1731#define HLSR_READ_LINE (-1)
1732#define HLSR_READ_ALL (-2)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001733__LJMP static int hlua_socket_receive_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001734{
1735 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
1736 int wanted = lua_tointeger(L, 2);
1737 struct hlua *hlua = hlua_gethlua(L);
1738 struct appctx *appctx;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001739 size_t len;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001740 int nblk;
Willy Tarreau206ba832018-06-14 15:27:31 +02001741 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001742 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02001743 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001744 size_t len2;
Thierry FOURNIER00543922015-03-09 18:35:06 +01001745 int skip_at_end = 0;
Willy Tarreau81389672015-03-10 12:03:52 +01001746 struct channel *oc;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001747 struct stream_interface *si;
1748 struct stream *s;
1749 struct xref *peer;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001750 int missing_bytes;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001751
1752 /* Check if this lua stack is schedulable. */
1753 if (!hlua || !hlua->task)
1754 WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in "
1755 "'frontend', 'backend' or 'task'"));
1756
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001757 /* Check if we run on the same thread than the xreator thread.
1758 * We cannot access to the socket if the thread is different.
1759 */
1760 if (socket->tid != tid)
1761 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1762
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001763 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001764 peer = xref_get_peer_and_lock(&socket->xref);
1765 if (!peer)
1766 goto no_peer;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001767 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
1768 si = appctx->owner;
1769 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001770
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001771 oc = &s->res;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001772 if (wanted == HLSR_READ_LINE) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001773 /* Read line. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001774 nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001775 if (nblk < 0) /* Connection close. */
1776 goto connection_closed;
1777 if (nblk == 0) /* No data avalaible. */
1778 goto connection_empty;
Thierry FOURNIER00543922015-03-09 18:35:06 +01001779
1780 /* remove final \r\n. */
1781 if (nblk == 1) {
1782 if (blk1[len1-1] == '\n') {
1783 len1--;
1784 skip_at_end++;
1785 if (blk1[len1-1] == '\r') {
1786 len1--;
1787 skip_at_end++;
1788 }
1789 }
1790 }
1791 else {
1792 if (blk2[len2-1] == '\n') {
1793 len2--;
1794 skip_at_end++;
1795 if (blk2[len2-1] == '\r') {
1796 len2--;
1797 skip_at_end++;
1798 }
1799 }
1800 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001801 }
1802
1803 else if (wanted == HLSR_READ_ALL) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001804 /* Read all the available data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001805 nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001806 if (nblk < 0) /* Connection close. */
1807 goto connection_closed;
1808 if (nblk == 0) /* No data avalaible. */
1809 goto connection_empty;
1810 }
1811
1812 else {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001813 /* Read a block of data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001814 nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001815 if (nblk < 0) /* Connection close. */
1816 goto connection_closed;
1817 if (nblk == 0) /* No data avalaible. */
1818 goto connection_empty;
1819
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001820 missing_bytes = wanted - socket->b.n;
1821 if (len1 > missing_bytes) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001822 nblk = 1;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001823 len1 = missing_bytes;
1824 } if (nblk == 2 && len1 + len2 > missing_bytes)
1825 len2 = missing_bytes - len1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001826 }
1827
1828 len = len1;
1829
1830 luaL_addlstring(&socket->b, blk1, len1);
1831 if (nblk == 2) {
1832 len += len2;
1833 luaL_addlstring(&socket->b, blk2, len2);
1834 }
1835
1836 /* Consume data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001837 co_skip(oc, len + skip_at_end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001838
1839 /* Don't wait anything. */
Thierry FOURNIER7e4ee472018-05-25 15:03:50 +02001840 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001841
1842 /* If the pattern reclaim to read all the data
1843 * in the connection, got out.
1844 */
1845 if (wanted == HLSR_READ_ALL)
1846 goto connection_empty;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001847 else if (wanted >= 0 && socket->b.n < wanted)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001848 goto connection_empty;
1849
1850 /* Return result. */
1851 luaL_pushresult(&socket->b);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001852 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001853 return 1;
1854
1855connection_closed:
1856
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001857 xref_unlock(&socket->xref, peer);
1858
1859no_peer:
1860
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001861 /* If the buffer containds data. */
1862 if (socket->b.n > 0) {
1863 luaL_pushresult(&socket->b);
1864 return 1;
1865 }
1866 lua_pushnil(L);
1867 lua_pushstring(L, "connection closed.");
1868 return 2;
1869
1870connection_empty:
1871
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001872 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) {
1873 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001874 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001875 }
1876 xref_unlock(&socket->xref, peer);
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +01001877 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_receive_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001878 return 0;
1879}
1880
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001881/* This Lua function gets two parameters. The first one can be string
1882 * or a number. If the string is "*l", the user requires one line. If
1883 * the string is "*a", the user requires all the contents of the stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001884 * If the value is a number, the user require a number of bytes equal
1885 * to the value. The default value is "*l" (a line).
1886 *
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001887 * This parameter with a variable type is converted in integer. This
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001888 * integer takes this values:
1889 * -1 : read a line
1890 * -2 : read all the stream
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001891 * >0 : amount of bytes.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001892 *
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001893 * The second parameter is optional. It contains a string that must be
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001894 * concatenated with the read data.
1895 */
1896__LJMP static int hlua_socket_receive(struct lua_State *L)
1897{
1898 int wanted = HLSR_READ_LINE;
1899 const char *pattern;
1900 int type;
1901 char *error;
1902 size_t len;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001903 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001904
1905 if (lua_gettop(L) < 1 || lua_gettop(L) > 3)
1906 WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments."));
1907
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001908 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001909
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001910 /* Check if we run on the same thread than the xreator thread.
1911 * We cannot access to the socket if the thread is different.
1912 */
1913 if (socket->tid != tid)
1914 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1915
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001916 /* check for pattern. */
1917 if (lua_gettop(L) >= 2) {
1918 type = lua_type(L, 2);
1919 if (type == LUA_TSTRING) {
1920 pattern = lua_tostring(L, 2);
1921 if (strcmp(pattern, "*a") == 0)
1922 wanted = HLSR_READ_ALL;
1923 else if (strcmp(pattern, "*l") == 0)
1924 wanted = HLSR_READ_LINE;
1925 else {
1926 wanted = strtoll(pattern, &error, 10);
1927 if (*error != '\0')
1928 WILL_LJMP(luaL_error(L, "Unsupported pattern."));
1929 }
1930 }
1931 else if (type == LUA_TNUMBER) {
1932 wanted = lua_tointeger(L, 2);
1933 if (wanted < 0)
1934 WILL_LJMP(luaL_error(L, "Unsupported size."));
1935 }
1936 }
1937
1938 /* Set pattern. */
1939 lua_pushinteger(L, wanted);
Tim Duesterhusc6e377e2018-01-04 19:32:13 +01001940
1941 /* Check if we would replace the top by itself. */
1942 if (lua_gettop(L) != 2)
1943 lua_replace(L, 2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001944
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001945 /* init buffer, and fill it with prefix. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001946 luaL_buffinit(L, &socket->b);
1947
1948 /* Check prefix. */
1949 if (lua_gettop(L) >= 3) {
1950 if (lua_type(L, 3) != LUA_TSTRING)
1951 WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix"));
1952 pattern = lua_tolstring(L, 3, &len);
1953 luaL_addlstring(&socket->b, pattern, len);
1954 }
1955
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001956 return __LJMP(hlua_socket_receive_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001957}
1958
1959/* Write the Lua input string in the output buffer.
Mark Lakes22154b42018-01-29 14:38:40 -08001960 * This function returns a yield if no space is available.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001961 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001962static int hlua_socket_write_yield(struct lua_State *L,int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001963{
1964 struct hlua_socket *socket;
1965 struct hlua *hlua = hlua_gethlua(L);
1966 struct appctx *appctx;
1967 size_t buf_len;
1968 const char *buf;
1969 int len;
1970 int send_len;
1971 int sent;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001972 struct xref *peer;
1973 struct stream_interface *si;
1974 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001975
1976 /* Check if this lua stack is schedulable. */
1977 if (!hlua || !hlua->task)
1978 WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in "
1979 "'frontend', 'backend' or 'task'"));
1980
1981 /* Get object */
1982 socket = MAY_LJMP(hlua_checksocket(L, 1));
1983 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001984 sent = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001985
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001986 /* Check if we run on the same thread than the xreator thread.
1987 * We cannot access to the socket if the thread is different.
1988 */
1989 if (socket->tid != tid)
1990 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1991
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001992 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001993 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001994 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001995 lua_pushinteger(L, -1);
1996 return 1;
1997 }
1998 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
1999 si = appctx->owner;
2000 s = si_strm(si);
2001
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002002 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002003 if (channel_output_closed(&s->req)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002004 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002005 lua_pushinteger(L, -1);
2006 return 1;
2007 }
2008
2009 /* Update the input buffer data. */
2010 buf += sent;
2011 send_len = buf_len - sent;
2012
2013 /* All the data are sent. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002014 if (sent >= buf_len) {
2015 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002016 return 1; /* Implicitly return the length sent. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002017 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002018
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01002019 /* Check if the buffer is avalaible because HAProxy doesn't allocate
2020 * the request buffer if its not required.
2021 */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002022 if (s->req.buf->size == 0) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002023 if (!channel_alloc_buffer(&s->req, &appctx->buffer_wait))
Christopher Faulet33834b12016-12-19 09:29:06 +01002024 goto hlua_socket_write_yield_return;
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01002025 }
2026
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002027 /* Check for avalaible space. */
Willy Tarreaueac52592018-06-15 13:59:36 +02002028 len = b_room(s->req.buf);
Christopher Faulet33834b12016-12-19 09:29:06 +01002029 if (len <= 0) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002030 goto hlua_socket_write_yield_return;
Christopher Faulet33834b12016-12-19 09:29:06 +01002031 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002032
2033 /* send data */
2034 if (len < send_len)
2035 send_len = len;
Thierry FOURNIER66b89192018-05-27 01:14:47 +02002036 len = ci_putblk(&s->req, buf, send_len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002037
2038 /* "Not enough space" (-1), "Buffer too little to contain
2039 * the data" (-2) are not expected because the available length
2040 * is tested.
2041 * Other unknown error are also not expected.
2042 */
2043 if (len <= 0) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01002044 if (len == -1)
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002045 s->req.flags |= CF_WAKE_WRITE;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002046
sada05ed3302018-05-11 11:48:18 -07002047 MAY_LJMP(hlua_socket_close_helper(L));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002048 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002049 lua_pushinteger(L, -1);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002050 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002051 return 1;
2052 }
2053
2054 /* update buffers. */
Thierry FOURNIER101b9762018-05-27 01:27:40 +02002055 appctx_wakeup(appctx);
Willy Tarreaude70fa12015-09-26 11:25:05 +02002056
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002057 s->req.rex = TICK_ETERNITY;
2058 s->res.wex = TICK_ETERNITY;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002059
2060 /* Update length sent. */
2061 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002062 lua_pushinteger(L, sent + len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002063
2064 /* All the data buffer is sent ? */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002065 if (sent + len >= buf_len) {
2066 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002067 return 1;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002068 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002069
2070hlua_socket_write_yield_return:
Thierry FOURNIERba42fcd2018-05-27 00:59:48 +02002071 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2072 xref_unlock(&socket->xref, peer);
2073 WILL_LJMP(luaL_error(L, "out of memory"));
2074 }
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002075 xref_unlock(&socket->xref, peer);
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +01002076 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_write_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002077 return 0;
2078}
2079
2080/* This function initiate the send of data. It just check the input
2081 * parameters and push an integer in the Lua stack that contain the
2082 * amount of data writed in the buffer. This is used by the function
2083 * "hlua_socket_write_yield" that can yield.
2084 *
2085 * The Lua function gets between 3 and 4 parameters. The first one is
2086 * the associated object. The second is a string buffer. The third is
2087 * a facultative integer that represents where is the buffer position
2088 * of the start of the data that can send. The first byte is the
2089 * position "1". The default value is "1". The fourth argument is a
2090 * facultative integer that represents where is the buffer position
2091 * of the end of the data that can send. The default is the last byte.
2092 */
2093static int hlua_socket_send(struct lua_State *L)
2094{
2095 int i;
2096 int j;
2097 const char *buf;
2098 size_t buf_len;
2099
2100 /* Check number of arguments. */
2101 if (lua_gettop(L) < 2 || lua_gettop(L) > 4)
2102 WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments"));
2103
2104 /* Get the string. */
2105 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
2106
2107 /* Get and check j. */
2108 if (lua_gettop(L) == 4) {
2109 j = MAY_LJMP(luaL_checkinteger(L, 4));
2110 if (j < 0)
2111 j = buf_len + j + 1;
2112 if (j > buf_len)
2113 j = buf_len + 1;
2114 lua_pop(L, 1);
2115 }
2116 else
2117 j = buf_len;
2118
2119 /* Get and check i. */
2120 if (lua_gettop(L) == 3) {
2121 i = MAY_LJMP(luaL_checkinteger(L, 3));
2122 if (i < 0)
2123 i = buf_len + i + 1;
2124 if (i > buf_len)
2125 i = buf_len + 1;
2126 lua_pop(L, 1);
2127 } else
2128 i = 1;
2129
2130 /* Check bth i and j. */
2131 if (i > j) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002132 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002133 return 1;
2134 }
2135 if (i == 0 && j == 0) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002136 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002137 return 1;
2138 }
2139 if (i == 0)
2140 i = 1;
2141 if (j == 0)
2142 j = 1;
2143
2144 /* Pop the string. */
2145 lua_pop(L, 1);
2146
2147 /* Update the buffer length. */
2148 buf += i - 1;
2149 buf_len = j - i + 1;
2150 lua_pushlstring(L, buf, buf_len);
2151
2152 /* This unsigned is used to remember the amount of sent data. */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002153 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002154
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002155 return MAY_LJMP(hlua_socket_write_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002156}
2157
Willy Tarreau22b0a682015-06-17 19:43:49 +02002158#define SOCKET_INFO_MAX_LEN sizeof("[0000:0000:0000:0000:0000:0000:0000:0000]:12345")
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002159__LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr)
2160{
2161 static char buffer[SOCKET_INFO_MAX_LEN];
2162 int ret;
2163 int len;
2164 char *p;
2165
2166 ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1);
2167 if (ret <= 0) {
2168 lua_pushnil(L);
2169 return 1;
2170 }
2171
2172 if (ret == AF_UNIX) {
2173 lua_pushstring(L, buffer+1);
2174 return 1;
2175 }
2176 else if (ret == AF_INET6) {
2177 buffer[0] = '[';
2178 len = strlen(buffer);
2179 buffer[len] = ']';
2180 len++;
2181 buffer[len] = ':';
2182 len++;
2183 p = buffer;
2184 }
2185 else if (ret == AF_INET) {
2186 p = buffer + 1;
2187 len = strlen(p);
2188 p[len] = ':';
2189 len++;
2190 }
2191 else {
2192 lua_pushnil(L);
2193 return 1;
2194 }
2195
2196 if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) {
2197 lua_pushnil(L);
2198 return 1;
2199 }
2200
2201 lua_pushstring(L, p);
2202 return 1;
2203}
2204
2205/* Returns information about the peer of the connection. */
2206__LJMP static int hlua_socket_getpeername(struct lua_State *L)
2207{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002208 struct hlua_socket *socket;
2209 struct connection *conn;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002210 struct xref *peer;
2211 struct appctx *appctx;
2212 struct stream_interface *si;
2213 struct stream *s;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002214 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002215
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002216 MAY_LJMP(check_args(L, 1, "getpeername"));
2217
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002218 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002219
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002220 /* Check if we run on the same thread than the xreator thread.
2221 * We cannot access to the socket if the thread is different.
2222 */
2223 if (socket->tid != tid)
2224 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2225
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002226 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002227 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002228 if (!peer) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002229 lua_pushnil(L);
2230 return 1;
2231 }
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002232 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2233 si = appctx->owner;
2234 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002235
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002236 conn = cs_conn(objt_cs(s->si[1].end));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002237 if (!conn) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002238 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002239 lua_pushnil(L);
2240 return 1;
2241 }
2242
Willy Tarreaua71f6422016-11-16 17:00:14 +01002243 conn_get_to_addr(conn);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002244 if (!(conn->flags & CO_FL_ADDR_TO_SET)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002245 xref_unlock(&socket->xref, peer);
Willy Tarreaua71f6422016-11-16 17:00:14 +01002246 lua_pushnil(L);
2247 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002248 }
2249
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002250 ret = MAY_LJMP(hlua_socket_info(L, &conn->addr.to));
2251 xref_unlock(&socket->xref, peer);
2252 return ret;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002253}
2254
2255/* Returns information about my connection side. */
2256static int hlua_socket_getsockname(struct lua_State *L)
2257{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002258 struct hlua_socket *socket;
2259 struct connection *conn;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002260 struct appctx *appctx;
2261 struct xref *peer;
2262 struct stream_interface *si;
2263 struct stream *s;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002264 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002265
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002266 MAY_LJMP(check_args(L, 1, "getsockname"));
2267
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002268 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002269
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002270 /* Check if we run on the same thread than the xreator thread.
2271 * We cannot access to the socket if the thread is different.
2272 */
2273 if (socket->tid != tid)
2274 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2275
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002276 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002277 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002278 if (!peer) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002279 lua_pushnil(L);
2280 return 1;
2281 }
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002282 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2283 si = appctx->owner;
2284 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002285
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002286 conn = cs_conn(objt_cs(s->si[1].end));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002287 if (!conn) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002288 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002289 lua_pushnil(L);
2290 return 1;
2291 }
2292
Willy Tarreaua71f6422016-11-16 17:00:14 +01002293 conn_get_from_addr(conn);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002294 if (!(conn->flags & CO_FL_ADDR_FROM_SET)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002295 xref_unlock(&socket->xref, peer);
Willy Tarreaua71f6422016-11-16 17:00:14 +01002296 lua_pushnil(L);
2297 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002298 }
2299
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002300 ret = hlua_socket_info(L, &conn->addr.from);
2301 xref_unlock(&socket->xref, peer);
2302 return ret;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002303}
2304
2305/* This struct define the applet. */
Willy Tarreau30576452015-04-13 13:50:30 +02002306static struct applet update_applet = {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002307 .obj_type = OBJ_TYPE_APPLET,
2308 .name = "<LUA_TCP>",
2309 .fct = hlua_socket_handler,
2310 .release = hlua_socket_release,
2311};
2312
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002313__LJMP static int hlua_socket_connect_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002314{
2315 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
2316 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002317 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002318 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002319 struct stream_interface *si;
2320 struct stream *s;
2321
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002322 /* Check if we run on the same thread than the xreator thread.
2323 * We cannot access to the socket if the thread is different.
2324 */
2325 if (socket->tid != tid)
2326 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2327
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002328 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002329 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002330 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002331 lua_pushnil(L);
2332 lua_pushstring(L, "Can't connect");
2333 return 2;
2334 }
2335 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2336 si = appctx->owner;
2337 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002338
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002339 /* Check if we run on the same thread than the xreator thread.
2340 * We cannot access to the socket if the thread is different.
2341 */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002342 if (socket->tid != tid) {
2343 xref_unlock(&socket->xref, peer);
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002344 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002345 }
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002346
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002347 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002348 if (!hlua || channel_output_closed(&s->req)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002349 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002350 lua_pushnil(L);
2351 lua_pushstring(L, "Can't connect");
2352 return 2;
2353 }
2354
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002355 appctx = objt_appctx(s->si[0].end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002356
2357 /* Check for connection established. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002358 if (appctx->ctx.hlua_cosocket.connected) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002359 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002360 lua_pushinteger(L, 1);
2361 return 1;
2362 }
2363
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002364 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2365 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002366 WILL_LJMP(luaL_error(L, "out of memory error"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002367 }
2368 xref_unlock(&socket->xref, peer);
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +01002369 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002370 return 0;
2371}
2372
2373/* This function fail or initite the connection. */
2374__LJMP static int hlua_socket_connect(struct lua_State *L)
2375{
2376 struct hlua_socket *socket;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002377 int port = -1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002378 const char *ip;
2379 struct connection *conn;
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002380 struct hlua *hlua;
2381 struct appctx *appctx;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002382 int low, high;
2383 struct sockaddr_storage *addr;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002384 struct xref *peer;
2385 struct stream_interface *si;
2386 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002387
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002388 if (lua_gettop(L) < 2)
2389 WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments"));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002390
2391 /* Get args. */
2392 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002393
2394 /* Check if we run on the same thread than the xreator thread.
2395 * We cannot access to the socket if the thread is different.
2396 */
2397 if (socket->tid != tid)
2398 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2399
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002400 ip = MAY_LJMP(luaL_checkstring(L, 2));
Tim Duesterhus6edab862018-01-06 19:04:45 +01002401 if (lua_gettop(L) >= 3) {
2402 luaL_Buffer b;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002403 port = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002404
Tim Duesterhus6edab862018-01-06 19:04:45 +01002405 /* Force the ip to end with a colon, to support IPv6 addresses
2406 * that are not enclosed within square brackets.
2407 */
2408 if (port > 0) {
2409 luaL_buffinit(L, &b);
2410 luaL_addstring(&b, ip);
2411 luaL_addchar(&b, ':');
2412 luaL_pushresult(&b);
2413 ip = lua_tolstring(L, lua_gettop(L), NULL);
2414 }
2415 }
2416
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002417 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002418 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002419 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002420 lua_pushnil(L);
2421 return 1;
2422 }
2423 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2424 si = appctx->owner;
2425 s = si_strm(si);
2426
2427 /* Initialise connection. */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002428 conn = cs_conn(si_alloc_cs(&s->si[1], NULL));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002429 if (!conn) {
2430 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002431 WILL_LJMP(luaL_error(L, "connect: internal error"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002432 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002433
Willy Tarreau3adac082015-09-26 17:51:09 +02002434 /* needed for the connection not to be closed */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002435 conn->target = s->target;
Willy Tarreau3adac082015-09-26 17:51:09 +02002436
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002437 /* Parse ip address. */
Willy Tarreau48ef4c92017-01-06 18:32:38 +01002438 addr = str2sa_range(ip, NULL, &low, &high, NULL, NULL, NULL, 0);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002439 if (!addr) {
2440 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002441 WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002442 }
2443 if (low != high) {
2444 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002445 WILL_LJMP(luaL_error(L, "connect: port ranges not supported : address '%s'", ip));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002446 }
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002447 memcpy(&conn->addr.to, addr, sizeof(struct sockaddr_storage));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002448
2449 /* Set port. */
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002450 if (low == 0) {
2451 if (conn->addr.to.ss_family == AF_INET) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002452 if (port == -1) {
2453 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002454 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002455 }
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002456 ((struct sockaddr_in *)&conn->addr.to)->sin_port = htons(port);
2457 } else if (conn->addr.to.ss_family == AF_INET6) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002458 if (port == -1) {
2459 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002460 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002461 }
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002462 ((struct sockaddr_in6 *)&conn->addr.to)->sin6_port = htons(port);
2463 }
2464 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002465
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002466 hlua = hlua_gethlua(L);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002467 appctx = objt_appctx(s->si[0].end);
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002468
2469 /* inform the stream that we want to be notified whenever the
2470 * connection completes.
2471 */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002472 si_applet_cant_get(&s->si[0]);
2473 si_applet_cant_put(&s->si[0]);
Thierry FOURNIER8c8fbbe2015-09-26 17:02:35 +02002474 appctx_wakeup(appctx);
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002475
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +02002476 hlua->flags |= HLUA_MUST_GC;
2477
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002478 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2479 xref_unlock(&socket->xref, peer);
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002480 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002481 }
2482 xref_unlock(&socket->xref, peer);
PiBa-NL706d5ee2018-05-05 23:51:42 +02002483
2484 task_wakeup(s->task, TASK_WOKEN_INIT);
2485 /* Return yield waiting for connection. */
2486
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +01002487 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002488
2489 return 0;
2490}
2491
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002492#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002493__LJMP static int hlua_socket_connect_ssl(struct lua_State *L)
2494{
2495 struct hlua_socket *socket;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002496 struct xref *peer;
2497 struct appctx *appctx;
2498 struct stream_interface *si;
2499 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002500
2501 MAY_LJMP(check_args(L, 3, "connect_ssl"));
2502 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002503
2504 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002505 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002506 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002507 lua_pushnil(L);
2508 return 1;
2509 }
2510 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2511 si = appctx->owner;
2512 s = si_strm(si);
2513
2514 s->target = &socket_ssl.obj_type;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002515 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002516 return MAY_LJMP(hlua_socket_connect(L));
2517}
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002518#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002519
2520__LJMP static int hlua_socket_setoption(struct lua_State *L)
2521{
2522 return 0;
2523}
2524
2525__LJMP static int hlua_socket_settimeout(struct lua_State *L)
2526{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002527 struct hlua_socket *socket;
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002528 int tmout;
Mark Lakes56cc1252018-03-27 09:48:06 +02002529 double dtmout;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002530 struct xref *peer;
2531 struct appctx *appctx;
2532 struct stream_interface *si;
2533 struct stream *s;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002534
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002535 MAY_LJMP(check_args(L, 2, "settimeout"));
2536
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002537 socket = MAY_LJMP(hlua_checksocket(L, 1));
Mark Lakes56cc1252018-03-27 09:48:06 +02002538
2539 /* round up for inputs that are fractions and convert to millis */
2540 dtmout = (0.5 + MAY_LJMP(luaL_checknumber(L, 2))) * 1000;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002541
Thierry Fournier17a921b2018-03-08 09:59:02 +01002542 /* Check for negative values */
Mark Lakes56cc1252018-03-27 09:48:06 +02002543 if (dtmout < 0)
Thierry Fournier17a921b2018-03-08 09:59:02 +01002544 WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values"));
2545
Mark Lakes56cc1252018-03-27 09:48:06 +02002546 if (dtmout > INT_MAX) /* overflow check */
2547 WILL_LJMP(luaL_error(L, "settimeout: cannot set values larger than %d", INT_MAX));
2548
2549 tmout = MS_TO_TICKS((int)dtmout);
2550
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002551 /* Check if we run on the same thread than the xreator thread.
2552 * We cannot access to the socket if the thread is different.
2553 */
2554 if (socket->tid != tid)
2555 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2556
Mark Lakes56cc1252018-03-27 09:48:06 +02002557 /* check for connection break. If some data were read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002558 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002559 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002560 hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts.");
2561 WILL_LJMP(lua_error(L));
2562 return 0;
2563 }
2564 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2565 si = appctx->owner;
2566 s = si_strm(si);
2567
2568 s->req.rto = tmout;
2569 s->req.wto = tmout;
2570 s->res.rto = tmout;
2571 s->res.wto = tmout;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002572 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002573
Thierry Fourniere9636f12018-03-08 09:54:32 +01002574 lua_pushinteger(L, 1);
Tim Duesterhus119a5f12018-01-06 19:16:25 +01002575 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002576}
2577
2578__LJMP static int hlua_socket_new(lua_State *L)
2579{
2580 struct hlua_socket *socket;
2581 struct appctx *appctx;
Willy Tarreau15b5e142015-04-04 14:38:25 +02002582 struct session *sess;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002583 struct stream *strm;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002584
2585 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002586 if (!lua_checkstack(L, 3)) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002587 hlua_pusherror(L, "socket: full stack");
2588 goto out_fail_conf;
2589 }
2590
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002591 /* Create the object: obj[0] = userdata. */
2592 lua_newtable(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002593 socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket)));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002594 lua_rawseti(L, -2, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002595 memset(socket, 0, sizeof(*socket));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002596 socket->tid = tid;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002597
Thierry FOURNIER4a6170c2015-03-09 17:07:10 +01002598 /* Check if the various memory pools are intialized. */
Willy Tarreaubafbe012017-11-24 17:34:44 +01002599 if (!pool_head_stream || !pool_head_buffer) {
Thierry FOURNIER4a6170c2015-03-09 17:07:10 +01002600 hlua_pusherror(L, "socket: uninitialized pools.");
2601 goto out_fail_conf;
2602 }
2603
Willy Tarreau87b09662015-04-03 00:22:06 +02002604 /* Pop a class stream metatable and affect it to the userdata. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002605 lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref);
2606 lua_setmetatable(L, -2);
2607
Willy Tarreaud420a972015-04-06 00:39:18 +02002608 /* Create the applet context */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01002609 appctx = appctx_new(&update_applet, tid_bit);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002610 if (!appctx) {
2611 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaufeb76402015-04-03 14:10:06 +02002612 goto out_fail_conf;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002613 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002614
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002615 appctx->ctx.hlua_cosocket.connected = 0;
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02002616 appctx->ctx.hlua_cosocket.die = 0;
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002617 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write);
2618 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read);
Willy Tarreaub2bf8332015-04-04 15:58:58 +02002619
Willy Tarreaud420a972015-04-06 00:39:18 +02002620 /* Now create a session, task and stream for this applet */
2621 sess = session_new(&socket_proxy, NULL, &appctx->obj_type);
2622 if (!sess) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002623 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002624 goto out_fail_sess;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002625 }
2626
Willy Tarreau87787ac2017-08-28 16:22:54 +02002627 strm = stream_new(sess, &appctx->obj_type);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002628 if (!strm) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002629 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002630 goto out_fail_stream;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002631 }
2632
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002633 /* Initialise cross reference between stream and Lua socket object. */
2634 xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002635
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002636 /* Configure "right" stream interface. this "si" is used to connect
2637 * and retrieve data from the server. The connection is initialized
2638 * with the "struct server".
2639 */
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002640 si_set_state(&strm->si[1], SI_ST_ASS);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002641
2642 /* Force destination server. */
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002643 strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET | SF_BE_ASSIGNED;
2644 strm->target = &socket_tcp.obj_type;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002645
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002646 return 1;
2647
Willy Tarreaud420a972015-04-06 00:39:18 +02002648 out_fail_stream:
Willy Tarreau11c36242015-04-04 15:54:03 +02002649 session_free(sess);
Willy Tarreaud420a972015-04-06 00:39:18 +02002650 out_fail_sess:
2651 appctx_free(appctx);
2652 out_fail_conf:
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002653 WILL_LJMP(lua_error(L));
2654 return 0;
2655}
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01002656
2657/*
2658 *
2659 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002660 * Class Channel
2661 *
2662 *
2663 */
2664
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002665/* The state between the channel data and the HTTP parser state can be
2666 * unconsistent, so reset the parser and call it again. Warning, this
2667 * action not revalidate the request and not send a 400 if the modified
2668 * resuest is not valid.
2669 *
Thierry FOURNIER6e01f382015-11-02 09:52:54 +01002670 * This function never fails. The direction is set using dir, which equals
2671 * either SMP_OPT_DIR_REQ or SMP_OPT_DIR_RES.
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002672 */
2673static void hlua_resynchonize_proto(struct stream *stream, int dir)
2674{
2675 /* Protocol HTTP. */
2676 if (stream->be->mode == PR_MODE_HTTP) {
2677
Thierry FOURNIER6e01f382015-11-02 09:52:54 +01002678 if (dir == SMP_OPT_DIR_REQ)
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002679 http_txn_reset_req(stream->txn);
Thierry FOURNIER6e01f382015-11-02 09:52:54 +01002680 else if (dir == SMP_OPT_DIR_RES)
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002681 http_txn_reset_res(stream->txn);
2682
2683 if (stream->txn->hdr_idx.v)
2684 hdr_idx_init(&stream->txn->hdr_idx);
2685
Thierry FOURNIER6e01f382015-11-02 09:52:54 +01002686 if (dir == SMP_OPT_DIR_REQ)
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002687 http_msg_analyzer(&stream->txn->req, &stream->txn->hdr_idx);
Thierry FOURNIER6e01f382015-11-02 09:52:54 +01002688 else if (dir == SMP_OPT_DIR_RES)
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002689 http_msg_analyzer(&stream->txn->rsp, &stream->txn->hdr_idx);
2690 }
2691}
2692
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002693/* This function is called before the Lua execution. It stores
2694 * the differents parsers state before executing some Lua code.
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002695 */
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002696static inline void consistency_set(struct stream *stream, int opt, struct hlua_consistency *c)
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002697{
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002698 c->mode = stream->be->mode;
2699 switch (c->mode) {
2700 case PR_MODE_HTTP:
2701 c->data.http.dir = opt & SMP_OPT_DIR;
2702 if (c->data.http.dir == SMP_OPT_DIR_REQ)
2703 c->data.http.state = stream->txn->req.msg_state;
2704 else
2705 c->data.http.state = stream->txn->rsp.msg_state;
2706 break;
2707 default:
2708 break;
2709 }
2710}
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002711
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002712/* This function is called after the Lua execution. it
2713 * returns true if the parser state is consistent, otherwise,
2714 * it return false.
2715 *
2716 * In HTTP mode, the parser state must be in the same state
2717 * or greater when we exit the function. Even if we do a
2718 * control yield. This prevent to break the HTTP message
2719 * from the Lua code.
2720 */
2721static inline int consistency_check(struct stream *stream, int opt, struct hlua_consistency *c)
2722{
2723 if (c->mode != stream->be->mode)
2724 return 0;
2725
2726 switch (c->mode) {
2727 case PR_MODE_HTTP:
2728 if (c->data.http.dir != (opt & SMP_OPT_DIR))
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002729 return 0;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002730 if (c->data.http.dir == SMP_OPT_DIR_REQ)
2731 return stream->txn->req.msg_state >= c->data.http.state;
2732 else
2733 return stream->txn->rsp.msg_state >= c->data.http.state;
2734 default:
2735 return 1;
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002736 }
2737 return 1;
2738}
2739
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002740/* Returns the struct hlua_channel join to the class channel in the
2741 * stack entry "ud" or throws an argument error.
2742 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002743__LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002744{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02002745 return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002746}
2747
Willy Tarreau47860ed2015-03-10 14:07:50 +01002748/* Pushes the channel onto the top of the stack. If the stask does not have a
2749 * free slots, the function fails and returns 0;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002750 */
Willy Tarreau2a71af42015-03-10 13:51:50 +01002751static int hlua_channel_new(lua_State *L, struct channel *channel)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002752{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002753 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002754 if (!lua_checkstack(L, 3))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002755 return 0;
2756
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002757 lua_newtable(L);
Willy Tarreau47860ed2015-03-10 14:07:50 +01002758 lua_pushlightuserdata(L, channel);
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002759 lua_rawseti(L, -2, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002760
2761 /* Pop a class sesison metatable and affect it to the userdata. */
2762 lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref);
2763 lua_setmetatable(L, -2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002764 return 1;
2765}
2766
2767/* Duplicate all the data present in the input channel and put it
2768 * in a string LUA variables. Returns -1 and push a nil value in
2769 * the stack if the channel is closed and all the data are consumed,
2770 * returns 0 if no data are available, otherwise it returns the length
2771 * of the builded string.
2772 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002773static inline int _hlua_channel_dup(struct channel *chn, lua_State *L)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002774{
2775 char *blk1;
2776 char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02002777 size_t len1;
2778 size_t len2;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002779 int ret;
2780 luaL_Buffer b;
2781
Willy Tarreau06d80a92017-10-19 14:32:15 +02002782 ret = ci_getblk_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002783 if (unlikely(ret == 0))
2784 return 0;
2785
2786 if (unlikely(ret < 0)) {
2787 lua_pushnil(L);
2788 return -1;
2789 }
2790
2791 luaL_buffinit(L, &b);
2792 luaL_addlstring(&b, blk1, len1);
2793 if (unlikely(ret == 2))
2794 luaL_addlstring(&b, blk2, len2);
2795 luaL_pushresult(&b);
2796
2797 if (unlikely(ret == 2))
2798 return len1 + len2;
2799 return len1;
2800}
2801
2802/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2803 * a yield. This function keep the data in the buffer.
2804 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002805__LJMP static int hlua_channel_dup_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002806{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002807 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002808
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002809 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2810
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002811 if (_hlua_channel_dup(chn, L) == 0)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002812 WILL_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
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002836 ret = _hlua_channel_dup(chn, L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002837 if (unlikely(ret == 0))
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002838 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002839
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002840 if (unlikely(ret == -1))
2841 return 1;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002842
Willy Tarreaua79021a2018-06-15 18:07:57 +02002843 b_sub(chn->buf, ret);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002844 hlua_resynchonize_proto(chn_strm(chn), !!(chn->flags & CF_ISRESP));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002845 return 1;
2846}
2847
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002848/* Check arguments for the fucntion "hlua_channel_get_yield". */
2849__LJMP static int hlua_channel_get(lua_State *L)
2850{
2851 MAY_LJMP(check_args(L, 1, "get"));
2852 MAY_LJMP(hlua_checkchannel(L, 1));
2853 return MAY_LJMP(hlua_channel_get_yield(L, 0, 0));
2854}
2855
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002856/* This functions consumes and returns one line. If the channel is closed,
2857 * and the last data does not contains a final '\n', the data are returned
2858 * without the final '\n'. When no more data are avalaible, it returns nil
2859 * value.
2860 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002861__LJMP static int hlua_channel_getline_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002862{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002863 char *blk1;
2864 char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02002865 size_t len1;
2866 size_t len2;
2867 size_t len;
Willy Tarreau47860ed2015-03-10 14:07:50 +01002868 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002869 int ret;
2870 luaL_Buffer b;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002871
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002872 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2873
Willy Tarreau06d80a92017-10-19 14:32:15 +02002874 ret = ci_getline_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002875 if (ret == 0)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002876 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_getline_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002877
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002878 if (ret == -1) {
2879 lua_pushnil(L);
2880 return 1;
2881 }
2882
2883 luaL_buffinit(L, &b);
2884 luaL_addlstring(&b, blk1, len1);
2885 len = len1;
2886 if (unlikely(ret == 2)) {
2887 luaL_addlstring(&b, blk2, len2);
2888 len += len2;
2889 }
2890 luaL_pushresult(&b);
Willy Tarreaua79021a2018-06-15 18:07:57 +02002891 buffer_replace2(chn->buf, ci_head(chn), ci_head(chn) + len, NULL, 0);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002892 hlua_resynchonize_proto(chn_strm(chn), !!(chn->flags & CF_ISRESP));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002893 return 1;
2894}
2895
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002896/* Check arguments for the fucntion "hlua_channel_getline_yield". */
2897__LJMP static int hlua_channel_getline(lua_State *L)
2898{
2899 MAY_LJMP(check_args(L, 1, "getline"));
2900 MAY_LJMP(hlua_checkchannel(L, 1));
2901 return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0));
2902}
2903
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002904/* This function takes a string as input, and append it at the
2905 * input side of channel. If the data is too big, but a space
2906 * is probably available after sending some data, the function
2907 * yield. If the data is bigger than the buffer, or if the
2908 * channel is closed, it returns -1. otherwise, it returns the
2909 * amount of data writed.
2910 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002911__LJMP static int hlua_channel_append_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002912{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002913 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002914 size_t len;
2915 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
2916 int l = MAY_LJMP(luaL_checkinteger(L, 3));
2917 int ret;
2918 int max;
2919
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002920 /* Check if the buffer is avalaible because HAProxy doesn't allocate
2921 * the request buffer if its not required.
2922 */
2923 if (chn->buf->size == 0) {
2924 si_applet_cant_put(chn_prod(chn));
2925 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
2926 }
2927
Willy Tarreaueac52592018-06-15 13:59:36 +02002928 max = channel_recv_limit(chn) - b_data(chn->buf);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002929 if (max > len - l)
2930 max = len - l;
2931
Willy Tarreau06d80a92017-10-19 14:32:15 +02002932 ret = ci_putblk(chn, str + l, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002933 if (ret == -2 || ret == -3) {
2934 lua_pushinteger(L, -1);
2935 return 1;
2936 }
Willy Tarreaubc18da12015-03-13 14:00:47 +01002937 if (ret == -1) {
2938 chn->flags |= CF_WAKE_WRITE;
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002939 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Willy Tarreaubc18da12015-03-13 14:00:47 +01002940 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002941 l += ret;
2942 lua_pop(L, 1);
2943 lua_pushinteger(L, l);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002944 hlua_resynchonize_proto(chn_strm(chn), !!(chn->flags & CF_ISRESP));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002945
Willy Tarreaueac52592018-06-15 13:59:36 +02002946 max = channel_recv_limit(chn) - b_data(chn->buf);
Willy Tarreaua79021a2018-06-15 18:07:57 +02002947 if (max == 0 && co_data(chn) == 0) {
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002948 /* There are no space avalaible, and the output buffer is empty.
2949 * in this case, we cannot add more data, so we cannot yield,
2950 * we return the amount of copyied data.
2951 */
2952 return 1;
2953 }
2954 if (l < len)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002955 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002956 return 1;
2957}
2958
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002959/* just a wrapper of "hlua_channel_append_yield". It returns the length
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002960 * of the writed string, or -1 if the channel is closed or if the
2961 * buffer size is too little for the data.
2962 */
2963__LJMP static int hlua_channel_append(lua_State *L)
2964{
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002965 size_t len;
2966
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002967 MAY_LJMP(check_args(L, 2, "append"));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002968 MAY_LJMP(hlua_checkchannel(L, 1));
2969 MAY_LJMP(luaL_checklstring(L, 2, &len));
2970 MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002971 lua_pushinteger(L, 0);
2972
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002973 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002974}
2975
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002976/* just a wrapper of "hlua_channel_append_yield". This wrapper starts
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002977 * his process by cleaning the buffer. The result is a replacement
2978 * of the current data. It returns the length of the writed string,
2979 * or -1 if the channel is closed or if the buffer size is too
2980 * little for the data.
2981 */
2982__LJMP static int hlua_channel_set(lua_State *L)
2983{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002984 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002985
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002986 MAY_LJMP(check_args(L, 2, "set"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002987 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002988 lua_pushinteger(L, 0);
2989
Willy Tarreaua79021a2018-06-15 18:07:57 +02002990 b_set_data(chn->buf, co_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002991
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002992 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002993}
2994
2995/* Append data in the output side of the buffer. This data is immediatly
2996 * sent. The fcuntion returns the ammount of data writed. If the buffer
2997 * cannot contains the data, the function yield. The function returns -1
2998 * if the channel is closed.
2999 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003000__LJMP static int hlua_channel_send_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003001{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003002 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003003 size_t len;
3004 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
3005 int l = MAY_LJMP(luaL_checkinteger(L, 3));
3006 int max;
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003007 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003008
Willy Tarreau47860ed2015-03-10 14:07:50 +01003009 if (unlikely(channel_output_closed(chn))) {
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003010 lua_pushinteger(L, -1);
3011 return 1;
3012 }
3013
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01003014 /* Check if the buffer is avalaible because HAProxy doesn't allocate
3015 * the request buffer if its not required.
3016 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003017 if (chn->buf->size == 0) {
Christopher Fauleta73e59b2016-12-09 17:30:18 +01003018 si_applet_cant_put(chn_prod(chn));
3019 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01003020 }
3021
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003022 /* the writed data will be immediatly sent, so we can check
3023 * the avalaible space without taking in account the reserve.
3024 * The reserve is guaranted for the processing of incoming
3025 * data, because the buffer will be flushed.
3026 */
Willy Tarreaueac52592018-06-15 13:59:36 +02003027 max = b_room(chn->buf);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003028
3029 /* If there are no space avalaible, and the output buffer is empty.
3030 * in this case, we cannot add more data, so we cannot yield,
3031 * we return the amount of copyied data.
3032 */
Willy Tarreaua79021a2018-06-15 18:07:57 +02003033 if (max == 0 && co_data(chn) == 0)
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003034 return 1;
3035
3036 /* Adjust the real required length. */
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003037 if (max > len - l)
3038 max = len - l;
3039
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003040 /* The buffer avalaible size may be not contiguous. This test
3041 * detects a non contiguous buffer and realign it.
3042 */
Willy Tarreau3f679992018-06-15 15:06:42 +02003043 if (ci_space_for_replace(chn) < max)
Willy Tarreaufd8d42f2018-07-12 10:57:15 +02003044 channel_slow_realign(chn, trash.str);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003045
3046 /* Copy input data in the buffer. */
Willy Tarreaua79021a2018-06-15 18:07:57 +02003047 max = buffer_replace2(chn->buf, ci_head(chn), ci_head(chn), str + l, max);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003048
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003049 /* buffer replace considers that the input part is filled.
3050 * so, I must forward these new data in the output part.
3051 */
Willy Tarreaubcbd3932018-06-06 07:13:22 +02003052 c_adv(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003053
3054 l += max;
3055 lua_pop(L, 1);
3056 lua_pushinteger(L, l);
3057
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003058 /* If there are no space avalaible, and the output buffer is empty.
3059 * in this case, we cannot add more data, so we cannot yield,
3060 * we return the amount of copyied data.
3061 */
Willy Tarreaueac52592018-06-15 13:59:36 +02003062 max = b_room(chn->buf);
Willy Tarreaua79021a2018-06-15 18:07:57 +02003063 if (max == 0 && co_data(chn) == 0)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003064 return 1;
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003065
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003066 if (l < len) {
3067 /* If we are waiting for space in the response buffer, we
3068 * must set the flag WAKERESWR. This flag required the task
3069 * wake up if any activity is detected on the response buffer.
3070 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003071 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003072 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01003073 else
3074 HLUA_SET_WAKEREQWR(hlua);
3075 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003076 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003077
3078 return 1;
3079}
3080
3081/* Just a wraper of "_hlua_channel_send". This wrapper permits
3082 * yield the LUA process, and resume it without checking the
3083 * input arguments.
3084 */
3085__LJMP static int hlua_channel_send(lua_State *L)
3086{
3087 MAY_LJMP(check_args(L, 2, "send"));
3088 lua_pushinteger(L, 0);
3089
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003090 return MAY_LJMP(hlua_channel_send_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003091}
3092
3093/* This function forward and amount of butes. The data pass from
3094 * the input side of the buffer to the output side, and can be
3095 * forwarded. This function never fails.
3096 *
3097 * The Lua function takes an amount of bytes to be forwarded in
3098 * imput. It returns the number of bytes forwarded.
3099 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003100__LJMP static int hlua_channel_forward_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003101{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003102 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003103 int len;
3104 int l;
3105 int max;
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003106 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003107
3108 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3109 len = MAY_LJMP(luaL_checkinteger(L, 2));
3110 l = MAY_LJMP(luaL_checkinteger(L, -1));
3111
3112 max = len - l;
Willy Tarreaua79021a2018-06-15 18:07:57 +02003113 if (max > ci_data(chn))
3114 max = ci_data(chn);
Willy Tarreau47860ed2015-03-10 14:07:50 +01003115 channel_forward(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003116 l += max;
3117
3118 lua_pop(L, 1);
3119 lua_pushinteger(L, l);
3120
3121 /* Check if it miss bytes to forward. */
3122 if (l < len) {
3123 /* The the input channel or the output channel are closed, we
3124 * must return the amount of data forwarded.
3125 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003126 if (channel_input_closed(chn) || channel_output_closed(chn))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003127 return 1;
3128
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003129 /* If we are waiting for space data in the response buffer, we
3130 * must set the flag WAKERESWR. This flag required the task
3131 * wake up if any activity is detected on the response buffer.
3132 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003133 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003134 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01003135 else
3136 HLUA_SET_WAKEREQWR(hlua);
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003137
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003138 /* Otherwise, we can yield waiting for new data in the inpout side. */
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +01003139 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_forward_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003140 }
3141
3142 return 1;
3143}
3144
3145/* Just check the input and prepare the stack for the previous
3146 * function "hlua_channel_forward_yield"
3147 */
3148__LJMP static int hlua_channel_forward(lua_State *L)
3149{
3150 MAY_LJMP(check_args(L, 2, "forward"));
3151 MAY_LJMP(hlua_checkchannel(L, 1));
3152 MAY_LJMP(luaL_checkinteger(L, 2));
3153
3154 lua_pushinteger(L, 0);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003155 return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003156}
3157
3158/* Just returns the number of bytes available in the input
3159 * side of the buffer. This function never fails.
3160 */
3161__LJMP static int hlua_channel_get_in_len(lua_State *L)
3162{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003163 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003164
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003165 MAY_LJMP(check_args(L, 1, "get_in_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003166 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Willy Tarreaua79021a2018-06-15 18:07:57 +02003167 lua_pushinteger(L, ci_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003168 return 1;
3169}
3170
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003171/* Returns true if the channel is full. */
3172__LJMP static int hlua_channel_is_full(lua_State *L)
3173{
3174 struct channel *chn;
3175 int rem;
3176
3177 MAY_LJMP(check_args(L, 1, "is_full"));
3178 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3179
Willy Tarreaueac52592018-06-15 13:59:36 +02003180 rem = b_room(chn->buf);
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003181 rem -= global.tune.maxrewrite; /* Rewrite reserved size */
3182
3183 lua_pushboolean(L, rem <= 0);
3184 return 1;
3185}
3186
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003187/* Just returns the number of bytes available in the output
3188 * side of the buffer. This function never fails.
3189 */
3190__LJMP static int hlua_channel_get_out_len(lua_State *L)
3191{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003192 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003193
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003194 MAY_LJMP(check_args(L, 1, "get_out_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003195 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Willy Tarreaua79021a2018-06-15 18:07:57 +02003196 lua_pushinteger(L, co_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003197 return 1;
3198}
3199
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003200/*
3201 *
3202 *
3203 * Class Fetches
3204 *
3205 *
3206 */
3207
3208/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003209 * a class stream, otherwise it throws an error.
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003210 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003211__LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003212{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003213 return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003214}
3215
3216/* This function creates and push in the stack a fetch object according
3217 * with a current TXN.
3218 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003219static int hlua_fetches_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003220{
Willy Tarreau7073c472015-04-06 11:15:40 +02003221 struct hlua_smp *hsmp;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003222
3223 /* Check stack size. */
3224 if (!lua_checkstack(L, 3))
3225 return 0;
3226
3227 /* Create the object: obj[0] = userdata.
3228 * Note that the base of the Fetches object is the
3229 * transaction object.
3230 */
3231 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003232 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003233 lua_rawseti(L, -2, 0);
3234
Willy Tarreau7073c472015-04-06 11:15:40 +02003235 hsmp->s = txn->s;
3236 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003237 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003238 hsmp->flags = flags;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003239
3240 /* Pop a class sesison metatable and affect it to the userdata. */
3241 lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref);
3242 lua_setmetatable(L, -2);
3243
3244 return 1;
3245}
3246
3247/* This function is an LUA binding. It is called with each sample-fetch.
3248 * It uses closure argument to store the associated sample-fetch. It
3249 * returns only one argument or throws an error. An error is thrown
3250 * only if an error is encountered during the argument parsing. If
3251 * the "sample-fetch" function fails, nil is returned.
3252 */
3253__LJMP static int hlua_run_sample_fetch(lua_State *L)
3254{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003255 struct hlua_smp *hsmp;
Willy Tarreau2ec22742015-03-10 14:27:20 +01003256 struct sample_fetch *f;
Frédéric Lécaillef874a832018-06-15 13:56:04 +02003257 struct arg args[ARGM_NBARGS + 1] = {{0}};
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003258 int i;
3259 struct sample smp;
3260
3261 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003262 f = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003263
3264 /* Get traditionnal arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003265 hsmp = MAY_LJMP(hlua_checkfetches(L, 1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003266
Thierry FOURNIERca988662015-12-20 18:43:03 +01003267 /* Check execution authorization. */
3268 if (f->use & SMP_USE_HTTP_ANY &&
3269 !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) {
3270 lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which "
3271 "is not available in Lua services", f->kw);
3272 WILL_LJMP(lua_error(L));
3273 }
3274
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003275 /* Get extra arguments. */
3276 for (i = 0; i < lua_gettop(L) - 1; i++) {
3277 if (i >= ARGM_NBARGS)
3278 break;
3279 hlua_lua2arg(L, i + 2, &args[i]);
3280 }
3281 args[i].type = ARGT_STOP;
David Carlierabdb00f2016-04-27 16:14:50 +01003282 args[i].data.str.str = NULL;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003283
3284 /* Check arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003285 MAY_LJMP(hlua_lua2arg_check(L, 2, args, f->arg_mask, hsmp->p));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003286
3287 /* Run the special args checker. */
Willy Tarreau2ec22742015-03-10 14:27:20 +01003288 if (f->val_args && !f->val_args(args, NULL)) {
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003289 lua_pushfstring(L, "error in arguments");
3290 WILL_LJMP(lua_error(L));
3291 }
3292
3293 /* Initialise the sample. */
3294 memset(&smp, 0, sizeof(smp));
3295
3296 /* Run the sample fetch process. */
Willy Tarreau1777ea62016-03-10 16:15:46 +01003297 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003298 if (!f->process(args, &smp, f->kw, f->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003299 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003300 lua_pushstring(L, "");
3301 else
3302 lua_pushnil(L);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003303 return 1;
3304 }
3305
3306 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003307 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003308 hlua_smp2lua_str(L, &smp);
3309 else
3310 hlua_smp2lua(L, &smp);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003311 return 1;
3312}
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003313
3314/*
3315 *
3316 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003317 * Class Converters
3318 *
3319 *
3320 */
3321
3322/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003323 * a class stream, otherwise it throws an error.
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003324 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003325__LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003326{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003327 return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003328}
3329
3330/* This function creates and push in the stack a Converters object
3331 * according with a current TXN.
3332 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003333static int hlua_converters_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003334{
Willy Tarreau7073c472015-04-06 11:15:40 +02003335 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003336
3337 /* Check stack size. */
3338 if (!lua_checkstack(L, 3))
3339 return 0;
3340
3341 /* Create the object: obj[0] = userdata.
3342 * Note that the base of the Converters object is the
3343 * same than the TXN object.
3344 */
3345 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003346 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003347 lua_rawseti(L, -2, 0);
3348
Willy Tarreau7073c472015-04-06 11:15:40 +02003349 hsmp->s = txn->s;
3350 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003351 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003352 hsmp->flags = flags;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003353
Willy Tarreau87b09662015-04-03 00:22:06 +02003354 /* Pop a class stream metatable and affect it to the table. */
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003355 lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref);
3356 lua_setmetatable(L, -2);
3357
3358 return 1;
3359}
3360
3361/* This function is an LUA binding. It is called with each converter.
3362 * It uses closure argument to store the associated converter. It
3363 * returns only one argument or throws an error. An error is thrown
3364 * only if an error is encountered during the argument parsing. If
3365 * the converter function function fails, nil is returned.
3366 */
3367__LJMP static int hlua_run_sample_conv(lua_State *L)
3368{
Willy Tarreauda5f1082015-04-06 11:17:13 +02003369 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003370 struct sample_conv *conv;
Frédéric Lécaillef874a832018-06-15 13:56:04 +02003371 struct arg args[ARGM_NBARGS + 1] = {{0}};
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003372 int i;
3373 struct sample smp;
3374
3375 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003376 conv = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003377
3378 /* Get traditionnal arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003379 hsmp = MAY_LJMP(hlua_checkconverters(L, 1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003380
3381 /* Get extra arguments. */
3382 for (i = 0; i < lua_gettop(L) - 2; i++) {
3383 if (i >= ARGM_NBARGS)
3384 break;
3385 hlua_lua2arg(L, i + 3, &args[i]);
3386 }
3387 args[i].type = ARGT_STOP;
David Carlierabdb00f2016-04-27 16:14:50 +01003388 args[i].data.str.str = NULL;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003389
3390 /* Check arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003391 MAY_LJMP(hlua_lua2arg_check(L, 3, args, conv->arg_mask, hsmp->p));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003392
3393 /* Run the special args checker. */
3394 if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) {
3395 hlua_pusherror(L, "error in arguments");
3396 WILL_LJMP(lua_error(L));
3397 }
3398
3399 /* Initialise the sample. */
3400 if (!hlua_lua2smp(L, 2, &smp)) {
3401 hlua_pusherror(L, "error in the input argument");
3402 WILL_LJMP(lua_error(L));
3403 }
3404
Willy Tarreau1777ea62016-03-10 16:15:46 +01003405 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
3406
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003407 /* Apply expected cast. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003408 if (!sample_casts[smp.data.type][conv->in_type]) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003409 hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'",
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003410 smp_to_type[smp.data.type], smp_to_type[conv->in_type]);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003411 WILL_LJMP(lua_error(L));
3412 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003413 if (sample_casts[smp.data.type][conv->in_type] != c_none &&
3414 !sample_casts[smp.data.type][conv->in_type](&smp)) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003415 hlua_pusherror(L, "error during the input argument casting");
3416 WILL_LJMP(lua_error(L));
3417 }
3418
3419 /* Run the sample conversion process. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003420 if (!conv->process(args, &smp, conv->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003421 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003422 lua_pushstring(L, "");
3423 else
Willy Tarreaua678b432015-08-28 10:14:59 +02003424 lua_pushnil(L);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003425 return 1;
3426 }
3427
3428 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003429 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003430 hlua_smp2lua_str(L, &smp);
3431 else
3432 hlua_smp2lua(L, &smp);
Willy Tarreaua678b432015-08-28 10:14:59 +02003433 return 1;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003434}
3435
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003436/*
3437 *
3438 *
3439 * Class AppletTCP
3440 *
3441 *
3442 */
3443
3444/* Returns a struct hlua_txn if the stack entry "ud" is
3445 * a class stream, otherwise it throws an error.
3446 */
3447__LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud)
3448{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003449 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003450}
3451
3452/* This function creates and push in the stack an Applet object
3453 * according with a current TXN.
3454 */
3455static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx)
3456{
3457 struct hlua_appctx *appctx;
3458 struct stream_interface *si = ctx->owner;
3459 struct stream *s = si_strm(si);
3460 struct proxy *p = s->be;
3461
3462 /* Check stack size. */
3463 if (!lua_checkstack(L, 3))
3464 return 0;
3465
3466 /* Create the object: obj[0] = userdata.
3467 * Note that the base of the Converters object is the
3468 * same than the TXN object.
3469 */
3470 lua_newtable(L);
3471 appctx = lua_newuserdata(L, sizeof(*appctx));
3472 lua_rawseti(L, -2, 0);
3473 appctx->appctx = ctx;
3474 appctx->htxn.s = s;
3475 appctx->htxn.p = p;
3476
3477 /* Create the "f" field that contains a list of fetches. */
3478 lua_pushstring(L, "f");
3479 if (!hlua_fetches_new(L, &appctx->htxn, 0))
3480 return 0;
3481 lua_settable(L, -3);
3482
3483 /* Create the "sf" field that contains a list of stringsafe fetches. */
3484 lua_pushstring(L, "sf");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003485 if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003486 return 0;
3487 lua_settable(L, -3);
3488
3489 /* Create the "c" field that contains a list of converters. */
3490 lua_pushstring(L, "c");
3491 if (!hlua_converters_new(L, &appctx->htxn, 0))
3492 return 0;
3493 lua_settable(L, -3);
3494
3495 /* Create the "sc" field that contains a list of stringsafe converters. */
3496 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003497 if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003498 return 0;
3499 lua_settable(L, -3);
3500
3501 /* Pop a class stream metatable and affect it to the table. */
3502 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref);
3503 lua_setmetatable(L, -2);
3504
3505 return 1;
3506}
3507
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003508__LJMP static int hlua_applet_tcp_set_var(lua_State *L)
3509{
3510 struct hlua_appctx *appctx;
3511 struct stream *s;
3512 const char *name;
3513 size_t len;
3514 struct sample smp;
3515
3516 MAY_LJMP(check_args(L, 3, "set_var"));
3517
3518 /* It is useles to retrieve the stream, but this function
3519 * runs only in a stream context.
3520 */
3521 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3522 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3523 s = appctx->htxn.s;
3524
3525 /* Converts the third argument in a sample. */
3526 hlua_lua2smp(L, 3, &smp);
3527
3528 /* Store the sample in a variable. */
3529 smp_set_owner(&smp, s->be, s->sess, s, 0);
3530 vars_set_by_name(name, len, &smp);
3531 return 0;
3532}
3533
3534__LJMP static int hlua_applet_tcp_unset_var(lua_State *L)
3535{
3536 struct hlua_appctx *appctx;
3537 struct stream *s;
3538 const char *name;
3539 size_t len;
3540 struct sample smp;
3541
3542 MAY_LJMP(check_args(L, 2, "unset_var"));
3543
3544 /* It is useles to retrieve the stream, but this function
3545 * runs only in a stream context.
3546 */
3547 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3548 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3549 s = appctx->htxn.s;
3550
3551 /* Unset the variable. */
3552 smp_set_owner(&smp, s->be, s->sess, s, 0);
3553 vars_unset_by_name(name, len, &smp);
3554 return 0;
3555}
3556
3557__LJMP static int hlua_applet_tcp_get_var(lua_State *L)
3558{
3559 struct hlua_appctx *appctx;
3560 struct stream *s;
3561 const char *name;
3562 size_t len;
3563 struct sample smp;
3564
3565 MAY_LJMP(check_args(L, 2, "get_var"));
3566
3567 /* It is useles to retrieve the stream, but this function
3568 * runs only in a stream context.
3569 */
3570 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3571 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3572 s = appctx->htxn.s;
3573
3574 smp_set_owner(&smp, s->be, s->sess, s, 0);
3575 if (!vars_get_by_name(name, len, &smp)) {
3576 lua_pushnil(L);
3577 return 1;
3578 }
3579
3580 return hlua_smp2lua(L, &smp);
3581}
3582
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003583__LJMP static int hlua_applet_tcp_set_priv(lua_State *L)
3584{
3585 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3586 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003587 struct hlua *hlua;
3588
3589 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003590 if (!s->hlua)
3591 return 0;
3592 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003593
3594 MAY_LJMP(check_args(L, 2, "set_priv"));
3595
3596 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02003597 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003598
3599 /* Get and store new value. */
3600 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
3601 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
3602
3603 return 0;
3604}
3605
3606__LJMP static int hlua_applet_tcp_get_priv(lua_State *L)
3607{
3608 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3609 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003610 struct hlua *hlua;
3611
3612 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003613 if (!s->hlua) {
3614 lua_pushnil(L);
3615 return 1;
3616 }
3617 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003618
3619 /* Push configuration index in the stack. */
3620 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
3621
3622 return 1;
3623}
3624
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003625/* If expected data not yet available, it returns a yield. This function
3626 * consumes the data in the buffer. It returns a string containing the
3627 * data. This string can be empty.
3628 */
3629__LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx)
3630{
3631 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3632 struct stream_interface *si = appctx->appctx->owner;
3633 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02003634 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003635 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02003636 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003637 size_t len2;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003638
3639 /* Read the maximum amount of data avalaible. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003640 ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003641
3642 /* Data not yet avalaible. return yield. */
3643 if (ret == 0) {
3644 si_applet_cant_get(si);
3645 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_getline_yield, TICK_ETERNITY, 0));
3646 }
3647
3648 /* End of data: commit the total strings and return. */
3649 if (ret < 0) {
3650 luaL_pushresult(&appctx->b);
3651 return 1;
3652 }
3653
3654 /* Ensure that the block 2 length is usable. */
3655 if (ret == 1)
3656 len2 = 0;
3657
3658 /* dont check the max length read and dont check. */
3659 luaL_addlstring(&appctx->b, blk1, len1);
3660 luaL_addlstring(&appctx->b, blk2, len2);
3661
3662 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003663 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003664 luaL_pushresult(&appctx->b);
3665 return 1;
3666}
3667
3668/* Check arguments for the fucntion "hlua_channel_get_yield". */
3669__LJMP static int hlua_applet_tcp_getline(lua_State *L)
3670{
3671 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3672
3673 /* Initialise the string catenation. */
3674 luaL_buffinit(L, &appctx->b);
3675
3676 return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0));
3677}
3678
3679/* If expected data not yet available, it returns a yield. This function
3680 * consumes the data in the buffer. It returns a string containing the
3681 * data. This string can be empty.
3682 */
3683__LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx)
3684{
3685 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3686 struct stream_interface *si = appctx->appctx->owner;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003687 size_t len = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003688 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02003689 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003690 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02003691 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003692 size_t len2;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003693
3694 /* Read the maximum amount of data avalaible. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003695 ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003696
3697 /* Data not yet avalaible. return yield. */
3698 if (ret == 0) {
3699 si_applet_cant_get(si);
3700 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
3701 }
3702
3703 /* End of data: commit the total strings and return. */
3704 if (ret < 0) {
3705 luaL_pushresult(&appctx->b);
3706 return 1;
3707 }
3708
3709 /* Ensure that the block 2 length is usable. */
3710 if (ret == 1)
3711 len2 = 0;
3712
3713 if (len == -1) {
3714
3715 /* If len == -1, catenate all the data avalaile and
3716 * yield because we want to get all the data until
3717 * the end of data stream.
3718 */
3719 luaL_addlstring(&appctx->b, blk1, len1);
3720 luaL_addlstring(&appctx->b, blk2, len2);
Willy Tarreau06d80a92017-10-19 14:32:15 +02003721 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003722 si_applet_cant_get(si);
3723 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
3724
3725 } else {
3726
3727 /* Copy the fisrt block caping to the length required. */
3728 if (len1 > len)
3729 len1 = len;
3730 luaL_addlstring(&appctx->b, blk1, len1);
3731 len -= len1;
3732
3733 /* Copy the second block. */
3734 if (len2 > len)
3735 len2 = len;
3736 luaL_addlstring(&appctx->b, blk2, len2);
3737 len -= len2;
3738
3739 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003740 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003741
3742 /* If we are no other data avalaible, yield waiting for new data. */
3743 if (len > 0) {
3744 lua_pushinteger(L, len);
3745 lua_replace(L, 2);
3746 si_applet_cant_get(si);
3747 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
3748 }
3749
3750 /* return the result. */
3751 luaL_pushresult(&appctx->b);
3752 return 1;
3753 }
3754
3755 /* we never executes this */
3756 hlua_pusherror(L, "Lua: internal error");
3757 WILL_LJMP(lua_error(L));
3758 return 0;
3759}
3760
3761/* Check arguments for the fucntion "hlua_channel_get_yield". */
3762__LJMP static int hlua_applet_tcp_recv(lua_State *L)
3763{
3764 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3765 int len = -1;
3766
3767 if (lua_gettop(L) > 2)
3768 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
3769 if (lua_gettop(L) >= 2) {
3770 len = MAY_LJMP(luaL_checkinteger(L, 2));
3771 lua_pop(L, 1);
3772 }
3773
3774 /* Confirm or set the required length */
3775 lua_pushinteger(L, len);
3776
3777 /* Initialise the string catenation. */
3778 luaL_buffinit(L, &appctx->b);
3779
3780 return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0));
3781}
3782
3783/* Append data in the output side of the buffer. This data is immediatly
3784 * sent. The fcuntion returns the ammount of data writed. If the buffer
3785 * cannot contains the data, the function yield. The function returns -1
3786 * if the channel is closed.
3787 */
3788__LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx)
3789{
3790 size_t len;
3791 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3792 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
3793 int l = MAY_LJMP(luaL_checkinteger(L, 3));
3794 struct stream_interface *si = appctx->appctx->owner;
3795 struct channel *chn = si_ic(si);
3796 int max;
3797
3798 /* Get the max amount of data which can write as input in the channel. */
3799 max = channel_recv_max(chn);
3800 if (max > (len - l))
3801 max = len - l;
3802
3803 /* Copy data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003804 ci_putblk(chn, str + l, max);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003805
3806 /* update counters. */
3807 l += max;
3808 lua_pop(L, 1);
3809 lua_pushinteger(L, l);
3810
3811 /* If some data is not send, declares the situation to the
3812 * applet, and returns a yield.
3813 */
3814 if (l < len) {
3815 si_applet_cant_put(si);
3816 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_send_yield, TICK_ETERNITY, 0));
3817 }
3818
3819 return 1;
3820}
3821
3822/* Just a wraper of "hlua_applet_tcp_send_yield". This wrapper permits
3823 * yield the LUA process, and resume it without checking the
3824 * input arguments.
3825 */
3826__LJMP static int hlua_applet_tcp_send(lua_State *L)
3827{
3828 MAY_LJMP(check_args(L, 2, "send"));
3829 lua_pushinteger(L, 0);
3830
3831 return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0));
3832}
3833
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003834/*
3835 *
3836 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003837 * Class AppletHTTP
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003838 *
3839 *
3840 */
3841
3842/* Returns a struct hlua_txn if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003843 * a class stream, otherwise it throws an error.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003844 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003845__LJMP static struct hlua_appctx *hlua_checkapplet_http(lua_State *L, int ud)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003846{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003847 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003848}
3849
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003850/* This function creates and push in the stack an Applet object
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003851 * according with a current TXN.
3852 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003853static int hlua_applet_http_new(lua_State *L, struct appctx *ctx)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003854{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003855 struct hlua_appctx *appctx;
Thierry FOURNIER841475e2015-12-11 17:10:09 +01003856 struct hlua_txn htxn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003857 struct stream_interface *si = ctx->owner;
3858 struct stream *s = si_strm(si);
3859 struct proxy *px = s->be;
3860 struct http_txn *txn = s->txn;
3861 const char *path;
3862 const char *end;
3863 const char *p;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003864
3865 /* Check stack size. */
3866 if (!lua_checkstack(L, 3))
3867 return 0;
3868
3869 /* Create the object: obj[0] = userdata.
3870 * Note that the base of the Converters object is the
3871 * same than the TXN object.
3872 */
3873 lua_newtable(L);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003874 appctx = lua_newuserdata(L, sizeof(*appctx));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003875 lua_rawseti(L, -2, 0);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003876 appctx->appctx = ctx;
3877 appctx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */
Robin H. Johnson52f5db22017-01-01 13:10:52 -08003878 appctx->appctx->ctx.hlua_apphttp.reason = NULL; /* Use default reason based on status */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003879 appctx->htxn.s = s;
3880 appctx->htxn.p = px;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003881
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003882 /* Create the "f" field that contains a list of fetches. */
3883 lua_pushstring(L, "f");
3884 if (!hlua_fetches_new(L, &appctx->htxn, 0))
3885 return 0;
3886 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003887
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003888 /* Create the "sf" field that contains a list of stringsafe fetches. */
3889 lua_pushstring(L, "sf");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003890 if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003891 return 0;
3892 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003893
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003894 /* Create the "c" field that contains a list of converters. */
3895 lua_pushstring(L, "c");
3896 if (!hlua_converters_new(L, &appctx->htxn, 0))
3897 return 0;
3898 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003899
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003900 /* Create the "sc" field that contains a list of stringsafe converters. */
3901 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003902 if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003903 return 0;
3904 lua_settable(L, -3);
Willy Tarreaueee5b512015-04-03 23:46:31 +02003905
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003906 /* Stores the request method. */
3907 lua_pushstring(L, "method");
Willy Tarreaua79021a2018-06-15 18:07:57 +02003908 lua_pushlstring(L, ci_head(txn->req.chn), txn->req.sl.rq.m_l);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003909 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003910
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003911 /* Stores the http version. */
3912 lua_pushstring(L, "version");
Willy Tarreaua79021a2018-06-15 18:07:57 +02003913 lua_pushlstring(L, ci_head(txn->req.chn) + txn->req.sl.rq.v, txn->req.sl.rq.v_l);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003914 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003915
Thierry FOURNIER841475e2015-12-11 17:10:09 +01003916 /* creates an array of headers. hlua_http_get_headers() crates and push
3917 * the array on the top of the stack.
3918 */
3919 lua_pushstring(L, "headers");
3920 htxn.s = s;
3921 htxn.p = px;
3922 htxn.dir = SMP_OPT_DIR_REQ;
3923 if (!hlua_http_get_headers(L, &htxn, &htxn.s->txn->req))
3924 return 0;
3925 lua_settable(L, -3);
3926
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003927 /* Get path and qs */
3928 path = http_get_path(txn);
Thierry FOURNIER7d388632017-02-22 02:06:16 +01003929 if (path) {
Willy Tarreaua79021a2018-06-15 18:07:57 +02003930 end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Thierry FOURNIER7d388632017-02-22 02:06:16 +01003931 p = path;
3932 while (p < end && *p != '?')
3933 p++;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003934
Thierry FOURNIER7d388632017-02-22 02:06:16 +01003935 /* Stores the request path. */
3936 lua_pushstring(L, "path");
3937 lua_pushlstring(L, path, p - path);
3938 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003939
Thierry FOURNIER7d388632017-02-22 02:06:16 +01003940 /* Stores the query string. */
3941 lua_pushstring(L, "qs");
3942 if (*p == '?')
3943 p++;
3944 lua_pushlstring(L, p, end - p);
3945 lua_settable(L, -3);
3946 }
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003947
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003948 /* Stores the request path. */
3949 lua_pushstring(L, "length");
3950 lua_pushinteger(L, txn->req.body_len);
3951 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003952
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003953 /* Create an array of HTTP request headers. */
3954 lua_pushstring(L, "headers");
3955 MAY_LJMP(hlua_http_get_headers(L, &appctx->htxn, &appctx->htxn.s->txn->req));
3956 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003957
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003958 /* Create an empty array of HTTP request headers. */
3959 lua_pushstring(L, "response");
3960 lua_newtable(L);
3961 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003962
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003963 /* Pop a class stream metatable and affect it to the table. */
3964 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref);
3965 lua_setmetatable(L, -2);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003966
3967 return 1;
3968}
3969
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003970__LJMP static int hlua_applet_http_set_var(lua_State *L)
3971{
3972 struct hlua_appctx *appctx;
3973 struct stream *s;
3974 const char *name;
3975 size_t len;
3976 struct sample smp;
3977
3978 MAY_LJMP(check_args(L, 3, "set_var"));
3979
3980 /* It is useles to retrieve the stream, but this function
3981 * runs only in a stream context.
3982 */
3983 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
3984 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3985 s = appctx->htxn.s;
3986
3987 /* Converts the third argument in a sample. */
3988 hlua_lua2smp(L, 3, &smp);
3989
3990 /* Store the sample in a variable. */
3991 smp_set_owner(&smp, s->be, s->sess, s, 0);
3992 vars_set_by_name(name, len, &smp);
3993 return 0;
3994}
3995
3996__LJMP static int hlua_applet_http_unset_var(lua_State *L)
3997{
3998 struct hlua_appctx *appctx;
3999 struct stream *s;
4000 const char *name;
4001 size_t len;
4002 struct sample smp;
4003
4004 MAY_LJMP(check_args(L, 2, "unset_var"));
4005
4006 /* It is useles to retrieve the stream, but this function
4007 * runs only in a stream context.
4008 */
4009 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4010 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4011 s = appctx->htxn.s;
4012
4013 /* Unset the variable. */
4014 smp_set_owner(&smp, s->be, s->sess, s, 0);
4015 vars_unset_by_name(name, len, &smp);
4016 return 0;
4017}
4018
4019__LJMP static int hlua_applet_http_get_var(lua_State *L)
4020{
4021 struct hlua_appctx *appctx;
4022 struct stream *s;
4023 const char *name;
4024 size_t len;
4025 struct sample smp;
4026
4027 MAY_LJMP(check_args(L, 2, "get_var"));
4028
4029 /* It is useles to retrieve the stream, but this function
4030 * runs only in a stream context.
4031 */
4032 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4033 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4034 s = appctx->htxn.s;
4035
4036 smp_set_owner(&smp, s->be, s->sess, s, 0);
4037 if (!vars_get_by_name(name, len, &smp)) {
4038 lua_pushnil(L);
4039 return 1;
4040 }
4041
4042 return hlua_smp2lua(L, &smp);
4043}
4044
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004045__LJMP static int hlua_applet_http_set_priv(lua_State *L)
4046{
4047 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4048 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004049 struct hlua *hlua;
4050
4051 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004052 if (!s->hlua)
4053 return 0;
4054 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004055
4056 MAY_LJMP(check_args(L, 2, "set_priv"));
4057
4058 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02004059 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004060
4061 /* Get and store new value. */
4062 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
4063 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
4064
4065 return 0;
4066}
4067
4068__LJMP static int hlua_applet_http_get_priv(lua_State *L)
4069{
4070 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4071 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004072 struct hlua *hlua;
4073
4074 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004075 if (!s->hlua) {
4076 lua_pushnil(L);
4077 return 1;
4078 }
4079 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004080
4081 /* Push configuration index in the stack. */
4082 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
4083
4084 return 1;
4085}
4086
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004087/* If expected data not yet available, it returns a yield. This function
4088 * consumes the data in the buffer. It returns a string containing the
4089 * data. This string can be empty.
4090 */
4091__LJMP static int hlua_applet_http_getline_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004092{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004093 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4094 struct stream_interface *si = appctx->appctx->owner;
4095 struct channel *chn = si_ic(si);
4096 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02004097 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02004098 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02004099 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02004100 size_t len2;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004101
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004102 /* Maybe we cant send a 100-continue ? */
4103 if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_100C) {
Willy Tarreau06d80a92017-10-19 14:32:15 +02004104 ret = ci_putblk(chn, HTTP_100C, strlen(HTTP_100C));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004105 /* if ret == -2 or -3 the channel closed or the message si too
4106 * big for the buffers. We cant send anything. So, we ignoring
4107 * the error, considers that the 100-continue is sent, and try
4108 * to receive.
4109 * If ret is -1, we dont have room in the buffer, so we yield.
4110 */
4111 if (ret == -1) {
4112 si_applet_cant_put(si);
4113 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_getline_yield, TICK_ETERNITY, 0));
4114 }
4115 appctx->appctx->ctx.hlua_apphttp.flags &= ~APPLET_100C;
4116 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004117
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004118 /* Check for the end of the data. */
4119 if (appctx->appctx->ctx.hlua_apphttp.left_bytes <= 0) {
4120 luaL_pushresult(&appctx->b);
4121 return 1;
4122 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004123
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004124 /* Read the maximum amount of data avalaible. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004125 ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004126
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004127 /* Data not yet avalaible. return yield. */
4128 if (ret == 0) {
4129 si_applet_cant_get(si);
4130 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_getline_yield, TICK_ETERNITY, 0));
4131 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004132
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004133 /* End of data: commit the total strings and return. */
4134 if (ret < 0) {
4135 luaL_pushresult(&appctx->b);
4136 return 1;
4137 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004138
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004139 /* Ensure that the block 2 length is usable. */
4140 if (ret == 1)
4141 len2 = 0;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004142
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004143 /* Copy the fisrt block caping to the length required. */
4144 if (len1 > appctx->appctx->ctx.hlua_apphttp.left_bytes)
4145 len1 = appctx->appctx->ctx.hlua_apphttp.left_bytes;
4146 luaL_addlstring(&appctx->b, blk1, len1);
4147 appctx->appctx->ctx.hlua_apphttp.left_bytes -= len1;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004148
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004149 /* Copy the second block. */
4150 if (len2 > appctx->appctx->ctx.hlua_apphttp.left_bytes)
4151 len2 = appctx->appctx->ctx.hlua_apphttp.left_bytes;
4152 luaL_addlstring(&appctx->b, blk2, len2);
4153 appctx->appctx->ctx.hlua_apphttp.left_bytes -= len2;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004154
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004155 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004156 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004157 luaL_pushresult(&appctx->b);
4158 return 1;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004159}
4160
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004161/* Check arguments for the fucntion "hlua_channel_get_yield". */
4162__LJMP static int hlua_applet_http_getline(lua_State *L)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004163{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004164 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004165
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004166 /* Initialise the string catenation. */
4167 luaL_buffinit(L, &appctx->b);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004168
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004169 return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004170}
4171
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004172/* If expected data not yet available, it returns a yield. This function
4173 * consumes the data in the buffer. It returns a string containing the
4174 * data. This string can be empty.
4175 */
4176__LJMP static int hlua_applet_http_recv_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004177{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004178 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4179 struct stream_interface *si = appctx->appctx->owner;
4180 int len = MAY_LJMP(luaL_checkinteger(L, 2));
4181 struct channel *chn = si_ic(si);
4182 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02004183 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02004184 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02004185 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02004186 size_t len2;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004187
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004188 /* Maybe we cant send a 100-continue ? */
4189 if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_100C) {
Willy Tarreau06d80a92017-10-19 14:32:15 +02004190 ret = ci_putblk(chn, HTTP_100C, strlen(HTTP_100C));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004191 /* if ret == -2 or -3 the channel closed or the message si too
4192 * big for the buffers. We cant send anything. So, we ignoring
4193 * the error, considers that the 100-continue is sent, and try
4194 * to receive.
4195 * If ret is -1, we dont have room in the buffer, so we yield.
4196 */
4197 if (ret == -1) {
4198 si_applet_cant_put(si);
4199 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0));
4200 }
4201 appctx->appctx->ctx.hlua_apphttp.flags &= ~APPLET_100C;
4202 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004203
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004204 /* Read the maximum amount of data avalaible. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004205 ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004206
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004207 /* Data not yet avalaible. return yield. */
4208 if (ret == 0) {
4209 si_applet_cant_get(si);
4210 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0));
4211 }
4212
4213 /* End of data: commit the total strings and return. */
4214 if (ret < 0) {
4215 luaL_pushresult(&appctx->b);
4216 return 1;
4217 }
4218
4219 /* Ensure that the block 2 length is usable. */
4220 if (ret == 1)
4221 len2 = 0;
4222
4223 /* Copy the fisrt block caping to the length required. */
4224 if (len1 > len)
4225 len1 = len;
4226 luaL_addlstring(&appctx->b, blk1, len1);
4227 len -= len1;
4228
4229 /* Copy the second block. */
4230 if (len2 > len)
4231 len2 = len;
4232 luaL_addlstring(&appctx->b, blk2, len2);
4233 len -= len2;
4234
4235 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004236 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004237 if (appctx->appctx->ctx.hlua_apphttp.left_bytes != -1)
4238 appctx->appctx->ctx.hlua_apphttp.left_bytes -= len;
4239
4240 /* If we are no other data avalaible, yield waiting for new data. */
4241 if (len > 0) {
4242 lua_pushinteger(L, len);
4243 lua_replace(L, 2);
4244 si_applet_cant_get(si);
4245 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0));
4246 }
4247
4248 /* return the result. */
4249 luaL_pushresult(&appctx->b);
4250 return 1;
4251}
4252
4253/* Check arguments for the fucntion "hlua_channel_get_yield". */
4254__LJMP static int hlua_applet_http_recv(lua_State *L)
4255{
4256 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4257 int len = -1;
4258
4259 /* Check arguments. */
4260 if (lua_gettop(L) > 2)
4261 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
4262 if (lua_gettop(L) >= 2) {
4263 len = MAY_LJMP(luaL_checkinteger(L, 2));
4264 lua_pop(L, 1);
4265 }
4266
4267 /* Check the required length */
4268 if (len == -1 || len > appctx->appctx->ctx.hlua_apphttp.left_bytes)
4269 len = appctx->appctx->ctx.hlua_apphttp.left_bytes;
4270 lua_pushinteger(L, len);
4271
4272 /* Initialise the string catenation. */
4273 luaL_buffinit(L, &appctx->b);
4274
4275 return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0));
4276}
4277
4278/* Append data in the output side of the buffer. This data is immediatly
4279 * sent. The fcuntion returns the ammount of data writed. If the buffer
4280 * cannot contains the data, the function yield. The function returns -1
4281 * if the channel is closed.
4282 */
4283__LJMP static int hlua_applet_http_send_yield(lua_State *L, int status, lua_KContext ctx)
4284{
4285 size_t len;
4286 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4287 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
4288 int l = MAY_LJMP(luaL_checkinteger(L, 3));
4289 struct stream_interface *si = appctx->appctx->owner;
4290 struct channel *chn = si_ic(si);
4291 int max;
4292
4293 /* Get the max amount of data which can write as input in the channel. */
4294 max = channel_recv_max(chn);
4295 if (max > (len - l))
4296 max = len - l;
4297
4298 /* Copy data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004299 ci_putblk(chn, str + l, max);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004300
4301 /* update counters. */
4302 l += max;
4303 lua_pop(L, 1);
4304 lua_pushinteger(L, l);
4305
4306 /* If some data is not send, declares the situation to the
4307 * applet, and returns a yield.
4308 */
4309 if (l < len) {
4310 si_applet_cant_put(si);
4311 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_send_yield, TICK_ETERNITY, 0));
4312 }
4313
4314 return 1;
4315}
4316
4317/* Just a wraper of "hlua_applet_send_yield". This wrapper permits
4318 * yield the LUA process, and resume it without checking the
4319 * input arguments.
4320 */
4321__LJMP static int hlua_applet_http_send(lua_State *L)
4322{
4323 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4324 size_t len;
4325 char hex[10];
4326
4327 MAY_LJMP(luaL_checklstring(L, 2, &len));
4328
4329 /* If transfer encoding chunked is selected, we surround the data
4330 * by chunk data.
4331 */
4332 if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_CHUNKED) {
4333 snprintf(hex, 9, "%x", (unsigned int)len);
4334 lua_pushfstring(L, "%s\r\n", hex);
4335 lua_insert(L, 2); /* swap the last 2 entries. */
4336 lua_pushstring(L, "\r\n");
4337 lua_concat(L, 3);
4338 }
4339
4340 /* This interger is used for followinf the amount of data sent. */
4341 lua_pushinteger(L, 0);
4342
4343 /* We want to send some data. Headers must be sent. */
4344 if (!(appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
4345 hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data.");
4346 WILL_LJMP(lua_error(L));
4347 }
4348
4349 return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0));
4350}
4351
4352__LJMP static int hlua_applet_http_addheader(lua_State *L)
4353{
4354 const char *name;
4355 int ret;
4356
4357 MAY_LJMP(hlua_checkapplet_http(L, 1));
4358 name = MAY_LJMP(luaL_checkstring(L, 2));
4359 MAY_LJMP(luaL_checkstring(L, 3));
4360
4361 /* Push in the stack the "response" entry. */
4362 ret = lua_getfield(L, 1, "response");
4363 if (ret != LUA_TTABLE) {
4364 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] "
4365 "is expected as an array. %s found", lua_typename(L, ret));
4366 WILL_LJMP(lua_error(L));
4367 }
4368
4369 /* check if the header is already registered if it is not
4370 * the case, register it.
4371 */
4372 ret = lua_getfield(L, -1, name);
4373 if (ret == LUA_TNIL) {
4374
4375 /* Entry not found. */
4376 lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */
4377
4378 /* Insert the new header name in the array in the top of the stack.
4379 * It left the new array in the top of the stack.
4380 */
4381 lua_newtable(L);
4382 lua_pushvalue(L, 2);
4383 lua_pushvalue(L, -2);
4384 lua_settable(L, -4);
4385
4386 } else if (ret != LUA_TTABLE) {
4387
4388 /* corruption error. */
4389 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] "
4390 "is expected as an array. %s found", name, lua_typename(L, ret));
4391 WILL_LJMP(lua_error(L));
4392 }
4393
4394 /* Now the top od thestack is an array of values. We push
4395 * the header value as new entry.
4396 */
4397 lua_pushvalue(L, 3);
4398 ret = lua_rawlen(L, -2);
4399 lua_rawseti(L, -2, ret + 1);
4400 lua_pushboolean(L, 1);
4401 return 1;
4402}
4403
4404__LJMP static int hlua_applet_http_status(lua_State *L)
4405{
4406 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4407 int status = MAY_LJMP(luaL_checkinteger(L, 2));
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004408 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004409
4410 if (status < 100 || status > 599) {
4411 lua_pushboolean(L, 0);
4412 return 1;
4413 }
4414
4415 appctx->appctx->ctx.hlua_apphttp.status = status;
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004416 appctx->appctx->ctx.hlua_apphttp.reason = reason;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004417 lua_pushboolean(L, 1);
4418 return 1;
4419}
4420
4421/* We will build the status line and the headers of the HTTP response.
4422 * We will try send at once if its not possible, we give back the hand
4423 * waiting for more room.
4424 */
4425__LJMP static int hlua_applet_http_start_response_yield(lua_State *L, int status, lua_KContext ctx)
4426{
4427 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4428 struct stream_interface *si = appctx->appctx->owner;
4429 struct channel *chn = si_ic(si);
4430 int ret;
4431 size_t len;
4432 const char *msg;
4433
4434 /* Get the message as the first argument on the stack. */
4435 msg = MAY_LJMP(luaL_checklstring(L, 2, &len));
4436
4437 /* Send the message at once. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004438 ret = ci_putblk(chn, msg, len);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004439
4440 /* if ret == -2 or -3 the channel closed or the message si too
4441 * big for the buffers.
4442 */
4443 if (ret == -2 || ret == -3) {
4444 hlua_pusherror(L, "Lua: 'start_response': response header block too big");
4445 WILL_LJMP(lua_error(L));
4446 }
4447
4448 /* If ret is -1, we dont have room in the buffer, so we yield. */
4449 if (ret == -1) {
4450 si_applet_cant_put(si);
4451 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_start_response_yield, TICK_ETERNITY, 0));
4452 }
4453
4454 /* Headers sent, set the flag. */
4455 appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT;
4456 return 0;
4457}
4458
4459__LJMP static int hlua_applet_http_start_response(lua_State *L)
4460{
4461 struct chunk *tmp = get_trash_chunk();
4462 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004463 const char *name;
Willy Tarreaua3294632017-08-23 11:24:47 +02004464 size_t name_len;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004465 const char *value;
Willy Tarreaua3294632017-08-23 11:24:47 +02004466 size_t value_len;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004467 int id;
Willy Tarreauc9f4ea02017-08-23 09:32:06 +02004468 long long hdr_contentlength = -1;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004469 int hdr_chunked = 0;
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004470 const char *reason = appctx->appctx->ctx.hlua_apphttp.reason;
4471
4472 if (reason == NULL)
4473 reason = get_reason(appctx->appctx->ctx.hlua_apphttp.status);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004474
4475 /* Use the same http version than the request. */
4476 chunk_appendf(tmp, "HTTP/1.%c %d %s\r\n",
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +01004477 appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11 ? '1' : '0',
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004478 appctx->appctx->ctx.hlua_apphttp.status,
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004479 reason);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004480
4481 /* Get the array associated to the field "response" in the object AppletHTTP. */
4482 lua_pushvalue(L, 0);
4483 if (lua_getfield(L, 1, "response") != LUA_TTABLE) {
4484 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n",
4485 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4486 WILL_LJMP(lua_error(L));
4487 }
4488
4489 /* Browse the list of headers. */
4490 lua_pushnil(L);
4491 while(lua_next(L, -2) != 0) {
4492
4493 /* We expect a string as -2. */
4494 if (lua_type(L, -2) != LUA_TSTRING) {
4495 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n",
4496 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4497 lua_typename(L, lua_type(L, -2)));
4498 WILL_LJMP(lua_error(L));
4499 }
Willy Tarreaua3294632017-08-23 11:24:47 +02004500 name = lua_tolstring(L, -2, &name_len);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004501
4502 /* We expect an array as -1. */
4503 if (lua_type(L, -1) != LUA_TTABLE) {
4504 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n",
4505 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4506 name,
4507 lua_typename(L, lua_type(L, -1)));
4508 WILL_LJMP(lua_error(L));
4509 }
4510
4511 /* Browse the table who is on the top of the stack. */
4512 lua_pushnil(L);
4513 while(lua_next(L, -2) != 0) {
4514
4515 /* We expect a number as -2. */
4516 if (lua_type(L, -2) != LUA_TNUMBER) {
4517 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n",
4518 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4519 name,
4520 lua_typename(L, lua_type(L, -2)));
4521 WILL_LJMP(lua_error(L));
4522 }
4523 id = lua_tointeger(L, -2);
4524
4525 /* We expect a string as -2. */
4526 if (lua_type(L, -1) != LUA_TSTRING) {
4527 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n",
4528 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4529 name, id,
4530 lua_typename(L, lua_type(L, -1)));
4531 WILL_LJMP(lua_error(L));
4532 }
Willy Tarreaua3294632017-08-23 11:24:47 +02004533 value = lua_tolstring(L, -1, &value_len);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004534
4535 /* Catenate a new header. */
Willy Tarreaua3294632017-08-23 11:24:47 +02004536 if (tmp->len + name_len + 2 + value_len + 2 < tmp->size) {
4537 memcpy(tmp->str + tmp->len, name, name_len);
4538 tmp->len += name_len;
4539 tmp->str[tmp->len++] = ':';
4540 tmp->str[tmp->len++] = ' ';
4541
4542 memcpy(tmp->str + tmp->len, value, value_len);
4543 tmp->len += value_len;
4544 tmp->str[tmp->len++] = '\r';
4545 tmp->str[tmp->len++] = '\n';
4546 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004547
4548 /* Protocol checks. */
4549
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004550 /* Copy the header content length. The length conversion
Willy Tarreauc9f4ea02017-08-23 09:32:06 +02004551 * is done without control. If it contains a bad value,
4552 * the content-length remains negative so that we can
4553 * switch to either chunked encoding or close.
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004554 */
Willy Tarreaua3294632017-08-23 11:24:47 +02004555 if (name_len == 14 && strcasecmp("content-length", name) == 0)
Willy Tarreauc9f4ea02017-08-23 09:32:06 +02004556 strl2llrc(value, strlen(value), &hdr_contentlength);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004557
4558 /* Check if the client annouces a transfer-encoding chunked it self. */
Willy Tarreaua3294632017-08-23 11:24:47 +02004559 if (name_len == 17 && value_len == 7 &&
4560 strcasecmp("transfer-encoding", name) == 0 &&
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004561 strcasecmp("chunked", value) == 0)
4562 hdr_chunked = 1;
4563
4564 /* Remove the array from the stack, and get next element with a remaining string. */
4565 lua_pop(L, 1);
4566 }
4567
4568 /* Remove the array from the stack, and get next element with a remaining string. */
4569 lua_pop(L, 1);
4570 }
4571
Willy Tarreau06c75fe2017-08-23 09:10:38 +02004572 /* If we dont have a content-length set, and the HTTP version is 1.1
4573 * and the status code implies the presence of a message body, we must
4574 * announce a transfer encoding chunked. This is required by haproxy
4575 * for the keepalive compliance. If the applet annouces a transfer-encoding
4576 * chunked itslef, don't do anything.
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004577 */
Willy Tarreauc9f4ea02017-08-23 09:32:06 +02004578 if (hdr_contentlength < 0 && hdr_chunked == 0 &&
Willy Tarreau06c75fe2017-08-23 09:10:38 +02004579 (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) &&
4580 appctx->appctx->ctx.hlua_apphttp.status >= 200 &&
4581 appctx->appctx->ctx.hlua_apphttp.status != 204 &&
4582 appctx->appctx->ctx.hlua_apphttp.status != 304) {
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004583 chunk_appendf(tmp, "Transfer-encoding: chunked\r\n");
4584 appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_CHUNKED;
4585 }
4586
4587 /* Finalize headers. */
4588 chunk_appendf(tmp, "\r\n");
4589
4590 /* Remove the last entry and the array of headers */
4591 lua_pop(L, 2);
4592
4593 /* Push the headers block. */
4594 lua_pushlstring(L, tmp->str, tmp->len);
4595
4596 return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0));
4597}
4598
4599/*
4600 *
4601 *
4602 * Class HTTP
4603 *
4604 *
4605 */
4606
4607/* Returns a struct hlua_txn if the stack entry "ud" is
4608 * a class stream, otherwise it throws an error.
4609 */
4610__LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud)
4611{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02004612 return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004613}
4614
4615/* This function creates and push in the stack a HTTP object
4616 * according with a current TXN.
4617 */
4618static int hlua_http_new(lua_State *L, struct hlua_txn *txn)
4619{
4620 struct hlua_txn *htxn;
4621
4622 /* Check stack size. */
4623 if (!lua_checkstack(L, 3))
4624 return 0;
4625
4626 /* Create the object: obj[0] = userdata.
4627 * Note that the base of the Converters object is the
4628 * same than the TXN object.
4629 */
4630 lua_newtable(L);
4631 htxn = lua_newuserdata(L, sizeof(*htxn));
4632 lua_rawseti(L, -2, 0);
4633
4634 htxn->s = txn->s;
4635 htxn->p = txn->p;
4636
4637 /* Pop a class stream metatable and affect it to the table. */
4638 lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref);
4639 lua_setmetatable(L, -2);
4640
4641 return 1;
4642}
4643
4644/* This function creates ans returns an array of HTTP headers.
4645 * This function does not fails. It is used as wrapper with the
4646 * 2 following functions.
4647 */
4648__LJMP static int hlua_http_get_headers(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg)
4649{
4650 const char *cur_ptr, *cur_next, *p;
4651 int old_idx, cur_idx;
4652 struct hdr_idx_elem *cur_hdr;
4653 const char *hn, *hv;
4654 int hnl, hvl;
4655 int type;
4656 const char *in;
4657 char *out;
4658 int len;
4659
4660 /* Create the table. */
4661 lua_newtable(L);
4662
4663 if (!htxn->s->txn)
4664 return 1;
4665
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004666 /* Check if a valid response is parsed */
4667 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4668 return 1;
4669
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004670 /* Build array of headers. */
4671 old_idx = 0;
Willy Tarreaua79021a2018-06-15 18:07:57 +02004672 cur_next = ci_head(msg->chn) + hdr_idx_first_pos(&htxn->s->txn->hdr_idx);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004673
4674 while (1) {
4675 cur_idx = htxn->s->txn->hdr_idx.v[old_idx].next;
4676 if (!cur_idx)
4677 break;
4678 old_idx = cur_idx;
4679
4680 cur_hdr = &htxn->s->txn->hdr_idx.v[cur_idx];
4681 cur_ptr = cur_next;
4682 cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1;
4683
4684 /* Now we have one full header at cur_ptr of len cur_hdr->len,
4685 * and the next header starts at cur_next. We'll check
4686 * this header in the list as well as against the default
4687 * rule.
4688 */
4689
4690 /* look for ': *'. */
4691 hn = cur_ptr;
4692 for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++);
4693 if (p >= cur_ptr+cur_hdr->len)
4694 continue;
4695 hnl = p - hn;
4696 p++;
4697 while (p < cur_ptr+cur_hdr->len && ( *p == ' ' || *p == '\t' ))
4698 p++;
4699 if (p >= cur_ptr+cur_hdr->len)
4700 continue;
4701 hv = p;
4702 hvl = cur_ptr+cur_hdr->len-p;
4703
4704 /* Lowercase the key. Don't check the size of trash, it have
4705 * the size of one buffer and the input data contains in one
4706 * buffer.
4707 */
4708 out = trash.str;
4709 for (in=hn; in<hn+hnl; in++, out++)
4710 *out = tolower(*in);
4711 *out = '\0';
4712
4713 /* Check for existing entry:
4714 * assume that the table is on the top of the stack, and
4715 * push the key in the stack, the function lua_gettable()
4716 * perform the lookup.
4717 */
4718 lua_pushlstring(L, trash.str, hnl);
4719 lua_gettable(L, -2);
4720 type = lua_type(L, -1);
4721
4722 switch (type) {
4723 case LUA_TNIL:
4724 /* Table not found, create it. */
4725 lua_pop(L, 1); /* remove the nil value. */
4726 lua_pushlstring(L, trash.str, hnl); /* push the header name as key. */
4727 lua_newtable(L); /* create and push empty table. */
4728 lua_pushlstring(L, hv, hvl); /* push header value. */
4729 lua_rawseti(L, -2, 0); /* index header value (pop it). */
4730 lua_rawset(L, -3); /* index new table with header name (pop the values). */
4731 break;
4732
4733 case LUA_TTABLE:
4734 /* Entry found: push the value in the table. */
4735 len = lua_rawlen(L, -1);
4736 lua_pushlstring(L, hv, hvl); /* push header value. */
4737 lua_rawseti(L, -2, len+1); /* index header value (pop it). */
4738 lua_pop(L, 1); /* remove the table (it is stored in the main table). */
4739 break;
4740
4741 default:
4742 /* Other cases are errors. */
4743 hlua_pusherror(L, "internal error during the parsing of headers.");
4744 WILL_LJMP(lua_error(L));
4745 }
4746 }
4747
4748 return 1;
4749}
4750
4751__LJMP static int hlua_http_req_get_headers(lua_State *L)
4752{
4753 struct hlua_txn *htxn;
4754
4755 MAY_LJMP(check_args(L, 1, "req_get_headers"));
4756 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4757
4758 return hlua_http_get_headers(L, htxn, &htxn->s->txn->req);
4759}
4760
4761__LJMP static int hlua_http_res_get_headers(lua_State *L)
4762{
4763 struct hlua_txn *htxn;
4764
4765 MAY_LJMP(check_args(L, 1, "res_get_headers"));
4766 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4767
4768 return hlua_http_get_headers(L, htxn, &htxn->s->txn->rsp);
4769}
4770
4771/* This function replace full header, or just a value in
4772 * the request or in the response. It is a wrapper fir the
4773 * 4 following functions.
4774 */
4775__LJMP static inline int hlua_http_rep_hdr(lua_State *L, struct hlua_txn *htxn,
4776 struct http_msg *msg, int action)
4777{
4778 size_t name_len;
4779 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
4780 const char *reg = MAY_LJMP(luaL_checkstring(L, 3));
4781 const char *value = MAY_LJMP(luaL_checkstring(L, 4));
4782 struct my_regex re;
4783
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004784 /* Check if a valid response is parsed */
4785 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4786 return 0;
4787
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004788 if (!regex_comp(reg, &re, 1, 1, NULL))
4789 WILL_LJMP(luaL_argerror(L, 3, "invalid regex"));
4790
4791 http_transform_header_str(htxn->s, msg, name, name_len, value, &re, action);
4792 regex_free(&re);
4793 return 0;
4794}
4795
4796__LJMP static int hlua_http_req_rep_hdr(lua_State *L)
4797{
4798 struct hlua_txn *htxn;
4799
4800 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
4801 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4802
4803 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_HDR));
4804}
4805
4806__LJMP static int hlua_http_res_rep_hdr(lua_State *L)
4807{
4808 struct hlua_txn *htxn;
4809
4810 MAY_LJMP(check_args(L, 4, "res_rep_hdr"));
4811 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4812
4813 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->rsp, ACT_HTTP_REPLACE_HDR));
4814}
4815
4816__LJMP static int hlua_http_req_rep_val(lua_State *L)
4817{
4818 struct hlua_txn *htxn;
4819
4820 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
4821 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4822
4823 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_VAL));
4824}
4825
4826__LJMP static int hlua_http_res_rep_val(lua_State *L)
4827{
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004828 struct hlua_txn *htxn;
4829
4830 MAY_LJMP(check_args(L, 4, "res_rep_val"));
4831 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4832
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02004833 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->rsp, ACT_HTTP_REPLACE_VAL));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004834}
4835
4836/* This function deletes all the occurences of an header.
4837 * It is a wrapper for the 2 following functions.
4838 */
4839__LJMP static inline int hlua_http_del_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg)
4840{
4841 size_t len;
4842 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4843 struct hdr_ctx ctx;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004844 struct http_txn *txn = htxn->s->txn;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004845
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004846 /* Check if a valid response is parsed */
4847 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4848 return 0;
4849
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004850 ctx.idx = 0;
Willy Tarreaua79021a2018-06-15 18:07:57 +02004851 while (http_find_header2(name, len, ci_head(msg->chn), &txn->hdr_idx, &ctx))
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004852 http_remove_header2(msg, &txn->hdr_idx, &ctx);
4853 return 0;
4854}
4855
4856__LJMP static int hlua_http_req_del_hdr(lua_State *L)
4857{
4858 struct hlua_txn *htxn;
4859
4860 MAY_LJMP(check_args(L, 2, "req_del_hdr"));
4861 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4862
Willy Tarreaueee5b512015-04-03 23:46:31 +02004863 return hlua_http_del_hdr(L, htxn, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004864}
4865
4866__LJMP static int hlua_http_res_del_hdr(lua_State *L)
4867{
4868 struct hlua_txn *htxn;
4869
4870 MAY_LJMP(check_args(L, 2, "req_del_hdr"));
4871 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4872
Willy Tarreaueee5b512015-04-03 23:46:31 +02004873 return hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004874}
4875
4876/* This function adds an header. It is a wrapper used by
4877 * the 2 following functions.
4878 */
4879__LJMP static inline int hlua_http_add_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg)
4880{
4881 size_t name_len;
4882 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
4883 size_t value_len;
4884 const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len));
4885 char *p;
4886
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004887 /* Check if a valid message is parsed */
4888 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4889 return 0;
4890
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004891 /* Check length. */
4892 trash.len = value_len + name_len + 2;
4893 if (trash.len > trash.size)
4894 return 0;
4895
4896 /* Creates the header string. */
4897 p = trash.str;
4898 memcpy(p, name, name_len);
4899 p += name_len;
4900 *p = ':';
4901 p++;
4902 *p = ' ';
4903 p++;
4904 memcpy(p, value, value_len);
4905
Willy Tarreaueee5b512015-04-03 23:46:31 +02004906 lua_pushboolean(L, http_header_add_tail2(msg, &htxn->s->txn->hdr_idx,
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004907 trash.str, trash.len) != 0);
4908
4909 return 0;
4910}
4911
4912__LJMP static int hlua_http_req_add_hdr(lua_State *L)
4913{
4914 struct hlua_txn *htxn;
4915
4916 MAY_LJMP(check_args(L, 3, "req_add_hdr"));
4917 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4918
Willy Tarreaueee5b512015-04-03 23:46:31 +02004919 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004920}
4921
4922__LJMP static int hlua_http_res_add_hdr(lua_State *L)
4923{
4924 struct hlua_txn *htxn;
4925
4926 MAY_LJMP(check_args(L, 3, "res_add_hdr"));
4927 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4928
Willy Tarreaueee5b512015-04-03 23:46:31 +02004929 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004930}
4931
4932static int hlua_http_req_set_hdr(lua_State *L)
4933{
4934 struct hlua_txn *htxn;
4935
4936 MAY_LJMP(check_args(L, 3, "req_set_hdr"));
4937 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4938
Willy Tarreaueee5b512015-04-03 23:46:31 +02004939 hlua_http_del_hdr(L, htxn, &htxn->s->txn->req);
4940 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004941}
4942
4943static int hlua_http_res_set_hdr(lua_State *L)
4944{
4945 struct hlua_txn *htxn;
4946
4947 MAY_LJMP(check_args(L, 3, "res_set_hdr"));
4948 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4949
Willy Tarreaueee5b512015-04-03 23:46:31 +02004950 hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp);
4951 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004952}
4953
4954/* This function set the method. */
4955static int hlua_http_req_set_meth(lua_State *L)
4956{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02004957 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004958 size_t name_len;
4959 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004960
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004961 /* Check if a valid request is parsed */
4962 if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) {
4963 lua_pushboolean(L, 0);
4964 return 1;
4965 }
4966
Willy Tarreau987e3fb2015-04-04 01:09:08 +02004967 lua_pushboolean(L, http_replace_req_line(0, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004968 return 1;
4969}
4970
4971/* This function set the method. */
4972static int hlua_http_req_set_path(lua_State *L)
4973{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02004974 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004975 size_t name_len;
4976 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004977
4978 /* Check if a valid request is parsed */
4979 if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) {
4980 lua_pushboolean(L, 0);
4981 return 1;
4982 }
4983
Willy Tarreau987e3fb2015-04-04 01:09:08 +02004984 lua_pushboolean(L, http_replace_req_line(1, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004985 return 1;
4986}
4987
4988/* This function set the query-string. */
4989static int hlua_http_req_set_query(lua_State *L)
4990{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02004991 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004992 size_t name_len;
4993 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004994
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004995 /* Check if a valid request is parsed */
4996 if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) {
4997 lua_pushboolean(L, 0);
4998 return 1;
4999 }
5000
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005001 /* Check length. */
5002 if (name_len > trash.size - 1) {
5003 lua_pushboolean(L, 0);
5004 return 1;
5005 }
5006
5007 /* Add the mark question as prefix. */
5008 chunk_reset(&trash);
5009 trash.str[trash.len++] = '?';
5010 memcpy(trash.str + trash.len, name, name_len);
5011 trash.len += name_len;
5012
Willy Tarreau987e3fb2015-04-04 01:09:08 +02005013 lua_pushboolean(L, http_replace_req_line(2, trash.str, trash.len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005014 return 1;
5015}
5016
5017/* This function set the uri. */
5018static int hlua_http_req_set_uri(lua_State *L)
5019{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005020 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005021 size_t name_len;
5022 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005023
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02005024 /* Check if a valid request is parsed */
5025 if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) {
5026 lua_pushboolean(L, 0);
5027 return 1;
5028 }
5029
Willy Tarreau987e3fb2015-04-04 01:09:08 +02005030 lua_pushboolean(L, http_replace_req_line(3, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005031 return 1;
5032}
5033
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005034/* This function set the response code & optionally reason. */
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005035static int hlua_http_res_set_status(lua_State *L)
5036{
5037 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5038 unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2));
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005039 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005040
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02005041 /* Check if a valid response is parsed */
5042 if (unlikely(htxn->s->txn->rsp.msg_state < HTTP_MSG_BODY))
5043 return 0;
5044
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005045 http_set_status(code, reason, htxn->s);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005046 return 0;
5047}
5048
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005049/*
5050 *
5051 *
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005052 * Class TXN
5053 *
5054 *
5055 */
5056
5057/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02005058 * a class stream, otherwise it throws an error.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005059 */
5060__LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud)
5061{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02005062 return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref));
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005063}
5064
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005065__LJMP static int hlua_set_var(lua_State *L)
5066{
5067 struct hlua_txn *htxn;
5068 const char *name;
5069 size_t len;
5070 struct sample smp;
5071
5072 MAY_LJMP(check_args(L, 3, "set_var"));
5073
5074 /* It is useles to retrieve the stream, but this function
5075 * runs only in a stream context.
5076 */
5077 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5078 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5079
5080 /* Converts the third argument in a sample. */
5081 hlua_lua2smp(L, 3, &smp);
5082
5083 /* Store the sample in a variable. */
Willy Tarreau7560dd42016-03-10 16:28:58 +01005084 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Willy Tarreau6204cd92016-03-10 16:33:04 +01005085 vars_set_by_name(name, len, &smp);
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005086 return 0;
5087}
5088
Christopher Faulet85d79c92016-11-09 16:54:56 +01005089__LJMP static int hlua_unset_var(lua_State *L)
5090{
5091 struct hlua_txn *htxn;
5092 const char *name;
5093 size_t len;
5094 struct sample smp;
5095
5096 MAY_LJMP(check_args(L, 2, "unset_var"));
5097
5098 /* It is useles to retrieve the stream, but this function
5099 * runs only in a stream context.
5100 */
5101 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5102 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5103
5104 /* Unset the variable. */
5105 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
5106 vars_unset_by_name(name, len, &smp);
5107 return 0;
5108}
5109
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005110__LJMP static int hlua_get_var(lua_State *L)
5111{
5112 struct hlua_txn *htxn;
5113 const char *name;
5114 size_t len;
5115 struct sample smp;
5116
5117 MAY_LJMP(check_args(L, 2, "get_var"));
5118
5119 /* It is useles to retrieve the stream, but this function
5120 * runs only in a stream context.
5121 */
5122 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5123 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5124
Willy Tarreau7560dd42016-03-10 16:28:58 +01005125 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Willy Tarreau6204cd92016-03-10 16:33:04 +01005126 if (!vars_get_by_name(name, len, &smp)) {
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005127 lua_pushnil(L);
5128 return 1;
5129 }
5130
5131 return hlua_smp2lua(L, &smp);
5132}
5133
Willy Tarreau59551662015-03-10 14:23:13 +01005134__LJMP static int hlua_set_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005135{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005136 struct hlua *hlua;
5137
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005138 MAY_LJMP(check_args(L, 2, "set_priv"));
5139
Willy Tarreau87b09662015-04-03 00:22:06 +02005140 /* It is useles to retrieve the stream, but this function
5141 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005142 */
5143 MAY_LJMP(hlua_checktxn(L, 1));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005144 hlua = hlua_gethlua(L);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005145
5146 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02005147 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005148
5149 /* Get and store new value. */
5150 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
5151 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
5152
5153 return 0;
5154}
5155
Willy Tarreau59551662015-03-10 14:23:13 +01005156__LJMP static int hlua_get_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005157{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005158 struct hlua *hlua;
5159
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005160 MAY_LJMP(check_args(L, 1, "get_priv"));
5161
Willy Tarreau87b09662015-04-03 00:22:06 +02005162 /* It is useles to retrieve the stream, but this function
5163 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005164 */
5165 MAY_LJMP(hlua_checktxn(L, 1));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005166 hlua = hlua_gethlua(L);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005167
5168 /* Push configuration index in the stack. */
5169 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
5170
5171 return 1;
5172}
5173
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005174/* Create stack entry containing a class TXN. This function
5175 * return 0 if the stack does not contains free slots,
5176 * otherwise it returns 1.
5177 */
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005178static int hlua_txn_new(lua_State *L, struct stream *s, struct proxy *p, int dir, int flags)
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005179{
Willy Tarreaude491382015-04-06 11:04:28 +02005180 struct hlua_txn *htxn;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005181
5182 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005183 if (!lua_checkstack(L, 3))
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005184 return 0;
5185
5186 /* NOTE: The allocation never fails. The failure
5187 * throw an error, and the function never returns.
5188 * if the throw is not avalaible, the process is aborted.
5189 */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005190 /* Create the object: obj[0] = userdata. */
5191 lua_newtable(L);
Willy Tarreaude491382015-04-06 11:04:28 +02005192 htxn = lua_newuserdata(L, sizeof(*htxn));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005193 lua_rawseti(L, -2, 0);
5194
Willy Tarreaude491382015-04-06 11:04:28 +02005195 htxn->s = s;
5196 htxn->p = p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01005197 htxn->dir = dir;
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005198 htxn->flags = flags;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005199
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005200 /* Create the "f" field that contains a list of fetches. */
5201 lua_pushstring(L, "f");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005202 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005203 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005204 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005205
5206 /* Create the "sf" field that contains a list of stringsafe fetches. */
5207 lua_pushstring(L, "sf");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005208 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP | HLUA_F_AS_STRING))
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005209 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005210 lua_rawset(L, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005211
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005212 /* Create the "c" field that contains a list of converters. */
5213 lua_pushstring(L, "c");
Willy Tarreaude491382015-04-06 11:04:28 +02005214 if (!hlua_converters_new(L, htxn, 0))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005215 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005216 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005217
5218 /* Create the "sc" field that contains a list of stringsafe converters. */
5219 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01005220 if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING))
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005221 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005222 lua_rawset(L, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005223
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005224 /* Create the "req" field that contains the request channel object. */
5225 lua_pushstring(L, "req");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005226 if (!hlua_channel_new(L, &s->req))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005227 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005228 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005229
5230 /* Create the "res" field that contains the response channel object. */
5231 lua_pushstring(L, "res");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005232 if (!hlua_channel_new(L, &s->res))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005233 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005234 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005235
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005236 /* Creates the HTTP object is the current proxy allows http. */
5237 lua_pushstring(L, "http");
5238 if (p->mode == PR_MODE_HTTP) {
Willy Tarreaude491382015-04-06 11:04:28 +02005239 if (!hlua_http_new(L, htxn))
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005240 return 0;
5241 }
5242 else
5243 lua_pushnil(L);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005244 lua_rawset(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005245
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005246 /* Pop a class sesison metatable and affect it to the userdata. */
5247 lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref);
5248 lua_setmetatable(L, -2);
5249
5250 return 1;
5251}
5252
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005253__LJMP static int hlua_txn_deflog(lua_State *L)
5254{
5255 const char *msg;
5256 struct hlua_txn *htxn;
5257
5258 MAY_LJMP(check_args(L, 2, "deflog"));
5259 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5260 msg = MAY_LJMP(luaL_checkstring(L, 2));
5261
5262 hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg);
5263 return 0;
5264}
5265
5266__LJMP static int hlua_txn_log(lua_State *L)
5267{
5268 int level;
5269 const char *msg;
5270 struct hlua_txn *htxn;
5271
5272 MAY_LJMP(check_args(L, 3, "log"));
5273 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5274 level = MAY_LJMP(luaL_checkinteger(L, 2));
5275 msg = MAY_LJMP(luaL_checkstring(L, 3));
5276
5277 if (level < 0 || level >= NB_LOG_LEVELS)
5278 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
5279
5280 hlua_sendlog(htxn->s->be, level, msg);
5281 return 0;
5282}
5283
5284__LJMP static int hlua_txn_log_debug(lua_State *L)
5285{
5286 const char *msg;
5287 struct hlua_txn *htxn;
5288
5289 MAY_LJMP(check_args(L, 2, "Debug"));
5290 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5291 msg = MAY_LJMP(luaL_checkstring(L, 2));
5292 hlua_sendlog(htxn->s->be, LOG_DEBUG, msg);
5293 return 0;
5294}
5295
5296__LJMP static int hlua_txn_log_info(lua_State *L)
5297{
5298 const char *msg;
5299 struct hlua_txn *htxn;
5300
5301 MAY_LJMP(check_args(L, 2, "Info"));
5302 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5303 msg = MAY_LJMP(luaL_checkstring(L, 2));
5304 hlua_sendlog(htxn->s->be, LOG_INFO, msg);
5305 return 0;
5306}
5307
5308__LJMP static int hlua_txn_log_warning(lua_State *L)
5309{
5310 const char *msg;
5311 struct hlua_txn *htxn;
5312
5313 MAY_LJMP(check_args(L, 2, "Warning"));
5314 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5315 msg = MAY_LJMP(luaL_checkstring(L, 2));
5316 hlua_sendlog(htxn->s->be, LOG_WARNING, msg);
5317 return 0;
5318}
5319
5320__LJMP static int hlua_txn_log_alert(lua_State *L)
5321{
5322 const char *msg;
5323 struct hlua_txn *htxn;
5324
5325 MAY_LJMP(check_args(L, 2, "Alert"));
5326 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5327 msg = MAY_LJMP(luaL_checkstring(L, 2));
5328 hlua_sendlog(htxn->s->be, LOG_ALERT, msg);
5329 return 0;
5330}
5331
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005332__LJMP static int hlua_txn_set_loglevel(lua_State *L)
5333{
5334 struct hlua_txn *htxn;
5335 int ll;
5336
5337 MAY_LJMP(check_args(L, 2, "set_loglevel"));
5338 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5339 ll = MAY_LJMP(luaL_checkinteger(L, 2));
5340
5341 if (ll < 0 || ll > 7)
5342 WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7"));
5343
5344 htxn->s->logs.level = ll;
5345 return 0;
5346}
5347
5348__LJMP static int hlua_txn_set_tos(lua_State *L)
5349{
5350 struct hlua_txn *htxn;
5351 struct connection *cli_conn;
5352 int tos;
5353
5354 MAY_LJMP(check_args(L, 2, "set_tos"));
5355 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5356 tos = MAY_LJMP(luaL_checkinteger(L, 2));
5357
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02005358 if ((cli_conn = objt_conn(htxn->s->sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02005359 inet_set_tos(cli_conn->handle.fd, &cli_conn->addr.from, tos);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005360
5361 return 0;
5362}
5363
5364__LJMP static int hlua_txn_set_mark(lua_State *L)
5365{
5366#ifdef SO_MARK
5367 struct hlua_txn *htxn;
5368 struct connection *cli_conn;
5369 int mark;
5370
5371 MAY_LJMP(check_args(L, 2, "set_mark"));
5372 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5373 mark = MAY_LJMP(luaL_checkinteger(L, 2));
5374
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02005375 if ((cli_conn = objt_conn(htxn->s->sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02005376 setsockopt(cli_conn->handle.fd, SOL_SOCKET, SO_MARK, &mark, sizeof(mark));
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005377#endif
5378 return 0;
5379}
5380
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005381/* This function is an Lua binding that send pending data
5382 * to the client, and close the stream interface.
5383 */
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005384__LJMP static int hlua_txn_done(lua_State *L)
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005385{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005386 struct hlua_txn *htxn;
Thierry FOURNIER9bd52d42016-07-14 11:45:33 +02005387 struct hlua *hlua;
Willy Tarreau81389672015-03-10 12:03:52 +01005388 struct channel *ic, *oc;
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005389
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005390 MAY_LJMP(check_args(L, 1, "close"));
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005391 htxn = MAY_LJMP(hlua_checktxn(L, 1));
Thierry FOURNIER9bd52d42016-07-14 11:45:33 +02005392 hlua = hlua_gethlua(L);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005393
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005394 /* If the flags NOTERM is set, we cannot terminate the http
5395 * session, so we just end the execution of the current
5396 * lua code.
5397 */
5398 if (htxn->flags & HLUA_TXN_NOTERM) {
5399 WILL_LJMP(hlua_done(L));
5400 return 0;
5401 }
5402
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005403 ic = &htxn->s->req;
5404 oc = &htxn->s->res;
Willy Tarreau81389672015-03-10 12:03:52 +01005405
Willy Tarreau630ef452015-08-28 10:06:15 +02005406 if (htxn->s->txn) {
5407 /* HTTP mode, let's stay in sync with the stream */
5408 bi_fast_delete(ic->buf, htxn->s->txn->req.sov);
5409 htxn->s->txn->req.next -= htxn->s->txn->req.sov;
5410 htxn->s->txn->req.sov = 0;
5411 ic->analysers &= AN_REQ_HTTP_XFER_BODY;
5412 oc->analysers = AN_RES_HTTP_XFER_BODY;
5413 htxn->s->txn->req.msg_state = HTTP_MSG_CLOSED;
5414 htxn->s->txn->rsp.msg_state = HTTP_MSG_DONE;
5415
Willy Tarreau630ef452015-08-28 10:06:15 +02005416 /* Note that if we want to support keep-alive, we need
5417 * to bypass the close/shutr_now calls below, but that
5418 * may only be done if the HTTP request was already
5419 * processed and the connection header is known (ie
5420 * not during TCP rules).
5421 */
5422 }
5423
Thierry FOURNIER10ec2142015-08-24 17:23:45 +02005424 channel_auto_read(ic);
Willy Tarreau81389672015-03-10 12:03:52 +01005425 channel_abort(ic);
5426 channel_auto_close(ic);
5427 channel_erase(ic);
Thierry FOURNIER10ec2142015-08-24 17:23:45 +02005428
5429 oc->wex = tick_add_ifset(now_ms, oc->wto);
Willy Tarreau81389672015-03-10 12:03:52 +01005430 channel_auto_read(oc);
5431 channel_auto_close(oc);
5432 channel_shutr_now(oc);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005433
Willy Tarreau0458b082015-08-28 09:40:04 +02005434 ic->analysers = 0;
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005435
Thierry FOURNIER9bd52d42016-07-14 11:45:33 +02005436 hlua->flags |= HLUA_STOP;
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005437 WILL_LJMP(hlua_done(L));
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005438 return 0;
5439}
5440
5441__LJMP static int hlua_log(lua_State *L)
5442{
5443 int level;
5444 const char *msg;
5445
5446 MAY_LJMP(check_args(L, 2, "log"));
5447 level = MAY_LJMP(luaL_checkinteger(L, 1));
5448 msg = MAY_LJMP(luaL_checkstring(L, 2));
5449
5450 if (level < 0 || level >= NB_LOG_LEVELS)
5451 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
5452
5453 hlua_sendlog(NULL, level, msg);
5454 return 0;
5455}
5456
5457__LJMP static int hlua_log_debug(lua_State *L)
5458{
5459 const char *msg;
5460
5461 MAY_LJMP(check_args(L, 1, "debug"));
5462 msg = MAY_LJMP(luaL_checkstring(L, 1));
5463 hlua_sendlog(NULL, LOG_DEBUG, msg);
5464 return 0;
5465}
5466
5467__LJMP static int hlua_log_info(lua_State *L)
5468{
5469 const char *msg;
5470
5471 MAY_LJMP(check_args(L, 1, "info"));
5472 msg = MAY_LJMP(luaL_checkstring(L, 1));
5473 hlua_sendlog(NULL, LOG_INFO, msg);
5474 return 0;
5475}
5476
5477__LJMP static int hlua_log_warning(lua_State *L)
5478{
5479 const char *msg;
5480
5481 MAY_LJMP(check_args(L, 1, "warning"));
5482 msg = MAY_LJMP(luaL_checkstring(L, 1));
5483 hlua_sendlog(NULL, LOG_WARNING, msg);
5484 return 0;
5485}
5486
5487__LJMP static int hlua_log_alert(lua_State *L)
5488{
5489 const char *msg;
5490
5491 MAY_LJMP(check_args(L, 1, "alert"));
5492 msg = MAY_LJMP(luaL_checkstring(L, 1));
5493 hlua_sendlog(NULL, LOG_ALERT, msg);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005494 return 0;
5495}
5496
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005497__LJMP static int hlua_sleep_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005498{
5499 int wakeup_ms = lua_tointeger(L, -1);
5500 if (now_ms < wakeup_ms)
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005501 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005502 return 0;
5503}
5504
5505__LJMP static int hlua_sleep(lua_State *L)
5506{
5507 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005508 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005509
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005510 MAY_LJMP(check_args(L, 1, "sleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005511
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005512 delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005513 wakeup_ms = tick_add(now_ms, delay);
5514 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005515
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005516 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
5517 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005518}
5519
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005520__LJMP static int hlua_msleep(lua_State *L)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005521{
5522 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005523 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005524
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005525 MAY_LJMP(check_args(L, 1, "msleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005526
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005527 delay = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005528 wakeup_ms = tick_add(now_ms, delay);
5529 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005530
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005531 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
5532 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005533}
5534
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01005535/* This functionis an LUA binding. it permits to give back
5536 * the hand at the HAProxy scheduler. It is used when the
5537 * LUA processing consumes a lot of time.
5538 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005539__LJMP static int hlua_yield_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005540{
5541 return 0;
5542}
5543
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01005544__LJMP static int hlua_yield(lua_State *L)
5545{
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005546 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_yield_yield, TICK_ETERNITY, HLUA_CTRLYIELD));
5547 return 0;
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01005548}
5549
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005550/* This function change the nice of the currently executed
5551 * task. It is used set low or high priority at the current
5552 * task.
5553 */
Willy Tarreau59551662015-03-10 14:23:13 +01005554__LJMP static int hlua_set_nice(lua_State *L)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005555{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005556 struct hlua *hlua;
5557 int nice;
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005558
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005559 MAY_LJMP(check_args(L, 1, "set_nice"));
5560 hlua = hlua_gethlua(L);
5561 nice = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005562
5563 /* If he task is not set, I'm in a start mode. */
5564 if (!hlua || !hlua->task)
5565 return 0;
5566
5567 if (nice < -1024)
5568 nice = -1024;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005569 else if (nice > 1024)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005570 nice = 1024;
5571
5572 hlua->task->nice = nice;
5573 return 0;
5574}
5575
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005576/* This function is used as a calback of a task. It is called by the
5577 * HAProxy task subsystem when the task is awaked. The LUA runtime can
5578 * return an E_AGAIN signal, the emmiter of this signal must set a
5579 * signal to wake the task.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005580 *
5581 * Task wrapper are longjmp safe because the only one Lua code
5582 * executed is the safe hlua_ctx_resume();
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005583 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02005584static struct task *hlua_process_task(struct task *task, void *context, unsigned short state)
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005585{
Olivier Houchard9f6af332018-05-25 14:04:04 +02005586 struct hlua *hlua = context;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005587 enum hlua_exec status;
5588
Christopher Faulet5bc99722018-04-25 10:34:45 +02005589 if (task->thread_mask == MAX_THREADS_MASK)
5590 task_set_affinity(task, tid_bit);
5591
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005592 /* If it is the first call to the task, we must initialize the
5593 * execution timeouts.
5594 */
5595 if (!HLUA_IS_RUNNING(hlua))
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02005596 hlua->max_time = hlua_timeout_task;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005597
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005598 /* Execute the Lua code. */
5599 status = hlua_ctx_resume(hlua, 1);
5600
5601 switch (status) {
5602 /* finished or yield */
5603 case HLUA_E_OK:
5604 hlua_ctx_destroy(hlua);
5605 task_delete(task);
5606 task_free(task);
Tim Duesterhuscd235c62018-04-24 13:56:01 +02005607 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005608 break;
5609
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01005610 case HLUA_E_AGAIN: /* co process or timeout wake me later. */
Thierry FOURNIERcb146882017-12-10 17:10:57 +01005611 notification_gc(&hlua->com);
PiBa-NLfe971b32018-05-02 22:27:14 +02005612 task->expire = hlua->wake_time;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005613 break;
5614
5615 /* finished with error. */
5616 case HLUA_E_ERRMSG:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005617 SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005618 hlua_ctx_destroy(hlua);
5619 task_delete(task);
5620 task_free(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02005621 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005622 break;
5623
5624 case HLUA_E_ERR:
5625 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005626 SEND_ERR(NULL, "Lua task: unknown error.\n");
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005627 hlua_ctx_destroy(hlua);
5628 task_delete(task);
5629 task_free(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02005630 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005631 break;
5632 }
Emeric Brun253e53e2017-10-17 18:58:40 +02005633 return task;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005634}
5635
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01005636/* This function is an LUA binding that register LUA function to be
5637 * executed after the HAProxy configuration parsing and before the
5638 * HAProxy scheduler starts. This function expect only one LUA
5639 * argument that is a function. This function returns nothing, but
5640 * throws if an error is encountered.
5641 */
5642__LJMP static int hlua_register_init(lua_State *L)
5643{
5644 struct hlua_init_function *init;
5645 int ref;
5646
5647 MAY_LJMP(check_args(L, 1, "register_init"));
5648
5649 ref = MAY_LJMP(hlua_checkfunction(L, 1));
5650
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02005651 init = calloc(1, sizeof(*init));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01005652 if (!init)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01005653 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01005654
5655 init->function_ref = ref;
5656 LIST_ADDQ(&hlua_init_functions, &init->l);
5657 return 0;
5658}
5659
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005660/* This functio is an LUA binding. It permits to register a task
5661 * executed in parallel of the main HAroxy activity. The task is
5662 * created and it is set in the HAProxy scheduler. It can be called
5663 * from the "init" section, "post init" or during the runtime.
5664 *
5665 * Lua prototype:
5666 *
5667 * <none> core.register_task(<function>)
5668 */
5669static int hlua_register_task(lua_State *L)
5670{
5671 struct hlua *hlua;
5672 struct task *task;
5673 int ref;
5674
5675 MAY_LJMP(check_args(L, 1, "register_task"));
5676
5677 ref = MAY_LJMP(hlua_checkfunction(L, 1));
5678
Willy Tarreaubafbe012017-11-24 17:34:44 +01005679 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005680 if (!hlua)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01005681 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005682
Emeric Brunc60def82017-09-27 14:59:38 +02005683 task = task_new(MAX_THREADS_MASK);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005684 task->context = hlua;
5685 task->process = hlua_process_task;
5686
5687 if (!hlua_ctx_init(hlua, task))
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01005688 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005689
5690 /* Restore the function in the stack. */
5691 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref);
5692 hlua->nargs = 0;
5693
5694 /* Schedule task. */
5695 task_schedule(task, now_ms);
5696
5697 return 0;
5698}
5699
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005700/* Wrapper called by HAProxy to execute an LUA converter. This wrapper
5701 * doesn't allow "yield" functions because the HAProxy engine cannot
5702 * resume converters.
5703 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02005704static int hlua_sample_conv_wrapper(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005705{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02005706 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02005707 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01005708 const char *error;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005709
Willy Tarreaube508f12016-03-10 11:47:01 +01005710 if (!stream)
5711 return 0;
5712
Willy Tarreau87b09662015-04-03 00:22:06 +02005713 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01005714 * Lua context can be not initialized. This behavior
5715 * permits to save performances because a systematic
5716 * Lua initialization cause 5% performances loss.
5717 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005718 if (!stream->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01005719 stream->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005720 if (!stream->hlua) {
5721 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
5722 return 0;
5723 }
5724 if (!hlua_ctx_init(stream->hlua, stream->task)) {
5725 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
5726 return 0;
5727 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01005728 }
5729
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005730 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005731 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005732
5733 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005734 if (!SET_SAFE_LJMP(stream->hlua->T)) {
5735 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
5736 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01005737 else
5738 error = "critical error";
5739 SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005740 return 0;
5741 }
5742
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005743 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005744 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005745 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005746 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005747 return 0;
5748 }
5749
5750 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005751 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005752
5753 /* convert input sample and pust-it in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005754 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005755 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005756 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005757 return 0;
5758 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005759 hlua_smp2lua(stream->hlua->T, smp);
5760 stream->hlua->nargs = 1;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005761
5762 /* push keywords in the stack. */
5763 if (arg_p) {
5764 for (; arg_p->type != ARGT_STOP; arg_p++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005765 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005766 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005767 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005768 return 0;
5769 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005770 hlua_arg2lua(stream->hlua->T, arg_p);
5771 stream->hlua->nargs++;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005772 }
5773 }
5774
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005775 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005776 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005777
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005778 /* At this point the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005779 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005780 }
5781
5782 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005783 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005784 /* finished. */
5785 case HLUA_E_OK:
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02005786 /* If the stack is empty, the function fails. */
5787 if (lua_gettop(stream->hlua->T) <= 0)
5788 return 0;
5789
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005790 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005791 hlua_lua2smp(stream->hlua->T, -1, smp);
5792 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005793 return 1;
5794
5795 /* yield. */
5796 case HLUA_E_AGAIN:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005797 SEND_ERR(stream->be, "Lua converter '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005798 return 0;
5799
5800 /* finished with error. */
5801 case HLUA_E_ERRMSG:
5802 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005803 SEND_ERR(stream->be, "Lua converter '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005804 fcn->name, lua_tostring(stream->hlua->T, -1));
5805 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005806 return 0;
5807
Thierry Fournierd5b073c2018-05-21 19:42:47 +02005808 case HLUA_E_ETMOUT:
5809 SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name);
5810 return 0;
5811
5812 case HLUA_E_NOMEM:
5813 SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name);
5814 return 0;
5815
5816 case HLUA_E_YIELD:
5817 SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name);
5818 return 0;
5819
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005820 case HLUA_E_ERR:
5821 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005822 SEND_ERR(stream->be, "Lua converter '%s' returns an unknown error.\n", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005823
5824 default:
5825 return 0;
5826 }
5827}
5828
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005829/* Wrapper called by HAProxy to execute a sample-fetch. this wrapper
5830 * doesn't allow "yield" functions because the HAProxy engine cannot
Willy Tarreaube508f12016-03-10 11:47:01 +01005831 * resume sample-fetches. This function will be called by the sample
5832 * fetch engine to call lua-based fetch operations.
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005833 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +02005834static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp,
5835 const char *kw, void *private)
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005836{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02005837 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02005838 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01005839 const char *error;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005840 const struct chunk msg = { .len = 0 };
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005841
Willy Tarreaube508f12016-03-10 11:47:01 +01005842 if (!stream)
5843 return 0;
5844
Willy Tarreau87b09662015-04-03 00:22:06 +02005845 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01005846 * Lua context can be not initialized. This behavior
5847 * permits to save performances because a systematic
5848 * Lua initialization cause 5% performances loss.
5849 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005850 if (!stream->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01005851 stream->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005852 if (!stream->hlua) {
5853 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
5854 return 0;
5855 }
5856 if (!hlua_ctx_init(stream->hlua, stream->task)) {
5857 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
5858 return 0;
5859 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01005860 }
5861
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005862 consistency_set(stream, smp->opt, &stream->hlua->cons);
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005863
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005864 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005865 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005866
5867 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005868 if (!SET_SAFE_LJMP(stream->hlua->T)) {
5869 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
5870 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01005871 else
5872 error = "critical error";
5873 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005874 return 0;
5875 }
5876
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005877 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005878 if (!lua_checkstack(stream->hlua->T, 2)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005879 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005880 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005881 return 0;
5882 }
5883
5884 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005885 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005886
5887 /* push arguments in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005888 if (!hlua_txn_new(stream->hlua->T, stream, smp->px, smp->opt & SMP_OPT_DIR,
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005889 HLUA_TXN_NOTERM)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005890 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005891 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005892 return 0;
5893 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005894 stream->hlua->nargs = 1;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005895
5896 /* push keywords in the stack. */
5897 for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) {
5898 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005899 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005900 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005901 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005902 return 0;
5903 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005904 hlua_arg2lua(stream->hlua->T, arg_p);
5905 stream->hlua->nargs++;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005906 }
5907
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005908 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005909 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005910
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005911 /* At this point the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005912 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005913 }
5914
5915 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005916 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005917 /* finished. */
5918 case HLUA_E_OK:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005919 if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) {
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005920 stream_int_retnclose(&stream->si[0], &msg);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02005921 return 0;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005922 }
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02005923 /* If the stack is empty, the function fails. */
5924 if (lua_gettop(stream->hlua->T) <= 0)
5925 return 0;
5926
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005927 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005928 hlua_lua2smp(stream->hlua->T, -1, smp);
5929 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005930
5931 /* Set the end of execution flag. */
5932 smp->flags &= ~SMP_F_MAY_CHANGE;
5933 return 1;
5934
5935 /* yield. */
5936 case HLUA_E_AGAIN:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005937 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005938 stream_int_retnclose(&stream->si[0], &msg);
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005939 SEND_ERR(smp->px, "Lua sample-fetch '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005940 return 0;
5941
5942 /* finished with error. */
5943 case HLUA_E_ERRMSG:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005944 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005945 stream_int_retnclose(&stream->si[0], &msg);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005946 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005947 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005948 fcn->name, lua_tostring(stream->hlua->T, -1));
5949 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005950 return 0;
5951
Thierry Fournierd5b073c2018-05-21 19:42:47 +02005952 case HLUA_E_ETMOUT:
5953 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
5954 stream_int_retnclose(&stream->si[0], &msg);
5955 SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name);
5956 return 0;
5957
5958 case HLUA_E_NOMEM:
5959 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
5960 stream_int_retnclose(&stream->si[0], &msg);
5961 SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name);
5962 return 0;
5963
5964 case HLUA_E_YIELD:
5965 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
5966 stream_int_retnclose(&stream->si[0], &msg);
5967 SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name);
5968 return 0;
5969
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005970 case HLUA_E_ERR:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005971 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005972 stream_int_retnclose(&stream->si[0], &msg);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005973 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005974 SEND_ERR(smp->px, "Lua sample-fetch '%s' returns an unknown error.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005975
5976 default:
5977 return 0;
5978 }
5979}
5980
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005981/* This function is an LUA binding used for registering
5982 * "sample-conv" functions. It expects a converter name used
5983 * in the haproxy configuration file, and an LUA function.
5984 */
5985__LJMP static int hlua_register_converters(lua_State *L)
5986{
5987 struct sample_conv_kw_list *sck;
5988 const char *name;
5989 int ref;
5990 int len;
5991 struct hlua_function *fcn;
5992
5993 MAY_LJMP(check_args(L, 2, "register_converters"));
5994
5995 /* First argument : converter name. */
5996 name = MAY_LJMP(luaL_checkstring(L, 1));
5997
5998 /* Second argument : lua function. */
5999 ref = MAY_LJMP(hlua_checkfunction(L, 2));
6000
6001 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006002 sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006003 if (!sck)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006004 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006005 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006006 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006007 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006008
6009 /* Fill fcn. */
6010 fcn->name = strdup(name);
6011 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006012 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006013 fcn->function_ref = ref;
6014
6015 /* List head */
6016 sck->list.n = sck->list.p = NULL;
6017
6018 /* converter keyword. */
6019 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006020 sck->kw[0].kw = calloc(1, len);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006021 if (!sck->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006022 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006023
6024 snprintf((char *)sck->kw[0].kw, len, "lua.%s", name);
6025 sck->kw[0].process = hlua_sample_conv_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01006026 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 +01006027 sck->kw[0].val_args = NULL;
6028 sck->kw[0].in_type = SMP_T_STR;
6029 sck->kw[0].out_type = SMP_T_STR;
6030 sck->kw[0].private = fcn;
6031
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006032 /* Register this new converter */
6033 sample_register_convs(sck);
6034
6035 return 0;
6036}
6037
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006038/* This fucntion is an LUA binding used for registering
6039 * "sample-fetch" functions. It expects a converter name used
6040 * in the haproxy configuration file, and an LUA function.
6041 */
6042__LJMP static int hlua_register_fetches(lua_State *L)
6043{
6044 const char *name;
6045 int ref;
6046 int len;
6047 struct sample_fetch_kw_list *sfk;
6048 struct hlua_function *fcn;
6049
6050 MAY_LJMP(check_args(L, 2, "register_fetches"));
6051
6052 /* First argument : sample-fetch name. */
6053 name = MAY_LJMP(luaL_checkstring(L, 1));
6054
6055 /* Second argument : lua function. */
6056 ref = MAY_LJMP(hlua_checkfunction(L, 2));
6057
6058 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006059 sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006060 if (!sfk)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006061 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006062 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006063 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006064 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006065
6066 /* Fill fcn. */
6067 fcn->name = strdup(name);
6068 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006069 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006070 fcn->function_ref = ref;
6071
6072 /* List head */
6073 sfk->list.n = sfk->list.p = NULL;
6074
6075 /* sample-fetch keyword. */
6076 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006077 sfk->kw[0].kw = calloc(1, len);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006078 if (!sfk->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006079 return luaL_error(L, "Lua out of memory error.");
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006080
6081 snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name);
6082 sfk->kw[0].process = hlua_sample_fetch_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01006083 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 +01006084 sfk->kw[0].val_args = NULL;
6085 sfk->kw[0].out_type = SMP_T_STR;
6086 sfk->kw[0].use = SMP_USE_HTTP_ANY;
6087 sfk->kw[0].val = 0;
6088 sfk->kw[0].private = fcn;
6089
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006090 /* Register this new fetch. */
6091 sample_register_fetches(sfk);
6092
6093 return 0;
6094}
6095
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006096/* This function is a wrapper to execute each LUA function declared
6097 * as an action wrapper during the initialisation period. This function
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006098 * return ACT_RET_CONT if the processing is finished (with or without
6099 * error) and return ACT_RET_YIELD if the function must be called again
6100 * because the LUA returns a yield.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006101 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006102static enum act_return hlua_action(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +02006103 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006104{
6105 char **arg;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006106 unsigned int analyzer;
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02006107 int dir;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006108 const char *error;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006109 const struct chunk msg = { .len = 0 };
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006110
6111 switch (rule->from) {
Thierry FOURNIER6e01f382015-11-02 09:52:54 +01006112 case ACT_F_TCP_REQ_CNT: analyzer = AN_REQ_INSPECT_FE ; dir = SMP_OPT_DIR_REQ; break;
6113 case ACT_F_TCP_RES_CNT: analyzer = AN_RES_INSPECT ; dir = SMP_OPT_DIR_RES; break;
6114 case ACT_F_HTTP_REQ: analyzer = AN_REQ_HTTP_PROCESS_FE; dir = SMP_OPT_DIR_REQ; break;
6115 case ACT_F_HTTP_RES: analyzer = AN_RES_HTTP_PROCESS_BE; dir = SMP_OPT_DIR_RES; break;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006116 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006117 SEND_ERR(px, "Lua: internal error while execute action.\n");
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006118 return ACT_RET_CONT;
6119 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006120
Willy Tarreau87b09662015-04-03 00:22:06 +02006121 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006122 * Lua context can be not initialized. This behavior
6123 * permits to save performances because a systematic
6124 * Lua initialization cause 5% performances loss.
6125 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006126 if (!s->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006127 s->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006128 if (!s->hlua) {
6129 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
6130 rule->arg.hlua_rule->fcn.name);
6131 return ACT_RET_CONT;
6132 }
6133 if (!hlua_ctx_init(s->hlua, s->task)) {
6134 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
6135 rule->arg.hlua_rule->fcn.name);
6136 return ACT_RET_CONT;
6137 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006138 }
6139
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006140 consistency_set(s, dir, &s->hlua->cons);
6141
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006142 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006143 if (!HLUA_IS_RUNNING(s->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006144
6145 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006146 if (!SET_SAFE_LJMP(s->hlua->T)) {
6147 if (lua_type(s->hlua->T, -1) == LUA_TSTRING)
6148 error = lua_tostring(s->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006149 else
6150 error = "critical error";
6151 SEND_ERR(px, "Lua function '%s': %s.\n",
6152 rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006153 return ACT_RET_CONT;
6154 }
6155
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006156 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006157 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006158 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006159 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006160 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006161 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006162 }
6163
6164 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006165 lua_rawgeti(s->hlua->T, LUA_REGISTRYINDEX, rule->arg.hlua_rule->fcn.function_ref);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006166
Willy Tarreau87b09662015-04-03 00:22:06 +02006167 /* Create and and push object stream in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006168 if (!hlua_txn_new(s->hlua->T, s, px, dir, 0)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006169 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006170 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006171 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006172 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006173 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006174 s->hlua->nargs = 1;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006175
6176 /* push keywords in the stack. */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006177 for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006178 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006179 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006180 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006181 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006182 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006183 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006184 lua_pushstring(s->hlua->T, *arg);
6185 s->hlua->nargs++;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006186 }
6187
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006188 /* Now the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006189 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006190
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006191 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006192 s->hlua->max_time = hlua_timeout_session;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006193 }
6194
6195 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006196 switch (hlua_ctx_resume(s->hlua, !(flags & ACT_FLAG_FINAL))) {
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006197 /* finished. */
6198 case HLUA_E_OK:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006199 if (!consistency_check(s, dir, &s->hlua->cons)) {
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006200 stream_int_retnclose(&s->si[0], &msg);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02006201 return ACT_RET_ERR;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006202 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006203 if (s->hlua->flags & HLUA_STOP)
Thierry FOURNIER9bd52d42016-07-14 11:45:33 +02006204 return ACT_RET_STOP;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006205 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006206
6207 /* yield. */
6208 case HLUA_E_AGAIN:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006209 /* Set timeout in the required channel. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006210 if (s->hlua->wake_time != TICK_ETERNITY) {
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006211 if (analyzer & (AN_REQ_INSPECT_FE|AN_REQ_HTTP_PROCESS_FE))
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006212 s->req.analyse_exp = s->hlua->wake_time;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006213 else if (analyzer & (AN_RES_INSPECT|AN_RES_HTTP_PROCESS_BE))
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006214 s->res.analyse_exp = s->hlua->wake_time;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006215 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006216 /* Some actions can be wake up when a "write" event
6217 * is detected on a response channel. This is useful
6218 * only for actions targetted on the requests.
6219 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006220 if (HLUA_IS_WAKERESWR(s->hlua)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006221 s->res.flags |= CF_WAKE_WRITE;
Willy Tarreau76bd97f2015-03-10 17:16:10 +01006222 if ((analyzer & (AN_REQ_INSPECT_FE|AN_REQ_HTTP_PROCESS_FE)))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006223 s->res.analysers |= analyzer;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006224 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006225 if (HLUA_IS_WAKEREQWR(s->hlua))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006226 s->req.flags |= CF_WAKE_WRITE;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006227 /* We can quit the fcuntion without consistency check
6228 * because HAProxy is not able to manipulate data, it
6229 * is only allowed to call me again. */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006230 return ACT_RET_YIELD;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006231
6232 /* finished with error. */
6233 case HLUA_E_ERRMSG:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006234 if (!consistency_check(s, dir, &s->hlua->cons)) {
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006235 stream_int_retnclose(&s->si[0], &msg);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02006236 return ACT_RET_ERR;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006237 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006238 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006239 SEND_ERR(px, "Lua function '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006240 rule->arg.hlua_rule->fcn.name, lua_tostring(s->hlua->T, -1));
6241 lua_pop(s->hlua->T, 1);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006242 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006243
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006244 case HLUA_E_ETMOUT:
6245 if (!consistency_check(s, dir, &s->hlua->cons)) {
6246 stream_int_retnclose(&s->si[0], &msg);
6247 return ACT_RET_ERR;
6248 }
6249 SEND_ERR(px, "Lua function '%s': execution timeout.\n", rule->arg.hlua_rule->fcn.name);
6250 return 0;
6251
6252 case HLUA_E_NOMEM:
6253 if (!consistency_check(s, dir, &s->hlua->cons)) {
6254 stream_int_retnclose(&s->si[0], &msg);
6255 return ACT_RET_ERR;
6256 }
6257 SEND_ERR(px, "Lua function '%s': out of memory error.\n", rule->arg.hlua_rule->fcn.name);
6258 return 0;
6259
6260 case HLUA_E_YIELD:
6261 if (!consistency_check(s, dir, &s->hlua->cons)) {
6262 stream_int_retnclose(&s->si[0], &msg);
6263 return ACT_RET_ERR;
6264 }
6265 SEND_ERR(px, "Lua function '%s': aborting Lua processing on expired timeout.\n",
6266 rule->arg.hlua_rule->fcn.name);
6267 return 0;
6268
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006269 case HLUA_E_ERR:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006270 if (!consistency_check(s, dir, &s->hlua->cons)) {
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006271 stream_int_retnclose(&s->si[0], &msg);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02006272 return ACT_RET_ERR;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006273 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006274 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006275 SEND_ERR(px, "Lua function '%s' return an unknown error.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006276 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006277
6278 default:
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006279 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006280 }
6281}
6282
Olivier Houchard9f6af332018-05-25 14:04:04 +02006283struct task *hlua_applet_wakeup(struct task *t, void *context, unsigned short state)
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006284{
Olivier Houchard9f6af332018-05-25 14:04:04 +02006285 struct appctx *ctx = context;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006286 struct stream_interface *si = ctx->owner;
6287
6288 /* If the applet is wake up without any expected work, the sheduler
6289 * remove it from the run queue. This flag indicate that the applet
6290 * is waiting for write. If the buffer is full, the main processing
6291 * will send some data and after call the applet, otherwise it call
6292 * the applet ASAP.
6293 */
6294 si_applet_cant_put(si);
6295 appctx_wakeup(ctx);
Willy Tarreaud9587412017-08-23 16:07:33 +02006296 t->expire = TICK_ETERNITY;
Willy Tarreaud1aa41f2017-07-21 16:41:56 +02006297 return t;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006298}
6299
6300static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
6301{
6302 struct stream_interface *si = ctx->owner;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006303 struct hlua *hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006304 struct task *task;
6305 char **arg;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006306 const char *error;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006307
Willy Tarreaubafbe012017-11-24 17:34:44 +01006308 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006309 if (!hlua) {
6310 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
6311 ctx->rule->arg.hlua_rule->fcn.name);
6312 return 0;
6313 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006314 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006315 ctx->ctx.hlua_apptcp.hlua = hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006316 ctx->ctx.hlua_apptcp.flags = 0;
6317
6318 /* Create task used by signal to wakeup applets. */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01006319 task = task_new(tid_bit);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006320 if (!task) {
6321 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
6322 ctx->rule->arg.hlua_rule->fcn.name);
6323 return 0;
6324 }
6325 task->nice = 0;
6326 task->context = ctx;
6327 task->process = hlua_applet_wakeup;
6328 ctx->ctx.hlua_apptcp.task = task;
6329
6330 /* In the execution wrappers linked with a stream, the
6331 * Lua context can be not initialized. This behavior
6332 * permits to save performances because a systematic
6333 * Lua initialization cause 5% performances loss.
6334 */
6335 if (!hlua_ctx_init(hlua, task)) {
6336 SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n",
6337 ctx->rule->arg.hlua_rule->fcn.name);
6338 return 0;
6339 }
6340
6341 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02006342 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006343
6344 /* The following Lua calls can fail. */
6345 if (!SET_SAFE_LJMP(hlua->T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01006346 if (lua_type(hlua->T, -1) == LUA_TSTRING)
6347 error = lua_tostring(hlua->T, -1);
6348 else
6349 error = "critical error";
6350 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
6351 ctx->rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006352 RESET_SAFE_LJMP(hlua->T);
6353 return 0;
6354 }
6355
6356 /* Check stack available size. */
6357 if (!lua_checkstack(hlua->T, 1)) {
6358 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
6359 ctx->rule->arg.hlua_rule->fcn.name);
6360 RESET_SAFE_LJMP(hlua->T);
6361 return 0;
6362 }
6363
6364 /* Restore the function in the stack. */
6365 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref);
6366
6367 /* Create and and push object stream in the stack. */
6368 if (!hlua_applet_tcp_new(hlua->T, ctx)) {
6369 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
6370 ctx->rule->arg.hlua_rule->fcn.name);
6371 RESET_SAFE_LJMP(hlua->T);
6372 return 0;
6373 }
6374 hlua->nargs = 1;
6375
6376 /* push keywords in the stack. */
6377 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
6378 if (!lua_checkstack(hlua->T, 1)) {
6379 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
6380 ctx->rule->arg.hlua_rule->fcn.name);
6381 RESET_SAFE_LJMP(hlua->T);
6382 return 0;
6383 }
6384 lua_pushstring(hlua->T, *arg);
6385 hlua->nargs++;
6386 }
6387
6388 RESET_SAFE_LJMP(hlua->T);
6389
6390 /* Wakeup the applet ASAP. */
6391 si_applet_cant_get(si);
6392 si_applet_cant_put(si);
6393
6394 return 1;
6395}
6396
6397static void hlua_applet_tcp_fct(struct appctx *ctx)
6398{
6399 struct stream_interface *si = ctx->owner;
6400 struct stream *strm = si_strm(si);
6401 struct channel *res = si_ic(si);
6402 struct act_rule *rule = ctx->rule;
6403 struct proxy *px = strm->be;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006404 struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006405
6406 /* The applet execution is already done. */
6407 if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE)
6408 return;
6409
6410 /* If the stream is disconnect or closed, ldo nothing. */
6411 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
6412 return;
6413
6414 /* Execute the function. */
6415 switch (hlua_ctx_resume(hlua, 1)) {
6416 /* finished. */
6417 case HLUA_E_OK:
6418 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
6419
6420 /* log time */
6421 strm->logs.tv_request = now;
6422
6423 /* eat the whole request */
Willy Tarreaua79021a2018-06-15 18:07:57 +02006424 co_skip(si_oc(si), co_data(si_oc(si)));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006425 res->flags |= CF_READ_NULL;
6426 si_shutr(si);
6427 return;
6428
6429 /* yield. */
6430 case HLUA_E_AGAIN:
Thierry Fournier0164f202016-02-20 17:47:43 +01006431 if (hlua->wake_time != TICK_ETERNITY)
6432 task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006433 return;
6434
6435 /* finished with error. */
6436 case HLUA_E_ERRMSG:
6437 /* Display log. */
6438 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
6439 rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1));
6440 lua_pop(hlua->T, 1);
6441 goto error;
6442
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006443 case HLUA_E_ETMOUT:
6444 SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n",
6445 rule->arg.hlua_rule->fcn.name);
6446 goto error;
6447
6448 case HLUA_E_NOMEM:
6449 SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n",
6450 rule->arg.hlua_rule->fcn.name);
6451 goto error;
6452
6453 case HLUA_E_YIELD: /* unexpected */
6454 SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n",
6455 rule->arg.hlua_rule->fcn.name);
6456 goto error;
6457
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006458 case HLUA_E_ERR:
6459 /* Display log. */
6460 SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n",
6461 rule->arg.hlua_rule->fcn.name);
6462 goto error;
6463
6464 default:
6465 goto error;
6466 }
6467
6468error:
6469
6470 /* For all other cases, just close the stream. */
6471 si_shutw(si);
6472 si_shutr(si);
6473 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
6474}
6475
6476static void hlua_applet_tcp_release(struct appctx *ctx)
6477{
Willy Tarreaubd7fc952017-07-24 17:35:27 +02006478 task_delete(ctx->ctx.hlua_apptcp.task);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006479 task_free(ctx->ctx.hlua_apptcp.task);
6480 ctx->ctx.hlua_apptcp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006481 hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006482 ctx->ctx.hlua_apptcp.hlua = NULL;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006483}
6484
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006485/* The function returns 1 if the initialisation is complete, 0 if
6486 * an errors occurs and -1 if more data are required for initializing
6487 * the applet.
6488 */
6489static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
6490{
6491 struct stream_interface *si = ctx->owner;
6492 struct channel *req = si_oc(si);
6493 struct http_msg *msg;
6494 struct http_txn *txn;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006495 struct hlua *hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006496 char **arg;
6497 struct hdr_ctx hdr;
6498 struct task *task;
6499 struct sample smp; /* just used for a valid call to smp_prefetch_http. */
Thierry Fournierfd107a22016-02-19 19:57:23 +01006500 const char *error;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006501
6502 /* Wait for a full HTTP request. */
6503 if (!smp_prefetch_http(px, strm, 0, NULL, &smp, 0)) {
6504 if (smp.flags & SMP_F_MAY_CHANGE)
6505 return -1;
6506 return 0;
6507 }
6508 txn = strm->txn;
6509 msg = &txn->req;
6510
Willy Tarreau0078bfc2015-10-07 20:20:28 +02006511 /* We want two things in HTTP mode :
6512 * - enforce server-close mode if we were in keep-alive, so that the
6513 * applet is released after each response ;
6514 * - enable request body transfer to the applet in order to resync
6515 * with the response body.
6516 */
6517 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)
6518 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreau0078bfc2015-10-07 20:20:28 +02006519
Willy Tarreaubafbe012017-11-24 17:34:44 +01006520 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006521 if (!hlua) {
6522 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
6523 ctx->rule->arg.hlua_rule->fcn.name);
6524 return 0;
6525 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006526 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006527 ctx->ctx.hlua_apphttp.hlua = hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006528 ctx->ctx.hlua_apphttp.left_bytes = -1;
6529 ctx->ctx.hlua_apphttp.flags = 0;
6530
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +01006531 if (txn->req.flags & HTTP_MSGF_VER_11)
6532 ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11;
6533
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006534 /* Create task used by signal to wakeup applets. */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01006535 task = task_new(tid_bit);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006536 if (!task) {
6537 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
6538 ctx->rule->arg.hlua_rule->fcn.name);
6539 return 0;
6540 }
6541 task->nice = 0;
6542 task->context = ctx;
6543 task->process = hlua_applet_wakeup;
6544 ctx->ctx.hlua_apphttp.task = task;
6545
6546 /* In the execution wrappers linked with a stream, the
6547 * Lua context can be not initialized. This behavior
6548 * permits to save performances because a systematic
6549 * Lua initialization cause 5% performances loss.
6550 */
6551 if (!hlua_ctx_init(hlua, task)) {
6552 SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n",
6553 ctx->rule->arg.hlua_rule->fcn.name);
6554 return 0;
6555 }
6556
6557 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02006558 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006559
6560 /* The following Lua calls can fail. */
6561 if (!SET_SAFE_LJMP(hlua->T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01006562 if (lua_type(hlua->T, -1) == LUA_TSTRING)
6563 error = lua_tostring(hlua->T, -1);
6564 else
6565 error = "critical error";
6566 SEND_ERR(px, "Lua applet http '%s': %s.\n",
6567 ctx->rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006568 return 0;
6569 }
6570
6571 /* Check stack available size. */
6572 if (!lua_checkstack(hlua->T, 1)) {
6573 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
6574 ctx->rule->arg.hlua_rule->fcn.name);
6575 RESET_SAFE_LJMP(hlua->T);
6576 return 0;
6577 }
6578
6579 /* Restore the function in the stack. */
6580 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref);
6581
6582 /* Create and and push object stream in the stack. */
6583 if (!hlua_applet_http_new(hlua->T, ctx)) {
6584 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
6585 ctx->rule->arg.hlua_rule->fcn.name);
6586 RESET_SAFE_LJMP(hlua->T);
6587 return 0;
6588 }
6589 hlua->nargs = 1;
6590
6591 /* Look for a 100-continue expected. */
6592 if (msg->flags & HTTP_MSGF_VER_11) {
6593 hdr.idx = 0;
Willy Tarreaua79021a2018-06-15 18:07:57 +02006594 if (http_find_header2("Expect", 6, ci_head(req), &txn->hdr_idx, &hdr) &&
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006595 unlikely(hdr.vlen == 12 && strncasecmp(hdr.line+hdr.val, "100-continue", 12) == 0))
6596 ctx->ctx.hlua_apphttp.flags |= APPLET_100C;
6597 }
6598
6599 /* push keywords in the stack. */
6600 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
6601 if (!lua_checkstack(hlua->T, 1)) {
6602 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
6603 ctx->rule->arg.hlua_rule->fcn.name);
6604 RESET_SAFE_LJMP(hlua->T);
6605 return 0;
6606 }
6607 lua_pushstring(hlua->T, *arg);
6608 hlua->nargs++;
6609 }
6610
6611 RESET_SAFE_LJMP(hlua->T);
6612
6613 /* Wakeup the applet when data is ready for read. */
6614 si_applet_cant_get(si);
6615
6616 return 1;
6617}
6618
6619static void hlua_applet_http_fct(struct appctx *ctx)
6620{
6621 struct stream_interface *si = ctx->owner;
6622 struct stream *strm = si_strm(si);
6623 struct channel *res = si_ic(si);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006624 struct act_rule *rule = ctx->rule;
6625 struct proxy *px = strm->be;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006626 struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua;
Willy Tarreau206ba832018-06-14 15:27:31 +02006627 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02006628 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02006629 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02006630 size_t len2;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006631 int ret;
6632
6633 /* If the stream is disconnect or closed, ldo nothing. */
6634 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
6635 return;
6636
6637 /* Set the currently running flag. */
6638 if (!HLUA_IS_RUNNING(hlua) &&
6639 !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
6640
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006641 /* Wait for full HTTP analysys. */
6642 if (unlikely(strm->txn->req.msg_state < HTTP_MSG_BODY)) {
6643 si_applet_cant_get(si);
6644 return;
6645 }
6646
6647 /* Store the max amount of bytes that we can read. */
6648 ctx->ctx.hlua_apphttp.left_bytes = strm->txn->req.body_len;
6649
6650 /* We need to flush the request header. This left the body
6651 * for the Lua.
6652 */
6653
6654 /* Read the maximum amount of data avalaible. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02006655 ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006656 if (ret == -1)
6657 return;
6658
6659 /* No data available, ask for more data. */
6660 if (ret == 1)
6661 len2 = 0;
6662 if (ret == 0)
6663 len1 = 0;
Thierry FOURNIER70d318c2018-06-30 10:37:33 +02006664 if (len1 + len2 < strm->txn->req.eoh + strm->txn->req.eol) {
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006665 si_applet_cant_get(si);
6666 return;
6667 }
6668
6669 /* skip the requests bytes. */
Thierry FOURNIER70d318c2018-06-30 10:37:33 +02006670 co_skip(si_oc(si), strm->txn->req.eoh + strm->txn->req.eol);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006671 }
6672
6673 /* Executes The applet if it is not done. */
6674 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
6675
6676 /* Execute the function. */
6677 switch (hlua_ctx_resume(hlua, 1)) {
6678 /* finished. */
6679 case HLUA_E_OK:
6680 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
6681 break;
6682
6683 /* yield. */
6684 case HLUA_E_AGAIN:
Thierry Fournier0164f202016-02-20 17:47:43 +01006685 if (hlua->wake_time != TICK_ETERNITY)
6686 task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006687 return;
6688
6689 /* finished with error. */
6690 case HLUA_E_ERRMSG:
6691 /* Display log. */
6692 SEND_ERR(px, "Lua applet http '%s': %s.\n",
6693 rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1));
6694 lua_pop(hlua->T, 1);
6695 goto error;
6696
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006697 case HLUA_E_ETMOUT:
6698 SEND_ERR(px, "Lua applet http '%s': execution timeout.\n",
6699 rule->arg.hlua_rule->fcn.name);
6700 goto error;
6701
6702 case HLUA_E_NOMEM:
6703 SEND_ERR(px, "Lua applet http '%s': out of memory error.\n",
6704 rule->arg.hlua_rule->fcn.name);
6705 goto error;
6706
6707 case HLUA_E_YIELD: /* unexpected */
6708 SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n",
6709 rule->arg.hlua_rule->fcn.name);
6710 goto error;
6711
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006712 case HLUA_E_ERR:
6713 /* Display log. */
6714 SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n",
6715 rule->arg.hlua_rule->fcn.name);
6716 goto error;
6717
6718 default:
6719 goto error;
6720 }
6721 }
6722
6723 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
6724
6725 /* We must send the final chunk. */
6726 if (ctx->ctx.hlua_apphttp.flags & APPLET_CHUNKED &&
6727 !(ctx->ctx.hlua_apphttp.flags & APPLET_LAST_CHK)) {
6728
6729 /* sent last chunk at once. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02006730 ret = ci_putblk(res, "0\r\n\r\n", 5);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006731
6732 /* critical error. */
6733 if (ret == -2 || ret == -3) {
6734 SEND_ERR(px, "Lua applet http '%s'cannont send last chunk.\n",
6735 rule->arg.hlua_rule->fcn.name);
6736 goto error;
6737 }
6738
6739 /* no enough space error. */
6740 if (ret == -1) {
6741 si_applet_cant_put(si);
6742 return;
6743 }
6744
6745 /* set the last chunk sent. */
6746 ctx->ctx.hlua_apphttp.flags |= APPLET_LAST_CHK;
6747 }
6748
6749 /* close the connection. */
6750
6751 /* status / log */
6752 strm->txn->status = ctx->ctx.hlua_apphttp.status;
6753 strm->logs.tv_request = now;
6754
6755 /* eat the whole request */
Willy Tarreaua79021a2018-06-15 18:07:57 +02006756 co_skip(si_oc(si), co_data(si_oc(si)));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006757 res->flags |= CF_READ_NULL;
6758 si_shutr(si);
6759
6760 return;
6761 }
6762
6763error:
6764
6765 /* If we are in HTTP mode, and we are not send any
6766 * data, return a 500 server error in best effort:
6767 * if there are no room avalaible in the buffer,
6768 * just close the connection.
6769 */
Willy Tarreau06d80a92017-10-19 14:32:15 +02006770 ci_putblk(res, error_500, strlen(error_500));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006771 if (!(strm->flags & SF_ERR_MASK))
6772 strm->flags |= SF_ERR_RESOURCE;
6773 si_shutw(si);
6774 si_shutr(si);
6775 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
6776}
6777
6778static void hlua_applet_http_release(struct appctx *ctx)
6779{
Willy Tarreaubd7fc952017-07-24 17:35:27 +02006780 task_delete(ctx->ctx.hlua_apphttp.task);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006781 task_free(ctx->ctx.hlua_apphttp.task);
6782 ctx->ctx.hlua_apphttp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006783 hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006784 ctx->ctx.hlua_apphttp.hlua = NULL;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006785}
6786
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006787/* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in
6788 * succes case, else return ACT_RET_PRS_ERR.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006789 *
6790 * This function can fail with an abort() due to an Lua critical error.
6791 * We are in the configuration parsing process of HAProxy, this abort() is
6792 * tolerated.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006793 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006794static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px,
6795 struct act_rule *rule, char **err)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006796{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006797 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006798 int i;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006799
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006800 /* Memory for the rule. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006801 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006802 if (!rule->arg.hlua_rule) {
6803 memprintf(err, "out of memory error");
Thierry FOURNIERafa80492015-08-19 09:04:15 +02006804 return ACT_RET_PRS_ERR;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006805 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006806
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006807 /* Memory for arguments. */
6808 rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, sizeof(char *));
6809 if (!rule->arg.hlua_rule->args) {
6810 memprintf(err, "out of memory error");
6811 return ACT_RET_PRS_ERR;
6812 }
6813
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006814 /* Reference the Lua function and store the reference. */
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006815 rule->arg.hlua_rule->fcn = *fcn;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006816
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006817 /* Expect some arguments */
6818 for (i = 0; i < fcn->nargs; i++) {
6819 if (*args[i+1] == '\0') {
6820 memprintf(err, "expect %d arguments", fcn->nargs);
6821 return ACT_RET_PRS_ERR;
6822 }
6823 rule->arg.hlua_rule->args[i] = strdup(args[i + 1]);
6824 if (!rule->arg.hlua_rule->args[i]) {
6825 memprintf(err, "out of memory error");
6826 return ACT_RET_PRS_ERR;
6827 }
6828 (*cur_arg)++;
6829 }
6830 rule->arg.hlua_rule->args[i] = NULL;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006831
Thierry FOURNIER42148732015-09-02 17:17:33 +02006832 rule->action = ACT_CUSTOM;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006833 rule->action_ptr = hlua_action;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02006834 return ACT_RET_PRS_OK;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006835}
6836
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006837static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px,
6838 struct act_rule *rule, char **err)
6839{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006840 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006841
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01006842 /* HTTP applets are forbidden in tcp-request rules.
6843 * HTTP applet request requires everything initilized by
6844 * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER).
6845 * The applet will be immediately initilized, but its before
6846 * the call of this analyzer.
6847 */
6848 if (rule->from != ACT_F_HTTP_REQ) {
6849 memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets");
6850 return ACT_RET_PRS_ERR;
6851 }
6852
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006853 /* Memory for the rule. */
6854 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
6855 if (!rule->arg.hlua_rule) {
6856 memprintf(err, "out of memory error");
6857 return ACT_RET_PRS_ERR;
6858 }
6859
6860 /* Reference the Lua function and store the reference. */
6861 rule->arg.hlua_rule->fcn = *fcn;
6862
6863 /* TODO: later accept arguments. */
6864 rule->arg.hlua_rule->args = NULL;
6865
6866 /* Add applet pointer in the rule. */
6867 rule->applet.obj_type = OBJ_TYPE_APPLET;
6868 rule->applet.name = fcn->name;
6869 rule->applet.init = hlua_applet_http_init;
6870 rule->applet.fct = hlua_applet_http_fct;
6871 rule->applet.release = hlua_applet_http_release;
6872 rule->applet.timeout = hlua_timeout_applet;
6873
6874 return ACT_RET_PRS_OK;
6875}
6876
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006877/* This function is an LUA binding used for registering
6878 * "sample-conv" functions. It expects a converter name used
6879 * in the haproxy configuration file, and an LUA function.
6880 */
6881__LJMP static int hlua_register_action(lua_State *L)
6882{
6883 struct action_kw_list *akl;
6884 const char *name;
6885 int ref;
6886 int len;
6887 struct hlua_function *fcn;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006888 int nargs;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006889
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006890 /* Initialise the number of expected arguments at 0. */
6891 nargs = 0;
6892
6893 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
6894 WILL_LJMP(luaL_error(L, "'register_action' needs between 3 and 4 arguments"));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006895
6896 /* First argument : converter name. */
6897 name = MAY_LJMP(luaL_checkstring(L, 1));
6898
6899 /* Second argument : environment. */
6900 if (lua_type(L, 2) != LUA_TTABLE)
6901 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
6902
6903 /* Third argument : lua function. */
6904 ref = MAY_LJMP(hlua_checkfunction(L, 3));
6905
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006906 /* Fouth argument : number of mandatories arguments expected on the configuration line. */
6907 if (lua_gettop(L) >= 4)
6908 nargs = MAY_LJMP(luaL_checkinteger(L, 4));
6909
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006910 /* browse the second argulent as an array. */
6911 lua_pushnil(L);
6912 while (lua_next(L, 2) != 0) {
6913 if (lua_type(L, -1) != LUA_TSTRING)
6914 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
6915
6916 /* Check required environment. Only accepted "http" or "tcp". */
6917 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006918 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006919 if (!akl)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006920 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006921 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006922 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006923 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006924
6925 /* Fill fcn. */
6926 fcn->name = strdup(name);
6927 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006928 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006929 fcn->function_ref = ref;
6930
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006931 /* Set the expected number od arguments. */
6932 fcn->nargs = nargs;
6933
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006934 /* List head */
6935 akl->list.n = akl->list.p = NULL;
6936
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006937 /* action keyword. */
6938 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006939 akl->kw[0].kw = calloc(1, len);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006940 if (!akl->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006941 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006942
6943 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
6944
6945 akl->kw[0].match_pfx = 0;
6946 akl->kw[0].private = fcn;
6947 akl->kw[0].parse = action_register_lua;
6948
6949 /* select the action registering point. */
6950 if (strcmp(lua_tostring(L, -1), "tcp-req") == 0)
6951 tcp_req_cont_keywords_register(akl);
6952 else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0)
6953 tcp_res_cont_keywords_register(akl);
6954 else if (strcmp(lua_tostring(L, -1), "http-req") == 0)
6955 http_req_keywords_register(akl);
6956 else if (strcmp(lua_tostring(L, -1), "http-res") == 0)
6957 http_res_keywords_register(akl);
6958 else
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006959 WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. "
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006960 "'tcp-req', 'tcp-res', 'http-req' or 'http-res' "
6961 "are expected.", lua_tostring(L, -1)));
6962
6963 /* pop the environment string. */
6964 lua_pop(L, 1);
6965 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006966 return ACT_RET_PRS_OK;
6967}
6968
6969static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px,
6970 struct act_rule *rule, char **err)
6971{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006972 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006973
6974 /* Memory for the rule. */
6975 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
6976 if (!rule->arg.hlua_rule) {
6977 memprintf(err, "out of memory error");
6978 return ACT_RET_PRS_ERR;
6979 }
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006980
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006981 /* Reference the Lua function and store the reference. */
6982 rule->arg.hlua_rule->fcn = *fcn;
6983
6984 /* TODO: later accept arguments. */
6985 rule->arg.hlua_rule->args = NULL;
6986
6987 /* Add applet pointer in the rule. */
6988 rule->applet.obj_type = OBJ_TYPE_APPLET;
6989 rule->applet.name = fcn->name;
6990 rule->applet.init = hlua_applet_tcp_init;
6991 rule->applet.fct = hlua_applet_tcp_fct;
6992 rule->applet.release = hlua_applet_tcp_release;
6993 rule->applet.timeout = hlua_timeout_applet;
6994
6995 return 0;
6996}
6997
6998/* This function is an LUA binding used for registering
6999 * "sample-conv" functions. It expects a converter name used
7000 * in the haproxy configuration file, and an LUA function.
7001 */
7002__LJMP static int hlua_register_service(lua_State *L)
7003{
7004 struct action_kw_list *akl;
7005 const char *name;
7006 const char *env;
7007 int ref;
7008 int len;
7009 struct hlua_function *fcn;
7010
7011 MAY_LJMP(check_args(L, 3, "register_service"));
7012
7013 /* First argument : converter name. */
7014 name = MAY_LJMP(luaL_checkstring(L, 1));
7015
7016 /* Second argument : environment. */
7017 env = MAY_LJMP(luaL_checkstring(L, 2));
7018
7019 /* Third argument : lua function. */
7020 ref = MAY_LJMP(hlua_checkfunction(L, 3));
7021
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007022 /* Allocate and fill the sample fetch keyword struct. */
7023 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
7024 if (!akl)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007025 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007026 fcn = calloc(1, sizeof(*fcn));
7027 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007028 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007029
7030 /* Fill fcn. */
7031 len = strlen("<lua.>") + strlen(name) + 1;
7032 fcn->name = calloc(1, len);
7033 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007034 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007035 snprintf((char *)fcn->name, len, "<lua.%s>", name);
7036 fcn->function_ref = ref;
7037
7038 /* List head */
7039 akl->list.n = akl->list.p = NULL;
7040
7041 /* converter keyword. */
7042 len = strlen("lua.") + strlen(name) + 1;
7043 akl->kw[0].kw = calloc(1, len);
7044 if (!akl->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007045 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007046
7047 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
7048
Thierry FOURNIER / OZON.IO02564fd2016-11-12 11:07:05 +01007049 /* Check required environment. Only accepted "http" or "tcp". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007050 if (strcmp(env, "tcp") == 0)
7051 akl->kw[0].parse = action_register_service_tcp;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007052 else if (strcmp(env, "http") == 0)
7053 akl->kw[0].parse = action_register_service_http;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007054 else
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007055 WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. "
Eric Salamafe7456f2017-12-21 14:30:07 +01007056 "'tcp' or 'http' are expected.", env));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007057
7058 akl->kw[0].match_pfx = 0;
7059 akl->kw[0].private = fcn;
7060
7061 /* End of array. */
7062 memset(&akl->kw[1], 0, sizeof(*akl->kw));
7063
7064 /* Register this new converter */
7065 service_keywords_register(akl);
7066
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007067 return 0;
7068}
7069
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007070/* This function initialises Lua cli handler. It copies the
7071 * arguments in the Lua stack and create channel IO objects.
7072 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02007073static int hlua_cli_parse_fct(char **args, char *payload, struct appctx *appctx, void *private)
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007074{
7075 struct hlua *hlua;
7076 struct hlua_function *fcn;
7077 int i;
7078 const char *error;
7079
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007080 fcn = private;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007081 appctx->ctx.hlua_cli.fcn = private;
7082
Willy Tarreaubafbe012017-11-24 17:34:44 +01007083 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007084 if (!hlua) {
7085 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007086 return 1;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007087 }
7088 HLUA_INIT(hlua);
7089 appctx->ctx.hlua_cli.hlua = hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007090
7091 /* Create task used by signal to wakeup applets.
7092 * We use the same wakeup fonction than the Lua applet_tcp and
7093 * applet_http. It is absolutely compatible.
7094 */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01007095 appctx->ctx.hlua_cli.task = task_new(tid_bit);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007096 if (!appctx->ctx.hlua_cli.task) {
Thierry FOURNIERffbf5692016-12-16 11:14:06 +01007097 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007098 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007099 }
7100 appctx->ctx.hlua_cli.task->nice = 0;
7101 appctx->ctx.hlua_cli.task->context = appctx;
7102 appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup;
7103
7104 /* Initialises the Lua context */
7105 if (!hlua_ctx_init(hlua, appctx->ctx.hlua_cli.task)) {
7106 SEND_ERR(NULL, "Lua cli '%s': can't initialize Lua context.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007107 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007108 }
7109
7110 /* The following Lua calls can fail. */
7111 if (!SET_SAFE_LJMP(hlua->T)) {
7112 if (lua_type(hlua->T, -1) == LUA_TSTRING)
7113 error = lua_tostring(hlua->T, -1);
7114 else
7115 error = "critical error";
7116 SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error);
7117 goto error;
7118 }
7119
7120 /* Check stack available size. */
7121 if (!lua_checkstack(hlua->T, 2)) {
7122 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7123 goto error;
7124 }
7125
7126 /* Restore the function in the stack. */
7127 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
7128
7129 /* Once the arguments parsed, the CLI is like an AppletTCP,
7130 * so push AppletTCP in the stack.
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007131 */
7132 if (!hlua_applet_tcp_new(hlua->T, appctx)) {
7133 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7134 goto error;
7135 }
7136 hlua->nargs = 1;
7137
7138 /* push keywords in the stack. */
7139 for (i = 0; *args[i]; i++) {
7140 /* Check stack available size. */
7141 if (!lua_checkstack(hlua->T, 1)) {
7142 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7143 goto error;
7144 }
7145 lua_pushstring(hlua->T, args[i]);
7146 hlua->nargs++;
7147 }
7148
7149 /* We must initialize the execution timeouts. */
7150 hlua->max_time = hlua_timeout_session;
7151
7152 /* At this point the execution is safe. */
7153 RESET_SAFE_LJMP(hlua->T);
7154
7155 /* It's ok */
7156 return 0;
7157
7158 /* It's not ok. */
7159error:
7160 RESET_SAFE_LJMP(hlua->T);
7161 hlua_ctx_destroy(hlua);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007162 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007163 return 1;
7164}
7165
7166static int hlua_cli_io_handler_fct(struct appctx *appctx)
7167{
7168 struct hlua *hlua;
7169 struct stream_interface *si;
7170 struct hlua_function *fcn;
7171
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007172 hlua = appctx->ctx.hlua_cli.hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007173 si = appctx->owner;
Willy Tarreau8ae4f752016-12-14 15:41:45 +01007174 fcn = appctx->ctx.hlua_cli.fcn;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007175
7176 /* If the stream is disconnect or closed, ldo nothing. */
7177 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
7178 return 1;
7179
7180 /* Execute the function. */
7181 switch (hlua_ctx_resume(hlua, 1)) {
7182
7183 /* finished. */
7184 case HLUA_E_OK:
7185 return 1;
7186
7187 /* yield. */
7188 case HLUA_E_AGAIN:
7189 /* We want write. */
7190 if (HLUA_IS_WAKERESWR(hlua))
7191 si_applet_cant_put(si);
7192 /* Set the timeout. */
7193 if (hlua->wake_time != TICK_ETERNITY)
7194 task_schedule(hlua->task, hlua->wake_time);
7195 return 0;
7196
7197 /* finished with error. */
7198 case HLUA_E_ERRMSG:
7199 /* Display log. */
7200 SEND_ERR(NULL, "Lua cli '%s': %s.\n",
7201 fcn->name, lua_tostring(hlua->T, -1));
7202 lua_pop(hlua->T, 1);
7203 return 1;
7204
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007205 case HLUA_E_ETMOUT:
7206 SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n",
7207 fcn->name);
7208 return 1;
7209
7210 case HLUA_E_NOMEM:
7211 SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n",
7212 fcn->name);
7213 return 1;
7214
7215 case HLUA_E_YIELD: /* unexpected */
7216 SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n",
7217 fcn->name);
7218 return 1;
7219
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007220 case HLUA_E_ERR:
7221 /* Display log. */
7222 SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n",
7223 fcn->name);
7224 return 1;
7225
7226 default:
7227 return 1;
7228 }
7229
7230 return 1;
7231}
7232
7233static void hlua_cli_io_release_fct(struct appctx *appctx)
7234{
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007235 hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007236 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007237}
7238
7239/* This function is an LUA binding used for registering
7240 * new keywords in the cli. It expects a list of keywords
7241 * which are the "path". It is limited to 5 keywords. A
7242 * description of the command, a function to be executed
7243 * for the parsing and a function for io handlers.
7244 */
7245__LJMP static int hlua_register_cli(lua_State *L)
7246{
7247 struct cli_kw_list *cli_kws;
7248 const char *message;
7249 int ref_io;
7250 int len;
7251 struct hlua_function *fcn;
7252 int index;
7253 int i;
7254
7255 MAY_LJMP(check_args(L, 3, "register_cli"));
7256
7257 /* First argument : an array of maximum 5 keywords. */
7258 if (!lua_istable(L, 1))
7259 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table"));
7260
7261 /* Second argument : string with contextual message. */
7262 message = MAY_LJMP(luaL_checkstring(L, 2));
7263
7264 /* Third and fourth argument : lua function. */
7265 ref_io = MAY_LJMP(hlua_checkfunction(L, 3));
7266
7267 /* Allocate and fill the sample fetch keyword struct. */
7268 cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2);
7269 if (!cli_kws)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007270 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007271 fcn = calloc(1, sizeof(*fcn));
7272 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007273 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007274
7275 /* Fill path. */
7276 index = 0;
7277 lua_pushnil(L);
7278 while(lua_next(L, 1) != 0) {
7279 if (index >= 5)
7280 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries"));
7281 if (lua_type(L, -1) != LUA_TSTRING)
7282 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings"));
7283 cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1));
7284 if (!cli_kws->kw[0].str_kw[index])
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007285 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007286 index++;
7287 lua_pop(L, 1);
7288 }
7289
7290 /* Copy help message. */
7291 cli_kws->kw[0].usage = strdup(message);
7292 if (!cli_kws->kw[0].usage)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007293 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007294
7295 /* Fill fcn io handler. */
7296 len = strlen("<lua.cli>") + 1;
7297 for (i = 0; i < index; i++)
7298 len += strlen(cli_kws->kw[0].str_kw[i]) + 1;
7299 fcn->name = calloc(1, len);
7300 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007301 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007302 strncat((char *)fcn->name, "<lua.cli", len);
7303 for (i = 0; i < index; i++) {
7304 strncat((char *)fcn->name, ".", len);
7305 strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len);
7306 }
7307 strncat((char *)fcn->name, ">", len);
7308 fcn->function_ref = ref_io;
7309
7310 /* Fill last entries. */
7311 cli_kws->kw[0].private = fcn;
7312 cli_kws->kw[0].parse = hlua_cli_parse_fct;
7313 cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct;
7314 cli_kws->kw[0].io_release = hlua_cli_io_release_fct;
7315
7316 /* Register this new converter */
7317 cli_register_kw(cli_kws);
7318
7319 return 0;
7320}
7321
Thierry FOURNIERbd413492015-03-03 16:52:26 +01007322static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx,
7323 struct proxy *defpx, const char *file, int line,
7324 char **err, unsigned int *timeout)
7325{
7326 const char *error;
7327
7328 error = parse_time_err(args[1], timeout, TIME_UNIT_MS);
7329 if (error && *error != '\0') {
7330 memprintf(err, "%s: invalid timeout", args[0]);
7331 return -1;
7332 }
7333 return 0;
7334}
7335
7336static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx,
7337 struct proxy *defpx, const char *file, int line,
7338 char **err)
7339{
7340 return hlua_read_timeout(args, section_type, curpx, defpx,
7341 file, line, err, &hlua_timeout_session);
7342}
7343
7344static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx,
7345 struct proxy *defpx, const char *file, int line,
7346 char **err)
7347{
7348 return hlua_read_timeout(args, section_type, curpx, defpx,
7349 file, line, err, &hlua_timeout_task);
7350}
7351
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007352static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx,
7353 struct proxy *defpx, const char *file, int line,
7354 char **err)
7355{
7356 return hlua_read_timeout(args, section_type, curpx, defpx,
7357 file, line, err, &hlua_timeout_applet);
7358}
7359
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01007360static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx,
7361 struct proxy *defpx, const char *file, int line,
7362 char **err)
7363{
7364 char *error;
7365
7366 hlua_nb_instruction = strtoll(args[1], &error, 10);
7367 if (*error != '\0') {
7368 memprintf(err, "%s: invalid number", args[0]);
7369 return -1;
7370 }
7371 return 0;
7372}
7373
Willy Tarreau32f61e22015-03-18 17:54:59 +01007374static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx,
7375 struct proxy *defpx, const char *file, int line,
7376 char **err)
7377{
7378 char *error;
7379
7380 if (*(args[1]) == 0) {
7381 memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]);
7382 return -1;
7383 }
7384 hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L;
7385 if (*error != '\0') {
7386 memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error);
7387 return -1;
7388 }
7389 return 0;
7390}
7391
7392
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007393/* This function is called by the main configuration key "lua-load". It loads and
7394 * execute an lua file during the parsing of the HAProxy configuration file. It is
7395 * the main lua entry point.
7396 *
7397 * This funtion runs with the HAProxy keywords API. It returns -1 if an error is
7398 * occured, otherwise it returns 0.
7399 *
7400 * In some error case, LUA set an error message in top of the stack. This function
7401 * returns this error message in the HAProxy logs and pop it from the stack.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007402 *
7403 * This function can fail with an abort() due to an Lua critical error.
7404 * We are in the configuration parsing process of HAProxy, this abort() is
7405 * tolerated.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007406 */
7407static int hlua_load(char **args, int section_type, struct proxy *curpx,
7408 struct proxy *defpx, const char *file, int line,
7409 char **err)
7410{
7411 int error;
7412
7413 /* Just load and compile the file. */
7414 error = luaL_loadfile(gL.T, args[1]);
7415 if (error) {
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007416 memprintf(err, "error in Lua file '%s': %s", args[1], lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007417 lua_pop(gL.T, 1);
7418 return -1;
7419 }
7420
7421 /* If no syntax error where detected, execute the code. */
7422 error = lua_pcall(gL.T, 0, LUA_MULTRET, 0);
7423 switch (error) {
7424 case LUA_OK:
7425 break;
7426 case LUA_ERRRUN:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007427 memprintf(err, "Lua runtime error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007428 lua_pop(gL.T, 1);
7429 return -1;
7430 case LUA_ERRMEM:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007431 memprintf(err, "Lua out of memory error.n");
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007432 return -1;
7433 case LUA_ERRERR:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007434 memprintf(err, "Lua message handler error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007435 lua_pop(gL.T, 1);
7436 return -1;
7437 case LUA_ERRGCMM:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007438 memprintf(err, "Lua garbage collector error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007439 lua_pop(gL.T, 1);
7440 return -1;
7441 default:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007442 memprintf(err, "Lua unknonwn error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007443 lua_pop(gL.T, 1);
7444 return -1;
7445 }
7446
7447 return 0;
7448}
7449
7450/* configuration keywords declaration */
7451static struct cfg_kw_list cfg_kws = {{ },{
Thierry FOURNIERbd413492015-03-03 16:52:26 +01007452 { CFG_GLOBAL, "lua-load", hlua_load },
7453 { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout },
7454 { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout },
Thierry FOURNIER56da1012015-10-01 08:42:31 +02007455 { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout },
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01007456 { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield },
Willy Tarreau32f61e22015-03-18 17:54:59 +01007457 { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem },
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007458 { 0, NULL, NULL },
7459}};
7460
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007461/* This function can fail with an abort() due to an Lua critical error.
7462 * We are in the initialisation process of HAProxy, this abort() is
7463 * tolerated.
7464 */
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007465int hlua_post_init()
7466{
7467 struct hlua_init_function *init;
7468 const char *msg;
7469 enum hlua_exec ret;
Thierry Fournierfd107a22016-02-19 19:57:23 +01007470 const char *error;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007471
Thierry Fournier3d4a6752016-02-19 20:53:30 +01007472 /* Call post initialisation function in safe environement. */
7473 if (!SET_SAFE_LJMP(gL.T)) {
7474 if (lua_type(gL.T, -1) == LUA_TSTRING)
7475 error = lua_tostring(gL.T, -1);
7476 else
7477 error = "critical error";
7478 fprintf(stderr, "Lua post-init: %s.\n", error);
7479 exit(1);
7480 }
7481 hlua_fcn_post_init(gL.T);
7482 RESET_SAFE_LJMP(gL.T);
7483
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007484 list_for_each_entry(init, &hlua_init_functions, l) {
7485 lua_rawgeti(gL.T, LUA_REGISTRYINDEX, init->function_ref);
7486 ret = hlua_ctx_resume(&gL, 0);
7487 switch (ret) {
7488 case HLUA_E_OK:
7489 lua_pop(gL.T, -1);
7490 return 1;
7491 case HLUA_E_AGAIN:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007492 ha_alert("Lua init: yield not allowed.\n");
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007493 return 0;
7494 case HLUA_E_ERRMSG:
7495 msg = lua_tostring(gL.T, -1);
Christopher Faulet767a84b2017-11-24 16:50:31 +01007496 ha_alert("lua init: %s.\n", msg);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007497 return 0;
7498 case HLUA_E_ERR:
7499 default:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007500 ha_alert("Lua init: unknown runtime error.\n");
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007501 return 0;
7502 }
7503 }
7504 return 1;
7505}
7506
Willy Tarreau32f61e22015-03-18 17:54:59 +01007507/* The memory allocator used by the Lua stack. <ud> is a pointer to the
7508 * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize>
7509 * is the previously allocated size or the kind of object in case of a new
7510 * allocation. <nsize> is the requested new size.
7511 */
7512static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
7513{
7514 struct hlua_mem_allocator *zone = ud;
7515
7516 if (nsize == 0) {
7517 /* it's a free */
7518 if (ptr)
7519 zone->allocated -= osize;
7520 free(ptr);
7521 return NULL;
7522 }
7523
7524 if (!ptr) {
7525 /* it's a new allocation */
7526 if (zone->limit && zone->allocated + nsize > zone->limit)
7527 return NULL;
7528
7529 ptr = malloc(nsize);
7530 if (ptr)
7531 zone->allocated += nsize;
7532 return ptr;
7533 }
7534
7535 /* it's a realloc */
7536 if (zone->limit && zone->allocated + nsize - osize > zone->limit)
7537 return NULL;
7538
7539 ptr = realloc(ptr, nsize);
7540 if (ptr)
7541 zone->allocated += nsize - osize;
7542 return ptr;
7543}
7544
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007545/* Ithis function can fail with an abort() due to an Lua critical error.
7546 * We are in the initialisation process of HAProxy, this abort() is
7547 * tolerated.
7548 */
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01007549void hlua_init(void)
7550{
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007551 int i;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01007552 int idx;
7553 struct sample_fetch *sf;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007554 struct sample_conv *sc;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01007555 char *p;
Thierry Fournierfd107a22016-02-19 19:57:23 +01007556 const char *error_msg;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01007557#ifdef USE_OPENSSL
Willy Tarreau80f5fae2015-02-27 16:38:20 +01007558 struct srv_kw *kw;
7559 int tmp_error;
7560 char *error;
Thierry FOURNIER36d13742015-03-17 16:48:53 +01007561 char *args[] = { /* SSL client configuration. */
7562 "ssl",
7563 "verify",
7564 "none",
Thierry FOURNIER36d13742015-03-17 16:48:53 +01007565 NULL
7566 };
Willy Tarreau80f5fae2015-02-27 16:38:20 +01007567#endif
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007568
Christopher Faulet2a944ee2017-11-07 10:42:54 +01007569 HA_SPIN_INIT(&hlua_global_lock);
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02007570
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007571 /* Initialise struct hlua and com signals pool */
Willy Tarreaubafbe012017-11-24 17:34:44 +01007572 pool_head_hlua = create_pool("hlua", sizeof(struct hlua), MEM_F_SHARED);
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01007573
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007574 /* Register configuration keywords. */
7575 cfg_register_keywords(&cfg_kws);
7576
Thierry FOURNIER380d0932015-01-23 14:27:52 +01007577 /* Init main lua stack. */
7578 gL.Mref = LUA_REFNIL;
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01007579 gL.flags = 0;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01007580 LIST_INIT(&gL.com);
Willy Tarreau42ef75f2017-04-12 21:40:29 +02007581 gL.T = lua_newstate(hlua_alloc, &hlua_global_allocator);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01007582 hlua_sethlua(&gL);
7583 gL.Tref = LUA_REFNIL;
7584 gL.task = NULL;
7585
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007586 /* From this point, until the end of the initialisation fucntion,
7587 * the Lua function can fail with an abort. We are in the initialisation
7588 * process of HAProxy, this abort() is tolerated.
7589 */
7590
Thierry FOURNIER380d0932015-01-23 14:27:52 +01007591 /* Initialise lua. */
7592 luaL_openlibs(gL.T);
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007593
Thierry Fournier75933d42016-01-21 09:30:18 +01007594 /* Set safe environment for the initialisation. */
7595 if (!SET_SAFE_LJMP(gL.T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01007596 if (lua_type(gL.T, -1) == LUA_TSTRING)
7597 error_msg = lua_tostring(gL.T, -1);
7598 else
7599 error_msg = "critical error";
7600 fprintf(stderr, "Lua init: %s.\n", error_msg);
Thierry Fournier75933d42016-01-21 09:30:18 +01007601 exit(1);
7602 }
7603
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007604 /*
7605 *
7606 * Create "core" object.
7607 *
7608 */
7609
Thierry FOURNIERa2d8c652015-03-11 17:29:39 +01007610 /* This table entry is the object "core" base. */
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007611 lua_newtable(gL.T);
7612
7613 /* Push the loglevel constants. */
Willy Tarreau80f5fae2015-02-27 16:38:20 +01007614 for (i = 0; i < NB_LOG_LEVELS; i++)
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007615 hlua_class_const_int(gL.T, log_levels[i], i);
7616
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007617 /* Register special functions. */
7618 hlua_class_function(gL.T, "register_init", hlua_register_init);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01007619 hlua_class_function(gL.T, "register_task", hlua_register_task);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01007620 hlua_class_function(gL.T, "register_fetches", hlua_register_fetches);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01007621 hlua_class_function(gL.T, "register_converters", hlua_register_converters);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007622 hlua_class_function(gL.T, "register_action", hlua_register_action);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007623 hlua_class_function(gL.T, "register_service", hlua_register_service);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007624 hlua_class_function(gL.T, "register_cli", hlua_register_cli);
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01007625 hlua_class_function(gL.T, "yield", hlua_yield);
Willy Tarreau59551662015-03-10 14:23:13 +01007626 hlua_class_function(gL.T, "set_nice", hlua_set_nice);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01007627 hlua_class_function(gL.T, "sleep", hlua_sleep);
7628 hlua_class_function(gL.T, "msleep", hlua_msleep);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01007629 hlua_class_function(gL.T, "add_acl", hlua_add_acl);
7630 hlua_class_function(gL.T, "del_acl", hlua_del_acl);
7631 hlua_class_function(gL.T, "set_map", hlua_set_map);
7632 hlua_class_function(gL.T, "del_map", hlua_del_map);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007633 hlua_class_function(gL.T, "tcp", hlua_socket_new);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01007634 hlua_class_function(gL.T, "log", hlua_log);
7635 hlua_class_function(gL.T, "Debug", hlua_log_debug);
7636 hlua_class_function(gL.T, "Info", hlua_log_info);
7637 hlua_class_function(gL.T, "Warning", hlua_log_warning);
7638 hlua_class_function(gL.T, "Alert", hlua_log_alert);
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02007639 hlua_class_function(gL.T, "done", hlua_done);
Thierry Fournierfb0b5462016-01-21 09:28:58 +01007640 hlua_fcn_reg_core_fcn(gL.T);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007641
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007642 lua_setglobal(gL.T, "core");
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01007643
7644 /*
7645 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02007646 * Register class Map
7647 *
7648 */
7649
7650 /* This table entry is the object "Map" base. */
7651 lua_newtable(gL.T);
7652
7653 /* register pattern types. */
7654 for (i=0; i<PAT_MATCH_NUM; i++)
7655 hlua_class_const_int(gL.T, pat_match_names[i], i);
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01007656 for (i=0; i<PAT_MATCH_NUM; i++) {
7657 snprintf(trash.str, trash.size, "_%s", pat_match_names[i]);
7658 hlua_class_const_int(gL.T, trash.str, i);
7659 }
Thierry FOURNIER3def3932015-04-07 11:27:54 +02007660
7661 /* register constructor. */
7662 hlua_class_function(gL.T, "new", hlua_map_new);
7663
7664 /* Create and fill the metatable. */
7665 lua_newtable(gL.T);
7666
7667 /* Create and fille the __index entry. */
7668 lua_pushstring(gL.T, "__index");
7669 lua_newtable(gL.T);
7670
7671 /* Register . */
7672 hlua_class_function(gL.T, "lookup", hlua_map_lookup);
7673 hlua_class_function(gL.T, "slookup", hlua_map_slookup);
7674
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007675 lua_rawset(gL.T, -3);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02007676
Thierry Fournier45e78d72016-02-19 18:34:46 +01007677 /* Register previous table in the registry with reference and named entry.
7678 * The function hlua_register_metatable() pops the stack, so we
7679 * previously create a copy of the table.
7680 */
Thierry FOURNIER3def3932015-04-07 11:27:54 +02007681 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007682 class_map_ref = hlua_register_metatable(gL.T, CLASS_MAP);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02007683
7684 /* Assign the metatable to the mai Map object. */
7685 lua_setmetatable(gL.T, -2);
7686
7687 /* Set a name to the table. */
7688 lua_setglobal(gL.T, "Map");
7689
7690 /*
7691 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01007692 * Register class Channel
7693 *
7694 */
7695
7696 /* Create and fill the metatable. */
7697 lua_newtable(gL.T);
7698
7699 /* Create and fille the __index entry. */
7700 lua_pushstring(gL.T, "__index");
7701 lua_newtable(gL.T);
7702
7703 /* Register . */
7704 hlua_class_function(gL.T, "get", hlua_channel_get);
7705 hlua_class_function(gL.T, "dup", hlua_channel_dup);
7706 hlua_class_function(gL.T, "getline", hlua_channel_getline);
7707 hlua_class_function(gL.T, "set", hlua_channel_set);
7708 hlua_class_function(gL.T, "append", hlua_channel_append);
7709 hlua_class_function(gL.T, "send", hlua_channel_send);
7710 hlua_class_function(gL.T, "forward", hlua_channel_forward);
7711 hlua_class_function(gL.T, "get_in_len", hlua_channel_get_in_len);
7712 hlua_class_function(gL.T, "get_out_len", hlua_channel_get_out_len);
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01007713 hlua_class_function(gL.T, "is_full", hlua_channel_is_full);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01007714
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007715 lua_rawset(gL.T, -3);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01007716
7717 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007718 class_channel_ref = hlua_register_metatable(gL.T, CLASS_CHANNEL);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01007719
7720 /*
7721 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01007722 * Register class Fetches
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01007723 *
7724 */
7725
7726 /* Create and fill the metatable. */
7727 lua_newtable(gL.T);
7728
7729 /* Create and fille the __index entry. */
7730 lua_pushstring(gL.T, "__index");
7731 lua_newtable(gL.T);
7732
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01007733 /* Browse existing fetches and create the associated
7734 * object method.
7735 */
7736 sf = NULL;
7737 while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) {
7738
7739 /* Dont register the keywork if the arguments check function are
7740 * not safe during the runtime.
7741 */
7742 if ((sf->val_args != NULL) &&
7743 (sf->val_args != val_payload_lv) &&
7744 (sf->val_args != val_hdr))
7745 continue;
7746
7747 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
7748 * by an underscore.
7749 */
7750 strncpy(trash.str, sf->kw, trash.size);
7751 trash.str[trash.size - 1] = '\0';
7752 for (p = trash.str; *p; p++)
7753 if (*p == '.' || *p == '-' || *p == '+')
7754 *p = '_';
7755
7756 /* Register the function. */
7757 lua_pushstring(gL.T, trash.str);
Willy Tarreau2ec22742015-03-10 14:27:20 +01007758 lua_pushlightuserdata(gL.T, sf);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01007759 lua_pushcclosure(gL.T, hlua_run_sample_fetch, 1);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007760 lua_rawset(gL.T, -3);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01007761 }
7762
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007763 lua_rawset(gL.T, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01007764
7765 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007766 class_fetches_ref = hlua_register_metatable(gL.T, CLASS_FETCHES);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01007767
7768 /*
7769 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007770 * Register class Converters
7771 *
7772 */
7773
7774 /* Create and fill the metatable. */
7775 lua_newtable(gL.T);
7776
7777 /* Create and fill the __index entry. */
7778 lua_pushstring(gL.T, "__index");
7779 lua_newtable(gL.T);
7780
7781 /* Browse existing converters and create the associated
7782 * object method.
7783 */
7784 sc = NULL;
7785 while ((sc = sample_conv_getnext(sc, &idx)) != NULL) {
7786 /* Dont register the keywork if the arguments check function are
7787 * not safe during the runtime.
7788 */
7789 if (sc->val_args != NULL)
7790 continue;
7791
7792 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
7793 * by an underscore.
7794 */
7795 strncpy(trash.str, sc->kw, trash.size);
7796 trash.str[trash.size - 1] = '\0';
7797 for (p = trash.str; *p; p++)
7798 if (*p == '.' || *p == '-' || *p == '+')
7799 *p = '_';
7800
7801 /* Register the function. */
7802 lua_pushstring(gL.T, trash.str);
7803 lua_pushlightuserdata(gL.T, sc);
7804 lua_pushcclosure(gL.T, hlua_run_sample_conv, 1);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007805 lua_rawset(gL.T, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007806 }
7807
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007808 lua_rawset(gL.T, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007809
7810 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007811 class_converters_ref = hlua_register_metatable(gL.T, CLASS_CONVERTERS);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007812
7813 /*
7814 *
Thierry FOURNIER08504f42015-03-16 14:17:08 +01007815 * Register class HTTP
7816 *
7817 */
7818
7819 /* Create and fill the metatable. */
7820 lua_newtable(gL.T);
7821
7822 /* Create and fille the __index entry. */
7823 lua_pushstring(gL.T, "__index");
7824 lua_newtable(gL.T);
7825
7826 /* Register Lua functions. */
7827 hlua_class_function(gL.T, "req_get_headers",hlua_http_req_get_headers);
7828 hlua_class_function(gL.T, "req_del_header", hlua_http_req_del_hdr);
7829 hlua_class_function(gL.T, "req_rep_header", hlua_http_req_rep_hdr);
7830 hlua_class_function(gL.T, "req_rep_value", hlua_http_req_rep_val);
7831 hlua_class_function(gL.T, "req_add_header", hlua_http_req_add_hdr);
7832 hlua_class_function(gL.T, "req_set_header", hlua_http_req_set_hdr);
7833 hlua_class_function(gL.T, "req_set_method", hlua_http_req_set_meth);
7834 hlua_class_function(gL.T, "req_set_path", hlua_http_req_set_path);
7835 hlua_class_function(gL.T, "req_set_query", hlua_http_req_set_query);
7836 hlua_class_function(gL.T, "req_set_uri", hlua_http_req_set_uri);
7837
7838 hlua_class_function(gL.T, "res_get_headers",hlua_http_res_get_headers);
7839 hlua_class_function(gL.T, "res_del_header", hlua_http_res_del_hdr);
7840 hlua_class_function(gL.T, "res_rep_header", hlua_http_res_rep_hdr);
7841 hlua_class_function(gL.T, "res_rep_value", hlua_http_res_rep_val);
7842 hlua_class_function(gL.T, "res_add_header", hlua_http_res_add_hdr);
7843 hlua_class_function(gL.T, "res_set_header", hlua_http_res_set_hdr);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02007844 hlua_class_function(gL.T, "res_set_status", hlua_http_res_set_status);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01007845
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007846 lua_rawset(gL.T, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01007847
7848 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007849 class_http_ref = hlua_register_metatable(gL.T, CLASS_HTTP);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01007850
7851 /*
7852 *
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007853 * Register class AppletTCP
7854 *
7855 */
7856
7857 /* Create and fill the metatable. */
7858 lua_newtable(gL.T);
7859
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007860 /* Create and fille the __index entry. */
7861 lua_pushstring(gL.T, "__index");
7862 lua_newtable(gL.T);
7863
7864 /* Register Lua functions. */
Thierry FOURNIER / OZON.IO3e1d7912016-12-12 12:29:34 +01007865 hlua_class_function(gL.T, "getline", hlua_applet_tcp_getline);
7866 hlua_class_function(gL.T, "receive", hlua_applet_tcp_recv);
7867 hlua_class_function(gL.T, "send", hlua_applet_tcp_send);
7868 hlua_class_function(gL.T, "set_priv", hlua_applet_tcp_set_priv);
7869 hlua_class_function(gL.T, "get_priv", hlua_applet_tcp_get_priv);
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01007870 hlua_class_function(gL.T, "set_var", hlua_applet_tcp_set_var);
7871 hlua_class_function(gL.T, "unset_var", hlua_applet_tcp_unset_var);
7872 hlua_class_function(gL.T, "get_var", hlua_applet_tcp_get_var);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007873
7874 lua_settable(gL.T, -3);
7875
7876 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007877 class_applet_tcp_ref = hlua_register_metatable(gL.T, CLASS_APPLET_TCP);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007878
7879 /*
7880 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007881 * Register class AppletHTTP
7882 *
7883 */
7884
7885 /* Create and fill the metatable. */
7886 lua_newtable(gL.T);
7887
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007888 /* Create and fille the __index entry. */
7889 lua_pushstring(gL.T, "__index");
7890 lua_newtable(gL.T);
7891
7892 /* Register Lua functions. */
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01007893 hlua_class_function(gL.T, "set_priv", hlua_applet_http_set_priv);
7894 hlua_class_function(gL.T, "get_priv", hlua_applet_http_get_priv);
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01007895 hlua_class_function(gL.T, "set_var", hlua_applet_http_set_var);
7896 hlua_class_function(gL.T, "unset_var", hlua_applet_http_unset_var);
7897 hlua_class_function(gL.T, "get_var", hlua_applet_http_get_var);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007898 hlua_class_function(gL.T, "getline", hlua_applet_http_getline);
7899 hlua_class_function(gL.T, "receive", hlua_applet_http_recv);
7900 hlua_class_function(gL.T, "send", hlua_applet_http_send);
7901 hlua_class_function(gL.T, "add_header", hlua_applet_http_addheader);
7902 hlua_class_function(gL.T, "set_status", hlua_applet_http_status);
7903 hlua_class_function(gL.T, "start_response", hlua_applet_http_start_response);
7904
7905 lua_settable(gL.T, -3);
7906
7907 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007908 class_applet_http_ref = hlua_register_metatable(gL.T, CLASS_APPLET_HTTP);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007909
7910 /*
7911 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01007912 * Register class TXN
7913 *
7914 */
7915
7916 /* Create and fill the metatable. */
7917 lua_newtable(gL.T);
7918
7919 /* Create and fille the __index entry. */
7920 lua_pushstring(gL.T, "__index");
7921 lua_newtable(gL.T);
7922
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01007923 /* Register Lua functions. */
Willy Tarreau59551662015-03-10 14:23:13 +01007924 hlua_class_function(gL.T, "set_priv", hlua_set_priv);
7925 hlua_class_function(gL.T, "get_priv", hlua_get_priv);
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02007926 hlua_class_function(gL.T, "set_var", hlua_set_var);
Christopher Faulet85d79c92016-11-09 16:54:56 +01007927 hlua_class_function(gL.T, "unset_var", hlua_unset_var);
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02007928 hlua_class_function(gL.T, "get_var", hlua_get_var);
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02007929 hlua_class_function(gL.T, "done", hlua_txn_done);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01007930 hlua_class_function(gL.T, "set_loglevel",hlua_txn_set_loglevel);
7931 hlua_class_function(gL.T, "set_tos", hlua_txn_set_tos);
7932 hlua_class_function(gL.T, "set_mark", hlua_txn_set_mark);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01007933 hlua_class_function(gL.T, "deflog", hlua_txn_deflog);
7934 hlua_class_function(gL.T, "log", hlua_txn_log);
7935 hlua_class_function(gL.T, "Debug", hlua_txn_log_debug);
7936 hlua_class_function(gL.T, "Info", hlua_txn_log_info);
7937 hlua_class_function(gL.T, "Warning", hlua_txn_log_warning);
7938 hlua_class_function(gL.T, "Alert", hlua_txn_log_alert);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01007939
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007940 lua_rawset(gL.T, -3);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01007941
7942 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007943 class_txn_ref = hlua_register_metatable(gL.T, CLASS_TXN);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007944
7945 /*
7946 *
7947 * Register class Socket
7948 *
7949 */
7950
7951 /* Create and fill the metatable. */
7952 lua_newtable(gL.T);
7953
7954 /* Create and fille the __index entry. */
7955 lua_pushstring(gL.T, "__index");
7956 lua_newtable(gL.T);
7957
Baptiste Assmann84bb4932015-03-02 21:40:06 +01007958#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007959 hlua_class_function(gL.T, "connect_ssl", hlua_socket_connect_ssl);
Baptiste Assmann84bb4932015-03-02 21:40:06 +01007960#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007961 hlua_class_function(gL.T, "connect", hlua_socket_connect);
7962 hlua_class_function(gL.T, "send", hlua_socket_send);
7963 hlua_class_function(gL.T, "receive", hlua_socket_receive);
7964 hlua_class_function(gL.T, "close", hlua_socket_close);
7965 hlua_class_function(gL.T, "getpeername", hlua_socket_getpeername);
7966 hlua_class_function(gL.T, "getsockname", hlua_socket_getsockname);
7967 hlua_class_function(gL.T, "setoption", hlua_socket_setoption);
7968 hlua_class_function(gL.T, "settimeout", hlua_socket_settimeout);
7969
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007970 lua_rawset(gL.T, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007971
7972 /* Register the garbage collector entry. */
7973 lua_pushstring(gL.T, "__gc");
7974 lua_pushcclosure(gL.T, hlua_socket_gc, 0);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007975 lua_rawset(gL.T, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007976
7977 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007978 class_socket_ref = hlua_register_metatable(gL.T, CLASS_SOCKET);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007979
7980 /* Proxy and server configuration initialisation. */
7981 memset(&socket_proxy, 0, sizeof(socket_proxy));
7982 init_new_proxy(&socket_proxy);
7983 socket_proxy.parent = NULL;
7984 socket_proxy.last_change = now.tv_sec;
7985 socket_proxy.id = "LUA-SOCKET";
7986 socket_proxy.cap = PR_CAP_FE | PR_CAP_BE;
7987 socket_proxy.maxconn = 0;
7988 socket_proxy.accept = NULL;
7989 socket_proxy.options2 |= PR_O2_INDEPSTR;
7990 socket_proxy.srv = NULL;
7991 socket_proxy.conn_retries = 0;
7992 socket_proxy.timeout.connect = 5000; /* By default the timeout connection is 5s. */
7993
7994 /* Init TCP server: unchanged parameters */
7995 memset(&socket_tcp, 0, sizeof(socket_tcp));
7996 socket_tcp.next = NULL;
7997 socket_tcp.proxy = &socket_proxy;
7998 socket_tcp.obj_type = OBJ_TYPE_SERVER;
7999 LIST_INIT(&socket_tcp.actconns);
8000 LIST_INIT(&socket_tcp.pendconns);
Christopher Faulet40a007c2017-07-03 15:41:01 +02008001 socket_tcp.priv_conns = NULL;
8002 socket_tcp.idle_conns = NULL;
8003 socket_tcp.safe_conns = NULL;
Emeric Brun52a91d32017-08-31 14:41:55 +02008004 socket_tcp.next_state = SRV_ST_RUNNING; /* early server setup */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008005 socket_tcp.last_change = 0;
8006 socket_tcp.id = "LUA-TCP-CONN";
8007 socket_tcp.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8008 socket_tcp.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8009 socket_tcp.pp_opts = 0; /* Remove proxy protocol. */
8010
8011 /* XXX: Copy default parameter from default server,
8012 * but the default server is not initialized.
8013 */
8014 socket_tcp.maxqueue = socket_proxy.defsrv.maxqueue;
8015 socket_tcp.minconn = socket_proxy.defsrv.minconn;
8016 socket_tcp.maxconn = socket_proxy.defsrv.maxconn;
8017 socket_tcp.slowstart = socket_proxy.defsrv.slowstart;
8018 socket_tcp.onerror = socket_proxy.defsrv.onerror;
8019 socket_tcp.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
8020 socket_tcp.onmarkedup = socket_proxy.defsrv.onmarkedup;
8021 socket_tcp.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
8022 socket_tcp.uweight = socket_proxy.defsrv.iweight;
8023 socket_tcp.iweight = socket_proxy.defsrv.iweight;
8024
8025 socket_tcp.check.status = HCHK_STATUS_INI;
8026 socket_tcp.check.rise = socket_proxy.defsrv.check.rise;
8027 socket_tcp.check.fall = socket_proxy.defsrv.check.fall;
8028 socket_tcp.check.health = socket_tcp.check.rise; /* socket, but will fall down at first failure */
8029 socket_tcp.check.server = &socket_tcp;
8030
8031 socket_tcp.agent.status = HCHK_STATUS_INI;
8032 socket_tcp.agent.rise = socket_proxy.defsrv.agent.rise;
8033 socket_tcp.agent.fall = socket_proxy.defsrv.agent.fall;
8034 socket_tcp.agent.health = socket_tcp.agent.rise; /* socket, but will fall down at first failure */
8035 socket_tcp.agent.server = &socket_tcp;
8036
Willy Tarreaua261e9b2016-12-22 20:44:00 +01008037 socket_tcp.xprt = xprt_get(XPRT_RAW);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008038
8039#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008040 /* Init TCP server: unchanged parameters */
8041 memset(&socket_ssl, 0, sizeof(socket_ssl));
8042 socket_ssl.next = NULL;
8043 socket_ssl.proxy = &socket_proxy;
8044 socket_ssl.obj_type = OBJ_TYPE_SERVER;
8045 LIST_INIT(&socket_ssl.actconns);
8046 LIST_INIT(&socket_ssl.pendconns);
Christopher Faulet40a007c2017-07-03 15:41:01 +02008047 socket_tcp.priv_conns = NULL;
8048 socket_tcp.idle_conns = NULL;
8049 socket_tcp.safe_conns = NULL;
Emeric Brun52a91d32017-08-31 14:41:55 +02008050 socket_ssl.next_state = SRV_ST_RUNNING; /* early server setup */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008051 socket_ssl.last_change = 0;
8052 socket_ssl.id = "LUA-SSL-CONN";
8053 socket_ssl.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8054 socket_ssl.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8055 socket_ssl.pp_opts = 0; /* Remove proxy protocol. */
8056
8057 /* XXX: Copy default parameter from default server,
8058 * but the default server is not initialized.
8059 */
8060 socket_ssl.maxqueue = socket_proxy.defsrv.maxqueue;
8061 socket_ssl.minconn = socket_proxy.defsrv.minconn;
8062 socket_ssl.maxconn = socket_proxy.defsrv.maxconn;
8063 socket_ssl.slowstart = socket_proxy.defsrv.slowstart;
8064 socket_ssl.onerror = socket_proxy.defsrv.onerror;
8065 socket_ssl.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
8066 socket_ssl.onmarkedup = socket_proxy.defsrv.onmarkedup;
8067 socket_ssl.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
8068 socket_ssl.uweight = socket_proxy.defsrv.iweight;
8069 socket_ssl.iweight = socket_proxy.defsrv.iweight;
8070
8071 socket_ssl.check.status = HCHK_STATUS_INI;
8072 socket_ssl.check.rise = socket_proxy.defsrv.check.rise;
8073 socket_ssl.check.fall = socket_proxy.defsrv.check.fall;
8074 socket_ssl.check.health = socket_ssl.check.rise; /* socket, but will fall down at first failure */
8075 socket_ssl.check.server = &socket_ssl;
8076
8077 socket_ssl.agent.status = HCHK_STATUS_INI;
8078 socket_ssl.agent.rise = socket_proxy.defsrv.agent.rise;
8079 socket_ssl.agent.fall = socket_proxy.defsrv.agent.fall;
8080 socket_ssl.agent.health = socket_ssl.agent.rise; /* socket, but will fall down at first failure */
8081 socket_ssl.agent.server = &socket_ssl;
8082
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008083 socket_ssl.use_ssl = 1;
Willy Tarreaua261e9b2016-12-22 20:44:00 +01008084 socket_ssl.xprt = xprt_get(XPRT_SSL);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008085
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008086 for (idx = 0; args[idx] != NULL; idx++) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008087 if ((kw = srv_find_kw(args[idx])) != NULL) { /* Maybe it's registered server keyword */
8088 /*
8089 *
8090 * If the keyword is not known, we can search in the registered
8091 * server keywords. This is usefull to configure special SSL
8092 * features like client certificates and ssl_verify.
8093 *
8094 */
8095 tmp_error = kw->parse(args, &idx, &socket_proxy, &socket_ssl, &error);
8096 if (tmp_error != 0) {
8097 fprintf(stderr, "INTERNAL ERROR: %s\n", error);
8098 abort(); /* This must be never arrives because the command line
8099 not editable by the user. */
8100 }
8101 idx += kw->skip;
8102 }
8103 }
8104
8105 /* Initialize SSL server. */
Willy Tarreaucbe6da52018-05-18 17:08:28 +02008106 if (socket_ssl.xprt->prepare_srv) {
8107 int saved_used_backed = global.ssl_used_backend;
8108 // don't affect maxconn automatic computation
Willy Tarreau17d45382016-12-22 21:16:08 +01008109 socket_ssl.xprt->prepare_srv(&socket_ssl);
Willy Tarreaucbe6da52018-05-18 17:08:28 +02008110 global.ssl_used_backend = saved_used_backed;
8111 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008112#endif
Thierry Fournier75933d42016-01-21 09:30:18 +01008113
8114 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01008115}
Willy Tarreaubb57d942016-12-21 19:04:56 +01008116
8117__attribute__((constructor))
8118static void __hlua_init(void)
8119{
8120 char *ptr = NULL;
8121 memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE);
8122 hap_register_build_opts(ptr, 1);
8123}