blob: 9ccd5c73e21844a3c027877b181dcbc59cfc1476 [file] [log] [blame]
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001.. toctree::
2 :maxdepth: 2
3
4
5How Lua runs in HAProxy
6=======================
7
8HAProxy Lua running contexts
9----------------------------
10
11The Lua code executed in HAProxy can be processed in 2 main modes. The first one
12is the **initialisation mode**, and the second is the **runtime mode**.
13
14* In the **initialisation mode**, we can perform DNS solves, but we cannot
15 perform socket I/O. In this initialisation mode, HAProxy still blocked during
16 the execution of the Lua program.
17
18* In the **runtime mode**, we cannot perform DNS solves, but we can use sockets.
19 The execution of the Lua code is multiplexed with the requests processing, so
20 the Lua code seems to be run in blocking, but it is not the case.
21
22The Lua code is loaded in one or more files. These files contains main code and
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +010023functions. Lua has 8 execution contexts.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +010024
251. The Lua file **body context**. It is executed during the load of the Lua file
26 in the HAProxy `[global]` section with the directive `lua-load`. It is
27 executed in initialisation mode. This section is use for configuring Lua
28 bindings in HAProxy.
29
David Carlier61fdf8b2015-10-02 11:59:38 +0100302. The Lua **init context**. It is a Lua function executed just after the
Thierry FOURNIER17bd1522015-03-11 20:31:00 +010031 HAProxy configuration parsing. The execution is in initialisation mode. In
32 this context the HAProxy environment are already initialized. It is useful to
33 check configuration, or initializing socket connections or tasks. These
34 functions are declared in the body context with the Lua function
35 `core.register_init()`. The prototype of the function is a simple function
36 without return value and without parameters, like this: `function fcn()`.
37
David Carlier61fdf8b2015-10-02 11:59:38 +0100383. The Lua **task context**. It is a Lua function executed after the start
Thierry FOURNIER17bd1522015-03-11 20:31:00 +010039 of the HAProxy scheduler, and just after the declaration of the task with the
40 Lua function `core.register_task()`. This context can be concurrent with the
41 traffic processing. It is executed in runtime mode. The prototype of the
42 function is a simple function without return value and without parameters,
43 like this: `function fcn()`.
44
David Carlier61fdf8b2015-10-02 11:59:38 +0100454. The **action context**. It is a Lua function conditionally executed. These
Thierry FOURNIERa2d02252015-10-01 15:00:42 +020046 actions are registered by the Lua directives "`core.register_action()`". The
47 prototype of the Lua called function is a function with doesn't returns
48 anything and that take an object of class TXN as entry. `function fcn(txn)`.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +010049
505. The **sample-fetch context**. This function takes a TXN object as entry
51 argument and returns a string. These types of function cannot execute any
52 blocking function. They are useful to aggregate some of original HAProxy
53 sample-fetches and return the result. The prototype of the function is
54 `function string fcn(txn)`. These functions can be registered with the Lua
55 function `core.register_fetches()`. Each declared sample-fetch is prefixed by
56 the string "lua.".
57
Christopher Faulet1e9b1b62021-08-11 10:14:30 +020058 .. note::
59 It is possible that this function cannot found the required data in the
60 original HAProxy sample-fetches, in this case, it cannot return the
61 result. This case is not yet supported
Thierry FOURNIER17bd1522015-03-11 20:31:00 +010062
David Carlier61fdf8b2015-10-02 11:59:38 +0100636. The **converter context**. It is a Lua function that takes a string as input
Thierry FOURNIER17bd1522015-03-11 20:31:00 +010064 and returns another string as output. These types of function are stateless,
65 it cannot access to any context. They don't execute any blocking function.
66 The call prototype is `function string fcn(string)`. This function can be
67 registered with the Lua function `core.register_converters()`. Each declared
68 converter is prefixed by the string "lua.".
69
Christopher Faulet5a2c6612021-08-15 20:35:25 +0200707. The **filter context**: It is a Lua object based on a class defining filter
71 callback functions. Lua filters are registered using
72 `core.register_filter()`. Each declared filter is prefixed by the string
73 "lua.".
74
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +0100758. The **event context**: Inside a function that handles events subscribed
76 through `core.event_sub()` or `Server.event_sub()`.
77
Christopher Faulet5a2c6612021-08-15 20:35:25 +020078
Thierry FOURNIER17bd1522015-03-11 20:31:00 +010079HAProxy Lua Hello world
80-----------------------
81
82HAProxy configuration file (`hello_world.conf`):
83
84::
85
86 global
87 lua-load hello_world.lua
88
89 listen proxy
90 bind 127.0.0.1:10001
Thierry FOURNIERa2d02252015-10-01 15:00:42 +020091 tcp-request inspect-delay 1s
92 tcp-request content use-service lua.hello_world
Thierry FOURNIER17bd1522015-03-11 20:31:00 +010093
94HAProxy Lua file (`hello_world.lua`):
95
96.. code-block:: lua
97
Thierry FOURNIERa2d02252015-10-01 15:00:42 +020098 core.register_service("hello_world", "tcp", function(applet)
99 applet:send("hello world\n")
100 end)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100101
102How to start HAProxy for testing this configuration:
103
104::
105
106 ./haproxy -f hello_world.conf
107
108On other terminal, you can test with telnet:
109
110::
111
112 #:~ telnet 127.0.0.1 10001
113 hello world
114
Thierry Fournierae6b5682022-09-19 09:04:16 +0200115Usage of load parameters
116------------------------
117
Ilya Shipitsin4a689da2022-10-29 09:34:32 +0500118HAProxy lua-load(-per-thread) directives allow a list of parameters after
Thierry Fournierae6b5682022-09-19 09:04:16 +0200119the lua file name. These parameters are accessible through an array of args
120using this code `local args = table.pack(...)` in the body of loaded file.
121
122Below, a new version of the hello world using load parameters
123
124HAProxy configuration file (`hello_world.conf`):
125
126::
127
128 global
129 lua-load hello_world.lua "this is not an hello world"
130
131 listen proxy
132 bind 127.0.0.1:10001
133 tcp-request inspect-delay 1s
134 tcp-request content use-service lua.hello_world
135
136HAProxy Lua file (`hello_world.lua`):
137
138.. code-block:: lua
139
140 local args = table.pack(...)
141
142 core.register_service("hello_world", "tcp", function(applet)
143 applet:send(args[1] .. "\n")
144 end)
145
146
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100147Core class
148==========
149
150.. js:class:: core
151
152 The "core" class contains all the HAProxy core functions. These function are
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200153 useful for the controlling of the execution flow, registering hooks,
154 manipulating global maps or ACL, ...
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100155
156 "core" class is basically provided with HAProxy. No `require` line is
157 required to uses these function.
158
David Carlier61fdf8b2015-10-02 11:59:38 +0100159 The "core" class is static, it is not possible to create a new object of this
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100160 type.
161
Christopher Faulet12a5ee72024-02-29 15:41:17 +0100162.. js:attribute:: core.silent
163
164 :returns: integer
165
166 This attribute is an integer, it contains the value -1. It is a special value
167 used to disable logging.
168
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100169.. js:attribute:: core.emerg
170
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100171 :returns: integer
172
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200173 This attribute is an integer, it contains the value of the loglevel
174 "emergency" (0).
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100175
176.. js:attribute:: core.alert
177
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100178 :returns: integer
179
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200180 This attribute is an integer, it contains the value of the loglevel
181 "alert" (1).
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100182
183.. js:attribute:: core.crit
184
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100185 :returns: integer
186
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200187 This attribute is an integer, it contains the value of the loglevel
188 "critical" (2).
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100189
190.. js:attribute:: core.err
191
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100192 :returns: integer
193
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200194 This attribute is an integer, it contains the value of the loglevel
195 "error" (3).
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100196
197.. js:attribute:: core.warning
198
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100199 :returns: integer
200
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200201 This attribute is an integer, it contains the value of the loglevel
202 "warning" (4).
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100203
204.. js:attribute:: core.notice
205
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100206 :returns: integer
207
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200208 This attribute is an integer, it contains the value of the loglevel
209 "notice" (5).
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100210
211.. js:attribute:: core.info
212
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100213 :returns: integer
214
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200215 This attribute is an integer, it contains the value of the loglevel
216 "info" (6).
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100217
218.. js:attribute:: core.debug
219
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100220 :returns: integer
221
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200222 This attribute is an integer, it contains the value of the loglevel
223 "debug" (7).
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100224
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100225.. js:attribute:: core.proxies
226
Aurelien DARRAGON2a295712023-05-11 17:31:46 +0200227 **context**: init, task, action, sample-fetch, converter
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100228
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400229 This attribute is a table of declared proxies (frontend and backends). Each
230 proxy give an access to his list of listeners and servers. The table is
231 indexed by proxy name, and each entry is of type :ref:`proxy_class`.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100232
Christopher Faulet1e9b1b62021-08-11 10:14:30 +0200233 .. Warning::
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200234 if you declared a frontend and backend with the same name, only one of
235 them will be listed.
Thierry FOURNIER9b82a582017-07-24 13:30:43 +0200236
237 :see: :js:attr:`core.backends`
238 :see: :js:attr:`core.frontends`
239
240.. js:attribute:: core.backends
241
Aurelien DARRAGON2a295712023-05-11 17:31:46 +0200242 **context**: init, task, action, sample-fetch, converter
Thierry FOURNIER9b82a582017-07-24 13:30:43 +0200243
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400244 This attribute is a table of declared proxies with backend capability. Each
245 proxy give an access to his list of listeners and servers. The table is
246 indexed by the backend name, and each entry is of type :ref:`proxy_class`.
Thierry FOURNIER9b82a582017-07-24 13:30:43 +0200247
248 :see: :js:attr:`core.proxies`
249 :see: :js:attr:`core.frontends`
250
251.. js:attribute:: core.frontends
252
Aurelien DARRAGON2a295712023-05-11 17:31:46 +0200253 **context**: init, task, action, sample-fetch, converter
Thierry FOURNIER9b82a582017-07-24 13:30:43 +0200254
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400255 This attribute is a table of declared proxies with frontend capability. Each
256 proxy give an access to his list of listeners and servers. The table is
257 indexed by the frontend name, and each entry is of type :ref:`proxy_class`.
Thierry FOURNIER9b82a582017-07-24 13:30:43 +0200258
259 :see: :js:attr:`core.proxies`
260 :see: :js:attr:`core.backends`
261
Thierry Fournierecb83c22020-11-28 15:49:44 +0100262.. js:attribute:: core.thread
263
264 **context**: task, action, sample-fetch, converter, applet
265
266 This variable contains the executing thread number starting at 1. 0 is a
267 special case for the common lua context. So, if thread is 0, Lua scope is
268 shared by all threads, otherwise the scope is dedicated to a single thread.
269 A program which needs to execute some parts exactly once regardless of the
270 number of threads can check that core.thread is 0 or 1.
271
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100272.. js:function:: core.log(loglevel, msg)
273
274 **context**: body, init, task, action, sample-fetch, converter
275
David Carlier61fdf8b2015-10-02 11:59:38 +0100276 This function sends a log. The log is sent, according with the HAProxy
Tristan2632d042023-10-23 13:07:39 +0100277 configuration file, to the loggers relevant to the current context and
278 to stderr if it is allowed.
279
280 The exact behaviour depends on tune.lua.log.loggers and tune.lua.log.stderr.
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100281
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100282 :param integer loglevel: Is the log level associated with the message. It is a
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200283 number between 0 and 7.
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100284 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100285 :see: :js:attr:`core.emerg`, :js:attr:`core.alert`, :js:attr:`core.crit`,
286 :js:attr:`core.err`, :js:attr:`core.warning`, :js:attr:`core.notice`,
287 :js:attr:`core.info`, :js:attr:`core.debug` (log level definitions)
288 :see: :js:func:`core.Debug`
289 :see: :js:func:`core.Info`
290 :see: :js:func:`core.Warning`
291 :see: :js:func:`core.Alert`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100292
293.. js:function:: core.Debug(msg)
294
295 **context**: body, init, task, action, sample-fetch, converter
296
297 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100298 :see: :js:func:`core.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100299
300 Does the same job than:
301
302.. code-block:: lua
303
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100304 function Debug(msg)
305 core.log(core.debug, msg)
306 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100307..
308
309.. js:function:: core.Info(msg)
310
311 **context**: body, init, task, action, sample-fetch, converter
312
313 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100314 :see: :js:func:`core.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100315
316.. code-block:: lua
317
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100318 function Info(msg)
319 core.log(core.info, msg)
320 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100321..
322
323.. js:function:: core.Warning(msg)
324
325 **context**: body, init, task, action, sample-fetch, converter
326
327 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100328 :see: :js:func:`core.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100329
330.. code-block:: lua
331
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100332 function Warning(msg)
333 core.log(core.warning, msg)
334 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100335..
336
337.. js:function:: core.Alert(msg)
338
339 **context**: body, init, task, action, sample-fetch, converter
340
341 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100342 :see: :js:func:`core.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100343
344.. code-block:: lua
345
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100346 function Alert(msg)
347 core.log(core.alert, msg)
348 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100349..
350
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100351.. js:function:: core.add_acl(filename, key)
352
353 **context**: init, task, action, sample-fetch, converter
354
355 Add the ACL *key* in the ACLs list referenced by the file *filename*.
356
357 :param string filename: the filename that reference the ACL entries.
358 :param string key: the key which will be added.
359
360.. js:function:: core.del_acl(filename, key)
361
362 **context**: init, task, action, sample-fetch, converter
363
364 Delete the ACL entry referenced by the key *key* in the list of ACLs
365 referenced by *filename*.
366
367 :param string filename: the filename that reference the ACL entries.
368 :param string key: the key which will be deleted.
369
370.. js:function:: core.del_map(filename, key)
371
372 **context**: init, task, action, sample-fetch, converter
373
374 Delete the map entry indexed with the specified key in the list of maps
375 referenced by his filename.
376
377 :param string filename: the filename that reference the map entries.
378 :param string key: the key which will be deleted.
379
Thierry Fourniereea77c02016-03-18 08:47:13 +0100380.. js:function:: core.get_info()
381
382 **context**: body, init, task, action, sample-fetch, converter
383
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200384 Returns HAProxy core information. We can find information like the uptime,
Thierry Fourniereea77c02016-03-18 08:47:13 +0100385 the pid, memory pool usage, tasks number, ...
386
Ilya Shipitsin5fa29b82022-12-07 09:46:19 +0500387 This information is also returned by the management socket via the command
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100388 "show info". See the management socket documentation for more information
Thierry Fourniereea77c02016-03-18 08:47:13 +0100389 about the content of these variables.
390
391 :returns: an array of values.
392
Thierry Fournierb1f46562016-01-21 09:46:15 +0100393.. js:function:: core.now()
394
395 **context**: body, init, task, action
396
397 This function returns the current time. The time returned is fixed by the
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100398 HAProxy core and assures than the hour will be monotonic and that the system
Thierry Fournierb1f46562016-01-21 09:46:15 +0100399 call 'gettimeofday' will not be called too. The time is refreshed between each
400 Lua execution or resume, so two consecutive call to the function "now" will
401 probably returns the same result.
402
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400403 :returns: a table which contains two entries "sec" and "usec". "sec"
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200404 contains the current at the epoch format, and "usec" contains the
405 current microseconds.
Thierry Fournierb1f46562016-01-21 09:46:15 +0100406
Thierry FOURNIERa78f0372016-12-14 19:04:41 +0100407.. js:function:: core.http_date(date)
408
409 **context**: body, init, task, action
410
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100411 This function take a string representing http date, and returns an integer
Thierry FOURNIERa78f0372016-12-14 19:04:41 +0100412 containing the corresponding date with a epoch format. A valid http date
413 me respect the format IMF, RFC850 or ASCTIME.
414
415 :param string date: a date http-date formatted
416 :returns: integer containing epoch date
417 :see: :js:func:`core.imf_date`.
418 :see: :js:func:`core.rfc850_date`.
419 :see: :js:func:`core.asctime_date`.
420 :see: https://tools.ietf.org/html/rfc7231#section-7.1.1.1
421
422.. js:function:: core.imf_date(date)
423
424 **context**: body, init, task, action
425
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100426 This function take a string representing IMF date, and returns an integer
Thierry FOURNIERa78f0372016-12-14 19:04:41 +0100427 containing the corresponding date with a epoch format.
428
429 :param string date: a date IMF formatted
430 :returns: integer containing epoch date
431 :see: https://tools.ietf.org/html/rfc7231#section-7.1.1.1
432
433 The IMF format is like this:
434
435.. code-block:: text
436
437 Sun, 06 Nov 1994 08:49:37 GMT
438..
439
440.. js:function:: core.rfc850_date(date)
441
442 **context**: body, init, task, action
443
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100444 This function take a string representing RFC850 date, and returns an integer
Thierry FOURNIERa78f0372016-12-14 19:04:41 +0100445 containing the corresponding date with a epoch format.
446
447 :param string date: a date RFC859 formatted
448 :returns: integer containing epoch date
449 :see: https://tools.ietf.org/html/rfc7231#section-7.1.1.1
450
451 The RFC850 format is like this:
452
453.. code-block:: text
454
455 Sunday, 06-Nov-94 08:49:37 GMT
456..
457
458.. js:function:: core.asctime_date(date)
459
460 **context**: body, init, task, action
461
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100462 This function take a string representing ASCTIME date, and returns an integer
Thierry FOURNIERa78f0372016-12-14 19:04:41 +0100463 containing the corresponding date with a epoch format.
464
465 :param string date: a date ASCTIME formatted
466 :returns: integer containing epoch date
467 :see: https://tools.ietf.org/html/rfc7231#section-7.1.1.1
468
469 The ASCTIME format is like this:
470
471.. code-block:: text
472
473 Sun Nov 6 08:49:37 1994
474..
475
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100476.. js:function:: core.msleep(milliseconds)
477
478 **context**: body, init, task, action
479
480 The `core.msleep()` stops the Lua execution between specified milliseconds.
481
482 :param integer milliseconds: the required milliseconds.
483
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100484.. js:function:: core.register_action(name, actions, func [, nb_args])
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200485
486 **context**: body
487
David Carlier61fdf8b2015-10-02 11:59:38 +0100488 Register a Lua function executed as action. All the registered action can be
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200489 used in HAProxy with the prefix "lua.". An action gets a TXN object class as
490 input.
491
Aurelien DARRAGONe239e702023-08-23 17:38:42 +0200492 :param string name: is the name of the action.
493 :param table actions: is a table of string describing the HAProxy actions
494 facilities where to expose the new action. Expected facilities are:
495 'tcp-req', 'tcp-res', 'http-req' or 'http-res'.
496 :param function func: is the Lua function called to work as an action.
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100497 :param integer nb_args: is the expected number of argument for the action.
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200498 By default the value is 0.
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200499
500 The prototype of the Lua function used as argument is:
501
502.. code-block:: lua
503
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100504 function(txn [, arg1 [, arg2]])
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200505..
506
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100507 * **txn** (:ref:`txn_class`): this is a TXN object used for manipulating the
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200508 current request or TCP stream.
509
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100510 * **argX**: this is argument provided through the HAProxy configuration file.
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100511
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100512 Here, an example of action registration. The action just send an 'Hello world'
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200513 in the logs.
514
515.. code-block:: lua
516
517 core.register_action("hello-world", { "tcp-req", "http-req" }, function(txn)
518 txn:Info("Hello world")
519 end)
520..
521
Willy Tarreau714f3452021-05-09 06:47:26 +0200522 This example code is used in HAProxy configuration like this:
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200523
524::
525
526 frontend tcp_frt
527 mode tcp
528 tcp-request content lua.hello-world
529
530 frontend http_frt
531 mode http
532 http-request lua.hello-world
Aurelien DARRAGONd5c80d72023-03-13 19:36:13 +0100533
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100534..
535
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100536 A second example using arguments
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100537
538.. code-block:: lua
539
540 function hello_world(txn, arg)
541 txn:Info("Hello world for " .. arg)
542 end
543 core.register_action("hello-world", { "tcp-req", "http-req" }, hello_world, 2)
Aurelien DARRAGONd5c80d72023-03-13 19:36:13 +0100544
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100545..
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200546
Willy Tarreau714f3452021-05-09 06:47:26 +0200547 This example code is used in HAProxy configuration like this:
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100548
549::
550
551 frontend tcp_frt
552 mode tcp
553 tcp-request content lua.hello-world everybody
Aurelien DARRAGONd5c80d72023-03-13 19:36:13 +0100554
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100555..
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200556
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100557.. js:function:: core.register_converters(name, func)
558
559 **context**: body
560
David Carlier61fdf8b2015-10-02 11:59:38 +0100561 Register a Lua function executed as converter. All the registered converters
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200562 can be used in HAProxy with the prefix "lua.". A converter gets a string as
563 input and returns a string as output. The registered function can take up to 9
564 values as parameter. All the values are strings.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100565
566 :param string name: is the name of the converter.
567 :param function func: is the Lua function called to work as converter.
568
569 The prototype of the Lua function used as argument is:
570
571.. code-block:: lua
572
573 function(str, [p1 [, p2 [, ... [, p5]]]])
574..
575
576 * **str** (*string*): this is the input value automatically converted in
577 string.
578 * **p1** .. **p5** (*string*): this is a list of string arguments declared in
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100579 the HAProxy configuration file. The number of arguments doesn't exceed 5.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200580 The order and the nature of these is conventionally chosen by the
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100581 developer.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100582
583.. js:function:: core.register_fetches(name, func)
584
585 **context**: body
586
David Carlier61fdf8b2015-10-02 11:59:38 +0100587 Register a Lua function executed as sample fetch. All the registered sample
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100588 fetch can be used in HAProxy with the prefix "lua.". A Lua sample fetch
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200589 returns a string as output. The registered function can take up to 9 values as
590 parameter. All the values are strings.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100591
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200592 :param string name: is the name of the sample fetch.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100593 :param function func: is the Lua function called to work as sample fetch.
594
595 The prototype of the Lua function used as argument is:
596
597.. code-block:: lua
598
599 string function(txn, [p1 [, p2 [, ... [, p5]]]])
600..
601
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200602 * **txn** (:ref:`txn_class`): this is the txn object associated with the
603 current request.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100604 * **p1** .. **p5** (*string*): this is a list of string arguments declared in
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100605 the HAProxy configuration file. The number of arguments doesn't exceed 5.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200606 The order and the nature of these is conventionally chosen by the
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100607 developer.
608 * **Returns**: A string containing some data, or nil if the value cannot be
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100609 returned now.
610
611 lua example code:
612
613.. code-block:: lua
614
615 core.register_fetches("hello", function(txn)
616 return "hello"
617 end)
618..
619
620 HAProxy example configuration:
621
622::
623
624 frontend example
625 http-request redirect location /%[lua.hello]
626
Christopher Faulet5a2c6612021-08-15 20:35:25 +0200627.. js:function:: core.register_filter(name, Flt, func)
628
629 **context**: body
630
631 Register a Lua function used to declare a filter. All the registered filters
632 can by used in HAProxy with the prefix "lua.".
633
634 :param string name: is the name of the filter.
635 :param table Flt: is a Lua class containing the filter definition (id, flags,
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200636 callbacks).
Christopher Faulet5a2c6612021-08-15 20:35:25 +0200637 :param function func: is the Lua function called to create the Lua filter.
638
639 The prototype of the Lua function used as argument is:
640
641.. code-block:: lua
642
643 function(flt, args)
644..
645
646 * **flt** : Is a filter object based on the class provided in
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200647 :js:func:`core.register_filter()` function.
Christopher Faulet5a2c6612021-08-15 20:35:25 +0200648
649 * **args**: Is a table of strings containing all arguments provided through
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200650 the HAProxy configuration file, on the filter line.
Christopher Faulet5a2c6612021-08-15 20:35:25 +0200651
652 It must return the filter to use or nil to ignore it. Here, an example of
653 filter registration.
654
655.. code-block:: lua
656
657 core.register_filter("my-filter", MyFilter, function(flt, args)
658 flt.args = args -- Save arguments
659 return flt
660 end)
661..
662
663 This example code is used in HAProxy configuration like this:
664
665::
666
667 frontend http
668 mode http
669 filter lua.my-filter arg1 arg2 arg3
Aurelien DARRAGONd5c80d72023-03-13 19:36:13 +0100670
Christopher Faulet5a2c6612021-08-15 20:35:25 +0200671..
672
673 :see: :js:class:`Filter`
674
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200675.. js:function:: core.register_service(name, mode, func)
676
677 **context**: body
678
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200679 Register a Lua function executed as a service. All the registered services
680 can be used in HAProxy with the prefix "lua.". A service gets an object class
681 as input according with the required mode.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200682
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200683 :param string name: is the name of the service.
Willy Tarreau61add3c2015-09-28 15:39:10 +0200684 :param string mode: is string describing the required mode. Only 'tcp' or
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200685 'http' are allowed.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200686 :param function func: is the Lua function called to work as service.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200687
688 The prototype of the Lua function used as argument is:
689
690.. code-block:: lua
691
692 function(applet)
693..
694
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100695 * **applet** *applet* will be a :ref:`applettcp_class` or a
696 :ref:`applethttp_class`. It depends the type of registered applet. An applet
697 registered with the 'http' value for the *mode* parameter will gets a
698 :ref:`applethttp_class`. If the *mode* value is 'tcp', the applet will gets
699 a :ref:`applettcp_class`.
700
Christopher Faulet1e9b1b62021-08-11 10:14:30 +0200701 .. warning::
702 Applets of type 'http' cannot be called from 'tcp-*' rulesets. Only the
703 'http-*' rulesets are authorized, this means that is not possible to call
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200704 a HTTP applet from a proxy in tcp mode. Applets of type 'tcp' can be
Christopher Faulet1e9b1b62021-08-11 10:14:30 +0200705 called from anywhere.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200706
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200707 Here, an example of service registration. The service just send an
708 'Hello world' as an http response.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200709
710.. code-block:: lua
711
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100712 core.register_service("hello-world", "http", function(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200713 local response = "Hello World !"
714 applet:set_status(200)
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +0200715 applet:add_header("content-length", string.len(response))
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200716 applet:add_header("content-type", "text/plain")
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +0200717 applet:start_response()
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200718 applet:send(response)
719 end)
720..
721
Willy Tarreau714f3452021-05-09 06:47:26 +0200722 This example code is used in HAProxy configuration like this:
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200723
724::
725
726 frontend example
727 http-request use-service lua.hello-world
728
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100729.. js:function:: core.register_init(func)
730
731 **context**: body
732
733 Register a function executed after the configuration parsing. This is useful
734 to check any parameters.
735
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100736 :param function func: is the Lua function called to work as initializer.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100737
738 The prototype of the Lua function used as argument is:
739
740.. code-block:: lua
741
742 function()
743..
744
745 It takes no input, and no output is expected.
746
Aurelien DARRAGONb8038992023-03-09 16:48:30 +0100747.. js:function:: core.register_task(func[, arg1[, arg2[, ...[, arg4]]]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100748
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +0100749 **context**: body, init, task, action, sample-fetch, converter, event
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100750
751 Register and start independent task. The task is started when the HAProxy
752 main scheduler starts. For example this type of tasks can be executed to
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100753 perform complex health checks.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100754
Aurelien DARRAGONb8038992023-03-09 16:48:30 +0100755 :param function func: is the Lua function called to work as an async task.
756
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200757 Up to 4 optional arguments (all types supported) may be passed to the
758 function. (They will be passed as-is to the task function)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100759
760 The prototype of the Lua function used as argument is:
761
762.. code-block:: lua
763
Aurelien DARRAGONb8038992023-03-09 16:48:30 +0100764 function([arg1[, arg2[, ...[, arg4]]]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100765..
766
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200767 It takes up to 4 optional arguments (provided when registering), and no
768 output is expected.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100769
Aurelien DARRAGON86fb22c2023-05-03 17:03:09 +0200770 See also :js:func:`core.queue` to dynamically pass data between main context
771 and tasks or even between tasks.
772
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +0100773.. js:function:: core.register_cli([path], usage, func)
774
775 **context**: body
776
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200777 Register a custom cli that will be available from haproxy stats socket.
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +0100778
779 :param array path: is the sequence of word for which the cli execute the Lua
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200780 binding.
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +0100781 :param string usage: is the usage message displayed in the help.
782 :param function func: is the Lua function called to handle the CLI commands.
783
784 The prototype of the Lua function used as argument is:
785
786.. code-block:: lua
787
788 function(AppletTCP, [arg1, [arg2, [...]]])
789..
790
791 I/O are managed with the :ref:`applettcp_class` object. Args are given as
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100792 parameter. The args embed the registered path. If the path is declared like
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +0100793 this:
794
795.. code-block:: lua
796
797 core.register_cli({"show", "ssl", "stats"}, "Display SSL stats..", function(applet, arg1, arg2, arg3, arg4, arg5)
798 end)
799..
800
801 And we execute this in the prompt:
802
803.. code-block:: text
804
805 > prompt
806 > show ssl stats all
807..
808
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200809 Then, arg1, arg2 and arg3 will contains respectively "show", "ssl" and
810 "stats".
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +0100811 arg4 will contain "all". arg5 contains nil.
812
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100813.. js:function:: core.set_nice(nice)
814
815 **context**: task, action, sample-fetch, converter
816
817 Change the nice of the current task or current session.
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100818
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100819 :param integer nice: the nice value, it must be between -1024 and 1024.
820
821.. js:function:: core.set_map(filename, key, value)
822
823 **context**: init, task, action, sample-fetch, converter
824
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100825 Set the value *value* associated to the key *key* in the map referenced by
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100826 *filename*.
827
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100828 :param string filename: the Map reference
829 :param string key: the key to set or replace
830 :param string value: the associated value
831
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100832.. js:function:: core.sleep(int seconds)
833
834 **context**: body, init, task, action
835
836 The `core.sleep()` functions stop the Lua execution between specified seconds.
837
838 :param integer seconds: the required seconds.
839
840.. js:function:: core.tcp()
841
842 **context**: init, task, action
843
844 This function returns a new object of a *socket* class.
845
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100846 :returns: A :ref:`socket_class` object.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100847
William Lallemand00a15022021-11-19 16:02:44 +0100848.. js:function:: core.httpclient()
849
850 **context**: init, task, action
851
852 This function returns a new object of a *httpclient* class.
853
854 :returns: A :ref:`httpclient_class` object.
855
Thierry Fournier1de16592016-01-27 09:49:07 +0100856.. js:function:: core.concat()
857
858 **context**: body, init, task, action, sample-fetch, converter
859
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100860 This function returns a new concat object.
Thierry Fournier1de16592016-01-27 09:49:07 +0100861
862 :returns: A :ref:`concat_class` object.
863
Aurelien DARRAGON86fb22c2023-05-03 17:03:09 +0200864.. js:function:: core.queue()
865
866 **context**: body, init, task, event, action, sample-fetch, converter
867
868 This function returns a new queue object.
869
870 :returns: A :ref:`queue_class` object.
871
Thierry FOURNIER0a99b892015-08-26 00:14:17 +0200872.. js:function:: core.done(data)
873
874 **context**: body, init, task, action, sample-fetch, converter
875
876 :param any data: Return some data for the caller. It is useful with
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200877 sample-fetches and sample-converters.
Thierry FOURNIER0a99b892015-08-26 00:14:17 +0200878
879 Immediately stops the current Lua execution and returns to the caller which
880 may be a sample fetch, a converter or an action and returns the specified
Thierry Fournier4234dbd2020-11-28 13:18:23 +0100881 value (ignored for actions and init). It is used when the LUA process finishes
882 its work and wants to give back the control to HAProxy without executing the
Thierry FOURNIER0a99b892015-08-26 00:14:17 +0200883 remaining code. It can be seen as a multi-level "return".
884
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100885.. js:function:: core.yield()
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100886
887 **context**: task, action, sample-fetch, converter
888
889 Give back the hand at the HAProxy scheduler. It is used when the LUA
890 processing consumes a lot of processing time.
891
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +0100892.. js:function:: core.parse_addr(address)
893
894 **context**: body, init, task, action, sample-fetch, converter
895
896 :param network: is a string describing an ipv4 or ipv6 address and optionally
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200897 its network length, like this: "127.0.0.1/8" or "aaaa::1234/32".
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +0100898 :returns: a userdata containing network or nil if an error occurs.
899
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100900 Parse ipv4 or ipv6 addresses and its facultative associated network.
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +0100901
902.. js:function:: core.match_addr(addr1, addr2)
903
904 **context**: body, init, task, action, sample-fetch, converter
905
906 :param addr1: is an address created with "core.parse_addr".
907 :param addr2: is an address created with "core.parse_addr".
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100908 :returns: boolean, true if the network of the addresses match, else returns
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200909 false.
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +0100910
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200911 Match two networks. For example "127.0.0.1/32" matches "127.0.0.0/8". The
912 order of network is not important.
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +0100913
Thierry FOURNIER / OZON.IO8a1027a2016-11-24 20:48:38 +0100914.. js:function:: core.tokenize(str, separators [, noblank])
915
916 **context**: body, init, task, action, sample-fetch, converter
917
918 This function is useful for tokenizing an entry, or splitting some messages.
919 :param string str: The string which will be split.
920 :param string separators: A string containing a list of separators.
921 :param boolean noblank: Ignore empty entries.
922 :returns: an array of string.
923
924 For example:
925
926.. code-block:: lua
927
928 local array = core.tokenize("This function is useful, for tokenizing an entry.", "., ", true)
929 print_r(array)
930..
931
932 Returns this array:
933
934.. code-block:: text
935
936 (table) table: 0x21c01e0 [
937 1: (string) "This"
938 2: (string) "function"
939 3: (string) "is"
940 4: (string) "useful"
941 5: (string) "for"
942 6: (string) "tokenizing"
943 7: (string) "an"
944 8: (string) "entry"
945 ]
946..
947
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +0100948.. js:function:: core.event_sub(event_types, func)
949
950 **context**: body, init, task, action, sample-fetch, converter
951
952 Register a function that will be called on specific system events.
953
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200954 :param array event_types: array of string containing the event types you want
955 to subscribe to
956 :param function func: is the Lua function called when one of the subscribed
957 events occur.
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +0100958 :returns: A :ref:`event_sub_class` object.
Aurelien DARRAGON223770d2023-03-10 15:34:35 +0100959 :see: :js:func:`Server.event_sub()`.
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +0100960
961 List of available event types :
962
963 **SERVER** Family:
964
965 * **SERVER_ADD**: when a server is added
966 * **SERVER_DEL**: when a server is removed
967 * **SERVER_DOWN**: when a server state goes from UP to DOWN
968 * **SERVER_UP**: when a server state goes from DOWN to UP
Aurelien DARRAGONc99f3ad2023-04-12 15:47:16 +0200969 * **SERVER_STATE**: when a server state changes
Aurelien DARRAGON948dd3d2023-04-26 11:27:09 +0200970 * **SERVER_ADMIN**: when a server administrative state changes
Aurelien DARRAGON0bd53b22023-03-30 15:53:33 +0200971 * **SERVER_CHECK**: when a server's check status change is reported.
972 Be careful when subscribing to this type since many events might be
973 generated.
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +0100974
975 .. Note::
Aurelien DARRAGONc99f3ad2023-04-12 15:47:16 +0200976 Use **SERVER** in **event_types** to subscribe to all server events types
977 at once. Note that this should only be used for testing purposes since a
978 single event source could result in multiple events types being generated.
979 (e.g.: SERVER_STATE will always be generated for each SERVER_DOWN or
980 SERVER_UP)
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +0100981
982 The prototype of the Lua function used as argument is:
983
984.. code-block:: lua
985
Aurelien DARRAGON096b3832023-04-20 11:32:46 +0200986 function(event, event_data, sub, when)
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +0100987..
988
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200989 * **event** (*string*): the event type (one of the **event_types** specified
990 when subscribing)
991 * **event_data**: specific to each event family (For **SERVER** family,
992 a :ref:`server_event_class` object)
993 * **sub**: class to manage the subscription from within the event
994 (a :ref:`event_sub_class` object)
Aurelien DARRAGON096b3832023-04-20 11:32:46 +0200995 * **when**: timestamp corresponding to the date when the event was generated.
996 It is an integer representing the number of seconds elapsed since Epoch.
997 It may be provided as optional argument to `os.date()` lua function to
998 convert it to a string according to a given format string.
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +0100999
1000 .. Warning::
1001 The callback function will only be scheduled on the very same thread that
1002 performed the subscription.
1003
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001004 Moreover, each thread treats events sequentially. It means that if you
1005 have, let's say SERVER_UP followed by a SERVER_DOWN in a short timelapse,
1006 then the cb function will first be called with SERVER_UP, and once it's
1007 done handling the event, the cb function will be called again with
1008 SERVER_DOWN.
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +01001009
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001010 This is to ensure event consistency when it comes to logging / triggering
1011 logic from lua.
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +01001012
1013 Your lua cb function may yield if needed, but you're pleased to process the
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001014 event as fast as possible to prevent the event queue from growing up,
1015 depending on the event flow that is expected for the given subscription.
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +01001016
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001017 To prevent abuses, if the event queue for the current subscription goes
1018 over a certain amount of unconsumed events, the subscription will pause
1019 itself automatically for as long as it takes for your handler to catch up.
1020 This would lead to events being missed, so an error will be reported in the
1021 logs to warn you about that.
1022 This is not something you want to let happen too often, it may indicate
1023 that you subscribed to an event that is occurring too frequently or/and
1024 that your callback function is too slow to keep up the pace and you should
1025 review it.
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +01001026
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001027 If you want to do some parallel processing because your callback functions
1028 are slow: you might want to create subtasks from lua using
1029 :js:func:`core.register_task()` from within your callback function to
1030 perform the heavy job in a dedicated task and allow remaining events to be
1031 processed more quickly.
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +01001032
Aurelien DARRAGON5bed48f2023-04-21 17:32:46 +02001033.. js:function:: core.disable_legacy_mailers()
1034
1035 **LEGACY**
1036
1037 **context**: body, init
1038
1039 Disable the sending of email alerts through the legacy email sending
1040 function when mailers are used in the configuration.
1041
1042 Use this when sending email alerts directly from lua.
1043
Aurelien DARRAGON717a38d2023-04-26 19:02:43 +02001044 :see: :js:func:`Proxy.get_mailers()`
1045
Thierry Fournierf61aa632016-02-19 20:56:00 +01001046.. _proxy_class:
1047
1048Proxy class
1049============
1050
1051.. js:class:: Proxy
1052
1053 This class provides a way for manipulating proxy and retrieving information
1054 like statistics.
1055
Thierry FOURNIER817e7592017-07-24 14:35:04 +02001056.. js:attribute:: Proxy.name
1057
1058 Contain the name of the proxy.
1059
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +01001060 .. warning::
1061 This attribute is now deprecated and will eventually be removed.
1062 Please use :js:func:`Proxy.get_name()` function instead.
1063
Thierry Fournierb0467732022-10-07 12:07:24 +02001064.. js:function:: Proxy.get_name()
1065
1066 Returns the name of the proxy.
1067
Baptiste Assmann46c72552017-10-26 21:51:58 +02001068.. js:attribute:: Proxy.uuid
1069
1070 Contain the unique identifier of the proxy.
1071
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +01001072 .. warning::
1073 This attribute is now deprecated and will eventually be removed.
1074 Please use :js:func:`Proxy.get_uuid()` function instead.
1075
Thierry Fournierb0467732022-10-07 12:07:24 +02001076.. js:function:: Proxy.get_uuid()
1077
1078 Returns the unique identifier of the proxy.
1079
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001080.. js:attribute:: Proxy.servers
1081
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001082 Contain a table with the attached servers. The table is indexed by server
1083 name, and each server entry is an object of type :ref:`server_class`.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001084
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02001085.. js:attribute:: Proxy.stktable
1086
Aurelien DARRAGONe5ad3472023-11-23 13:47:54 +01001087 Contains a stick table object of type :ref:`sticktable_class` attached to the
1088 proxy.
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02001089
Thierry Fournierff480422016-02-25 08:36:46 +01001090.. js:attribute:: Proxy.listeners
1091
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001092 Contain a table with the attached listeners. The table is indexed by listener
1093 name, and each each listeners entry is an object of type
1094 :ref:`listener_class`.
Thierry Fournierff480422016-02-25 08:36:46 +01001095
Thierry Fournierf61aa632016-02-19 20:56:00 +01001096.. js:function:: Proxy.pause(px)
1097
1098 Pause the proxy. See the management socket documentation for more information.
1099
1100 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001101 proxy.
Thierry Fournierf61aa632016-02-19 20:56:00 +01001102
1103.. js:function:: Proxy.resume(px)
1104
1105 Resume the proxy. See the management socket documentation for more
1106 information.
1107
1108 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001109 proxy.
Thierry Fournierf61aa632016-02-19 20:56:00 +01001110
1111.. js:function:: Proxy.stop(px)
1112
1113 Stop the proxy. See the management socket documentation for more information.
1114
1115 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001116 proxy.
Thierry Fournierf61aa632016-02-19 20:56:00 +01001117
1118.. js:function:: Proxy.shut_bcksess(px)
1119
1120 Kill the session attached to a backup server. See the management socket
1121 documentation for more information.
1122
1123 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001124 proxy.
Thierry Fournierf61aa632016-02-19 20:56:00 +01001125
1126.. js:function:: Proxy.get_cap(px)
1127
1128 Returns a string describing the capabilities of the proxy.
1129
1130 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001131 proxy.
Thierry Fournierf61aa632016-02-19 20:56:00 +01001132 :returns: a string "frontend", "backend", "proxy" or "ruleset".
1133
1134.. js:function:: Proxy.get_mode(px)
1135
1136 Returns a string describing the mode of the current proxy.
1137
1138 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001139 proxy.
Aurelien DARRAGONa5c331a2023-11-23 16:02:14 +01001140 :returns: a string "tcp", "http" or "unknown"
Thierry Fournierf61aa632016-02-19 20:56:00 +01001141
Aurelien DARRAGONfc845532023-04-03 11:00:18 +02001142.. js:function:: Proxy.get_srv_act(px)
1143
1144 Returns the number of current active servers for the current proxy that are
1145 eligible for LB.
1146
1147 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
1148 proxy.
1149 :returns: an integer
1150
1151.. js:function:: Proxy.get_srv_bck(px)
1152
1153 Returns the number backup servers for the current proxy that are eligible
1154 for LB.
1155
1156 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
1157 proxy.
1158 :returns: an integer
1159
Thierry Fournierf61aa632016-02-19 20:56:00 +01001160.. js:function:: Proxy.get_stats(px)
1161
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001162 Returns a table containing the proxy statistics. The statistics returned are
Thierry Fournierf61aa632016-02-19 20:56:00 +01001163 not the same if the proxy is frontend or a backend.
1164
1165 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001166 proxy.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001167 :returns: a key/value table containing stats
Thierry Fournierf61aa632016-02-19 20:56:00 +01001168
Aurelien DARRAGON717a38d2023-04-26 19:02:43 +02001169.. js:function:: Proxy.get_mailers(px)
1170
1171 **LEGACY**
1172
1173 Returns a table containing mailers config for the current proxy or nil
1174 if mailers are not available for the proxy.
1175
1176 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
1177 proxy.
1178 :returns: a :ref:`proxy_mailers_class` containing proxy mailers config
1179
1180.. _proxy_mailers_class:
1181
1182ProxyMailers class
1183==================
1184
1185**LEGACY**
1186
1187.. js:class:: ProxyMailers
1188
1189 This class provides mailers config for a given proxy.
1190
1191 If sending emails directly from lua, please consider
1192 :js:func:`core.disable_legacy_mailers()` to disable the email sending from
1193 haproxy. (Or email alerts will be sent twice...)
1194
1195.. js:attribute:: ProxyMailers.track_server_health
1196
1197 Boolean set to true if the option "log-health-checks" is configured on
1198 the proxy, meaning that all server checks event should trigger email alerts.
1199
1200.. js:attribute:: ProxyMailers.log_level
1201
1202 An integer, the maximum log level that triggers email alerts. It is a number
1203 between 0 and 7 as defined by option "email-alert level".
1204
1205.. js:attribute:: ProxyMailers.mailservers
1206
1207 An array containing the list of mail servers that should receive email alerts.
1208 Each array entry is a name:desc pair where desc represents the full server
1209 address (including port) as described in haproxy's configuration file.
1210
Aurelien DARRAGON2b8f7ab2023-07-07 16:55:43 +02001211.. js:attribute:: ProxyMailers.mailservers_timeout
1212
1213 An integer representing the maximum time in milliseconds to wait for the
1214 email to be sent. See "timeout mail" directive from "mailers" section in
1215 haproxy configuration file.
1216
Aurelien DARRAGON717a38d2023-04-26 19:02:43 +02001217.. js:attribute:: ProxyMailers.smtp_hostname
1218
1219 A string containing the hostname to use for the SMTP transaction.
1220 (option "email-alert myhostname")
1221
1222.. js:attribute:: ProxyMailers.smtp_from
1223
1224 A string containing the "MAIL FROM" address to use for the SMTP transaction.
1225 (option "email-alert from")
1226
1227.. js:attribute:: ProxyMailers.smtp_to
1228
1229 A string containing the "RCPT TO" address to use for the SMTP transaction.
1230 (option "email-alert to")
1231
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001232.. _server_class:
1233
1234Server class
1235============
1236
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001237.. js:class:: Server
1238
1239 This class provides a way for manipulating servers and retrieving information.
1240
Patrick Hemmera62ae7e2018-04-29 14:23:48 -04001241.. js:attribute:: Server.name
1242
1243 Contain the name of the server.
1244
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +01001245 .. warning::
1246 This attribute is now deprecated and will eventually be removed.
1247 Please use :js:func:`Server.get_name()` function instead.
1248
Thierry Fournierb0467732022-10-07 12:07:24 +02001249.. js:function:: Server.get_name(sv)
1250
1251 Returns the name of the server.
1252
Patrick Hemmera62ae7e2018-04-29 14:23:48 -04001253.. js:attribute:: Server.puid
1254
1255 Contain the proxy unique identifier of the server.
1256
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +01001257 .. warning::
1258 This attribute is now deprecated and will eventually be removed.
1259 Please use :js:func:`Server.get_puid()` function instead.
1260
Thierry Fournierb0467732022-10-07 12:07:24 +02001261.. js:function:: Server.get_puid(sv)
1262
1263 Returns the proxy unique identifier of the server.
1264
Aurelien DARRAGON94ee6632023-03-10 15:11:27 +01001265.. js:function:: Server.get_rid(sv)
1266
1267 Returns the rid (revision ID) of the server.
1268 It is an unsigned integer that is set upon server creation. Value is derived
1269 from a global counter that starts at 0 and is incremented each time one or
1270 multiple server deletions are followed by a server addition (meaning that
1271 old name/id reuse could occur).
1272
1273 Combining server name/id with server rid yields a process-wide unique
1274 identifier.
1275
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001276.. js:function:: Server.is_draining(sv)
1277
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001278 Return true if the server is currently draining sticky connections.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001279
1280 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001281 server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001282 :returns: a boolean
1283
Aurelien DARRAGONc72051d2023-03-29 10:44:38 +02001284.. js:function:: Server.is_backup(sv)
1285
1286 Return true if the server is a backup server
1287
1288 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1289 server.
1290 :returns: a boolean
1291
Aurelien DARRAGON7a03dee2023-03-29 10:49:30 +02001292.. js:function:: Server.is_dynamic(sv)
1293
1294 Return true if the server was instantiated at runtime (e.g.: from the cli)
1295
1296 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1297 server.
1298 :returns: a boolean
1299
Aurelien DARRAGONfc759b42023-04-03 10:43:17 +02001300.. js:function:: Server.get_cur_sess(sv)
1301
1302 Return the number of currently active sessions on the server
1303
1304 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1305 server.
1306 :returns: an integer
1307
1308.. js:function:: Server.get_pend_conn(sv)
1309
1310 Return the number of pending connections to the server
1311
1312 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1313 server.
1314 :returns: an integer
1315
Patrick Hemmer32d539f2018-04-29 14:25:46 -04001316.. js:function:: Server.set_maxconn(sv, weight)
1317
1318 Dynamically change the maximum connections of the server. See the management
1319 socket documentation for more information about the format of the string.
1320
1321 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001322 server.
Patrick Hemmer32d539f2018-04-29 14:25:46 -04001323 :param string maxconn: A string describing the server maximum connections.
1324
1325.. js:function:: Server.get_maxconn(sv, weight)
1326
1327 This function returns an integer representing the server maximum connections.
1328
1329 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001330 server.
Patrick Hemmer32d539f2018-04-29 14:25:46 -04001331 :returns: an integer.
1332
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001333.. js:function:: Server.set_weight(sv, weight)
1334
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001335 Dynamically change the weight of the server. See the management socket
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001336 documentation for more information about the format of the string.
1337
1338 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001339 server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001340 :param string weight: A string describing the server weight.
1341
1342.. js:function:: Server.get_weight(sv)
1343
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001344 This function returns an integer representing the server weight.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001345
1346 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001347 server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001348 :returns: an integer.
1349
Joseph C. Sible49bbf522020-05-04 22:20:32 -04001350.. js:function:: Server.set_addr(sv, addr[, port])
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001351
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001352 Dynamically change the address of the server. See the management socket
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001353 documentation for more information about the format of the string.
1354
1355 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001356 server.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001357 :param string addr: A string describing the server address.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001358
1359.. js:function:: Server.get_addr(sv)
1360
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001361 Returns a string describing the address of the server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001362
1363 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001364 server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001365 :returns: A string
1366
1367.. js:function:: Server.get_stats(sv)
1368
1369 Returns server statistics.
1370
1371 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001372 server.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001373 :returns: a key/value table containing stats
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001374
Aurelien DARRAGON3889efa2023-04-03 14:00:58 +02001375.. js:function:: Server.get_proxy(sv)
1376
1377 Returns the parent proxy to which the server belongs.
1378
1379 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1380 server.
1381 :returns: a :ref:`proxy_class` or nil if not available
1382
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001383.. js:function:: Server.shut_sess(sv)
1384
1385 Shutdown all the sessions attached to the server. See the management socket
1386 documentation for more information about this function.
1387
1388 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001389 server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001390
1391.. js:function:: Server.set_drain(sv)
1392
1393 Drain sticky sessions. See the management socket documentation for more
1394 information about this function.
1395
1396 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001397 server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001398
1399.. js:function:: Server.set_maint(sv)
1400
1401 Set maintenance mode. See the management socket documentation for more
1402 information about this function.
1403
1404 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001405 server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001406
1407.. js:function:: Server.set_ready(sv)
1408
1409 Set normal mode. See the management socket documentation for more information
1410 about this function.
1411
1412 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001413 server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001414
1415.. js:function:: Server.check_enable(sv)
1416
1417 Enable health checks. See the management socket documentation for more
1418 information about this function.
1419
1420 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001421 server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001422
1423.. js:function:: Server.check_disable(sv)
1424
1425 Disable health checks. See the management socket documentation for more
1426 information about this function.
1427
1428 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001429 server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001430
1431.. js:function:: Server.check_force_up(sv)
1432
1433 Force health-check up. See the management socket documentation for more
1434 information about this function.
1435
1436 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001437 server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001438
1439.. js:function:: Server.check_force_nolb(sv)
1440
1441 Force health-check nolb mode. See the management socket documentation for more
1442 information about this function.
1443
1444 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001445 server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001446
1447.. js:function:: Server.check_force_down(sv)
1448
1449 Force health-check down. See the management socket documentation for more
1450 information about this function.
1451
1452 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001453 server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001454
1455.. js:function:: Server.agent_enable(sv)
1456
1457 Enable agent check. See the management socket documentation for more
1458 information about this function.
1459
1460 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001461 server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001462
1463.. js:function:: Server.agent_disable(sv)
1464
1465 Disable agent check. See the management socket documentation for more
1466 information about this function.
1467
1468 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001469 server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001470
1471.. js:function:: Server.agent_force_up(sv)
1472
1473 Force agent check up. See the management socket documentation for more
1474 information about this function.
1475
1476 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001477 server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001478
1479.. js:function:: Server.agent_force_down(sv)
1480
1481 Force agent check down. See the management socket documentation for more
1482 information about this function.
1483
1484 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001485 server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001486
Aurelien DARRAGON406511a2023-03-29 11:30:36 +02001487.. js:function:: Server.tracking(sv)
1488
1489 Check if the current server is tracking another server.
1490
1491 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1492 server.
1493 :returns: A :ref:`server_class` which indicates the tracked server or nil if
1494 the server doesn't track another one.
1495
Aurelien DARRAGON4be36a12023-03-29 14:02:39 +02001496.. js:function:: Server.get_trackers(sv)
1497
1498 Check if the current server is being tracked by other servers.
1499
1500 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1501 server.
1502 :returns: An array of :ref:`server_class` which indicates the tracking
1503 servers (might be empty)
1504
Aurelien DARRAGON223770d2023-03-10 15:34:35 +01001505.. js:function:: Server.event_sub(sv, event_types, func)
1506
1507 Register a function that will be called on specific server events.
1508 It works exactly like :js:func:`core.event_sub()` except that the subscription
1509 will be performed within the server dedicated subscription list instead of the
1510 global one.
1511 (Your callback function will only be called for server events affecting sv)
1512
1513 See :js:func:`core.event_sub()` for function usage.
1514
1515 A key advantage to using :js:func:`Server.event_sub()` over
1516 :js:func:`core.event_sub()` for servers is that :js:func:`Server.event_sub()`
1517 allows you to be notified for servers events of a single server only.
1518 It removes the needs for extra filtering in your callback function if you only
1519 care about a single server, and also prevents useless wakeups.
1520
1521 For instance, if you want to be notified for UP/DOWN events on a given set of
Ilya Shipitsinccf80122023-04-22 20:20:39 +02001522 servers, it is recommended to perform multiple per-server subscriptions since
Aurelien DARRAGON223770d2023-03-10 15:34:35 +01001523 it will be more efficient that doing a single global subscription that will
1524 filter the received events.
1525 Unless you really want to be notified for servers events of ALL servers of
1526 course, which could make sense given you setup but should be avoided if you
1527 have an important number of servers as it will add a significant load on your
1528 haproxy process in case of multiple servers state change in a short amount of
1529 time.
1530
1531 .. Note::
1532 You may also combine :js:func:`core.event_sub()` with
1533 :js:func:`Server.event_sub()`.
1534
1535 Also, don't forget that you can use :js:func:`core.register_task()` from
1536 your callback function if needed. (ie: parallel work)
1537
1538 Here is a working example combining :js:func:`core.event_sub()` with
1539 :js:func:`Server.event_sub()` and :js:func:`core.register_task()`
1540 (This only serves as a demo, this is not necessarily useful to do so)
1541
1542.. code-block:: lua
1543
1544 core.event_sub({"SERVER_ADD"}, function(event, data, sub)
1545 -- in the global event handler
1546 if data["reference"] ~= nil then
1547 print("Tracking new server: ", data["name"])
1548 data["reference"]:event_sub({"SERVER_UP", "SERVER_DOWN"}, function(event, data, sub)
1549 -- in the per-server event handler
1550 if data["reference"] ~= nil then
1551 core.register_task(function(server)
1552 -- subtask to perform some async work (e.g.: HTTP API calls, sending emails...)
1553 print("ASYNC: SERVER ", server:get_name(), " is ", event == "SERVER_UP" and "UP" or "DOWN")
1554 end, data["reference"])
1555 end
1556 end)
1557 end
1558 end)
1559
1560..
1561
1562 In this example, we will first track global server addition events.
1563 For each newly added server ("add server" on the cli), we will register a
1564 UP/DOWN server subscription.
1565 Then, the callback function will schedule the event handling in an async
1566 subtask which will receive the server reference as an argument.
1567
Thierry Fournierff480422016-02-25 08:36:46 +01001568.. _listener_class:
1569
1570Listener class
1571==============
1572
1573.. js:function:: Listener.get_stats(ls)
1574
1575 Returns server statistics.
1576
1577 :param class_listener ls: A :ref:`listener_class` which indicates the
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001578 manipulated listener.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001579 :returns: a key/value table containing stats
Thierry Fournierff480422016-02-25 08:36:46 +01001580
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +01001581.. _event_sub_class:
1582
1583EventSub class
1584==============
1585
1586.. js:function:: EventSub.unsub()
1587
1588 End the subscription, the callback function will not be called again.
1589
1590.. _server_event_class:
1591
1592ServerEvent class
1593=================
1594
Aurelien DARRAGONc4ae8902023-04-17 17:24:48 +02001595.. js:class:: ServerEvent
1596
1597This class is provided with every **SERVER** events.
1598
1599See :js:func:`core.event_sub()` for more info.
1600
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +01001601.. js:attribute:: ServerEvent.name
1602
1603 Contains the name of the server.
1604
1605.. js:attribute:: ServerEvent.puid
1606
1607 Contains the proxy-unique uid of the server
1608
1609.. js:attribute:: ServerEvent.rid
1610
1611 Contains the revision ID of the server
1612
1613.. js:attribute:: ServerEvent.proxy_name
1614
1615 Contains the name of the proxy to which the server belongs
1616
Aurelien DARRAGON55f84c72023-03-22 17:49:04 +01001617.. js:attribute:: ServerEvent.proxy_uuid
1618
1619 Contains the uuid of the proxy to which the server belongs
1620
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +01001621.. js:attribute:: ServerEvent.reference
1622
1623 Reference to the live server (A :ref:`server_class`).
1624
1625 .. Warning::
1626 Not available if the server was removed in the meantime.
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001627 (Will never be set for SERVER_DEL event since the server does not exist
1628 anymore)
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +01001629
Aurelien DARRAGONc99f3ad2023-04-12 15:47:16 +02001630.. js:attribute:: ServerEvent.state
1631
1632 A :ref:`server_event_state_class`
1633
1634 .. Note::
1635 Only available for SERVER_STATE event
1636
Aurelien DARRAGON948dd3d2023-04-26 11:27:09 +02001637.. js:attribute:: ServerEvent.admin
1638
1639 A :ref:`server_event_admin_class`
1640
1641 .. Note::
1642 Only available for SERVER_ADMIN event
1643
Aurelien DARRAGON0bd53b22023-03-30 15:53:33 +02001644.. js:attribute:: ServerEvent.check
1645
1646 A :ref:`server_event_checkres_class`
1647
1648 .. Note::
1649 Only available for SERVER_CHECK event
1650
Aurelien DARRAGONc99f3ad2023-04-12 15:47:16 +02001651.. _server_event_checkres_class:
1652
1653ServerEventCheckRes class
1654=========================
1655
1656.. js:class:: ServerEventCheckRes
1657
1658This class describes the result of a server's check.
1659
1660.. js:attribute:: ServerEventCheckRes.result
1661
1662 Effective check result.
1663
1664 Check result is a string and will be set to one of the following values:
1665 - "FAILED": the check failed
1666 - "PASSED": the check succeeded
1667 - "CONDPASS": the check conditionally passed
1668
1669.. js:attribute:: ServerEventCheckRes.agent
1670
1671 Boolean set to true if the check is an agent check.
1672 Else it is a health check.
1673
1674.. js:attribute:: ServerEventCheckRes.duration
1675
1676 Check's duration in milliseconds
1677
1678.. js:attribute:: ServerEventCheckRes.reason
1679
1680 Check's status. An array containing three fields:
1681 - **short**: a string representing check status short name
1682 - **desc**: a string representing check status description
1683 - **code**: an integer, this extra information is provided for checks
1684 that went through the data analysis stage (>= layer 5)
1685
1686.. js:attribute:: ServerEventCheckRes.health
1687
1688 An array containing values about check's health (integers):
1689 - **cur**: current health counter:
1690 - 0 to (**rise** - 1) = BAD
1691 - **rise** to (**rise** + **fall** - 1) = GOOD
1692 - **rise**: server will be considered as operational after **rise**
1693 consecutive successful checks
1694 - **fall**: server will be considered as dead after **fall** consecutive
1695 unsuccessful checks
1696
1697.. _server_event_state_class:
1698
1699ServerEventState class
1700======================
1701
1702.. js:class:: ServerEventState
1703
1704This class contains additional info related to **SERVER_STATE** event.
1705
1706.. js:attribute:: ServerEventState.admin
1707
1708 Boolean set to true if the server state change is due to an administrative
1709 change. Else it is an operational change.
1710
1711.. js:attribute:: ServerEventState.check
1712
1713 A :ref:`server_event_checkres_class`, provided if the state change is
1714 due to a server check (must be an operational change).
1715
1716.. js:attribute:: ServerEventState.cause
1717
1718 Printable state change cause. Might be empty.
1719
1720.. js:attribute:: ServerEventState.new_state
1721
1722 New server state due to operational or admin change.
1723
1724 It is a string that can be any of the following values:
1725 - "STOPPED": The server is down
1726 - "STOPPING": The server is up but soft-stopping
1727 - "STARTING": The server is warming up
1728 - "RUNNING": The server is fully up
1729
1730.. js:attribute:: ServerEventState.old_state
1731
1732 Previous server state prior to the operational or admin change.
1733
1734 Can be any value described in **new_state**, but they should differ.
1735
1736.. js:attribute:: ServerEventState.requeued
1737
1738 Number of connections that were requeued due to the server state change.
1739
1740 For a server going DOWN: it is the number of pending server connections
1741 that are requeued to the backend (such connections will be redispatched
1742 to any server that is suitable according to the configured load balancing
1743 algorithm).
1744
1745 For a server doing UP: it is the number of pending connections on the
1746 backend that may be redispatched to the server according to the load
1747 balancing algorithm that is in use.
1748
Aurelien DARRAGON948dd3d2023-04-26 11:27:09 +02001749.. _server_event_admin_class:
1750
1751ServerEventAdmin class
1752======================
1753
1754.. js:class:: ServerEventAdmin
1755
1756This class contains additional info related to **SERVER_ADMIN** event.
1757
1758.. js:attribute:: ServerEventAdmin.cause
1759
1760 Printable admin state change cause. Might be empty.
1761
1762.. js:attribute:: ServerEventAdmin.new_admin
1763
1764 New server admin state due to the admin change.
1765
1766 It is an array of string containing a composition of following values:
1767 - "**MAINT**": server is in maintenance mode
1768 - "FMAINT": server is in forced maintenance mode (MAINT is also set)
1769 - "IMAINT": server is in inherited maintenance mode (MAINT is also set)
1770 - "RMAINT": server is in resolve maintenance mode (MAINT is also set)
1771 - "CMAINT": server is in config maintenance mode (MAINT is also set)
1772 - "**DRAIN**": server is in drain mode
1773 - "FDRAIN": server is in forced drain mode (DRAIN is also set)
1774 - "IDRAIN": server is in inherited drain mode (DRAIN is also set)
1775
1776.. js:attribute:: ServerEventAdmin.old_admin
1777
1778 Previous server admin state prior to the admin change.
1779
1780 Values are presented as in **new_admin**, but they should differ.
1781 (Comparing old and new helps to find out the change(s))
1782
1783.. js:attribute:: ServerEventAdmin.requeued
1784
1785 Same as :js:attr:`ServerEventState.requeued` but when the requeue is due to
1786 the server administrative state change.
1787
Aurelien DARRAGON86fb22c2023-05-03 17:03:09 +02001788.. _queue_class:
1789
1790Queue class
1791===========
1792
1793.. js:class:: Queue
1794
1795 This class provides a generic FIFO storage mechanism that may be shared
1796 between multiple lua contexts to easily pass data between them, as stock
1797 Lua doesn't provide easy methods for passing data between multiple coroutines.
1798
1799 inter-task example:
1800
1801.. code-block:: lua
1802
1803 -- script wide shared queue
1804 local queue = core.queue()
1805
1806 -- master task
1807 core.register_task(function()
1808 -- send the date every second
1809 while true do
1810 queue:push(os.date("%c", core.now().sec))
1811 core.sleep(1)
1812 end
1813 end)
1814
1815 -- worker task
1816 core.register_task(function()
1817 while true do
1818 -- print the date sent by master
1819 print(queue:pop_wait())
1820 end
1821 end)
1822..
1823
1824 Of course, queue may also be used as a local storage mechanism.
1825
1826 Use :js:func:`core.queue` to get a new Queue object.
1827
1828.. js:function:: Queue.size(queue)
1829
1830 This function returns the number of items within the Queue.
1831
1832 :param class_queue queue: A :ref:`queue_class` to the current queue
1833
1834.. js:function:: Queue.push(queue, item)
1835
1836 This function pushes the item (may be of any type) to the queue.
1837 Pushed item cannot be nil or invalid, or an error will be thrown.
1838
1839 :param class_queue queue: A :ref:`queue_class` to the current queue
1840 :returns: boolean true for success and false for error
1841
1842.. js:function:: Queue.pop(queue)
1843
1844 This function immediately tries to pop an item from the queue.
1845 It returns nil of no item is available at the time of the call.
1846
1847 :param class_queue queue: A :ref:`queue_class` to the current queue
1848 :returns: the item at the top of the stack (any type) or nil if no items
1849
1850.. js:function:: Queue.pop_wait(queue)
1851
1852 **context**: task
1853
1854 This is an alternative to pop() that may be used within task contexts.
1855
1856 The call waits for data if no item is currently available. This may be
1857 useful when used in a while loop to prevent cpu waste.
1858
1859 Note that this requires yielding, thus it is only available within contexts
1860 that support yielding (mainly task context).
1861
1862 :param class_queue queue: A :ref:`queue_class` to the current queue
1863 :returns: the item at the top of the stack (any type) or nil in case of error
1864
Thierry Fournier1de16592016-01-27 09:49:07 +01001865.. _concat_class:
1866
1867Concat class
1868============
1869
1870.. js:class:: Concat
1871
1872 This class provides a fast way for string concatenation. The way using native
1873 Lua concatenation like the code below is slow for some reasons.
1874
1875.. code-block:: lua
1876
1877 str = "string1"
1878 str = str .. ", string2"
1879 str = str .. ", string3"
1880..
1881
1882 For each concatenation, Lua:
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001883 - allocates memory for the result,
1884 - catenates the two string copying the strings in the new memory block,
1885 - frees the old memory block containing the string which is no longer used.
1886
Thierry Fournier1de16592016-01-27 09:49:07 +01001887 This process does many memory move, allocation and free. In addition, the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001888 memory is not really freed, it is just marked as unused and waits for the
Thierry Fournier1de16592016-01-27 09:49:07 +01001889 garbage collector.
1890
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001891 The Concat class provides an alternative way to concatenate strings. It uses
Thierry Fournier1de16592016-01-27 09:49:07 +01001892 the internal Lua mechanism (it does not allocate memory), but it doesn't copy
1893 the data more than once.
1894
1895 On my computer, the following loops spends 0.2s for the Concat method and
1896 18.5s for the pure Lua implementation. So, the Concat class is about 1000x
1897 faster than the embedded solution.
1898
1899.. code-block:: lua
1900
1901 for j = 1, 100 do
1902 c = core.concat()
1903 for i = 1, 20000 do
1904 c:add("#####")
1905 end
1906 end
1907..
1908
1909.. code-block:: lua
1910
1911 for j = 1, 100 do
1912 c = ""
1913 for i = 1, 20000 do
1914 c = c .. "#####"
1915 end
1916 end
1917..
1918
1919.. js:function:: Concat.add(concat, string)
1920
1921 This function adds a string to the current concatenated string.
1922
1923 :param class_concat concat: A :ref:`concat_class` which contains the currently
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001924 built string.
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001925 :param string string: A new string to concatenate to the current built
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001926 string.
Thierry Fournier1de16592016-01-27 09:49:07 +01001927
1928.. js:function:: Concat.dump(concat)
1929
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001930 This function returns the concatenated string.
Thierry Fournier1de16592016-01-27 09:49:07 +01001931
1932 :param class_concat concat: A :ref:`concat_class` which contains the currently
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001933 built string.
Thierry Fournier1de16592016-01-27 09:49:07 +01001934 :returns: the concatenated string
1935
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001936.. _fetches_class:
1937
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001938Fetches class
1939=============
1940
1941.. js:class:: Fetches
1942
1943 This class contains a lot of internal HAProxy sample fetches. See the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001944 HAProxy "configuration.txt" documentation for more information.
1945 (chapters 7.3.2 to 7.3.6)
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001946
Christopher Faulet1e9b1b62021-08-11 10:14:30 +02001947 .. warning::
1948 some sample fetches are not available in some context. These limitations
1949 are specified in this documentation when they're useful.
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001950
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001951 :see: :js:attr:`TXN.f`
1952 :see: :js:attr:`TXN.sf`
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001953
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001954 Fetches are useful to:
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001955
1956 * get system time,
1957 * get environment variable,
1958 * get random numbers,
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001959 * know backend status like the number of users in queue or the number of
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001960 connections established,
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001961 * get client information like ip source or destination,
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001962 * deal with stick tables,
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001963 * fetch established SSL information,
1964 * fetch HTTP information like headers or method.
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001965
1966.. code-block:: lua
1967
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001968 function action(txn)
1969 -- Get source IP
1970 local clientip = txn.f:src()
1971 end
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001972..
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001973
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001974.. _converters_class:
1975
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001976Converters class
1977================
1978
1979.. js:class:: Converters
1980
1981 This class contains a lot of internal HAProxy sample converters. See the
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001982 HAProxy documentation "configuration.txt" for more information about her
1983 usage. Its the chapter 7.3.1.
1984
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001985 :see: :js:attr:`TXN.c`
1986 :see: :js:attr:`TXN.sc`
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001987
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001988 Converters provides stateful transformation. They are useful to:
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001989
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001990 * convert input to base64,
1991 * apply hash on input string (djb2, crc32, sdbm, wt6),
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001992 * format date,
1993 * json escape,
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001994 * extract preferred language comparing two lists,
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001995 * turn to lower or upper chars,
1996 * deal with stick tables.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001997
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001998.. _channel_class:
1999
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002000Channel class
2001=============
2002
2003.. js:class:: Channel
2004
Christopher Faulet5a2c6612021-08-15 20:35:25 +02002005 **context**: action, sample-fetch, convert, filter
2006
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002007 HAProxy uses two buffers for the processing of the requests. The first one is
2008 used with the request data (from the client to the server) and the second is
2009 used for the response data (from the server to the client).
2010
2011 Each buffer contains two types of data. The first type is the incoming data
2012 waiting for a processing. The second part is the outgoing data already
2013 processed. Usually, the incoming data is processed, after it is tagged as
2014 outgoing data, and finally it is sent. The following functions provides tools
2015 for manipulating these data in a buffer.
2016
2017 The following diagram shows where the channel class function are applied.
2018
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002019 .. image:: _static/channel.png
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002020
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002021 .. warning::
2022 It is not possible to read from the response in request action, and it is
Boyang Li60cfe8b2022-05-10 18:11:00 +00002023 not possible to read from the request channel in response action.
Christopher Faulet09530392021-06-14 11:43:18 +02002024
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002025 .. warning::
2026 It is forbidden to alter the Channels buffer from HTTP contexts. So only
2027 :js:func:`Channel.input`, :js:func:`Channel.output`,
2028 :js:func:`Channel.may_recv`, :js:func:`Channel.is_full` and
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002029 :js:func:`Channel.is_resp` can be called from a HTTP context.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002030
Christopher Faulet5a2c6612021-08-15 20:35:25 +02002031 All the functions provided by this class are available in the
2032 **sample-fetches**, **actions** and **filters** contexts. For **filters**,
2033 incoming data (offset and length) are relative to the filter. Some functions
Boyang Li60cfe8b2022-05-10 18:11:00 +00002034 may yield, but only for **actions**. Yield is not possible for
Christopher Faulet5a2c6612021-08-15 20:35:25 +02002035 **sample-fetches**, **converters** and **filters**.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002036
2037.. js:function:: Channel.append(channel, string)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002038
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002039 This function copies the string **string** at the end of incoming data of the
2040 channel buffer. The function returns the copied length on success or -1 if
2041 data cannot be copied.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002042
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002043 Same that :js:func:`Channel.insert(channel, string, channel:input())`.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002044
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002045 :param class_channel channel: The manipulated Channel.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002046 :param string string: The data to copy at the end of incoming data.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002047 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002048
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002049.. js:function:: Channel.data(channel [, offset [, length]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002050
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002051 This function returns **length** bytes of incoming data from the channel
2052 buffer, starting at the offset **offset**. The data are not removed from the
2053 buffer.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002054
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002055 By default, if no length is provided, all incoming data found, starting at the
2056 given offset, are returned. If **length** is set to -1, the function tries to
Christopher Faulet5a2c6612021-08-15 20:35:25 +02002057 retrieve a maximum of data and, if called by an action, it yields if
2058 necessary. It also waits for more data if the requested length exceeds the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002059 available amount of incoming data. Not providing an offset is the same as
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05002060 setting it to 0. A positive offset is relative to the beginning of incoming
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002061 data of the channel buffer while negative offset is relative to the end.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002062
2063 If there is no incoming data and the channel can't receive more data, a 'nil'
2064 value is returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002065
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002066 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002067 :param integer offset: *optional* The offset in incoming data to start to get
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002068 data. 0 by default. May be negative to be relative to the end of incoming
2069 data.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002070 :param integer length: *optional* The expected length of data to retrieve. All
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002071 incoming data by default. May be set to -1 to get a maximum of data.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002072 :returns: a string containing the data found or nil.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002073
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002074.. js:function:: Channel.forward(channel, length)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002075
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002076 This function forwards **length** bytes of data from the channel buffer. If
Christopher Faulet5a2c6612021-08-15 20:35:25 +02002077 the requested length exceeds the available amount of incoming data, and if
2078 called by an action, the function yields, waiting for more data to forward. It
2079 returns the amount of data forwarded.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002080
2081 :param class_channel channel: The manipulated Channel.
2082 :param integer int: The amount of data to forward.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002083
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002084.. js:function:: Channel.input(channel)
2085
Christopher Faulet5a2c6612021-08-15 20:35:25 +02002086 This function returns the length of incoming data in the channel buffer. When
2087 called by a filter, this value is relative to the filter.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002088
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002089 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002090 :returns: an integer containing the amount of available bytes.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002091
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002092.. js:function:: Channel.insert(channel, string [, offset])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002093
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002094 This function copies the string **string** at the offset **offset** in
2095 incoming data of the channel buffer. The function returns the copied length on
2096 success or -1 if data cannot be copied.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002097
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002098 By default, if no offset is provided, the string is copied in front of
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05002099 incoming data. A positive offset is relative to the beginning of incoming data
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002100 of the channel buffer while negative offset is relative to their end.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002101
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002102 :param class_channel channel: The manipulated Channel.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002103 :param string string: The data to copy into incoming data.
2104 :param integer offset: *optional* The offset in incoming data where to copy
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002105 data. 0 by default. May be negative to be relative to the end of incoming
2106 data.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01002107 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002108
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002109.. js:function:: Channel.is_full(channel)
2110
2111 This function returns true if the channel buffer is full.
2112
Christopher Faulet5a2c6612021-08-15 20:35:25 +02002113 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002114 :returns: a boolean
2115
2116.. js:function:: Channel.is_resp(channel)
2117
2118 This function returns true if the channel is the response one.
2119
Christopher Faulet5a2c6612021-08-15 20:35:25 +02002120 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002121 :returns: a boolean
2122
2123.. js:function:: Channel.line(channel [, offset [, length]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002124
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002125 This function parses **length** bytes of incoming data of the channel buffer,
2126 starting at offset **offset**, and returns the first line found, including the
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002127 '\\n'. The data are not removed from the buffer. If no line is found, all
2128 data are returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002129
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002130 By default, if no length is provided, all incoming data, starting at the given
2131 offset, are evaluated. If **length** is set to -1, the function tries to
Christopher Faulet5a2c6612021-08-15 20:35:25 +02002132 retrieve a maximum of data and, if called by an action, yields if
2133 necessary. It also waits for more data if the requested length exceeds the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002134 available amount of incoming data. Not providing an offset is the same as
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05002135 setting it to 0. A positive offset is relative to the beginning of incoming
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002136 data of the channel buffer while negative offset is relative to the end.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002137
2138 If there is no incoming data and the channel can't receive more data, a 'nil'
2139 value is returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002140
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002141 :param class_channel channel: The manipulated Channel.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002142 :param integer offset: *optional* The offset in incoming data to start to
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002143 parse data. 0 by default. May be negative to be relative to the end of
2144 incoming data.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002145 :param integer length: *optional* The length of data to parse. All incoming
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002146 data by default. May be set to -1 to get a maximum of data.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002147 :returns: a string containing the line found or nil.
2148
2149.. js:function:: Channel.may_recv(channel)
2150
2151 This function returns true if the channel may still receive data.
2152
Christopher Faulet5a2c6612021-08-15 20:35:25 +02002153 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002154 :returns: a boolean
2155
2156.. js:function:: Channel.output(channel)
2157
Christopher Faulet5a2c6612021-08-15 20:35:25 +02002158 This function returns the length of outgoing data of the channel buffer. When
2159 called by a filter, this value is relative to the filter.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002160
2161 :param class_channel channel: The manipulated Channel.
2162 :returns: an integer containing the amount of available bytes.
2163
2164.. js:function:: Channel.prepend(channel, string)
2165
2166 This function copies the string **string** in front of incoming data of the
2167 channel buffer. The function returns the copied length on success or -1 if
2168 data cannot be copied.
2169
2170 Same that :js:func:`Channel.insert(channel, string, 0)`.
2171
2172 :param class_channel channel: The manipulated Channel.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002173 :param string string: The data to copy in front of incoming data.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01002174 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002175
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002176.. js:function:: Channel.remove(channel [, offset [, length]])
2177
2178 This function removes **length** bytes of incoming data of the channel buffer,
2179 starting at offset **offset**. This function returns number of bytes removed
2180 on success.
2181
2182 By default, if no length is provided, all incoming data, starting at the given
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002183 offset, are removed. Not providing an offset is the same as setting it
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05002184 to 0. A positive offset is relative to the beginning of incoming data of the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002185 channel buffer while negative offset is relative to the end.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002186
2187 :param class_channel channel: The manipulated Channel.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002188 :param integer offset: *optional* The offset in incoming data where to start
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002189 to remove data. 0 by default. May be negative to be relative to the end of
2190 incoming data.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002191 :param integer length: *optional* The length of data to remove. All incoming
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002192 data by default.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002193 :returns: an integer containing the amount of bytes removed.
2194
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002195.. js:function:: Channel.send(channel, string)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002196
Christopher Faulet5a2c6612021-08-15 20:35:25 +02002197 This function requires immediate send of the string **string**. It means the
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05002198 string is copied at the beginning of incoming data of the channel buffer and
Christopher Faulet5a2c6612021-08-15 20:35:25 +02002199 immediately forwarded. Unless if the connection is close, and if called by an
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002200 action, this function yields to copy and forward all the string.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002201
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002202 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002203 :param string string: The data to send.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01002204 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002205
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002206.. js:function:: Channel.set(channel, string [, offset [, length]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002207
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002208 This function replaces **length** bytes of incoming data of the channel
2209 buffer, starting at offset **offset**, by the string **string**. The function
2210 returns the copied length on success or -1 if data cannot be copied.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002211
2212 By default, if no length is provided, all incoming data, starting at the given
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002213 offset, are replaced. Not providing an offset is the same as setting it
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05002214 to 0. A positive offset is relative to the beginning of incoming data of the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002215 channel buffer while negative offset is relative to the end.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002216
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002217 :param class_channel channel: The manipulated Channel.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002218 :param string string: The data to copy into incoming data.
2219 :param integer offset: *optional* The offset in incoming data where to start
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002220 the data replacement. 0 by default. May be negative to be relative to the
2221 end of incoming data.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002222 :param integer length: *optional* The length of data to replace. All incoming
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002223 data by default.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002224 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002225
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002226.. js:function:: Channel.dup(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002227
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002228 **DEPRECATED**
2229
2230 This function returns all incoming data found in the channel buffer. The data
Boyang Li60cfe8b2022-05-10 18:11:00 +00002231 are not removed from the buffer and can be reprocessed later.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002232
2233 If there is no incoming data and the channel can't receive more data, a 'nil'
2234 value is returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002235
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002236 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002237 :returns: a string containing all data found or nil.
2238
2239 .. warning::
2240 This function is deprecated. :js:func:`Channel.data()` must be used
2241 instead.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002242
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002243.. js:function:: Channel.get(channel)
2244
2245 **DEPRECATED**
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002246
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002247 This function returns all incoming data found in the channel buffer and remove
2248 them from the buffer.
2249
2250 If there is no incoming data and the channel can't receive more data, a 'nil'
2251 value is returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002252
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002253 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002254 :returns: a string containing all the data found or nil.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002255
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002256 .. warning::
2257 This function is deprecated. :js:func:`Channel.data()` must be used to
2258 retrieve data followed by a call to :js:func:`Channel:remove()` to remove
2259 data.
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01002260
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002261 .. code-block:: lua
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01002262
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002263 local data = chn:data()
2264 chn:remove(0, data:len())
2265
2266 ..
2267
2268.. js:function:: Channel.getline(channel)
2269
2270 **DEPRECATED**
2271
2272 This function returns the first line found in incoming data of the channel
Christopher Faulet5a2c6612021-08-15 20:35:25 +02002273 buffer, including the '\\n'. The returned data are removed from the buffer. If
2274 no line is found, and if called by an action, this function yields to wait for
2275 more data, except if the channel can't receive more data. In this case all
2276 data are returned.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002277
2278 If there is no incoming data and the channel can't receive more data, a 'nil'
2279 value is returned.
2280
2281 :param class_channel channel: The manipulated Channel.
2282 :returns: a string containing the line found or nil.
2283
2284 .. warning::
Boyang Li60cfe8b2022-05-10 18:11:00 +00002285 This function is deprecated. :js:func:`Channel.line()` must be used to
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002286 retrieve a line followed by a call to :js:func:`Channel:remove()` to remove
2287 data.
2288
2289 .. code-block:: lua
2290
2291 local line = chn:line(0, -1)
2292 chn:remove(0, line:len())
2293
2294 ..
2295
2296.. js:function:: Channel.get_in_len(channel)
2297
Boyang Li60cfe8b2022-05-10 18:11:00 +00002298 **DEPRECATED**
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002299
Christopher Faulet5a2c6612021-08-15 20:35:25 +02002300 This function returns the length of the input part of the buffer. When called
2301 by a filter, this value is relative to the filter.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002302
2303 :param class_channel channel: The manipulated Channel.
2304 :returns: an integer containing the amount of available bytes.
2305
2306 .. warning::
2307 This function is deprecated. :js:func:`Channel.input()` must be used
2308 instead.
2309
2310.. js:function:: Channel.get_out_len(channel)
2311
Boyang Li60cfe8b2022-05-10 18:11:00 +00002312 **DEPRECATED**
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002313
Christopher Faulet5a2c6612021-08-15 20:35:25 +02002314 This function returns the length of the output part of the buffer. When called
2315 by a filter, this value is relative to the filter.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002316
2317 :param class_channel channel: The manipulated Channel.
2318 :returns: an integer containing the amount of available bytes.
2319
2320 .. warning::
2321 This function is deprecated. :js:func:`Channel.output()` must be used
2322 instead.
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002323
2324.. _http_class:
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002325
2326HTTP class
2327==========
2328
2329.. js:class:: HTTP
2330
2331 This class contain all the HTTP manipulation functions.
2332
Pieter Baauw386a1272015-08-16 15:26:24 +02002333.. js:function:: HTTP.req_get_headers(http)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002334
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04002335 Returns a table containing all the request headers.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002336
2337 :param class_http http: The related http object.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04002338 :returns: table of headers.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002339 :see: :js:func:`HTTP.res_get_headers`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002340
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04002341 This is the form of the returned table:
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002342
2343.. code-block:: lua
2344
2345 HTTP:req_get_headers()['<header-name>'][<header-index>] = "<header-value>"
2346
2347 local hdr = HTTP:req_get_headers()
2348 hdr["host"][0] = "www.test.com"
2349 hdr["accept"][0] = "audio/basic q=1"
2350 hdr["accept"][1] = "audio/*, q=0.2"
2351 hdr["accept"][2] = "*/*, q=0.1"
2352..
2353
Pieter Baauw386a1272015-08-16 15:26:24 +02002354.. js:function:: HTTP.res_get_headers(http)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002355
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04002356 Returns a table containing all the response headers.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002357
2358 :param class_http http: The related http object.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04002359 :returns: table of headers.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002360 :see: :js:func:`HTTP.req_get_headers`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002361
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04002362 This is the form of the returned table:
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002363
2364.. code-block:: lua
2365
2366 HTTP:res_get_headers()['<header-name>'][<header-index>] = "<header-value>"
2367
2368 local hdr = HTTP:req_get_headers()
2369 hdr["host"][0] = "www.test.com"
2370 hdr["accept"][0] = "audio/basic q=1"
2371 hdr["accept"][1] = "audio/*, q=0.2"
2372 hdr["accept"][2] = "*.*, q=0.1"
2373..
2374
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002375.. js:function:: HTTP.req_add_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002376
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002377 Appends a HTTP header field in the request whose name is
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002378 specified in "name" and whose value is defined in "value".
2379
2380 :param class_http http: The related http object.
2381 :param string name: The header name.
2382 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002383 :see: :js:func:`HTTP.res_add_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002384
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002385.. js:function:: HTTP.res_add_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002386
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002387 Appends a HTTP header field in the response whose name is
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002388 specified in "name" and whose value is defined in "value".
2389
2390 :param class_http http: The related http object.
2391 :param string name: The header name.
2392 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002393 :see: :js:func:`HTTP.req_add_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002394
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002395.. js:function:: HTTP.req_del_header(http, name)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002396
2397 Removes all HTTP header fields in the request whose name is
2398 specified in "name".
2399
2400 :param class_http http: The related http object.
2401 :param string name: The header name.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002402 :see: :js:func:`HTTP.res_del_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002403
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002404.. js:function:: HTTP.res_del_header(http, name)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002405
2406 Removes all HTTP header fields in the response whose name is
2407 specified in "name".
2408
2409 :param class_http http: The related http object.
2410 :param string name: The header name.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002411 :see: :js:func:`HTTP.req_del_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002412
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002413.. js:function:: HTTP.req_set_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002414
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002415 This variable replace all occurrence of all header "name", by only
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002416 one containing the "value".
2417
2418 :param class_http http: The related http object.
2419 :param string name: The header name.
2420 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002421 :see: :js:func:`HTTP.res_set_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002422
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002423 This function does the same work as the following code:
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002424
2425.. code-block:: lua
2426
2427 function fcn(txn)
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002428 TXN.http:req_del_header("header")
2429 TXN.http:req_add_header("header", "value")
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002430 end
2431..
2432
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002433.. js:function:: HTTP.res_set_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002434
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002435 This function replaces all occurrence of all header "name", by only
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002436 one containing the "value".
2437
2438 :param class_http http: The related http object.
2439 :param string name: The header name.
2440 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002441 :see: :js:func:`HTTP.req_rep_header()`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002442
Pieter Baauw386a1272015-08-16 15:26:24 +02002443.. js:function:: HTTP.req_rep_header(http, name, regex, replace)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002444
2445 Matches the regular expression in all occurrences of header field "name"
2446 according to "regex", and replaces them with the "replace" argument. The
2447 replacement value can contain back references like \1, \2, ... This
2448 function works with the request.
2449
2450 :param class_http http: The related http object.
2451 :param string name: The header name.
2452 :param string regex: The match regular expression.
2453 :param string replace: The replacement value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002454 :see: :js:func:`HTTP.res_rep_header()`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002455
Pieter Baauw386a1272015-08-16 15:26:24 +02002456.. js:function:: HTTP.res_rep_header(http, name, regex, string)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002457
2458 Matches the regular expression in all occurrences of header field "name"
2459 according to "regex", and replaces them with the "replace" argument. The
2460 replacement value can contain back references like \1, \2, ... This
2461 function works with the request.
2462
2463 :param class_http http: The related http object.
2464 :param string name: The header name.
2465 :param string regex: The match regular expression.
2466 :param string replace: The replacement value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002467 :see: :js:func:`HTTP.req_rep_header()`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002468
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002469.. js:function:: HTTP.req_set_method(http, method)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002470
2471 Rewrites the request method with the parameter "method".
2472
2473 :param class_http http: The related http object.
2474 :param string method: The new method.
2475
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002476.. js:function:: HTTP.req_set_path(http, path)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002477
2478 Rewrites the request path with the "path" parameter.
2479
2480 :param class_http http: The related http object.
2481 :param string path: The new path.
2482
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002483.. js:function:: HTTP.req_set_query(http, query)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002484
2485 Rewrites the request's query string which appears after the first question
2486 mark ("?") with the parameter "query".
2487
2488 :param class_http http: The related http object.
2489 :param string query: The new query.
2490
Thierry FOURNIER0d79cf62015-08-26 14:20:58 +02002491.. js:function:: HTTP.req_set_uri(http, uri)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002492
2493 Rewrites the request URI with the parameter "uri".
2494
2495 :param class_http http: The related http object.
2496 :param string uri: The new uri.
2497
Robin H. Johnson52f5db22017-01-01 13:10:52 -08002498.. js:function:: HTTP.res_set_status(http, status [, reason])
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02002499
Robin H. Johnson52f5db22017-01-01 13:10:52 -08002500 Rewrites the response status code with the parameter "code".
2501
2502 If no custom reason is provided, it will be generated from the status.
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02002503
2504 :param class_http http: The related http object.
2505 :param integer status: The new response status code.
Robin H. Johnson52f5db22017-01-01 13:10:52 -08002506 :param string reason: The new response reason (optional).
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02002507
William Lallemand00a15022021-11-19 16:02:44 +01002508.. _httpclient_class:
2509
2510HTTPClient class
2511================
2512
2513.. js:class:: HTTPClient
2514
2515 The httpclient class allows issue of outbound HTTP requests through a simple
2516 API without the knowledge of HAProxy internals.
2517
2518.. js:function:: HTTPClient.get(httpclient, request)
2519.. js:function:: HTTPClient.head(httpclient, request)
2520.. js:function:: HTTPClient.put(httpclient, request)
2521.. js:function:: HTTPClient.post(httpclient, request)
2522.. js:function:: HTTPClient.delete(httpclient, request)
2523
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002524 Send a HTTP request and wait for a response. GET, HEAD PUT, POST and DELETE
2525 methods can be used.
2526 The HTTPClient will send asynchronously the data and is able to send and
2527 receive more than HAProxy bufsize.
William Lallemand00a15022021-11-19 16:02:44 +01002528
William Lallemanda9256192022-10-21 11:48:24 +02002529 The HTTPClient interface is not able to decompress responses, it is not
2530 recommended to send an Accept-Encoding in the request so the response is
2531 received uncompressed.
William Lallemand00a15022021-11-19 16:02:44 +01002532
2533 :param class httpclient: Is the manipulated HTTPClient.
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002534 :param table request: Is a table containing the parameters of the request
2535 that will be send.
2536 :param string request.url: Is a mandatory parameter for the request that
2537 contains the URL.
2538 :param string request.body: Is an optional parameter for the request that
2539 contains the body to send.
2540 :param table request.headers: Is an optional parameter for the request that
2541 contains the headers to send.
2542 :param string request.dst: Is an optional parameter for the destination in
2543 haproxy address format.
2544 :param integer request.timeout: Optional timeout parameter, set a
2545 "timeout server" on the connections.
William Lallemand00a15022021-11-19 16:02:44 +01002546 :returns: Lua table containing the response
2547
2548
2549.. code-block:: lua
2550
2551 local httpclient = core.httpclient()
William Lallemand4f4f2b72022-02-17 20:00:23 +01002552 local response = httpclient:post{url="http://127.0.0.1", body=body, dst="unix@/var/run/http.sock"}
William Lallemand00a15022021-11-19 16:02:44 +01002553
2554..
2555
2556.. code-block:: lua
2557
2558 response = {
2559 status = 400,
2560 reason = "Bad request",
2561 headers = {
2562 ["content-type"] = { "text/html" },
2563 ["cache-control"] = { "no-cache", "no-store" },
2564 },
William Lallemand4f4f2b72022-02-17 20:00:23 +01002565 body = "<html><body><h1>invalid request<h1></body></html>",
William Lallemand00a15022021-11-19 16:02:44 +01002566 }
2567..
2568
2569
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002570.. _txn_class:
2571
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002572TXN class
2573=========
2574
2575.. js:class:: TXN
2576
2577 The txn class contain all the functions relative to the http or tcp
2578 transaction (Note than a tcp stream is the same than a tcp transaction, but
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002579 a HTTP transaction is not the same than a tcp stream).
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002580
2581 The usage of this class permits to retrieve data from the requests, alter it
2582 and forward it.
2583
2584 All the functions provided by this class are available in the context
Christopher Faulet5a2c6612021-08-15 20:35:25 +02002585 **sample-fetches**, **actions** and **filters**.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002586
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002587.. js:attribute:: TXN.c
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002588
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002589 :returns: An :ref:`converters_class`.
2590
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002591 This attribute contains a Converters class object.
2592
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002593.. js:attribute:: TXN.sc
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002594
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002595 :returns: An :ref:`converters_class`.
2596
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002597 This attribute contains a Converters class object. The functions of
2598 this object returns always a string.
2599
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002600.. js:attribute:: TXN.f
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002601
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002602 :returns: An :ref:`fetches_class`.
2603
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002604 This attribute contains a Fetches class object.
2605
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002606.. js:attribute:: TXN.sf
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002607
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002608 :returns: An :ref:`fetches_class`.
2609
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002610 This attribute contains a Fetches class object. The functions of
2611 this object returns always a string.
2612
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002613.. js:attribute:: TXN.req
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002614
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002615 :returns: An :ref:`channel_class`.
2616
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002617 This attribute contains a channel class object for the request buffer.
2618
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002619.. js:attribute:: TXN.res
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002620
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002621 :returns: An :ref:`channel_class`.
2622
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002623 This attribute contains a channel class object for the response buffer.
2624
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002625.. js:attribute:: TXN.http
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002626
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002627 :returns: An :ref:`http_class`.
2628
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002629 This attribute contains a HTTP class object. It is available only if the
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002630 proxy has the "mode http" enabled.
2631
Christopher Faulet5a2c6612021-08-15 20:35:25 +02002632.. js:attribute:: TXN.http_req
2633
2634 :returns: An :ref:`httpmessage_class`.
2635
2636 This attribute contains the request HTTPMessage class object. It is available
2637 only if the proxy has the "mode http" enabled and only in the **filters**
2638 context.
2639
2640.. js:attribute:: TXN.http_res
2641
2642 :returns: An :ref:`httpmessage_class`.
2643
2644 This attribute contains the response HTTPMessage class object. It is available
2645 only if the proxy has the "mode http" enabled and only in the **filters**
2646 context.
2647
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002648.. js:function:: TXN.log(TXN, loglevel, msg)
2649
2650 This function sends a log. The log is sent, according with the HAProxy
Tristan2632d042023-10-23 13:07:39 +01002651 configuration file, to the loggers relevant to the current context and
2652 to stderr if it is allowed.
2653
2654 The exact behaviour depends on tune.lua.log.loggers and tune.lua.log.stderr.
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002655
2656 :param class_txn txn: The class txn object containing the data.
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002657 :param integer loglevel: Is the log level associated with the message. It is
2658 a number between 0 and 7.
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002659 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002660 :see: :js:attr:`core.emerg`, :js:attr:`core.alert`, :js:attr:`core.crit`,
2661 :js:attr:`core.err`, :js:attr:`core.warning`, :js:attr:`core.notice`,
2662 :js:attr:`core.info`, :js:attr:`core.debug` (log level definitions)
2663 :see: :js:func:`TXN.deflog`
2664 :see: :js:func:`TXN.Debug`
2665 :see: :js:func:`TXN.Info`
2666 :see: :js:func:`TXN.Warning`
2667 :see: :js:func:`TXN.Alert`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002668
2669.. js:function:: TXN.deflog(TXN, msg)
2670
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002671 Sends a log line with the default loglevel for the proxy associated with the
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002672 transaction.
2673
2674 :param class_txn txn: The class txn object containing the data.
2675 :param string msg: The log content.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002676 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002677
2678.. js:function:: TXN.Debug(txn, msg)
2679
2680 :param class_txn txn: The class txn object containing the data.
2681 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002682 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002683
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002684 Does the same job as:
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002685
2686.. code-block:: lua
2687
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002688 function Debug(txn, msg)
2689 TXN.log(txn, core.debug, msg)
2690 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002691..
2692
2693.. js:function:: TXN.Info(txn, msg)
2694
2695 :param class_txn txn: The class txn object containing the data.
2696 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002697 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002698
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002699 Does the same job as:
2700
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002701.. code-block:: lua
2702
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002703 function Info(txn, msg)
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002704 TXN.log(txn, core.info, msg)
2705 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002706..
2707
2708.. js:function:: TXN.Warning(txn, msg)
2709
2710 :param class_txn txn: The class txn object containing the data.
2711 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002712 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002713
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002714 Does the same job as:
2715
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002716.. code-block:: lua
2717
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002718 function Warning(txn, msg)
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002719 TXN.log(txn, core.warning, msg)
2720 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002721..
2722
2723.. js:function:: TXN.Alert(txn, msg)
2724
2725 :param class_txn txn: The class txn object containing the data.
2726 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002727 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002728
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002729 Does the same job as:
2730
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002731.. code-block:: lua
2732
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002733 function Alert(txn, msg)
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002734 TXN.log(txn, core.alert, msg)
2735 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002736..
2737
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002738.. js:function:: TXN.get_priv(txn)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002739
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002740 Return Lua data stored in the current transaction (with the `TXN.set_priv()`)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002741 function. If no data are stored, it returns a nil value.
2742
2743 :param class_txn txn: The class txn object containing the data.
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002744 :returns: the opaque data previously stored, or nil if nothing is
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002745 available.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002746
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002747.. js:function:: TXN.set_priv(txn, data)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002748
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002749 Store any data in the current HAProxy transaction. This action replaces the
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002750 old stored data.
2751
2752 :param class_txn txn: The class txn object containing the data.
2753 :param opaque data: The data which is stored in the transaction.
2754
Tim Duesterhus4e172c92020-05-19 13:49:42 +02002755.. js:function:: TXN.set_var(TXN, var, value[, ifexist])
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02002756
David Carlier61fdf8b2015-10-02 11:59:38 +01002757 Converts a Lua type in a HAProxy type and store it in a variable <var>.
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02002758
2759 :param class_txn txn: The class txn object containing the data.
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002760 :param string var: The variable name according with the HAProxy variable
2761 syntax.
2762 :param type value: The value associated to the variable. The type can be
2763 string or integer.
2764 :param boolean ifexist: If this parameter is set to true the variable will
2765 only be set if it was defined elsewhere (i.e. used within the configuration).
2766 For global variables (using the "proc" scope), they will only be updated and
2767 never created. It is highly recommended to always set this to true.
Christopher Faulet85d79c92016-11-09 16:54:56 +01002768
2769.. js:function:: TXN.unset_var(TXN, var)
2770
2771 Unset the variable <var>.
2772
2773 :param class_txn txn: The class txn object containing the data.
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002774 :param string var: The variable name according with the HAProxy variable
2775 syntax.
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02002776
2777.. js:function:: TXN.get_var(TXN, var)
2778
2779 Returns data stored in the variable <var> converter in Lua type.
2780
2781 :param class_txn txn: The class txn object containing the data.
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002782 :param string var: The variable name according with the HAProxy variable
2783 syntax.
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02002784
Christopher Faulet700d9e82020-01-31 12:21:52 +01002785.. js:function:: TXN.reply([reply])
2786
2787 Return a new reply object
2788
2789 :param table reply: A table containing info to initialize the reply fields.
2790 :returns: A :ref:`reply_class` object.
2791
2792 The table used to initialized the reply object may contain following entries :
2793
2794 * status : The reply status code. the code 200 is used by default.
2795 * reason : The reply reason. The reason corresponding to the status code is
2796 used by default.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002797 * headers : A list of headers, indexed by header name. Empty by default. For
Christopher Faulet700d9e82020-01-31 12:21:52 +01002798 a given name, multiple values are possible, stored in an ordered list.
2799 * body : The reply body, empty by default.
2800
2801.. code-block:: lua
2802
2803 local reply = txn:reply{
2804 status = 400,
2805 reason = "Bad request",
2806 headers = {
2807 ["content-type"] = { "text/html" },
2808 ["cache-control"] = {"no-cache", "no-store" }
2809 },
2810 body = "<html><body><h1>invalid request<h1></body></html>"
2811 }
2812..
2813 :see: :js:class:`Reply`
2814
2815.. js:function:: TXN.done(txn[, reply])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002816
Willy Tarreaubc183a62015-08-28 10:39:11 +02002817 This function terminates processing of the transaction and the associated
Christopher Faulet700d9e82020-01-31 12:21:52 +01002818 session and optionally reply to the client for HTTP sessions.
2819
2820 :param class_txn txn: The class txn object containing the data.
2821 :param class_reply reply: The class reply object to return to the client.
2822
2823 This functions can be used when a critical error is detected or to terminate
Willy Tarreaubc183a62015-08-28 10:39:11 +02002824 processing after some data have been returned to the client (eg: a redirect).
Christopher Faulet700d9e82020-01-31 12:21:52 +01002825 To do so, a reply may be provided. This object is optional and may contain a
2826 status code, a reason, a header list and a body. All these fields are
Christopher Faulet7855b192021-11-09 18:39:51 +01002827 optional. When not provided, the default values are used. By default, with an
2828 empty reply object, an empty HTTP 200 response is returned to the client. If
2829 no reply object is provided, the transaction is terminated without any
2830 reply. If a reply object is provided, it must not exceed the buffer size once
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002831 converted into the internal HTTP representation. Because for now there is no
Christopher Faulet7855b192021-11-09 18:39:51 +01002832 easy way to be sure it fits, it is probably better to keep it reasonably
2833 small.
Christopher Faulet700d9e82020-01-31 12:21:52 +01002834
2835 The reply object may be fully created in lua or the class Reply may be used to
2836 create it.
2837
2838.. code-block:: lua
2839
2840 local reply = txn:reply()
2841 reply:set_status(400, "Bad request")
2842 reply:add_header("content-type", "text/html")
2843 reply:add_header("cache-control", "no-cache")
2844 reply:add_header("cache-control", "no-store")
2845 reply:set_body("<html><body><h1>invalid request<h1></body></html>")
2846 txn:done(reply)
2847..
2848
2849.. code-block:: lua
2850
2851 txn:done{
2852 status = 400,
2853 reason = "Bad request",
2854 headers = {
2855 ["content-type"] = { "text/html" },
2856 ["cache-control"] = { "no-cache", "no-store" },
2857 },
2858 body = "<html><body><h1>invalid request<h1></body></html>"
2859 }
2860..
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002861
Christopher Faulet1e9b1b62021-08-11 10:14:30 +02002862 .. warning::
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002863 It does not make sense to call this function from sample-fetches. In this
2864 case the behavior is the same than core.done(): it finishes the Lua
Christopher Faulet1e9b1b62021-08-11 10:14:30 +02002865 execution. The transaction is really aborted only from an action registered
2866 function.
Thierry FOURNIERab00df62016-07-14 11:42:37 +02002867
Christopher Faulet700d9e82020-01-31 12:21:52 +01002868 :see: :js:func:`TXN.reply`, :js:class:`Reply`
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002869
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002870.. js:function:: TXN.set_loglevel(txn, loglevel)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01002871
2872 Is used to change the log level of the current request. The "loglevel" must
Christopher Faulet12a5ee72024-02-29 15:41:17 +01002873 be an integer between 0 and 7 or the special value -1 to disable logging.
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01002874
2875 :param class_txn txn: The class txn object containing the data.
2876 :param integer loglevel: The required log level. This variable can be one of
Christopher Faulet12a5ee72024-02-29 15:41:17 +01002877 :see: :js:attr:`core.silent`, :js:attr:`core.emerg`, :js:attr:`core.alert`,
2878 :js:attr:`core.crit`, :js:attr:`core.err`, :js:attr:`core.warning`, :js:attr:`core.notice`,
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002879 :js:attr:`core.info`, :js:attr:`core.debug` (log level definitions)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01002880
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002881.. js:function:: TXN.set_tos(txn, tos)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01002882
2883 Is used to set the TOS or DSCP field value of packets sent to the client to
2884 the value passed in "tos" on platforms which support this.
2885
2886 :param class_txn txn: The class txn object containing the data.
2887 :param integer tos: The new TOS os DSCP.
2888
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002889.. js:function:: TXN.set_mark(txn, mark)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01002890
2891 Is used to set the Netfilter MARK on all packets sent to the client to the
2892 value passed in "mark" on platforms which support it.
2893
2894 :param class_txn txn: The class txn object containing the data.
2895 :param integer mark: The mark value.
2896
Patrick Hemmer268a7072018-05-11 12:52:31 -04002897.. js:function:: TXN.set_priority_class(txn, prio)
2898
2899 This function adjusts the priority class of the transaction. The value should
2900 be within the range -2047..2047. Values outside this range will be
2901 truncated.
2902
2903 See the HAProxy configuration.txt file keyword "http-request" action
2904 "set-priority-class" for details.
2905
2906.. js:function:: TXN.set_priority_offset(txn, prio)
2907
2908 This function adjusts the priority offset of the transaction. The value
2909 should be within the range -524287..524287. Values outside this range will be
2910 truncated.
2911
2912 See the HAProxy configuration.txt file keyword "http-request" action
2913 "set-priority-offset" for details.
2914
Christopher Faulet700d9e82020-01-31 12:21:52 +01002915.. _reply_class:
2916
2917Reply class
2918============
2919
2920.. js:class:: Reply
2921
2922 **context**: action
2923
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002924 This class represents a HTTP response message. It provides some methods to
Christopher Faulet7855b192021-11-09 18:39:51 +01002925 enrich it. Once converted into the internal HTTP representation, the response
2926 message must not exceed the buffer size. Because for now there is no
2927 easy way to be sure it fits, it is probably better to keep it reasonably
2928 small.
2929
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002930 See tune.bufsize in the configuration manual for details.
Christopher Faulet700d9e82020-01-31 12:21:52 +01002931
2932.. code-block:: lua
2933
2934 local reply = txn:reply({status = 400}) -- default HTTP 400 reason-phase used
2935 reply:add_header("content-type", "text/html")
2936 reply:add_header("cache-control", "no-cache")
2937 reply:add_header("cache-control", "no-store")
2938 reply:set_body("<html><body><h1>invalid request<h1></body></html>")
2939..
2940
2941 :see: :js:func:`TXN.reply`
2942
2943.. js:attribute:: Reply.status
2944
2945 The reply status code. By default, the status code is set to 200.
2946
2947 :returns: integer
2948
2949.. js:attribute:: Reply.reason
2950
2951 The reason string describing the status code.
2952
2953 :returns: string
2954
2955.. js:attribute:: Reply.headers
2956
2957 A table indexing all reply headers by name. To each name is associated an
2958 ordered list of values.
2959
2960 :returns: Lua table
2961
2962.. code-block:: lua
2963
2964 {
2965 ["content-type"] = { "text/html" },
2966 ["cache-control"] = {"no-cache", "no-store" },
2967 x_header_name = { "value1", "value2", ... }
2968 ...
2969 }
2970..
2971
2972.. js:attribute:: Reply.body
2973
2974 The reply payload.
2975
2976 :returns: string
2977
2978.. js:function:: Reply.set_status(REPLY, status[, reason])
2979
2980 Set the reply status code and optionally the reason-phrase. If the reason is
2981 not provided, the default reason corresponding to the status code is used.
2982
2983 :param class_reply reply: The related Reply object.
2984 :param integer status: The reply status code.
2985 :param string reason: The reply status reason (optional).
2986
2987.. js:function:: Reply.add_header(REPLY, name, value)
2988
2989 Add a header to the reply object. If the header does not already exist, a new
2990 entry is created with its name as index and a one-element list containing its
2991 value as value. Otherwise, the header value is appended to the ordered list of
2992 values associated to the header name.
2993
2994 :param class_reply reply: The related Reply object.
2995 :param string name: The header field name.
2996 :param string value: The header field value.
2997
2998.. js:function:: Reply.del_header(REPLY, name)
2999
3000 Remove all occurrences of a header name from the reply object.
3001
3002 :param class_reply reply: The related Reply object.
3003 :param string name: The header field name.
3004
3005.. js:function:: Reply.set_body(REPLY, body)
3006
3007 Set the reply payload.
3008
3009 :param class_reply reply: The related Reply object.
3010 :param string body: The reply payload.
3011
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003012.. _socket_class:
3013
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003014Socket class
3015============
3016
3017.. js:class:: Socket
3018
3019 This class must be compatible with the Lua Socket class. Only the 'client'
3020 functions are available. See the Lua Socket documentation:
3021
3022 `http://w3.impa.br/~diego/software/luasocket/tcp.html
3023 <http://w3.impa.br/~diego/software/luasocket/tcp.html>`_
3024
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01003025.. js:function:: Socket.close(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003026
3027 Closes a TCP object. The internal socket used by the object is closed and the
3028 local address to which the object was bound is made available to other
3029 applications. No further operations (except for further calls to the close
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01003030 method) are allowed on a closed Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003031
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01003032 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003033
3034 Note: It is important to close all used sockets once they are not needed,
3035 since, in many systems, each socket uses a file descriptor, which are limited
3036 system resources. Garbage-collected objects are automatically closed before
3037 destruction, though.
3038
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003039.. js:function:: Socket.connect(socket, address[, port])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003040
3041 Attempts to connect a socket object to a remote host.
3042
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003043
3044 In case of error, the method returns nil followed by a string describing the
3045 error. In case of success, the method returns 1.
3046
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01003047 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003048 :param string address: can be an IP address or a host name. See below for more
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003049 information.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003050 :param integer port: must be an integer number in the range [1..64K].
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003051 :returns: 1 or nil.
3052
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01003053 An address field extension permits to use the connect() function to connect to
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003054 other stream than TCP. The syntax containing a simpleipv4 or ipv6 address is
3055 the basically expected format. This format requires the port.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003056
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003057 Other format accepted are a socket path like "/socket/path", it permits to
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01003058 connect to a socket. Abstract namespaces are supported with the prefix
Joseph Herlant02cedc42018-11-13 19:45:17 -08003059 "abns@", and finally a file descriptor can be passed with the prefix "fd@".
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003060 The prefix "ipv4@", "ipv6@" and "unix@" are also supported. The port can be
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01003061 passed int the string. The syntax "127.0.0.1:1234" is valid. In this case, the
Tim Duesterhus6edab862018-01-06 19:04:45 +01003062 parameter *port* must not be set.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003063
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01003064.. js:function:: Socket.connect_ssl(socket, address, port)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003065
3066 Same behavior than the function socket:connect, but uses SSL.
3067
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01003068 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003069 :returns: 1 or nil.
3070
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01003071.. js:function:: Socket.getpeername(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003072
3073 Returns information about the remote side of a connected client object.
3074
3075 Returns a string with the IP address of the peer, followed by the port number
3076 that peer is using for the connection. In case of error, the method returns
3077 nil.
3078
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01003079 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003080 :returns: a string containing the server information.
3081
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01003082.. js:function:: Socket.getsockname(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003083
3084 Returns the local address information associated to the object.
3085
3086 The method returns a string with local IP address and a number with the port.
3087 In case of error, the method returns nil.
3088
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01003089 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003090 :returns: a string containing the client information.
3091
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01003092.. js:function:: Socket.receive(socket, [pattern [, prefix]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003093
3094 Reads data from a client object, according to the specified read pattern.
3095 Patterns follow the Lua file I/O format, and the difference in performance
3096 between all patterns is negligible.
3097
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01003098 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003099 :param string|integer pattern: Describe what is required (see below).
3100 :param string prefix: A string which will be prefix the returned data.
3101 :returns: a string containing the required data or nil.
3102
3103 Pattern can be any of the following:
3104
3105 * **`*a`**: reads from the socket until the connection is closed. No
3106 end-of-line translation is performed;
3107
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01003108 * **`*l`**: reads a line of text from the Socket. The line is terminated by a
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003109 LF character (ASCII 10), optionally preceded by a CR character
3110 (ASCII 13). The CR and LF characters are not included in the
3111 returned line. In fact, all CR characters are ignored by the
3112 pattern. This is the default pattern.
3113
3114 * **number**: causes the method to read a specified number of bytes from the
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01003115 Socket. Prefix is an optional string to be concatenated to the
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003116 beginning of any received data before return.
3117
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003118 * **empty**: If the pattern is left empty, the default option is `*l`.
3119
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003120 If successful, the method returns the received pattern. In case of error, the
3121 method returns nil followed by an error message which can be the string
3122 'closed' in case the connection was closed before the transmission was
3123 completed or the string 'timeout' in case there was a timeout during the
3124 operation. Also, after the error message, the function returns the partial
3125 result of the transmission.
3126
3127 Important note: This function was changed severely. It used to support
3128 multiple patterns (but I have never seen this feature used) and now it
3129 doesn't anymore. Partial results used to be returned in the same way as
3130 successful results. This last feature violated the idea that all functions
3131 should return nil on error. Thus it was changed too.
3132
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01003133.. js:function:: Socket.send(socket, data [, start [, end ]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003134
3135 Sends data through client object.
3136
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01003137 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003138 :param string data: The data that will be sent.
3139 :param integer start: The start position in the buffer of the data which will
3140 be sent.
3141 :param integer end: The end position in the buffer of the data which will
3142 be sent.
3143 :returns: see below.
3144
3145 Data is the string to be sent. The optional arguments i and j work exactly
3146 like the standard string.sub Lua function to allow the selection of a
3147 substring to be sent.
3148
3149 If successful, the method returns the index of the last byte within [start,
3150 end] that has been sent. Notice that, if start is 1 or absent, this is
3151 effectively the total number of bytes sent. In case of error, the method
3152 returns nil, followed by an error message, followed by the index of the last
3153 byte within [start, end] that has been sent. You might want to try again from
3154 the byte following that. The error message can be 'closed' in case the
3155 connection was closed before the transmission was completed or the string
3156 'timeout' in case there was a timeout during the operation.
3157
3158 Note: Output is not buffered. For small strings, it is always better to
3159 concatenate them in Lua (with the '..' operator) and send the result in one
3160 call instead of calling the method several times.
3161
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01003162.. js:function:: Socket.setoption(socket, option [, value])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003163
3164 Just implemented for compatibility, this cal does nothing.
3165
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01003166.. js:function:: Socket.settimeout(socket, value [, mode])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003167
3168 Changes the timeout values for the object. All I/O operations are blocking.
3169 That is, any call to the methods send, receive, and accept will block
3170 indefinitely, until the operation completes. The settimeout method defines a
3171 limit on the amount of time the I/O methods can block. When a timeout time
3172 has elapsed, the affected methods give up and fail with an error code.
3173
3174 The amount of time to wait is specified as the value parameter, in seconds.
3175
Mark Lakes56cc1252018-03-27 09:48:06 +02003176 The timeout modes are not implemented, the only settable timeout is the
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003177 inactivity time waiting for complete the internal buffer send or waiting for
3178 receive data.
3179
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01003180 :param class_socket socket: Is the manipulated Socket.
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01003181 :param float value: The timeout value. Use floating point to specify
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003182 milliseconds.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003183
Thierry FOURNIER31904272017-10-25 12:59:51 +02003184.. _regex_class:
3185
3186Regex class
3187===========
3188
3189.. js:class:: Regex
3190
3191 This class allows the usage of HAProxy regexes because classic lua doesn't
3192 provides regexes. This class inherits the HAProxy compilation options, so the
3193 regexes can be libc regex, pcre regex or pcre JIT regex.
3194
3195 The expression matching number is limited to 20 per regex. The only available
3196 option is case sensitive.
3197
3198 Because regexes compilation is a heavy process, it is better to define all
3199 your regexes in the **body context** and use it during the runtime.
3200
3201.. code-block:: lua
3202
3203 -- Create the regex
3204 st, regex = Regex.new("needle (..) (...)", true);
3205
3206 -- Check compilation errors
3207 if st == false then
3208 print "error: " .. regex
3209 end
3210
3211 -- Match the regexes
3212 print(regex:exec("Looking for a needle in the haystack")) -- true
3213 print(regex:exec("Lokking for a cat in the haystack")) -- false
3214
3215 -- Extract words
3216 st, list = regex:match("Looking for a needle in the haystack")
3217 print(st) -- true
3218 print(list[1]) -- needle in the
3219 print(list[2]) -- in
3220 print(list[3]) -- the
3221
3222.. js:function:: Regex.new(regex, case_sensitive)
3223
3224 Create and compile a regex.
3225
3226 :param string regex: The regular expression according with the libc or pcre
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003227 standard
Thierry FOURNIER31904272017-10-25 12:59:51 +02003228 :param boolean case_sensitive: Match is case sensitive or not.
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003229 :returns: boolean status and :ref:`regex_class` or string containing fail
3230 reason.
Thierry FOURNIER31904272017-10-25 12:59:51 +02003231
3232.. js:function:: Regex.exec(regex, str)
3233
3234 Execute the regex.
3235
3236 :param class_regex regex: A :ref:`regex_class` object.
3237 :param string str: The input string will be compared with the compiled regex.
3238 :returns: a boolean status according with the match result.
3239
3240.. js:function:: Regex.match(regex, str)
3241
3242 Execute the regex and return matched expressions.
3243
3244 :param class_map map: A :ref:`regex_class` object.
3245 :param string str: The input string will be compared with the compiled regex.
3246 :returns: a boolean status according with the match result, and
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003247 a table containing all the string matched in order of declaration.
Thierry FOURNIER31904272017-10-25 12:59:51 +02003248
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003249.. _map_class:
3250
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003251Map class
3252=========
3253
3254.. js:class:: Map
3255
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003256 This class permits to do some lookups in HAProxy maps. The declared maps can
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01003257 be modified during the runtime through the HAProxy management socket.
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003258
3259.. code-block:: lua
3260
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003261 default = "usa"
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003262
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003263 -- Create and load map
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01003264 geo = Map.new("geo.map", Map._ip);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003265
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003266 -- Create new fetch that returns the user country
3267 core.register_fetches("country", function(txn)
3268 local src;
3269 local loc;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003270
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003271 src = txn.f:fhdr("x-forwarded-for");
3272 if (src == nil) then
3273 src = txn.f:src()
3274 if (src == nil) then
3275 return default;
3276 end
3277 end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003278
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003279 -- Perform lookup
3280 loc = geo:lookup(src);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003281
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003282 if (loc == nil) then
3283 return default;
3284 end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003285
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003286 return loc;
3287 end);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003288
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01003289.. js:attribute:: Map._int
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003290
3291 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05003292 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003293 method.
3294
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01003295 Note that :js:attr:`Map.int` is also available for compatibility.
3296
3297.. js:attribute:: Map._ip
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003298
3299 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05003300 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003301 method.
3302
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01003303 Note that :js:attr:`Map.ip` is also available for compatibility.
3304
3305.. js:attribute:: Map._str
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003306
3307 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05003308 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003309 method.
3310
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01003311 Note that :js:attr:`Map.str` is also available for compatibility.
3312
3313.. js:attribute:: Map._beg
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003314
3315 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05003316 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003317 method.
3318
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01003319 Note that :js:attr:`Map.beg` is also available for compatibility.
3320
3321.. js:attribute:: Map._sub
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003322
3323 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05003324 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003325 method.
3326
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01003327 Note that :js:attr:`Map.sub` is also available for compatibility.
3328
3329.. js:attribute:: Map._dir
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003330
3331 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05003332 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003333 method.
3334
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01003335 Note that :js:attr:`Map.dir` is also available for compatibility.
3336
3337.. js:attribute:: Map._dom
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003338
3339 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05003340 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003341 method.
3342
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01003343 Note that :js:attr:`Map.dom` is also available for compatibility.
3344
3345.. js:attribute:: Map._end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003346
3347 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05003348 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003349 method.
3350
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01003351.. js:attribute:: Map._reg
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003352
3353 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05003354 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003355 method.
3356
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01003357 Note that :js:attr:`Map.reg` is also available for compatibility.
3358
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003359
3360.. js:function:: Map.new(file, method)
3361
3362 Creates and load a map.
3363
3364 :param string file: Is the file containing the map.
3365 :param integer method: Is the map pattern matching method. See the attributes
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003366 of the Map class.
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003367 :returns: a class Map object.
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01003368 :see: The Map attributes: :js:attr:`Map._int`, :js:attr:`Map._ip`,
3369 :js:attr:`Map._str`, :js:attr:`Map._beg`, :js:attr:`Map._sub`,
3370 :js:attr:`Map._dir`, :js:attr:`Map._dom`, :js:attr:`Map._end` and
3371 :js:attr:`Map._reg`.
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003372
3373.. js:function:: Map.lookup(map, str)
3374
3375 Perform a lookup in a map.
3376
3377 :param class_map map: Is the class Map object.
3378 :param string str: Is the string used as key.
3379 :returns: a string containing the result or nil if no match.
3380
3381.. js:function:: Map.slookup(map, str)
3382
3383 Perform a lookup in a map.
3384
3385 :param class_map map: Is the class Map object.
3386 :param string str: Is the string used as key.
3387 :returns: a string containing the result or empty string if no match.
3388
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003389.. _applethttp_class:
3390
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003391AppletHTTP class
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003392================
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003393
3394.. js:class:: AppletHTTP
3395
3396 This class is used with applets that requires the 'http' mode. The http applet
3397 can be registered with the *core.register_service()* function. They are used
3398 for processing an http request like a server in back of HAProxy.
3399
3400 This is an hello world sample code:
3401
3402.. code-block:: lua
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003403
Pieter Baauw4d7f7662015-11-08 16:38:08 +01003404 core.register_service("hello-world", "http", function(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003405 local response = "Hello World !"
3406 applet:set_status(200)
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +02003407 applet:add_header("content-length", string.len(response))
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003408 applet:add_header("content-type", "text/plain")
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +02003409 applet:start_response()
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003410 applet:send(response)
3411 end)
3412
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003413.. js:attribute:: AppletHTTP.c
3414
3415 :returns: A :ref:`converters_class`
3416
3417 This attribute contains a Converters class object.
3418
3419.. js:attribute:: AppletHTTP.sc
3420
3421 :returns: A :ref:`converters_class`
3422
3423 This attribute contains a Converters class object. The
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003424 functions of this object always return a string.
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003425
3426.. js:attribute:: AppletHTTP.f
3427
3428 :returns: A :ref:`fetches_class`
3429
3430 This attribute contains a Fetches class object. Note that the
3431 applet execution place cannot access to a valid HAProxy core HTTP
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05003432 transaction, so some sample fetches related to the HTTP dependent
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003433 values (hdr, path, ...) are not available.
3434
3435.. js:attribute:: AppletHTTP.sf
3436
3437 :returns: A :ref:`fetches_class`
3438
3439 This attribute contains a Fetches class object. The functions of
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003440 this object always return a string. Note that the applet
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003441 execution place cannot access to a valid HAProxy core HTTP
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05003442 transaction, so some sample fetches related to the HTTP dependent
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003443 values (hdr, path, ...) are not available.
3444
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003445.. js:attribute:: AppletHTTP.method
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003446
3447 :returns: string
3448
3449 The attribute method returns a string containing the HTTP
3450 method.
3451
3452.. js:attribute:: AppletHTTP.version
3453
3454 :returns: string
3455
3456 The attribute version, returns a string containing the HTTP
3457 request version.
3458
3459.. js:attribute:: AppletHTTP.path
3460
3461 :returns: string
3462
3463 The attribute path returns a string containing the HTTP
3464 request path.
3465
3466.. js:attribute:: AppletHTTP.qs
3467
3468 :returns: string
3469
3470 The attribute qs returns a string containing the HTTP
3471 request query string.
3472
3473.. js:attribute:: AppletHTTP.length
3474
3475 :returns: integer
3476
3477 The attribute length returns an integer containing the HTTP
3478 body length.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003479
Thierry FOURNIER841475e2015-12-11 17:10:09 +01003480.. js:attribute:: AppletHTTP.headers
3481
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04003482 :returns: table
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003483
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04003484 The attribute headers returns a table containing the HTTP
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003485 headers. The header names are always in lower case. As the header name can be
3486 encountered more than once in each request, the value is indexed with 0 as
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003487 first index value. The table has this form:
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003488
3489.. code-block:: lua
3490
3491 AppletHTTP.headers['<header-name>'][<header-index>] = "<header-value>"
3492
3493 AppletHTTP.headers["host"][0] = "www.test.com"
3494 AppletHTTP.headers["accept"][0] = "audio/basic q=1"
3495 AppletHTTP.headers["accept"][1] = "audio/*, q=0.2"
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003496 AppletHTTP.headers["accept"][2] = "*/*, q=0.1"
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003497..
3498
Robin H. Johnson52f5db22017-01-01 13:10:52 -08003499.. js:function:: AppletHTTP.set_status(applet, code [, reason])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003500
3501 This function sets the HTTP status code for the response. The allowed code are
3502 from 100 to 599.
3503
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003504 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003505 :param integer code: the status code returned to the client.
Robin H. Johnson52f5db22017-01-01 13:10:52 -08003506 :param string reason: the status reason returned to the client (optional).
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003507
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003508.. js:function:: AppletHTTP.add_header(applet, name, value)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003509
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003510 This function adds a header in the response. Duplicated headers are not
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003511 collapsed. The special header *content-length* is used to determinate the
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003512 response length. If it does not exist, a *transfer-encoding: chunked* is set,
3513 and all the write from the function *AppletHTTP:send()* become a chunk.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003514
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003515 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003516 :param string name: the header name
3517 :param string value: the header value
3518
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003519.. js:function:: AppletHTTP.start_response(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003520
3521 This function indicates to the HTTP engine that it can process and send the
3522 response headers. After this called we cannot add headers to the response; We
3523 cannot use the *AppletHTTP:send()* function if the
3524 *AppletHTTP:start_response()* is not called.
3525
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003526 :param class_AppletHTTP applet: An :ref:`applethttp_class`
3527
3528.. js:function:: AppletHTTP.getline(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003529
3530 This function returns a string containing one line from the http body. If the
3531 data returned doesn't contains a final '\\n' its assumed than its the last
3532 available data before the end of stream.
3533
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003534 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003535 :returns: a string. The string can be empty if we reach the end of the stream.
3536
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003537.. js:function:: AppletHTTP.receive(applet, [size])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003538
3539 Reads data from the HTTP body, according to the specified read *size*. If the
3540 *size* is missing, the function tries to read all the content of the stream
3541 until the end. If the *size* is bigger than the http body, it returns the
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01003542 amount of data available.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003543
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003544 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003545 :param integer size: the required read size.
Ilya Shipitsin11057a32020-06-21 21:18:27 +05003546 :returns: always return a string,the string can be empty is the connection is
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003547 closed.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003548
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003549.. js:function:: AppletHTTP.send(applet, msg)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003550
3551 Send the message *msg* on the http request body.
3552
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003553 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003554 :param string msg: the message to send.
3555
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003556.. js:function:: AppletHTTP.get_priv(applet)
3557
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003558 Return Lua data stored in the current transaction. If no data are stored,
3559 it returns a nil value.
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003560
3561 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01003562 :returns: the opaque data previously stored, or nil if nothing is
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003563 available.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003564 :see: :js:func:`AppletHTTP.set_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003565
3566.. js:function:: AppletHTTP.set_priv(applet, data)
3567
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003568 Store any data in the current HAProxy transaction. This action replaces the
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003569 old stored data.
3570
3571 :param class_AppletHTTP applet: An :ref:`applethttp_class`
3572 :param opaque data: The data which is stored in the transaction.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003573 :see: :js:func:`AppletHTTP.get_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003574
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003575.. js:function:: AppletHTTP.set_var(applet, var, value[, ifexist])
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003576
3577 Converts a Lua type in a HAProxy type and store it in a variable <var>.
3578
3579 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003580 :param string var: The variable name according with the HAProxy variable
3581 syntax.
3582 :param type value: The value associated to the variable. The type ca be string
3583 or integer.
3584 :param boolean ifexist: If this parameter is set to true the variable will
3585 only be set if it was defined elsewhere (i.e. used within the configuration).
3586 For global variables (using the "proc" scope), they will only be updated and
3587 never created. It is highly recommended to always set this to true.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003588
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003589 :see: :js:func:`AppletHTTP.unset_var`
3590 :see: :js:func:`AppletHTTP.get_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003591
3592.. js:function:: AppletHTTP.unset_var(applet, var)
3593
3594 Unset the variable <var>.
3595
3596 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003597 :param string var: The variable name according with the HAProxy variable
3598 syntax.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003599 :see: :js:func:`AppletHTTP.set_var`
3600 :see: :js:func:`AppletHTTP.get_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003601
3602.. js:function:: AppletHTTP.get_var(applet, var)
3603
3604 Returns data stored in the variable <var> converter in Lua type.
3605
3606 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003607 :param string var: The variable name according with the HAProxy variable
3608 syntax.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003609 :see: :js:func:`AppletHTTP.set_var`
3610 :see: :js:func:`AppletHTTP.unset_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003611
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003612.. _applettcp_class:
3613
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003614AppletTCP class
3615===============
3616
3617.. js:class:: AppletTCP
3618
3619 This class is used with applets that requires the 'tcp' mode. The tcp applet
3620 can be registered with the *core.register_service()* function. They are used
3621 for processing a tcp stream like a server in back of HAProxy.
3622
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003623.. js:attribute:: AppletTCP.c
3624
3625 :returns: A :ref:`converters_class`
3626
3627 This attribute contains a Converters class object.
3628
3629.. js:attribute:: AppletTCP.sc
3630
3631 :returns: A :ref:`converters_class`
3632
3633 This attribute contains a Converters class object. The
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003634 functions of this object always return a string.
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003635
3636.. js:attribute:: AppletTCP.f
3637
3638 :returns: A :ref:`fetches_class`
3639
3640 This attribute contains a Fetches class object.
3641
3642.. js:attribute:: AppletTCP.sf
3643
3644 :returns: A :ref:`fetches_class`
3645
3646 This attribute contains a Fetches class object.
3647
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003648.. js:function:: AppletTCP.getline(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003649
3650 This function returns a string containing one line from the stream. If the
3651 data returned doesn't contains a final '\\n' its assumed than its the last
3652 available data before the end of stream.
3653
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003654 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003655 :returns: a string. The string can be empty if we reach the end of the stream.
3656
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003657.. js:function:: AppletTCP.receive(applet, [size])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003658
3659 Reads data from the TCP stream, according to the specified read *size*. If the
3660 *size* is missing, the function tries to read all the content of the stream
3661 until the end.
3662
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003663 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003664 :param integer size: the required read size.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003665 :returns: always return a string, the string can be empty if the connection is
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003666 closed.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003667
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003668.. js:function:: AppletTCP.send(appletmsg)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003669
3670 Send the message on the stream.
3671
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003672 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003673 :param string msg: the message to send.
3674
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003675.. js:function:: AppletTCP.get_priv(applet)
3676
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003677 Return Lua data stored in the current transaction. If no data are stored,
3678 it returns a nil value.
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003679
3680 :param class_AppletTCP applet: An :ref:`applettcp_class`
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01003681 :returns: the opaque data previously stored, or nil if nothing is
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003682 available.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003683 :see: :js:func:`AppletTCP.set_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003684
3685.. js:function:: AppletTCP.set_priv(applet, data)
3686
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003687 Store any data in the current HAProxy transaction. This action replaces the
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003688 old stored data.
3689
3690 :param class_AppletTCP applet: An :ref:`applettcp_class`
3691 :param opaque data: The data which is stored in the transaction.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003692 :see: :js:func:`AppletTCP.get_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003693
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003694.. js:function:: AppletTCP.set_var(applet, var, value[, ifexist])
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003695
3696 Converts a Lua type in a HAProxy type and stores it in a variable <var>.
3697
3698 :param class_AppletTCP applet: An :ref:`applettcp_class`
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003699 :param string var: The variable name according with the HAProxy variable
3700 syntax.
3701 :param type value: The value associated to the variable. The type can be
3702 string or integer.
3703 :param boolean ifexist: If this parameter is set to true the variable will
3704 only be set if it was defined elsewhere (i.e. used within the configuration).
3705 For global variables (using the "proc" scope), they will only be updated and
3706 never created. It is highly recommended to always set this to true.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003707
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003708 :see: :js:func:`AppletTCP.unset_var`
3709 :see: :js:func:`AppletTCP.get_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003710
3711.. js:function:: AppletTCP.unset_var(applet, var)
3712
3713 Unsets the variable <var>.
3714
3715 :param class_AppletTCP applet: An :ref:`applettcp_class`
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003716 :param string var: The variable name according with the HAProxy variable
3717 syntax.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003718 :see: :js:func:`AppletTCP.unset_var`
3719 :see: :js:func:`AppletTCP.set_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003720
3721.. js:function:: AppletTCP.get_var(applet, var)
3722
3723 Returns data stored in the variable <var> converter in Lua type.
3724
3725 :param class_AppletTCP applet: An :ref:`applettcp_class`
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003726 :param string var: The variable name according with the HAProxy variable
3727 syntax.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003728 :see: :js:func:`AppletTCP.unset_var`
3729 :see: :js:func:`AppletTCP.set_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003730
Aurelien DARRAGONe5ad3472023-11-23 13:47:54 +01003731.. _sticktable_class:
3732
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02003733StickTable class
3734================
3735
3736.. js:class:: StickTable
3737
3738 **context**: task, action, sample-fetch
3739
3740 This class can be used to access the HAProxy stick tables from Lua.
3741
3742.. js:function:: StickTable.info()
3743
3744 Returns stick table attributes as a Lua table. See HAProxy documentation for
Ilya Shipitsin2272d8a2020-12-21 01:22:40 +05003745 "stick-table" for canonical info, or check out example below.
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02003746
3747 :returns: Lua table
3748
3749 Assume our table has IPv4 key and gpc0 and conn_rate "columns":
3750
3751.. code-block:: lua
3752
3753 {
3754 expire=<int>, # Value in ms
3755 size=<int>, # Maximum table size
3756 used=<int>, # Actual number of entries in table
3757 data={ # Data columns, with types as key, and periods as values
3758 (-1 if type is not rate counter)
3759 conn_rate=<int>,
3760 gpc0=-1
3761 },
3762 length=<int>, # max string length for string table keys, key length
3763 # otherwise
3764 nopurge=<boolean>, # purge oldest entries when table is full
3765 type="ip" # can be "ip", "ipv6", "integer", "string", "binary"
3766 }
3767
3768.. js:function:: StickTable.lookup(key)
3769
3770 Returns stick table entry for given <key>
3771
3772 :param string key: Stick table key (IP addresses and strings are supported)
3773 :returns: Lua table
3774
3775.. js:function:: StickTable.dump([filter])
3776
3777 Returns all entries in stick table. An optional filter can be used
3778 to extract entries with specific data values. Filter is a table with valid
3779 comparison operators as keys followed by data type name and value pairs.
3780 Check out the HAProxy docs for "show table" for more details. For the
3781 reference, the supported operators are:
Aurelien DARRAGON21f7ebb2023-03-13 19:49:31 +01003782
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02003783 "eq", "ne", "le", "lt", "ge", "gt"
3784
3785 For large tables, execution of this function can take a long time (for
3786 HAProxy standards). That's also true when filter is used, so take care and
3787 measure the impact.
3788
3789 :param table filter: Stick table filter
3790 :returns: Stick table entries (table)
3791
3792 See below for example filter, which contains 4 entries (or comparisons).
3793 (Maximum number of filter entries is 4, defined in the source code)
3794
3795.. code-block:: lua
3796
3797 local filter = {
3798 {"gpc0", "gt", 30}, {"gpc1", "gt", 20}}, {"conn_rate", "le", 10}
3799 }
3800
Christopher Faulet0f3c8902020-01-31 18:57:12 +01003801.. _action_class:
3802
3803Action class
3804=============
3805
3806.. js:class:: Act
3807
3808 **context**: action
3809
3810 This class contains all return codes an action may return. It is the lua
3811 equivalent to HAProxy "ACT_RET_*" code.
3812
3813.. code-block:: lua
3814
3815 core.register_action("deny", { "http-req" }, function (txn)
3816 return act.DENY
3817 end)
3818..
3819.. js:attribute:: act.CONTINUE
3820
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003821 This attribute is an integer (0). It instructs HAProxy to continue the
3822 current ruleset processing on the message. It is the default return code
3823 for a lua action.
Christopher Faulet0f3c8902020-01-31 18:57:12 +01003824
3825 :returns: integer
3826
3827.. js:attribute:: act.STOP
3828
3829 This attribute is an integer (1). It instructs HAProxy to stop the current
3830 ruleset processing on the message.
3831
3832.. js:attribute:: act.YIELD
3833
3834 This attribute is an integer (2). It instructs HAProxy to temporarily pause
3835 the message processing. It will be resumed later on the same rule. The
3836 corresponding lua script is re-executed for the start.
3837
3838.. js:attribute:: act.ERROR
3839
3840 This attribute is an integer (3). It triggers an internal errors The message
3841 processing is stopped and the transaction is terminated. For HTTP streams, an
3842 HTTP 500 error is returned to the client.
3843
3844 :returns: integer
3845
3846.. js:attribute:: act.DONE
3847
3848 This attribute is an integer (4). It instructs HAProxy to stop the message
3849 processing.
3850
3851 :returns: integer
3852
3853.. js:attribute:: act.DENY
3854
3855 This attribute is an integer (5). It denies the current message. The message
3856 processing is stopped and the transaction is terminated. For HTTP streams, an
3857 HTTP 403 error is returned to the client if the deny is returned during the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003858 request analysis. During the response analysis, a HTTP 502 error is returned
Christopher Faulet0f3c8902020-01-31 18:57:12 +01003859 and the server response is discarded.
3860
3861 :returns: integer
3862
3863.. js:attribute:: act.ABORT
3864
3865 This attribute is an integer (6). It aborts the current message. The message
3866 processing is stopped and the transaction is terminated. For HTTP streams,
Willy Tarreau714f3452021-05-09 06:47:26 +02003867 HAProxy assumes a response was already sent to the client. From the Lua
Christopher Faulet0f3c8902020-01-31 18:57:12 +01003868 actions point of view, when this code is used, the transaction is terminated
3869 with no reply.
3870
3871 :returns: integer
3872
3873.. js:attribute:: act.INVALID
3874
3875 This attribute is an integer (7). It triggers an internal errors. The message
3876 processing is stopped and the transaction is terminated. For HTTP streams, an
3877 HTTP 400 error is returned to the client if the error is returned during the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003878 request analysis. During the response analysis, a HTTP 502 error is returned
Christopher Faulet0f3c8902020-01-31 18:57:12 +01003879 and the server response is discarded.
3880
3881 :returns: integer
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02003882
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01003883.. js:function:: act:wake_time(milliseconds)
3884
3885 **context**: action
3886
3887 Set the script pause timeout to the specified time, defined in
3888 milliseconds.
3889
3890 :param integer milliseconds: the required milliseconds.
3891
3892 This function may be used when a lua action returns `act.YIELD`, to force its
3893 wake-up at most after the specified number of milliseconds.
3894
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003895.. _filter_class:
3896
3897Filter class
3898=============
3899
3900.. js:class:: filter
3901
3902 **context**: filter
3903
3904 This class contains return codes some filter callback functions may return. It
3905 also contains configuration flags and some helper functions. To understand how
Aurelien DARRAGON0ca21ea2024-05-10 09:31:47 +02003906 the filter API works, see `doc/internals/api/filters.txt` documentation.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003907
3908.. js:attribute:: filter.CONTINUE
3909
3910 This attribute is an integer (1). It may be returned by some filter callback
3911 functions to instruct this filtering step is finished for this filter.
3912
3913.. js:attribute:: filter.WAIT
3914
3915 This attribute is an integer (0). It may be returned by some filter callback
3916 functions to instruct the filtering must be paused, waiting for more data or
3917 for an external event depending on this filter.
3918
3919.. js:attribute:: filter.ERROR
3920
3921 This attribute is an integer (-1). It may be returned by some filter callback
3922 functions to trigger an error.
3923
3924.. js:attribute:: filter.FLT_CFG_FL_HTX
3925
3926 This attribute is a flag corresponding to the filter flag FLT_CFG_FL_HTX. When
3927 it is set for a filter, it means the filter is able to filter HTTP streams.
3928
3929.. js:function:: filter.register_data_filter(chn)
3930
3931 **context**: filter
3932
3933 Enable the data filtering on the channel **chn** for the current filter. It
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003934 may be called at any time from any callback functions proceeding the data
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003935 analysis.
3936
3937 :param class_Channel chn: A :ref:`channel_class`.
3938
3939.. js:function:: filter.unregister_data_filter(chn)
3940
3941 **context**: filter
3942
3943 Disable the data filtering on the channel **chn** for the current filter. It
3944 may be called at any time from any callback functions.
3945
3946 :param class_Channel chn: A :ref:`channel_class`.
3947
3948.. js:function:: filter.wake_time(milliseconds)
3949
3950 **context**: filter
3951
3952 Set the script pause timeout to the specified time, defined in
3953 milliseconds.
3954
3955 :param integer milliseconds: the required milliseconds.
3956
3957 This function may be used from any lua filter callback function to force its
3958 wake-up at most after the specified number of milliseconds. Especially, when
3959 `filter.CONTINUE` is returned.
3960
3961
3962A filters is declared using :js:func:`core.register_filter()` function. The
3963provided class will be used to instantiate filters. It may define following
3964attributes:
3965
3966* id: The filter identifier. It is a string that identifies the filter and is
3967 optional.
3968
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003969* flags: The filter flags. Only :js:attr:`filter.FLT_CFG_FL_HTX` may be set
3970 for now.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003971
3972Such filter class must also define all required callback functions in the
3973following list. Note that :js:func:`Filter.new()` must be defined otherwise the
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003974filter is ignored. Others are optional.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003975
3976* .. js:function:: FILTER.new()
3977
3978 Called to instantiate a new filter. This function must be defined.
3979
3980 :returns: a Lua object that will be used as filter instance for the current
3981 stream.
3982
3983* .. js:function:: FILTER.start_analyze(flt, txn, chn)
3984
3985 Called when the analysis starts on the channel **chn**.
3986
3987* .. js:function:: FILTER.end_analyze(flt, txn, chn)
3988
3989 Called when the analysis ends on the channel **chn**.
3990
3991* .. js:function:: FILTER.http_headers(flt, txn, http_msg)
3992
3993 Called just before the HTTP payload analysis and after any processing on the
3994 HTTP message **http_msg**. This callback functions is only called for HTTP
3995 streams.
3996
3997* .. js:function:: FILTER.http_payload(flt, txn, http_msg)
3998
3999 Called during the HTTP payload analysis on the HTTP message **http_msg**. This
4000 callback functions is only called for HTTP streams.
4001
4002* .. js:function:: FILTER.http_end(flt, txn, http_msg)
4003
4004 Called after the HTTP payload analysis on the HTTP message **http_msg**. This
4005 callback functions is only called for HTTP streams.
4006
4007* .. js:function:: FILTER.tcp_payload(flt, txn, chn)
4008
4009 Called during the TCP payload analysis on the channel **chn**.
4010
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02004011Here is a full example:
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004012
4013.. code-block:: lua
4014
4015 Trace = {}
4016 Trace.id = "Lua trace filter"
4017 Trace.flags = filter.FLT_CFG_FL_HTX;
4018 Trace.__index = Trace
4019
4020 function Trace:new()
4021 local trace = {}
4022 setmetatable(trace, Trace)
4023 trace.req_len = 0
4024 trace.res_len = 0
4025 return trace
4026 end
4027
4028 function Trace:start_analyze(txn, chn)
4029 if chn:is_resp() then
4030 print("Start response analysis")
4031 else
4032 print("Start request analysis")
4033 end
4034 filter.register_data_filter(self, chn)
4035 end
4036
4037 function Trace:end_analyze(txn, chn)
4038 if chn:is_resp() then
4039 print("End response analysis: "..self.res_len.." bytes filtered")
4040 else
4041 print("End request analysis: "..self.req_len.." bytes filtered")
4042 end
4043 end
4044
4045 function Trace:http_headers(txn, http_msg)
4046 stline = http_msg:get_stline()
4047 if http_msg.channel:is_resp() then
4048 print("response:")
4049 print(stline.version.." "..stline.code.." "..stline.reason)
4050 else
4051 print("request:")
4052 print(stline.method.." "..stline.uri.." "..stline.version)
4053 end
4054
4055 for n, hdrs in pairs(http_msg:get_headers()) do
4056 for i,v in pairs(hdrs) do
4057 print(n..": "..v)
4058 end
4059 end
4060 return filter.CONTINUE
4061 end
4062
4063 function Trace:http_payload(txn, http_msg)
4064 body = http_msg:body(-20000)
4065 if http_msg.channel:is_resp() then
4066 self.res_len = self.res_len + body:len()
4067 else
4068 self.req_len = self.req_len + body:len()
4069 end
4070 end
4071
4072 core.register_filter("trace", Trace, function(trace, args)
4073 return trace
4074 end)
4075
4076..
4077
4078.. _httpmessage_class:
4079
4080HTTPMessage class
4081===================
4082
4083.. js:class:: HTTPMessage
4084
4085 **context**: filter
4086
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02004087 This class contains all functions to manipulate a HTTP message. For now, this
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004088 class is only available from a filter context.
4089
4090.. js:function:: HTTPMessage.add_header(http_msg, name, value)
4091
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02004092 Appends a HTTP header field in the HTTP message **http_msg** whose name is
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004093 specified in **name** and whose value is defined in **value**.
4094
4095 :param class_httpmessage http_msg: The manipulated HTTP message.
4096 :param string name: The header name.
4097 :param string value: The header value.
4098
4099.. js:function:: HTTPMessage.append(http_msg, string)
4100
4101 This function copies the string **string** at the end of incoming data of the
4102 HTTP message **http_msg**. The function returns the copied length on success
4103 or -1 if data cannot be copied.
4104
4105 Same that :js:func:`HTTPMessage.insert(http_msg, string, http_msg:input())`.
4106
4107 :param class_httpmessage http_msg: The manipulated HTTP message.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02004108 :param string string: The data to copy at the end of incoming data.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004109 :returns: an integer containing the amount of bytes copied or -1.
4110
4111.. js:function:: HTTPMessage.body(http_msgl[, offset[, length]])
4112
4113 This function returns **length** bytes of incoming data from the HTTP message
4114 **http_msg**, starting at the offset **offset**. The data are not removed from
4115 the buffer.
4116
4117 By default, if no length is provided, all incoming data found, starting at the
4118 given offset, are returned. If **length** is set to -1, the function tries to
4119 retrieve a maximum of data. Because it is called in the filter context, it
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02004120 never yield. Not providing an offset is the same as setting it to 0. A
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05004121 positive offset is relative to the beginning of incoming data of the
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004122 http_message buffer while negative offset is relative to their end.
4123
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02004124 If there is no incoming data and the HTTP message can't receive more data,
4125 a 'nil' value is returned.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004126
4127 :param class_httpmessage http_msg: The manipulated HTTP message.
4128 :param integer offset: *optional* The offset in incoming data to start to get
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02004129 data. 0 by default. May be negative to be relative to the end of incoming
4130 data.
4131 :param integer length: *optional* The expected length of data to retrieve.
4132 All incoming data by default. May be set to -1 to get a maximum of data.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004133 :returns: a string containing the data found or nil.
4134
4135.. js:function:: HTTPMessage.eom(http_msg)
4136
4137 This function returns true if the end of message is reached for the HTTP
4138 message **http_msg**.
4139
4140 :param class_httpmessage http_msg: The manipulated HTTP message.
4141 :returns: an integer containing the amount of available bytes.
4142
4143.. js:function:: HTTPMessage.del_header(http_msg, name)
4144
4145 Removes all HTTP header fields in the HTTP message **http_msg** whose name is
4146 specified in **name**.
4147
4148 :param class_httpmessage http_msg: The manipulated http message.
4149 :param string name: The header name.
4150
4151.. js:function:: HTTPMessage.get_headers(http_msg)
4152
4153 Returns a table containing all the headers of the HTTP message **http_msg**.
4154
4155 :param class_httpmessage http_msg: The manipulated http message.
4156 :returns: table of headers.
4157
4158 This is the form of the returned table:
4159
4160.. code-block:: lua
4161
4162 http_msg:get_headers()['<header-name>'][<header-index>] = "<header-value>"
4163
4164 local hdr = http_msg:get_headers()
4165 hdr["host"][0] = "www.test.com"
4166 hdr["accept"][0] = "audio/basic q=1"
4167 hdr["accept"][1] = "audio/*, q=0.2"
4168 hdr["accept"][2] = "*.*, q=0.1"
4169..
4170
4171.. js:function:: HTTPMessage.get_stline(http_msg)
4172
4173 Returns a table containing the start-line of the HTTP message **http_msg**.
4174
4175 :param class_httpmessage http_msg: The manipulated http message.
4176 :returns: the start-line.
4177
4178 This is the form of the returned table:
4179
4180.. code-block:: lua
4181
4182 -- for the request :
4183 {"method" = string, "uri" = string, "version" = string}
4184
4185 -- for the response:
4186 {"version" = string, "code" = string, "reason" = string}
4187..
4188
4189.. js:function:: HTTPMessage.forward(http_msg, length)
4190
4191 This function forwards **length** bytes of data from the HTTP message
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02004192 **http_msg**. Because it is called in the filter context, it never yields. Only
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004193 available incoming data may be forwarded, event if the requested length
4194 exceeds the available amount of incoming data. It returns the amount of data
4195 forwarded.
4196
4197 :param class_httpmessage http_msg: The manipulated HTTP message.
4198 :param integer int: The amount of data to forward.
4199
4200.. js:function:: HTTPMessage.input(http_msg)
4201
4202 This function returns the length of incoming data in the HTTP message
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02004203 **http_msg** from the filter point of view.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004204
4205 :param class_httpmessage http_msg: The manipulated HTTP message.
4206 :returns: an integer containing the amount of available bytes.
4207
4208.. js:function:: HTTPMessage.insert(http_msg, string[, offset])
4209
4210 This function copies the string **string** at the offset **offset** in
4211 incoming data of the HTTP message **http_msg**. The function returns the
4212 copied length on success or -1 if data cannot be copied.
4213
4214 By default, if no offset is provided, the string is copied in front of
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05004215 incoming data. A positive offset is relative to the beginning of incoming data
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004216 of the HTTP message while negative offset is relative to their end.
4217
4218 :param class_httpmessage http_msg: The manipulated HTTP message.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02004219 :param string string: The data to copy into incoming data.
4220 :param integer offset: *optional* The offset in incoming data where to copy
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02004221 data. 0 by default. May be negative to be relative to the end of incoming
4222 data.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004223 :returns: an integer containing the amount of bytes copied or -1.
4224
4225.. js:function:: HTTPMessage.is_full(http_msg)
4226
4227 This function returns true if the HTTP message **http_msg** is full.
4228
4229 :param class_httpmessage http_msg: The manipulated HTTP message.
4230 :returns: a boolean
4231
4232.. js:function:: HTTPMessage.is_resp(http_msg)
4233
4234 This function returns true if the HTTP message **http_msg** is the response
4235 one.
4236
4237 :param class_httpmessage http_msg: The manipulated HTTP message.
4238 :returns: a boolean
4239
4240.. js:function:: HTTPMessage.may_recv(http_msg)
4241
4242 This function returns true if the HTTP message **http_msg** may still receive
4243 data.
4244
4245 :param class_httpmessage http_msg: The manipulated HTTP message.
4246 :returns: a boolean
4247
4248.. js:function:: HTTPMessage.output(http_msg)
4249
4250 This function returns the length of outgoing data of the HTTP message
4251 **http_msg**.
4252
4253 :param class_httpmessage http_msg: The manipulated HTTP message.
4254 :returns: an integer containing the amount of available bytes.
4255
4256.. js:function:: HTTPMessage.prepend(http_msg, string)
4257
4258 This function copies the string **string** in front of incoming data of the
4259 HTTP message **http_msg**. The function returns the copied length on success
4260 or -1 if data cannot be copied.
4261
4262 Same that :js:func:`HTTPMessage.insert(http_msg, string, 0)`.
4263
4264 :param class_httpmessage http_msg: The manipulated HTTP message.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02004265 :param string string: The data to copy in front of incoming data.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004266 :returns: an integer containing the amount of bytes copied or -1.
4267
4268.. js:function:: HTTPMessage.remove(http_msg[, offset[, length]])
4269
4270 This function removes **length** bytes of incoming data of the HTTP message
4271 **http_msg**, starting at offset **offset**. This function returns number of
4272 bytes removed on success.
4273
4274 By default, if no length is provided, all incoming data, starting at the given
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02004275 offset, are removed. Not providing an offset is the same that setting it
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05004276 to 0. A positive offset is relative to the beginning of incoming data of the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02004277 HTTP message while negative offset is relative to the end.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004278
4279 :param class_httpmessage http_msg: The manipulated HTTP message.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02004280 :param integer offset: *optional* The offset in incoming data where to start
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02004281 to remove data. 0 by default. May be negative to be relative to the end of
4282 incoming data.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004283 :param integer length: *optional* The length of data to remove. All incoming
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02004284 data by default.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004285 :returns: an integer containing the amount of bytes removed.
4286
4287.. js:function:: HTTPMessage.rep_header(http_msg, name, regex, replace)
4288
4289 Matches the regular expression in all occurrences of header field **name**
4290 according to regex **regex**, and replaces them with the string **replace**.
4291 The replacement value can contain back references like \1, \2, ... This
4292 function acts on whole header lines, regardless of the number of values they
4293 may contain.
4294
4295 :param class_httpmessage http_msg: The manipulated HTTP message.
4296 :param string name: The header name.
4297 :param string regex: The match regular expression.
4298 :param string replace: The replacement value.
4299
4300.. js:function:: HTTPMessage.rep_value(http_msg, name, regex, replace)
4301
4302 Matches the regular expression on every comma-delimited value of header field
4303 **name** according to regex **regex**, and replaces them with the string
4304 **replace**. The replacement value can contain back references like \1, \2,
4305 ...
4306
4307 :param class_httpmessage http_msg: The manipulated HTTP message.
4308 :param string name: The header name.
4309 :param string regex: The match regular expression.
4310 :param string replace: The replacement value.
4311
4312.. js:function:: HTTPMessage.send(http_msg, string)
4313
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02004314 This function requires immediate send of the string **string**. It means the
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05004315 string is copied at the beginning of incoming data of the HTTP message
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004316 **http_msg** and immediately forwarded. Because it is called in the filter
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02004317 context, it never yields.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004318
4319 :param class_httpmessage http_msg: The manipulated HTTP message.
4320 :param string string: The data to send.
4321 :returns: an integer containing the amount of bytes copied or -1.
4322
4323.. js:function:: HTTPMessage.set(http_msg, string[, offset[, length]])
4324
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02004325 This function replaces **length** bytes of incoming data of the HTTP message
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004326 **http_msg**, starting at offset **offset**, by the string **string**. The
4327 function returns the copied length on success or -1 if data cannot be copied.
4328
4329 By default, if no length is provided, all incoming data, starting at the given
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02004330 offset, are replaced. Not providing an offset is the same as setting it
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05004331 to 0. A positive offset is relative to the beginning of incoming data of the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02004332 HTTP message while negative offset is relative to the end.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004333
4334 :param class_httpmessage http_msg: The manipulated HTTP message.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02004335 :param string string: The data to copy into incoming data.
4336 :param integer offset: *optional* The offset in incoming data where to start
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02004337 the data replacement. 0 by default. May be negative to be relative to the
4338 end of incoming data.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004339 :param integer length: *optional* The length of data to replace. All incoming
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02004340 data by default.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004341 :returns: an integer containing the amount of bytes copied or -1.
4342
4343.. js:function:: HTTPMessage.set_eom(http_msg)
4344
4345 This function set the end of message for the HTTP message **http_msg**.
4346
4347 :param class_httpmessage http_msg: The manipulated HTTP message.
4348
4349.. js:function:: HTTPMessage.set_header(http_msg, name, value)
4350
4351 This variable replace all occurrence of all header matching the name **name**,
4352 by only one containing the value **value**.
4353
4354 :param class_httpmessage http_msg: The manipulated HTTP message.
4355 :param string name: The header name.
4356 :param string value: The header value.
4357
4358 This function does the same work as the following code:
4359
4360.. code-block:: lua
4361
4362 http_msg:del_header("header")
4363 http_msg:add_header("header", "value")
4364..
4365
4366.. js:function:: HTTPMessage.set_method(http_msg, method)
4367
4368 Rewrites the request method with the string **method**. The HTTP message
4369 **http_msg** must be the request.
4370
4371 :param class_httpmessage http_msg: The manipulated HTTP message.
4372 :param string method: The new method.
4373
4374.. js:function:: HTTPMessage.set_path(http_msg, path)
4375
4376 Rewrites the request path with the string **path**. The HTTP message
4377 **http_msg** must be the request.
4378
4379 :param class_httpmessage http_msg: The manipulated HTTP message.
4380 :param string method: The new method.
4381
4382.. js:function:: HTTPMessage.set_query(http_msg, query)
4383
4384 Rewrites the request's query string which appears after the first question
4385 mark ("?") with the string **query**. The HTTP message **http_msg** must be
4386 the request.
4387
4388 :param class_httpmessage http_msg: The manipulated HTTP message.
4389 :param string query: The new query.
4390
4391.. js:function:: HTTPMessage.set_status(http_msg, status[, reason])
4392
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05004393 Rewrites the response status code with the integer **code** and optional the
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004394 reason **reason**. If no custom reason is provided, it will be generated from
4395 the status. The HTTP message **http_msg** must be the response.
4396
4397 :param class_httpmessage http_msg: The manipulated HTTP message.
4398 :param integer status: The new response status code.
4399 :param string reason: The new response reason (optional).
4400
4401.. js:function:: HTTPMessage.set_uri(http_msg, uri)
4402
4403 Rewrites the request URI with the string **uri**. The HTTP message
4404 **http_msg** must be the request.
4405
4406 :param class_httpmessage http_msg: The manipulated HTTP message.
4407 :param string uri: The new uri.
4408
4409.. js:function:: HTTPMessage.unset_eom(http_msg)
4410
4411 This function remove the end of message for the HTTP message **http_msg**.
4412
4413 :param class_httpmessage http_msg: The manipulated HTTP message.
4414
William Lallemand10cea5c2022-03-30 16:02:43 +02004415.. _CertCache_class:
4416
4417CertCache class
4418================
4419
4420.. js:class:: CertCache
4421
4422 This class allows to update an SSL certificate file in the memory of the
4423 current HAProxy process. It will do the same as "set ssl cert" + "commit ssl
4424 cert" over the HAProxy CLI.
4425
4426.. js:function:: CertCache.set(certificate)
4427
4428 This function updates a certificate in memory.
4429
4430 :param table certificate: A table containing the fields to update.
4431 :param string certificate.filename: The mandatory filename of the certificate
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02004432 to update, it must already exist in memory.
William Lallemand10cea5c2022-03-30 16:02:43 +02004433 :param string certificate.crt: A certificate in the PEM format. It can also
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02004434 contain a private key.
William Lallemand10cea5c2022-03-30 16:02:43 +02004435 :param string certificate.key: A private key in the PEM format.
4436 :param string certificate.ocsp: An OCSP response in base64. (cf management.txt)
4437 :param string certificate.issuer: The certificate of the OCSP issuer.
4438 :param string certificate.sctl: An SCTL file.
4439
Aurelien DARRAGON60497352024-06-03 16:18:27 +02004440 .. Note::
4441 This function may be slow. As such, it may only be used during startup
4442 (main or init context) or from a yield-capable runtime context.
4443
William Lallemand10cea5c2022-03-30 16:02:43 +02004444.. code-block:: lua
4445
4446 CertCache.set{filename="certs/localhost9994.pem.rsa", crt=crt}
4447
4448
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01004449External Lua libraries
4450======================
4451
4452A lot of useful lua libraries can be found here:
4453
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02004454* Lua toolbox has been superseded by
4455 `https://luarocks.org/ <https://luarocks.org/>`_
4456
4457 The old lua toolbox source code is still available here
4458 `https://github.com/catwell/lua-toolbox <https://github.com/catwell/lua-toolbox>`_ (DEPRECATED)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01004459
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05004460Redis client library:
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01004461
4462* `https://github.com/nrk/redis-lua <https://github.com/nrk/redis-lua>`_
4463
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02004464This is an example about the usage of the Redis library within HAProxy.
4465Note that each call to any function of this library can throw an error if
4466the socket connection fails.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01004467
4468.. code-block:: lua
4469
4470 -- load the redis library
4471 local redis = require("redis");
4472
4473 function do_something(txn)
4474
4475 -- create and connect new tcp socket
4476 local tcp = core.tcp();
4477 tcp:settimeout(1);
4478 tcp:connect("127.0.0.1", 6379);
4479
4480 -- use the redis library with this new socket
4481 local client = redis.connect({socket=tcp});
4482 client:ping();
4483
4484 end
4485
4486OpenSSL:
4487
4488* `http://mkottman.github.io/luacrypto/index.html
4489 <http://mkottman.github.io/luacrypto/index.html>`_
4490
4491* `https://github.com/brunoos/luasec/wiki
4492 <https://github.com/brunoos/luasec/wiki>`_